DTDValidatingReader.cs 37 KB

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