DTDValidatingReader2.cs 34 KB

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