DTDValidatingReader.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. using System;
  2. using System.Collections;
  3. using System.Xml;
  4. using System.Xml.Schema;
  5. namespace Mono.Xml
  6. {
  7. public class DTDValidatingReader : XmlValidatingReader, IXmlLineInfo
  8. {
  9. public DTDValidatingReader (XmlReader reader)
  10. : base (reader)
  11. {
  12. this.reader = reader;
  13. elementStack = new Stack ();
  14. automataStack = new Stack ();
  15. }
  16. XmlReader reader;
  17. DTDObjectModel dtd;
  18. Stack elementStack;
  19. Stack automataStack;
  20. string currentAttribute;
  21. public DTDObjectModel DTD {
  22. get { return dtd; }
  23. }
  24. public override void Close ()
  25. {
  26. reader.Close ();
  27. }
  28. // We had already done attribute validation, so can ignore name.
  29. public override string GetAttribute (int i)
  30. {
  31. if (dtd == null)
  32. return reader.GetAttribute (i);
  33. // It should access attributes by *defined* order.
  34. if (elementStack.Count == 0)
  35. return String.Empty;
  36. DTDAttributeDefinition def = dtd.ElementDecls [elementStack.Peek () as string]
  37. .Attributes [i] as DTDAttributeDefinition;
  38. string specified = reader.GetAttribute (def.Name);
  39. return (specified != null) ? specified : def.DefaultValue;
  40. }
  41. public override string GetAttribute (string name)
  42. {
  43. if (dtd == null)
  44. return reader.GetAttribute (name);
  45. if (elementStack.Count == 0)
  46. return String.Empty;
  47. string specified = reader.GetAttribute (name);
  48. if (specified != null)
  49. return specified;
  50. DTDAttributeDefinition def = dtd.ElementDecls [elementStack.Peek () as string]
  51. .Attributes [name] as DTDAttributeDefinition;
  52. return def.DefaultValue;
  53. }
  54. public override string GetAttribute (string name, string ns)
  55. {
  56. if (dtd == null)
  57. return reader.GetAttribute (name, ns);
  58. if (ns != String.Empty)
  59. throw new InvalidOperationException ("DTD validating reader does not support namespace.");
  60. return GetAttribute (name);
  61. }
  62. bool IXmlLineInfo.HasLineInfo ()
  63. {
  64. IXmlLineInfo ixli = reader as IXmlLineInfo;
  65. if (ixli != null)
  66. return ixli.HasLineInfo ();
  67. else
  68. return false;
  69. }
  70. public override string LookupNamespace (string prefix)
  71. {
  72. // Does it mean anything with DTD?
  73. return reader.LookupNamespace (prefix);
  74. }
  75. public override void MoveToAttribute (int i)
  76. {
  77. if (dtd == null) {
  78. reader.MoveToAttribute (i);
  79. currentAttribute = reader.Name;
  80. return;
  81. }
  82. // It should access attributes by *defined* order.
  83. if (elementStack.Count == 0)
  84. return;
  85. DTDAttListDeclaration decl = dtd.ElementDecls [elementStack.Peek () as string].Attributes;
  86. if (decl.Count <= i)
  87. throw new ArgumentOutOfRangeException ("i");
  88. DTDAttributeDefinition def = decl [i] as DTDAttributeDefinition;
  89. currentAttribute = def.Name;
  90. // We can ignore return value here.
  91. reader.MoveToAttribute (def.Name);
  92. }
  93. public override bool MoveToAttribute (string name)
  94. {
  95. if (dtd == null) {
  96. bool b = reader.MoveToAttribute (name);
  97. if (b)
  98. currentAttribute = reader.Name;
  99. return b;
  100. }
  101. if (elementStack.Count == 0)
  102. return false;
  103. DTDAttributeDefinition def = dtd.ElementDecls [elementStack.Peek () as string]
  104. .Attributes [name] as DTDAttributeDefinition;
  105. if (def == null)
  106. return false;
  107. reader.MoveToAttribute (name);
  108. currentAttribute = name;
  109. return true;
  110. }
  111. public override bool MoveToAttribute (string name, string ns)
  112. {
  113. if (dtd == null) {
  114. bool b = reader.MoveToAttribute (name, ns);
  115. if (b)
  116. currentAttribute = reader.Name;
  117. return b;
  118. }
  119. if (ns != String.Empty)
  120. throw new InvalidOperationException ("DTD validating reader does not support namespace.");
  121. return MoveToAttribute (name);
  122. }
  123. public override bool MoveToElement ()
  124. {
  125. bool b = reader.MoveToElement ();
  126. if (!b)
  127. return false;
  128. currentAttribute = null;
  129. return true;
  130. }
  131. public override bool MoveToFirstAttribute ()
  132. {
  133. if (dtd == null) {
  134. bool b = reader.MoveToFirstAttribute ();
  135. if (b)
  136. currentAttribute = reader.Name;
  137. return b;
  138. }
  139. // It should access attributes by *defined* order.
  140. if (elementStack.Count == 0)
  141. return false;
  142. DTDAttListDeclaration decl = dtd.ElementDecls [elementStack.Peek () as string].Attributes;
  143. if (decl.Count == 0)
  144. return false;
  145. return reader.MoveToAttribute (decl [0].Name);
  146. }
  147. public override bool MoveToNextAttribute ()
  148. {
  149. if (dtd == null) {
  150. bool b = reader.MoveToNextAttribute ();
  151. if (b)
  152. currentAttribute = reader.Name;
  153. return b;
  154. }
  155. if (currentAttribute == null)
  156. return false;
  157. DTDAttListDeclaration decl = dtd.ElementDecls [elementStack.Peek () as string].Attributes;
  158. int pos = 0;
  159. for (; pos < decl.Count; pos++) {
  160. if (decl [pos].Name == currentAttribute)
  161. break;
  162. }
  163. if (pos == decl.Count)
  164. return false;
  165. currentAttribute = decl [pos].Name;
  166. reader.MoveToAttribute (currentAttribute);
  167. return true;
  168. }
  169. bool inContent;
  170. DTDAutomata currentAutomata;
  171. DTDAutomata previousAutomata;
  172. bool isStandalone;
  173. [MonoTODO]
  174. public override bool Read ()
  175. {
  176. MoveToElement ();
  177. bool b = reader.Read ();
  178. if (!inContent && reader.NodeType == XmlNodeType.Element) {
  179. inContent = true;
  180. if (dtd == null)
  181. currentAutomata = null;
  182. else
  183. currentAutomata = dtd.RootAutomata;
  184. }
  185. if (!b) {
  186. if (elementStack.Count != 0)
  187. throw new InvalidOperationException ("Unexpected end of XmlReader.");
  188. return false;
  189. }
  190. switch (reader.NodeType) {
  191. case XmlNodeType.XmlDeclaration:
  192. if (GetAttribute ("standalone") == "yes")
  193. isStandalone = true;
  194. break;
  195. case XmlNodeType.DocumentType:
  196. #if true
  197. XmlTextReader xmlTextReader = reader as XmlTextReader;
  198. this.dtd = xmlTextReader.currentSubset;
  199. #else
  200. // It will support external DTD reader in the future.
  201. this.dtd = new DTDReader (reader).DTD;
  202. #endif
  203. break;
  204. case XmlNodeType.Element: // startElementDeriv
  205. // If no schema specification, then skip validation.
  206. if (currentAutomata == null)
  207. break;
  208. previousAutomata = currentAutomata;
  209. currentAutomata = currentAutomata.TryStartElement (reader.Name);
  210. if (currentAutomata == DTD.Invalid)
  211. throw new XmlException (reader as IXmlLineInfo,
  212. String.Format ("Invalid start element found: {0}", reader.Name));
  213. DTDElementDeclaration decl = DTD.ElementDecls [reader.Name];
  214. if (decl == null)
  215. throw new XmlException (reader as IXmlLineInfo,
  216. String.Format ("Element {0} is not declared.", reader.Name));
  217. elementStack.Push (reader.Name);
  218. automataStack.Push (currentAutomata);
  219. currentAutomata = decl.ContentModel.GetAutomata ();
  220. // TODO: check attributes
  221. if (decl.Attributes == null) {
  222. if (reader.HasAttributes)
  223. throw new XmlException (reader as IXmlLineInfo,
  224. String.Format ("Attributes are found on element {0} while it has no attribute definitions.",
  225. decl.Name));
  226. }
  227. else
  228. ValidateAttributes (decl);
  229. // If it is empty element then directly check end element.
  230. if (reader.IsEmptyElement)
  231. goto case XmlNodeType.EndElement;
  232. break;
  233. case XmlNodeType.EndElement: // endTagDeriv
  234. // If no schema specification, then skip validation.
  235. if (currentAutomata == null)
  236. break;
  237. decl = DTD.ElementDecls [reader.Name];
  238. if (decl == null)
  239. throw new XmlException (reader as IXmlLineInfo,
  240. String.Format ("Element {0} is not declared.", reader.Name));
  241. previousAutomata = currentAutomata;
  242. // Don't let currentAutomata
  243. DTDAutomata tmpAutomata = currentAutomata.TryEndElement ();
  244. if (tmpAutomata == DTD.Invalid)
  245. throw new XmlException (reader as IXmlLineInfo,
  246. String.Format ("Invalid end element found: {0}", reader.Name));
  247. elementStack.Pop ();
  248. currentAutomata = automataStack.Pop () as DTDAutomata;
  249. break;
  250. case XmlNodeType.CDATA:
  251. case XmlNodeType.SignificantWhitespace:
  252. case XmlNodeType.Text:
  253. // If no schema specification, then skip validation.
  254. if (currentAutomata == null)
  255. break;
  256. DTDElementDeclaration elem = dtd.ElementDecls [elementStack.Peek () as string];
  257. if (!elem.IsMixedContent)
  258. throw new XmlException (reader as IXmlLineInfo,
  259. String.Format ("Current element {0} does not allow character data content.", elementStack.Peek ()));
  260. break;
  261. }
  262. return true;
  263. }
  264. private void ValidateAttributes (DTDElementDeclaration decl)
  265. {
  266. Hashtable atts = new Hashtable ();
  267. if (reader.MoveToFirstAttribute ()) {
  268. do {
  269. atts.Add (reader.Name, reader.Value);
  270. } while (reader.MoveToNextAttribute ());
  271. }
  272. foreach (DTDAttributeDefinition def in decl.Attributes.Definitions) {
  273. string value = atts [def.Name] as string;
  274. switch (def.OccurenceType) {
  275. case DTDAttributeOccurenceType.Required:
  276. if (value == null)
  277. throw new XmlException (reader as IXmlLineInfo,
  278. String.Format ("Required attribute {0} in element {1} not found .",
  279. def.Name, decl.Name));
  280. break;
  281. case DTDAttributeOccurenceType.Fixed:
  282. if (value != def.DefaultValue)
  283. throw new XmlException (reader as IXmlLineInfo,
  284. String.Format ("Fixed attribute {0} in element {1} has invalid value {2}.",
  285. def.Name, decl.Name, reader.Value));
  286. break;
  287. }
  288. atts.Remove (def.Name);
  289. }
  290. if (atts.Count > 0) {
  291. string [] extraneous = new string [atts.Count];
  292. int i=0;
  293. foreach (string attribute in atts.Keys)
  294. extraneous [i++] = attribute;
  295. throw new XmlException (reader as IXmlLineInfo,
  296. String.Format ("These attributes are not declared in element {0}: {1}",
  297. decl.Name, String.Join (",", extraneous)));
  298. }
  299. reader.MoveToElement ();
  300. }
  301. public override bool ReadAttributeValue ()
  302. {
  303. return reader.ReadAttributeValue ();
  304. }
  305. public override string ReadInnerXml ()
  306. {
  307. // MS.NET 1.0 has a serious bug here. It skips validation.
  308. return reader.ReadInnerXml ();
  309. }
  310. public override string ReadOuterXml ()
  311. {
  312. // MS.NET 1.0 has a serious bug here. It skips validation.
  313. return reader.ReadOuterXml ();
  314. }
  315. public override string ReadString ()
  316. {
  317. // It seems to be the same as ReadInnerXml().
  318. return reader.ReadString ();
  319. }
  320. // public override void ResolveEntity ()
  321. // {
  322. // }
  323. public override int AttributeCount {
  324. get {
  325. if (dtd == null || !inContent)
  326. return reader.AttributeCount;
  327. if (elementStack.Count == 0)
  328. return 0;
  329. return dtd.ElementDecls [elementStack.Peek () as string]
  330. .Attributes.Count;
  331. }
  332. }
  333. [MonoTODO ("Should consider general entities.")]
  334. public override string BaseURI {
  335. get {
  336. return reader.BaseURI;
  337. }
  338. }
  339. public override bool CanResolveEntity {
  340. get { return true; }
  341. }
  342. [MonoTODO ("Should consider general entities' depth")]
  343. public override int Depth {
  344. get {
  345. return (currentAttribute != null
  346. || reader.NodeType != XmlNodeType.Attribute) ?
  347. reader.Depth : reader.Depth + 1;
  348. }
  349. }
  350. [MonoTODO]
  351. public override bool EOF {
  352. get { return reader.EOF; }
  353. }
  354. public override bool HasValue {
  355. get {
  356. return (currentAttribute != null
  357. || reader.NodeType != XmlNodeType.Attribute) ?
  358. reader.HasValue : true;
  359. }
  360. }
  361. public override bool IsDefault {
  362. get {
  363. if (currentAttribute == null)
  364. return false;
  365. return reader.GetAttribute (currentAttribute) != null;
  366. }
  367. }
  368. public override bool IsEmptyElement {
  369. get { return reader.IsEmptyElement; }
  370. }
  371. public override string this [int i] {
  372. get { return GetAttribute (i); }
  373. }
  374. public override string this [string name] {
  375. get { return GetAttribute (name); }
  376. }
  377. public override string this [string name, string ns] {
  378. get { return GetAttribute (name, ns); }
  379. }
  380. public override string LocalName {
  381. get {
  382. return (currentAttribute != null
  383. || reader.NodeType != XmlNodeType.Attribute) ?
  384. reader.LocalName : currentAttribute;
  385. }
  386. }
  387. public override string Name {
  388. get {
  389. return (currentAttribute != null
  390. || reader.NodeType != XmlNodeType.Attribute) ?
  391. reader.Name : currentAttribute;
  392. }
  393. }
  394. public override string NamespaceURI {
  395. get {
  396. if (currentAttribute != null)
  397. return String.Empty;
  398. return reader.NamespaceURI;
  399. }
  400. }
  401. public override XmlNameTable NameTable {
  402. get { return reader.NameTable; }
  403. }
  404. public override XmlNodeType NodeType {
  405. get {
  406. return (currentAttribute != null
  407. || reader.NodeType != XmlNodeType.Attribute) ?
  408. reader.NodeType : XmlNodeType.Attribute;
  409. }
  410. }
  411. public override string Prefix {
  412. get {
  413. if (currentAttribute != null) {
  414. int colon = currentAttribute.IndexOf (':');
  415. return colon < 0 ?
  416. currentAttribute :
  417. currentAttribute.Substring (0, colon - 1);
  418. }
  419. else
  420. return reader.Prefix;
  421. }
  422. }
  423. public override char QuoteChar {
  424. get {
  425. // If it is not actually on an attribute, then it returns
  426. // undefined value or '"'.
  427. return reader.QuoteChar;
  428. }
  429. }
  430. public override ReadState ReadState {
  431. get {
  432. return reader.ReadState;
  433. }
  434. }
  435. public override string Value {
  436. get {
  437. if (currentAttribute != null || reader.NodeType != XmlNodeType.Attribute)
  438. return reader.Value;
  439. return reader.GetAttribute (currentAttribute);
  440. }
  441. }
  442. [MonoTODO ("Should consider default xml:lang values.")]
  443. public override string XmlLang {
  444. get { return reader.XmlLang; }
  445. }
  446. [MonoTODO ("Should consider default xml:space values.")]
  447. public override XmlSpace XmlSpace {
  448. get { return reader.XmlSpace; }
  449. }
  450. }
  451. }