DTDValidatingReader2.cs 34 KB

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