DTDValidatingReader.cs 34 KB

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