DTDValidatingReader.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. //
  2. // DTDValidatingReader.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto ([email protected])
  6. //
  7. // (C)2003 Atsushi Enomoto
  8. // (C)2004 Novell Inc.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.IO;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. #if NET_2_0
  37. using XmlTextReaderImpl = Mono.Xml2.XmlTextReader;
  38. #else
  39. using XmlTextReaderImpl = System.Xml.XmlTextReader;
  40. #endif
  41. namespace Mono.Xml
  42. {
  43. #if NET_2_0
  44. internal class DTDValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext, IHasXmlSchemaInfo, IXmlNamespaceResolver
  45. #else
  46. internal class DTDValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext, IHasXmlSchemaInfo
  47. #endif
  48. {
  49. public DTDValidatingReader (XmlReader reader)
  50. : this (reader, null)
  51. {
  52. }
  53. internal DTDValidatingReader (XmlReader reader,
  54. XmlValidatingReader validatingReader)
  55. {
  56. entityReaderStack = new Stack ();
  57. entityReaderDepthStack = new Stack ();
  58. this.reader = reader;
  59. this.sourceTextReader = reader as XmlTextReader;
  60. elementStack = new Stack ();
  61. automataStack = new Stack ();
  62. attributes = new ArrayList ();
  63. attributeValues = new Hashtable ();
  64. attributeLocalNames = new Hashtable ();
  65. attributeNamespaces = new Hashtable ();
  66. attributePrefixes = new Hashtable ();
  67. nsmgr = new XmlNamespaceManager (reader.NameTable);
  68. this.validatingReader = validatingReader;
  69. valueBuilder = new StringBuilder ();
  70. idList = new ArrayList ();
  71. missingIDReferences = new ArrayList ();
  72. XmlTextReader xtReader = reader as XmlTextReader;
  73. if (xtReader != null) {
  74. resolver = xtReader.Resolver;
  75. }
  76. else
  77. resolver = new XmlUrlResolver ();
  78. }
  79. Stack entityReaderStack;
  80. Stack entityReaderDepthStack;
  81. XmlReader reader;
  82. XmlTextReader sourceTextReader;
  83. XmlTextReader nextEntityReader;
  84. DTDObjectModel dtd;
  85. Stack elementStack;
  86. Stack automataStack;
  87. string currentElement;
  88. string currentAttribute;
  89. string currentTextValue;
  90. string constructingTextValue;
  91. bool shouldResetCurrentTextValue;
  92. bool consumedAttribute;
  93. bool insideContent;
  94. DTDAutomata currentAutomata;
  95. DTDAutomata previousAutomata;
  96. bool isStandalone;
  97. ArrayList attributes;
  98. Hashtable attributeValues;
  99. Hashtable attributeLocalNames;
  100. Hashtable attributeNamespaces;
  101. Hashtable attributePrefixes;
  102. XmlNamespaceManager nsmgr;
  103. StringBuilder valueBuilder;
  104. ArrayList idList;
  105. ArrayList missingIDReferences;
  106. XmlResolver resolver;
  107. EntityHandling currentEntityHandling;
  108. bool isSignificantWhitespace;
  109. bool isWhitespace;
  110. bool isText;
  111. bool dontResetTextType;
  112. // This field is used to get properties and to raise events.
  113. XmlValidatingReader validatingReader;
  114. public DTDObjectModel DTD {
  115. get { return dtd; }
  116. }
  117. public EntityHandling EntityHandling {
  118. get { return currentEntityHandling; }
  119. set { currentEntityHandling = value; }
  120. }
  121. public override void Close ()
  122. {
  123. reader.Close ();
  124. }
  125. // We had already done attribute validation, so can ignore name.
  126. public override string GetAttribute (int i)
  127. {
  128. if (currentTextValue != null)
  129. throw new IndexOutOfRangeException ("Specified index is out of range: " + i);
  130. if (dtd == null)
  131. return reader.GetAttribute (i);
  132. if (attributes.Count <= i)
  133. throw new IndexOutOfRangeException ("Specified index is out of range: " + i);
  134. string attrName = (string) attributes [i];
  135. return FilterNormalization (attrName, (string) attributeValues [attrName]);
  136. }
  137. public override string GetAttribute (string name)
  138. {
  139. if (currentTextValue != null)
  140. return null;
  141. if (dtd == null)
  142. return reader.GetAttribute (name);
  143. return FilterNormalization (name, (string) attributeValues [name]);
  144. }
  145. public override string GetAttribute (string name, string ns)
  146. {
  147. if (currentTextValue != null)
  148. return null;
  149. if (dtd == null)
  150. return reader.GetAttribute (name, ns);
  151. return reader.GetAttribute ((string) attributeLocalNames [name], ns);
  152. }
  153. #if NET_2_0
  154. IDictionary IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  155. {
  156. IXmlNamespaceResolver res = reader as IXmlNamespaceResolver;
  157. return res != null ? res.GetNamespacesInScope (scope) : new Hashtable ();
  158. }
  159. #endif
  160. bool IXmlLineInfo.HasLineInfo ()
  161. {
  162. IXmlLineInfo ixli = reader as IXmlLineInfo;
  163. if (ixli != null)
  164. return ixli.HasLineInfo ();
  165. else
  166. return false;
  167. }
  168. public override string LookupNamespace (string prefix)
  169. {
  170. return nsmgr.LookupNamespace (NameTable.Get (prefix));
  171. }
  172. #if NET_2_0
  173. string IXmlNamespaceResolver.LookupPrefix (string ns)
  174. {
  175. IXmlNamespaceResolver res = reader as IXmlNamespaceResolver;
  176. return res != null ? res.LookupPrefix (ns) : null;
  177. }
  178. #endif
  179. public override void MoveToAttribute (int i)
  180. {
  181. if (currentTextValue != null)
  182. throw new IndexOutOfRangeException ("The index is out of range.");
  183. if (dtd == null) {
  184. reader.MoveToAttribute (i);
  185. currentAttribute = reader.Name;
  186. consumedAttribute = false;
  187. return;
  188. }
  189. if (currentElement == null)
  190. throw new IndexOutOfRangeException ("The index is out of range.");
  191. if (attributes.Count > i) {
  192. if (reader.AttributeCount > i)
  193. reader.MoveToAttribute (i);
  194. currentAttribute = (string) attributes [i];
  195. consumedAttribute = false;
  196. return;
  197. } else
  198. throw new IndexOutOfRangeException ("The index is out of range.");
  199. }
  200. public override bool MoveToAttribute (string name)
  201. {
  202. if (currentTextValue != null)
  203. return false;
  204. if (dtd == null) {
  205. bool b = reader.MoveToAttribute (name);
  206. if (b) {
  207. currentAttribute = reader.Name;
  208. consumedAttribute = false;
  209. }
  210. return b;
  211. }
  212. if (currentElement == null)
  213. return false;
  214. int idx = attributes.IndexOf (name);
  215. if (idx >= 0) {
  216. currentAttribute = name;
  217. consumedAttribute = false;
  218. return true;
  219. }
  220. return false;
  221. }
  222. public override bool MoveToAttribute (string name, string ns)
  223. {
  224. if (currentTextValue != null)
  225. return false;
  226. if (dtd == null) {
  227. bool b = reader.MoveToAttribute (name, ns);
  228. if (b) {
  229. currentAttribute = reader.Name;
  230. consumedAttribute = false;
  231. }
  232. return b;
  233. }
  234. if (reader.MoveToAttribute (name, ns)) {
  235. currentAttribute = reader.Name;
  236. consumedAttribute = false;
  237. return true;
  238. }
  239. for (int i = 0; i < attributes.Count; i++) {
  240. string iter = (string) attributes [i];
  241. if ((string) attributeLocalNames [iter] == name)
  242. return MoveToAttribute (iter);
  243. }
  244. return false;
  245. }
  246. public override bool MoveToElement ()
  247. {
  248. if (currentTextValue != null)
  249. return false;
  250. bool b = reader.MoveToElement ();
  251. if (!b && !IsDefault)
  252. return false;
  253. currentAttribute = null;
  254. consumedAttribute = false;
  255. return true;
  256. }
  257. public override bool MoveToFirstAttribute ()
  258. {
  259. if (currentTextValue != null)
  260. return false;
  261. if (dtd == null) {
  262. bool b = reader.MoveToFirstAttribute ();
  263. if (b) {
  264. currentAttribute = reader.Name;
  265. consumedAttribute = false;
  266. }
  267. return b;
  268. }
  269. if (attributes.Count == 0)
  270. return false;
  271. currentAttribute = (string) attributes [0];
  272. reader.MoveToAttribute (currentAttribute);
  273. consumedAttribute = false;
  274. return true;
  275. }
  276. public override bool MoveToNextAttribute ()
  277. {
  278. if (currentTextValue != null)
  279. return false;
  280. if (dtd == null) {
  281. bool b = reader.MoveToNextAttribute ();
  282. if (b) {
  283. currentAttribute = reader.Name;
  284. consumedAttribute = false;
  285. }
  286. return b;
  287. }
  288. if (currentAttribute == null)
  289. return MoveToFirstAttribute ();
  290. int idx = attributes.IndexOf (currentAttribute);
  291. if (idx + 1 < attributes.Count) {
  292. currentAttribute = (string) attributes [idx + 1];
  293. reader.MoveToAttribute (currentAttribute);
  294. consumedAttribute = false;
  295. return true;
  296. } else
  297. return false;
  298. }
  299. /*
  300. private void OnValidationEvent (object o, ValidationEventArgs e)
  301. {
  302. this.HandleError (e.Exception, e.Severity);
  303. }
  304. */
  305. public override bool Read ()
  306. {
  307. if (currentTextValue != null)
  308. shouldResetCurrentTextValue = true;
  309. MoveToElement ();
  310. currentElement = null;
  311. currentAttribute = null;
  312. consumedAttribute = false;
  313. attributes.Clear ();
  314. attributeLocalNames.Clear ();
  315. attributeValues.Clear ();
  316. attributeNamespaces.Clear ();
  317. attributePrefixes.Clear ();
  318. isWhitespace = false;
  319. isSignificantWhitespace = false;
  320. isText = false;
  321. dontResetTextType = false;
  322. bool b = ReadContent () || currentTextValue != null;
  323. if (!b && this.missingIDReferences.Count > 0) {
  324. this.HandleError ("Missing ID reference was found: " +
  325. String.Join (",", missingIDReferences.ToArray (typeof (string)) as string []),
  326. XmlSeverityType.Error);
  327. // Don't output the same errors so many times.
  328. this.missingIDReferences.Clear ();
  329. }
  330. if (validatingReader != null)
  331. EntityHandling = validatingReader.EntityHandling;
  332. return b;
  333. }
  334. private bool ReadContent ()
  335. {
  336. if (nextEntityReader != null) {
  337. if (DTD == null || DTD.EntityDecls [reader.Name] == null)
  338. throw NotWFError (String.Format ("Entity '{0}' was not declared.", reader.Name));
  339. entityReaderStack.Push (reader);
  340. entityReaderDepthStack.Push (Depth);
  341. reader = sourceTextReader = nextEntityReader;
  342. nextEntityReader = null;
  343. return ReadContent ();
  344. } else if (reader.EOF && entityReaderStack.Count > 0) {
  345. reader.Close ();
  346. reader = entityReaderStack.Pop () as XmlReader;
  347. entityReaderDepthStack.Pop ();
  348. sourceTextReader = reader as XmlTextReader;
  349. return ReadContent ();
  350. }
  351. bool b = !reader.EOF;
  352. if (shouldResetCurrentTextValue) {
  353. currentTextValue = null;
  354. shouldResetCurrentTextValue = false;
  355. }
  356. else
  357. b = reader.Read ();
  358. if (!insideContent && reader.NodeType == XmlNodeType.Element) {
  359. insideContent = true;
  360. if (dtd == null)
  361. currentAutomata = null;
  362. else
  363. currentAutomata = dtd.RootAutomata;
  364. }
  365. if (!b) {
  366. if (entityReaderStack.Count > 0) {
  367. if (validatingReader.EntityHandling == EntityHandling.ExpandEntities)
  368. return ReadContent ();
  369. else
  370. return true; // EndEntity
  371. }
  372. if (elementStack.Count != 0)
  373. throw new InvalidOperationException ("Unexpected end of XmlReader.");
  374. return false;
  375. }
  376. DTDElementDeclaration elem = null;
  377. switch (reader.NodeType) {
  378. case XmlNodeType.XmlDeclaration:
  379. if (GetAttribute ("standalone") == "yes")
  380. isStandalone = true;
  381. ValidateAttributes (null, false);
  382. break;
  383. case XmlNodeType.DocumentType:
  384. // XmlTextReader xmlTextReader = reader as XmlTextReader;
  385. IHasXmlParserContext ctx = reader as IHasXmlParserContext;
  386. if (ctx != null)
  387. dtd = ctx.ParserContext.Dtd;
  388. if (dtd == null) {
  389. XmlTextReaderImpl xmlTextReader = new XmlTextReaderImpl ("", XmlNodeType.Document, null);
  390. xmlTextReader.XmlResolver = resolver;
  391. xmlTextReader.GenerateDTDObjectModel (reader.Name,
  392. reader ["PUBLIC"], reader ["SYSTEM"], reader.Value);
  393. dtd = xmlTextReader.DTD;
  394. }
  395. // Validity Constraints Check.
  396. if (DTD.Errors.Length > 0)
  397. for (int i = 0; i < DTD.Errors.Length; i++)
  398. HandleError (DTD.Errors [i].Message, XmlSeverityType.Error);
  399. // NData target exists.
  400. foreach (DTDEntityDeclaration ent in dtd.EntityDecls.Values)
  401. if (ent.NotationName != null && dtd.NotationDecls [ent.NotationName] == null)
  402. this.HandleError ("Target notation was not found for NData in entity declaration " + ent.Name + ".",
  403. XmlSeverityType.Error);
  404. // NOTATION exists for attribute default values
  405. foreach (DTDAttListDeclaration attListIter in dtd.AttListDecls.Values)
  406. foreach (DTDAttributeDefinition def in attListIter.Definitions)
  407. if (def.Datatype.TokenizedType == XmlTokenizedType.NOTATION) {
  408. foreach (string notation in def.EnumeratedNotations)
  409. if (dtd.NotationDecls [notation] == null)
  410. this.HandleError ("Target notation was not found for NOTATION typed attribute default " + def.Name + ".",
  411. XmlSeverityType.Error);
  412. }
  413. break;
  414. case XmlNodeType.Element:
  415. if (constructingTextValue != null) {
  416. currentTextValue = constructingTextValue;
  417. constructingTextValue = null;
  418. if (isWhitespace)
  419. ValidateWhitespaceNode ();
  420. return true;
  421. }
  422. elementStack.Push (reader.Name);
  423. // startElementDeriv
  424. // If no schema specification, then skip validation.
  425. if (currentAutomata == null) {
  426. ValidateAttributes (null, false);
  427. if (reader.IsEmptyElement)
  428. goto case XmlNodeType.EndElement;
  429. break;
  430. }
  431. previousAutomata = currentAutomata;
  432. currentAutomata = currentAutomata.TryStartElement (reader.Name);
  433. if (currentAutomata == DTD.Invalid) {
  434. HandleError (String.Format ("Invalid start element found: {0}", reader.Name),
  435. XmlSeverityType.Error);
  436. currentAutomata = previousAutomata;
  437. }
  438. elem = DTD.ElementDecls [reader.Name];
  439. if (elem == null) {
  440. HandleError (String.Format ("Element {0} is not declared.", reader.Name),
  441. XmlSeverityType.Error);
  442. currentAutomata = previousAutomata;
  443. }
  444. currentElement = Name;
  445. automataStack.Push (currentAutomata);
  446. if (elem != null) // i.e. not invalid
  447. currentAutomata = elem.ContentModel.GetAutomata ();
  448. DTDAttListDeclaration attList = dtd.AttListDecls [currentElement];
  449. if (attList != null) {
  450. // check attributes
  451. ValidateAttributes (attList, true);
  452. currentAttribute = null;
  453. } else {
  454. if (reader.HasAttributes) {
  455. HandleError (String.Format (
  456. "Attributes are found on element {0} while it has no attribute definitions.", currentElement),
  457. XmlSeverityType.Error);
  458. }
  459. // SetupValidityIgnorantAttributes ();
  460. ValidateAttributes (null, false);
  461. }
  462. // If it is empty element then directly check end element.
  463. if (reader.IsEmptyElement)
  464. goto case XmlNodeType.EndElement;
  465. break;
  466. case XmlNodeType.EndElement:
  467. if (constructingTextValue != null) {
  468. currentTextValue = constructingTextValue;
  469. constructingTextValue = null;
  470. return true;
  471. }
  472. nsmgr.PopScope ();
  473. elementStack.Pop ();
  474. // endElementDeriv
  475. // If no schema specification, then skip validation.
  476. if (currentAutomata == null)
  477. break;
  478. elem = DTD.ElementDecls [reader.Name];
  479. if (elem == null) {
  480. HandleError (String.Format ("Element {0} is not declared.", reader.Name),
  481. XmlSeverityType.Error);
  482. }
  483. previousAutomata = currentAutomata;
  484. // Don't let currentAutomata
  485. DTDAutomata tmpAutomata = currentAutomata.TryEndElement ();
  486. if (tmpAutomata == DTD.Invalid) {
  487. HandleError (String.Format ("Invalid end element found: {0}", reader.Name),
  488. XmlSeverityType.Error);
  489. currentAutomata = previousAutomata;
  490. }
  491. currentAutomata = automataStack.Pop () as DTDAutomata;
  492. break;
  493. case XmlNodeType.CDATA:
  494. isSignificantWhitespace = isWhitespace = false;
  495. isText = true;
  496. ValidateText ();
  497. if (currentTextValue != null) {
  498. currentTextValue = constructingTextValue;
  499. constructingTextValue = null;
  500. return true;
  501. }
  502. break;
  503. case XmlNodeType.SignificantWhitespace:
  504. if (!isText)
  505. isSignificantWhitespace = true;
  506. isWhitespace = false;
  507. dontResetTextType = true;
  508. goto case XmlNodeType.DocumentFragment;
  509. case XmlNodeType.Text:
  510. isWhitespace = isSignificantWhitespace = false;
  511. isText = true;
  512. goto case XmlNodeType.DocumentFragment;
  513. case XmlNodeType.DocumentFragment:
  514. // it should not happen, but in case if
  515. // XmlReader really returns it, just ignore.
  516. if (reader.NodeType == XmlNodeType.DocumentFragment)
  517. break;
  518. ValidateText ();
  519. if (entityReaderStack.Count > 0 && validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
  520. constructingTextValue += reader.Value;
  521. return ReadContent ();
  522. }
  523. break;
  524. case XmlNodeType.Whitespace:
  525. if (!isText && !isSignificantWhitespace)
  526. isWhitespace = true;
  527. goto case XmlNodeType.DocumentFragment;
  528. case XmlNodeType.EntityReference:
  529. if (validatingReader.EntityHandling == EntityHandling.ExpandEntities) {
  530. ResolveEntity ();
  531. return ReadContent ();
  532. }
  533. break;
  534. }
  535. if (isWhitespace)
  536. ValidateWhitespaceNode ();
  537. currentTextValue = constructingTextValue;
  538. constructingTextValue = null;
  539. MoveToElement ();
  540. return true;
  541. }
  542. private void ValidateText ()
  543. {
  544. if (currentAutomata == null)
  545. return;
  546. DTDElementDeclaration elem = null;
  547. if (elementStack.Count > 0)
  548. elem = dtd.ElementDecls [elementStack.Peek () as string];
  549. // Here element should have been already validated, so
  550. // if no matching declaration is found, simply ignore.
  551. if (elem != null && !elem.IsMixedContent && !elem.IsAny && !isWhitespace) {
  552. HandleError (String.Format ("Current element {0} does not allow character data content.", elementStack.Peek () as string),
  553. XmlSeverityType.Error);
  554. currentAutomata = previousAutomata;
  555. }
  556. }
  557. private void ValidateWhitespaceNode ()
  558. {
  559. // VC Standalone Document Declaration (2.9)
  560. if (this.isStandalone && DTD != null && elementStack.Count > 0) {
  561. DTDElementDeclaration elem = DTD.ElementDecls [elementStack.Peek () as string];
  562. if (elem != null && !elem.IsInternalSubset && !elem.IsMixedContent && !elem.IsAny && !elem.IsEmpty)
  563. HandleError ("In standalone document, whitespace cannot appear in an element whose declaration explicitly contains child content model, not Mixed content.", XmlSeverityType.Error);
  564. }
  565. }
  566. private XmlException NotWFError (string message)
  567. {
  568. return new XmlException (this as IXmlLineInfo, BaseURI, message);
  569. }
  570. private void HandleError (string message, XmlSeverityType severity)
  571. {
  572. if (validatingReader != null &&
  573. validatingReader.ValidationType == ValidationType.None)
  574. return;
  575. IXmlLineInfo info = this as IXmlLineInfo;
  576. bool hasLine = info.HasLineInfo ();
  577. XmlSchemaException ex = new XmlSchemaException (
  578. message,
  579. hasLine ? info.LineNumber : 0,
  580. hasLine ? info.LinePosition : 0,
  581. null,
  582. BaseURI,
  583. null);
  584. HandleError (ex, severity);
  585. }
  586. private void HandleError (XmlSchemaException ex, XmlSeverityType severity)
  587. {
  588. if (validatingReader != null &&
  589. validatingReader.ValidationType == ValidationType.None)
  590. return;
  591. if (validatingReader != null)
  592. this.validatingReader.OnValidationEvent (this,
  593. new ValidationEventArgs (ex, ex.Message, severity));
  594. else if (severity == XmlSeverityType.Error)
  595. throw ex;
  596. }
  597. Stack attributeValueEntityStack = new Stack ();
  598. private void ValidateAttributes (DTDAttListDeclaration decl, bool validate)
  599. {
  600. DtdValidateAttributes (decl, validate);
  601. foreach (string attr in attributes)
  602. if (attr == "xmlns" ||
  603. String.CompareOrdinal (attr, 0, "xmlns:", 0, 6) == 0)
  604. nsmgr.AddNamespace (
  605. attr == "xmlns" ? String.Empty : (string) attributeLocalNames [attr],
  606. (string) attributeValues [attr]);
  607. nsmgr.PushScope ();
  608. foreach (string attr in attributes) {
  609. string prefix = attr == "xmlns" ? "xmlns" : attributePrefixes [attr] as string;
  610. if (prefix == String.Empty)
  611. attributeNamespaces.Add (attr, String.Empty);
  612. else
  613. attributeNamespaces.Add (attr, LookupNamespace (prefix));
  614. }
  615. }
  616. private void DtdValidateAttributes (DTDAttListDeclaration decl, bool validate)
  617. {
  618. while (reader.MoveToNextAttribute ()) {
  619. string attrName = reader.Name;
  620. this.currentAttribute = attrName;
  621. attributes.Add (attrName);
  622. attributeLocalNames.Add (attrName, reader.LocalName);
  623. attributePrefixes.Add (attrName, reader.Prefix);
  624. XmlReader targetReader = reader;
  625. string attrValue = null;
  626. // It always resolves entity references on attributes (documented as such).
  627. // if (currentEntityHandling == EntityHandling.ExpandCharEntities)
  628. // attrValue = reader.Value;
  629. // else
  630. {
  631. while (attributeValueEntityStack.Count >= 0) {
  632. if (!targetReader.ReadAttributeValue ()) {
  633. if (attributeValueEntityStack.Count > 0) {
  634. targetReader = attributeValueEntityStack.Pop () as XmlReader;
  635. continue;
  636. } else
  637. break;
  638. }
  639. switch (targetReader.NodeType) {
  640. case XmlNodeType.EntityReference:
  641. DTDEntityDeclaration edecl = DTD.EntityDecls [targetReader.Name];
  642. if (edecl == null) {
  643. HandleError (String.Format ("Referenced entity {0} is not declared.", targetReader.Name),
  644. XmlSeverityType.Error);
  645. } else {
  646. XmlTextReader etr = new XmlTextReader (edecl.EntityValue, XmlNodeType.Attribute, ParserContext);
  647. attributeValueEntityStack.Push (targetReader);
  648. targetReader = etr;
  649. continue;
  650. }
  651. break;
  652. case XmlNodeType.EndEntity:
  653. break;
  654. default:
  655. if (attrValue != null) {
  656. valueBuilder.Append (attrValue);
  657. attrValue = null;
  658. }
  659. if (valueBuilder.Length != 0)
  660. valueBuilder.Append (targetReader.Value);
  661. else
  662. attrValue = targetReader.Value;
  663. break;
  664. }
  665. }
  666. if (attrValue == null) {
  667. attrValue = valueBuilder.ToString ();
  668. valueBuilder.Length = 0;
  669. }
  670. }
  671. reader.MoveToElement ();
  672. reader.MoveToAttribute (attrName);
  673. attributeValues.Add (attrName, attrValue);
  674. if (!validate)
  675. continue;
  676. // Validation
  677. DTDAttributeDefinition def = decl [reader.Name];
  678. if (def == null) {
  679. HandleError (String.Format ("Attribute {0} is not declared.", reader.Name),
  680. XmlSeverityType.Error);
  681. continue;
  682. }
  683. // check enumeration constraint
  684. if (def.EnumeratedAttributeDeclaration.Count > 0)
  685. if (!def.EnumeratedAttributeDeclaration.Contains (
  686. FilterNormalization (reader.Name, attrValue)))
  687. HandleError (String.Format ("Attribute enumeration constraint error in attribute {0}, value {1}.",
  688. reader.Name, attrValue), XmlSeverityType.Error);
  689. if (def.EnumeratedNotations.Count > 0)
  690. if (!def.EnumeratedNotations.Contains (
  691. FilterNormalization (reader.Name, attrValue)))
  692. HandleError (String.Format ("Attribute notation enumeration constraint error in attribute {0}, value {1}.",
  693. reader.Name, attrValue), XmlSeverityType.Error);
  694. // check type constraint
  695. string normalized = null;
  696. if (def.Datatype != null)
  697. normalized = FilterNormalization (def.Name, attrValue);
  698. else
  699. normalized = attrValue;
  700. DTDEntityDeclaration ent;
  701. // Common process to get list value
  702. string [] list = null;
  703. switch (def.Datatype.TokenizedType) {
  704. case XmlTokenizedType.IDREFS:
  705. case XmlTokenizedType.ENTITIES:
  706. case XmlTokenizedType.NMTOKENS:
  707. try {
  708. list = def.Datatype.ParseValue (normalized, NameTable, null) as string [];
  709. } catch (Exception) {
  710. HandleError ("Attribute value is invalid against its data type.", XmlSeverityType.Error);
  711. list = new string [0];
  712. }
  713. break;
  714. default:
  715. try {
  716. def.Datatype.ParseValue (normalized, NameTable, null);
  717. } catch (Exception ex) {
  718. HandleError (String.Format ("Attribute value is invalid against its data type '{0}'. {1}", def.Datatype, ex.Message), XmlSeverityType.Error);
  719. }
  720. break;
  721. }
  722. switch (def.Datatype.TokenizedType) {
  723. case XmlTokenizedType.ID:
  724. if (this.idList.Contains (normalized)) {
  725. HandleError (String.Format ("Node with ID {0} was already appeared.", attrValue),
  726. XmlSeverityType.Error);
  727. } else {
  728. if (missingIDReferences.Contains (normalized))
  729. missingIDReferences.Remove (normalized);
  730. idList.Add (normalized);
  731. }
  732. break;
  733. case XmlTokenizedType.IDREF:
  734. if (!idList.Contains (normalized))
  735. missingIDReferences.Add (normalized);
  736. break;
  737. case XmlTokenizedType.IDREFS:
  738. for (int i = 0; i < list.Length; i++) {
  739. string idref = list [i];
  740. if (!idList.Contains (idref))
  741. missingIDReferences.Add (idref);
  742. }
  743. break;
  744. case XmlTokenizedType.ENTITY:
  745. ent = dtd.EntityDecls [normalized];
  746. if (ent == null)
  747. HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
  748. else if (ent.NotationName == null)
  749. HandleError ("The entity specified by entity type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
  750. break;
  751. case XmlTokenizedType.ENTITIES:
  752. for (int i = 0; i < list.Length; i++) {
  753. string entref = list [i];
  754. ent = dtd.EntityDecls [FilterNormalization (reader.Name, entref)];
  755. if (ent == null)
  756. HandleError ("Reference to undeclared entity was found in attribute: " + reader.Name + ".", XmlSeverityType.Error);
  757. else if (ent.NotationName == null)
  758. HandleError ("The entity specified by ENTITIES type value must be an unparsed entity. The entity definition has no NDATA in attribute: " + reader.Name + ".", XmlSeverityType.Error);
  759. }
  760. break;
  761. // case XmlTokenizedType.NMTOKEN: nothing to do
  762. // case XmlTokenizedType.NMTOKENS: nothing to do
  763. }
  764. if (isStandalone && !def.IsInternalSubset &&
  765. attrValue != normalized)
  766. HandleError ("In standalone document, attribute value characters must not be checked against external definition.", XmlSeverityType.Error);
  767. if (def.OccurenceType ==
  768. DTDAttributeOccurenceType.Fixed &&
  769. attrValue != def.DefaultValue)
  770. HandleError (String.Format ("Fixed attribute {0} in element {1} has invalid value {2}.",
  771. def.Name, decl.Name, attrValue),
  772. XmlSeverityType.Error);
  773. }
  774. if (validate)
  775. VerifyDeclaredAttributes (decl);
  776. MoveToElement ();
  777. }
  778. private void VerifyDeclaredAttributes (DTDAttListDeclaration decl)
  779. {
  780. // Check if all required attributes exist, and/or
  781. // if there is default values, then add them.
  782. for (int i = 0; i < decl.Definitions.Count; i++) {
  783. DTDAttributeDefinition def = (DTDAttributeDefinition) decl.Definitions [i];
  784. if (attributes.Contains (def.Name))
  785. continue;
  786. if (def.OccurenceType == DTDAttributeOccurenceType.Required) {
  787. HandleError (String.Format ("Required attribute {0} in element {1} not found .",
  788. def.Name, decl.Name),
  789. XmlSeverityType.Error);
  790. continue;
  791. }
  792. else if (def.DefaultValue == null)
  793. continue;
  794. if (this.isStandalone && !def.IsInternalSubset)
  795. HandleError ("In standalone document, external default value definition must not be applied.", XmlSeverityType.Error);
  796. switch (validatingReader.ValidationType) {
  797. case ValidationType.Auto:
  798. if (validatingReader.Schemas.Count == 0)
  799. goto case ValidationType.DTD;
  800. break;
  801. case ValidationType.DTD:
  802. case ValidationType.None:
  803. // Other than them, ignore DTD defaults.
  804. attributes.Add (def.Name);
  805. int colonAt = def.Name.IndexOf (':');
  806. attributeLocalNames.Add (def.Name,
  807. colonAt < 0 ? def.Name :
  808. def.Name.Substring (colonAt + 1));
  809. string prefix = colonAt < 0 ?
  810. String.Empty :
  811. def.Name.Substring (0, colonAt);
  812. attributePrefixes.Add (def.Name, prefix);
  813. attributeValues.Add (def.Name, def.DefaultValue);
  814. break;
  815. }
  816. }
  817. }
  818. public override bool ReadAttributeValue ()
  819. {
  820. if (consumedAttribute)
  821. return false;
  822. if (NodeType == XmlNodeType.Attribute &&
  823. currentEntityHandling == EntityHandling.ExpandEntities) {
  824. consumedAttribute = true;
  825. return true;
  826. }
  827. else if (IsDefault) {
  828. consumedAttribute = true;
  829. return true;
  830. }
  831. else
  832. return reader.ReadAttributeValue ();
  833. }
  834. #if NET_1_0
  835. public override string ReadInnerXml ()
  836. {
  837. // MS.NET 1.0 has a serious bug here. It skips validation.
  838. return reader.ReadInnerXml ();
  839. }
  840. public override string ReadOuterXml ()
  841. {
  842. // MS.NET 1.0 has a serious bug here. It skips validation.
  843. return reader.ReadOuterXml ();
  844. }
  845. #endif
  846. public override string ReadString ()
  847. {
  848. // It seems to be the same as ReadInnerXml().
  849. return base.ReadStringInternal ();
  850. }
  851. public override void ResolveEntity ()
  852. {
  853. if (resolver == null)
  854. return;
  855. // "reader." is required since NodeType must not be entityref by nature.
  856. if (reader.NodeType != XmlNodeType.EntityReference)
  857. throw new InvalidOperationException ("The current node is not an Entity Reference");
  858. DTDEntityDeclaration entity = DTD != null ? DTD.EntityDecls [reader.Name] as DTDEntityDeclaration : null;
  859. XmlNodeType xmlReaderNodeType =
  860. (currentAttribute != null) ? XmlNodeType.Attribute : XmlNodeType.Element;
  861. #if NET_2_0
  862. if (entity == null)
  863. throw NotWFError (String.Format ("Reference to undeclared entity '{0}'.", reader.Name));
  864. #endif
  865. // MS.NET 1.x ignores undeclared entity reference here..
  866. if (entity != null && entity.SystemId != null) {
  867. Uri baseUri = entity.BaseURI == String.Empty ? null : new Uri (entity.BaseURI);
  868. Stream stream = resolver.GetEntity (resolver.ResolveUri (baseUri, entity.SystemId), null, typeof (Stream)) as Stream;
  869. nextEntityReader = new XmlTextReader (stream, xmlReaderNodeType, ParserContext);
  870. } else {
  871. string replacementText =
  872. (entity != null) ? entity.EntityValue : String.Empty;
  873. nextEntityReader = new XmlTextReader (replacementText, xmlReaderNodeType, ParserContext);
  874. }
  875. nextEntityReader.XmlResolver = resolver;
  876. }
  877. public override int AttributeCount {
  878. get {
  879. if (currentTextValue != null)
  880. return 0;
  881. if (dtd == null || !insideContent)
  882. return reader.AttributeCount;
  883. return attributes.Count;
  884. }
  885. }
  886. public override string BaseURI {
  887. get {
  888. return reader.BaseURI;
  889. }
  890. }
  891. public override bool CanResolveEntity {
  892. get { return true; }
  893. }
  894. public override int Depth {
  895. get {
  896. int baseNum = reader.Depth;
  897. if (entityReaderDepthStack.Count > 0) {
  898. baseNum += (int) entityReaderDepthStack.Peek ();
  899. if (NodeType != XmlNodeType.EndEntity)
  900. baseNum++;
  901. }
  902. if (currentTextValue != null && reader.NodeType == XmlNodeType.EndElement)
  903. baseNum++;
  904. return IsDefault ? baseNum + 1 : baseNum;
  905. }
  906. }
  907. public override bool EOF {
  908. get { return reader.EOF && entityReaderStack.Count == 0; }
  909. }
  910. public override bool HasValue {
  911. get {
  912. return IsDefault ? true :
  913. currentTextValue != null ? true :
  914. reader.HasValue; }
  915. }
  916. public override bool IsDefault {
  917. get {
  918. if (currentTextValue != null)
  919. return false;
  920. if (currentAttribute == null)
  921. return false;
  922. return reader.GetAttribute (currentAttribute) == null;
  923. }
  924. }
  925. public override bool IsEmptyElement {
  926. get {
  927. if (currentTextValue != null)
  928. return false;
  929. return reader.IsEmptyElement;
  930. }
  931. }
  932. public override string this [int i] {
  933. get { return GetAttribute (i); }
  934. }
  935. public override string this [string name] {
  936. get { return GetAttribute (name); }
  937. }
  938. public override string this [string name, string ns] {
  939. get { return GetAttribute (name, ns); }
  940. }
  941. public int LineNumber {
  942. get {
  943. IXmlLineInfo info = reader as IXmlLineInfo;
  944. return (info != null) ? info.LineNumber : 0;
  945. }
  946. }
  947. public int LinePosition {
  948. get {
  949. IXmlLineInfo info = reader as IXmlLineInfo;
  950. return (info != null) ? info.LinePosition : 0;
  951. }
  952. }
  953. public override string LocalName {
  954. get {
  955. if (currentTextValue != null || consumedAttribute)
  956. return String.Empty;
  957. if (entityReaderStack.Count > 0 && reader.EOF)
  958. return ((XmlReader) entityReaderStack.Peek ()).LocalName; // name of EndEntity
  959. else if (NodeType == XmlNodeType.Attribute)
  960. return (string) attributeLocalNames [currentAttribute];
  961. else
  962. return reader.LocalName;
  963. }
  964. }
  965. public override string Name {
  966. get {
  967. if (currentTextValue != null || consumedAttribute)
  968. return String.Empty;
  969. if (entityReaderStack.Count > 0 && reader.EOF)
  970. return ((XmlReader) entityReaderStack.Peek ()).Name; // name of EndEntity
  971. else if (NodeType == XmlNodeType.Attribute)
  972. return currentAttribute;
  973. else
  974. return reader.Name;
  975. }
  976. }
  977. public override string NamespaceURI {
  978. get {
  979. if (currentTextValue != null || consumedAttribute)
  980. return String.Empty;
  981. else if (NodeType == XmlNodeType.Attribute)
  982. return (string) attributeNamespaces [currentAttribute];
  983. return nsmgr.LookupNamespace (Prefix);
  984. }
  985. }
  986. public override XmlNameTable NameTable {
  987. get { return reader.NameTable; }
  988. }
  989. public override XmlNodeType NodeType {
  990. get {
  991. if (currentTextValue != null)
  992. return isSignificantWhitespace ? XmlNodeType.SignificantWhitespace :
  993. isWhitespace ? XmlNodeType.Whitespace :
  994. XmlNodeType.Text;
  995. if (entityReaderStack.Count > 0 && reader.EOF)
  996. return XmlNodeType.EndEntity;
  997. // If consumedAttribute is true, then entities must be resolved.
  998. return consumedAttribute ? XmlNodeType.Text :
  999. IsDefault ? XmlNodeType.Attribute :
  1000. reader.NodeType;
  1001. }
  1002. }
  1003. public XmlParserContext ParserContext {
  1004. get { return XmlSchemaUtil.GetParserContext (reader); }
  1005. }
  1006. public override string Prefix {
  1007. get {
  1008. if (currentTextValue != null || consumedAttribute)
  1009. return String.Empty;
  1010. else if (NodeType == XmlNodeType.Attribute)
  1011. return (string) attributePrefixes [currentAttribute];
  1012. else
  1013. return reader.Prefix;
  1014. }
  1015. }
  1016. public override char QuoteChar {
  1017. get {
  1018. // If it is not actually on an attribute, then it returns
  1019. // undefined value or '"'.
  1020. return reader.QuoteChar;
  1021. }
  1022. }
  1023. public override ReadState ReadState {
  1024. get {
  1025. if (reader.ReadState == ReadState.EndOfFile && currentTextValue != null)
  1026. return ReadState.Interactive;
  1027. return reader.ReadState;
  1028. }
  1029. }
  1030. public object SchemaType {
  1031. get {
  1032. if (currentElement == null)
  1033. return null;
  1034. DTDAttListDeclaration decl =
  1035. DTD.AttListDecls [currentElement];
  1036. DTDAttributeDefinition def =
  1037. decl != null ? decl [currentAttribute] : null;
  1038. return def != null ? def.Datatype : null;
  1039. }
  1040. }
  1041. char [] whitespaceChars = new char [] {' '};
  1042. private string FilterNormalization (string attrName, string rawValue)
  1043. {
  1044. if (DTD == null || NodeType != XmlNodeType.Attribute ||
  1045. sourceTextReader == null ||
  1046. !sourceTextReader.Normalization)
  1047. return rawValue;
  1048. DTDAttributeDefinition def =
  1049. dtd.AttListDecls [currentElement].Get (attrName);
  1050. valueBuilder.Append (rawValue);
  1051. valueBuilder.Replace ('\r', ' ');
  1052. valueBuilder.Replace ('\n', ' ');
  1053. valueBuilder.Replace ('\t', ' ');
  1054. try {
  1055. if (def.Datatype.TokenizedType == XmlTokenizedType.CDATA)
  1056. return valueBuilder.ToString ();
  1057. for (int i=0; i < valueBuilder.Length; i++) {
  1058. if (valueBuilder [i] != ' ')
  1059. continue;
  1060. while (++i < valueBuilder.Length && valueBuilder [i] == ' ')
  1061. valueBuilder.Remove (i, 1);
  1062. }
  1063. return valueBuilder.ToString ().Trim (whitespaceChars);
  1064. } finally {
  1065. valueBuilder.Length = 0;
  1066. }
  1067. }
  1068. public override string Value {
  1069. get {
  1070. if (currentTextValue != null)
  1071. return currentTextValue;
  1072. // As to this property, MS.NET seems ignorant of EntityHandling...
  1073. else if (NodeType == XmlNodeType.Attribute
  1074. // It also covers default attribute text.
  1075. || consumedAttribute)
  1076. return FilterNormalization (Name, (string) attributeValues [currentAttribute]);
  1077. else
  1078. return FilterNormalization (Name, reader.Value);
  1079. }
  1080. }
  1081. public override string XmlLang {
  1082. get {
  1083. string val = this ["xml:lang"];
  1084. return val != null ? val : reader.XmlLang;
  1085. }
  1086. }
  1087. public XmlResolver XmlResolver {
  1088. set {
  1089. resolver = value;
  1090. }
  1091. }
  1092. public override XmlSpace XmlSpace {
  1093. get {
  1094. string val = this ["xml:space"];
  1095. switch (val) {
  1096. case "preserve":
  1097. return XmlSpace.Preserve;
  1098. case "default":
  1099. return XmlSpace.Default;
  1100. default:
  1101. return reader.XmlSpace;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }