XmlValidatingReader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. //
  2. // System.Xml.XmlValidatingReader.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. // (C)2003 Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. #if NET_2_0
  32. using System.Collections.Generic;
  33. #endif
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml.Schema;
  37. using Mono.Xml;
  38. using Mono.Xml.Schema;
  39. namespace System.Xml
  40. {
  41. #if NET_2_0
  42. [Obsolete("Use XmlReader created by XmlReader.Create() method using"
  43. + " appropriate XmlReaderSettings instead.")]
  44. public class XmlValidatingReader : XmlReader, IXmlLineInfo, IXmlNamespaceResolver, IHasXmlParserContext
  45. #else
  46. public class XmlValidatingReader : XmlReader, IXmlLineInfo, IHasXmlParserContext
  47. #endif
  48. {
  49. #region Fields
  50. EntityHandling entityHandling;
  51. XmlReader sourceReader;
  52. XmlTextReader xmlTextReader;
  53. XmlReader validatingReader;
  54. XmlResolver resolver; // Only used to non-XmlTextReader XmlReader
  55. bool resolverSpecified;
  56. ValidationType validationType;
  57. // for 2.0: Now it is obsolete. It is allocated only when it is required
  58. XmlSchemaSet schemas;
  59. DTDValidatingReader dtdReader;
  60. IHasXmlSchemaInfo schemaInfo;
  61. StringBuilder storedCharacters;
  62. #endregion // Fields
  63. #region Constructors
  64. public XmlValidatingReader (XmlReader reader)
  65. {
  66. sourceReader = reader;
  67. xmlTextReader = reader as XmlTextReader;
  68. if (xmlTextReader == null)
  69. resolver = new XmlUrlResolver ();
  70. entityHandling = EntityHandling.ExpandEntities;
  71. validationType = ValidationType.Auto;
  72. storedCharacters = new StringBuilder ();
  73. }
  74. public XmlValidatingReader (Stream xmlFragment, XmlNodeType fragType, XmlParserContext context)
  75. : this (new XmlTextReader (xmlFragment, fragType, context))
  76. {
  77. }
  78. public XmlValidatingReader (string xmlFragment, XmlNodeType fragType, XmlParserContext context)
  79. : this (new XmlTextReader (xmlFragment, fragType, context))
  80. {
  81. }
  82. #endregion // Constructors
  83. #region Properties
  84. public override int AttributeCount {
  85. get { return validatingReader == null ? 0 : validatingReader.AttributeCount; }
  86. }
  87. public override string BaseURI {
  88. get { return validatingReader == null ? sourceReader.BaseURI : validatingReader.BaseURI; }
  89. }
  90. // This property for this class always return true.
  91. public override bool CanResolveEntity {
  92. get { return true; }
  93. }
  94. public override int Depth {
  95. get { return validatingReader == null ? 0 : validatingReader.Depth; }
  96. }
  97. public Encoding Encoding {
  98. get {
  99. if (xmlTextReader != null)
  100. return xmlTextReader.Encoding;
  101. else
  102. throw new NotSupportedException ("Encoding is supported only for XmlTextReader.");
  103. }
  104. }
  105. public EntityHandling EntityHandling {
  106. get { return entityHandling; }
  107. set {
  108. entityHandling = value;
  109. if (dtdReader != null)
  110. dtdReader.EntityHandling = value;
  111. }
  112. }
  113. public override bool EOF {
  114. get { return validatingReader == null ? false : validatingReader.EOF; }
  115. }
  116. #if DTD_HANDLE_EVENTS
  117. internal bool HasValidationEvent {
  118. get { return ValidationEventHandler != null; }
  119. }
  120. #endif
  121. public override bool HasValue {
  122. get { return validatingReader == null ? false : validatingReader.HasValue; }
  123. }
  124. public override bool IsDefault {
  125. get { return validatingReader == null ? false : validatingReader.IsDefault; }
  126. }
  127. public override bool IsEmptyElement {
  128. get { return validatingReader == null ? false : validatingReader.IsEmptyElement; }
  129. }
  130. #if NET_2_0
  131. #else
  132. public override string this [int i] {
  133. get { return GetAttribute (i); }
  134. }
  135. public override string this [string name] {
  136. get { return GetAttribute (name); }
  137. }
  138. public override string this [string localName, string namespaceName] {
  139. get { return GetAttribute (localName, namespaceName); }
  140. }
  141. #endif
  142. #if NET_2_0
  143. public int LineNumber {
  144. #else
  145. int IXmlLineInfo.LineNumber {
  146. #endif
  147. get {
  148. if (IsDefault)
  149. return 0;
  150. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  151. return info != null ? info.LineNumber : 0;
  152. }
  153. }
  154. #if NET_2_0
  155. public int LinePosition {
  156. #else
  157. int IXmlLineInfo.LinePosition {
  158. #endif
  159. get {
  160. if (IsDefault)
  161. return 0;
  162. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  163. return info != null ? info.LinePosition : 0;
  164. }
  165. }
  166. public override string LocalName {
  167. get {
  168. if (validatingReader == null)
  169. return String.Empty;
  170. else if (Namespaces)
  171. return validatingReader.LocalName;
  172. else
  173. return validatingReader.Name;
  174. }
  175. }
  176. public override string Name {
  177. get { return validatingReader == null ? String.Empty : validatingReader.Name; }
  178. }
  179. public bool Namespaces {
  180. get {
  181. if (xmlTextReader != null)
  182. return xmlTextReader.Namespaces;
  183. else
  184. return true;
  185. }
  186. set {
  187. if (ReadState != ReadState.Initial)
  188. throw new InvalidOperationException ("Namespaces have to be set before reading.");
  189. if (xmlTextReader != null)
  190. xmlTextReader.Namespaces = value;
  191. else
  192. throw new NotSupportedException ("Property 'Namespaces' is supported only for XmlTextReader.");
  193. }
  194. }
  195. public override string NamespaceURI {
  196. get {
  197. if (validatingReader == null)
  198. return String.Empty;
  199. else if (Namespaces)
  200. return validatingReader.NamespaceURI;
  201. else
  202. return String.Empty;
  203. }
  204. }
  205. public override XmlNameTable NameTable {
  206. get { return validatingReader == null ? sourceReader.NameTable : validatingReader.NameTable; }
  207. }
  208. public override XmlNodeType NodeType {
  209. get { return validatingReader == null ? XmlNodeType.None : validatingReader.NodeType; }
  210. }
  211. public override string Prefix {
  212. get { return validatingReader == null ? String.Empty : validatingReader.Prefix; }
  213. }
  214. public override char QuoteChar {
  215. get { return validatingReader == null ? sourceReader.QuoteChar : validatingReader.QuoteChar; }
  216. }
  217. public XmlReader Reader {
  218. get { return sourceReader; }
  219. }
  220. public override ReadState ReadState {
  221. get {
  222. if (validatingReader == null)
  223. return ReadState.Initial;
  224. return validatingReader.ReadState;
  225. }
  226. }
  227. internal XmlResolver Resolver {
  228. get {
  229. // This is special rule... MS.NET shares the
  230. // XmlResolver between XmlTextReader and
  231. // XmlValidatingReader, so we mimick that
  232. // silly behavior here.
  233. if (this.xmlTextReader != null)
  234. return this.xmlTextReader.Resolver;
  235. else if (resolverSpecified)
  236. return resolver;
  237. else
  238. return null;
  239. }
  240. }
  241. public XmlSchemaCollection Schemas {
  242. get {
  243. if (schemas == null)
  244. schemas = new XmlSchemaSet ();
  245. return schemas.SchemaCollection;
  246. }
  247. }
  248. internal void SetSchemas (XmlSchemaSet schemas)
  249. {
  250. this.schemas = schemas;
  251. }
  252. public object SchemaType {
  253. get { return schemaInfo.SchemaType; }
  254. }
  255. #if NET_2_0
  256. [MonoTODO]
  257. public override XmlReaderSettings Settings {
  258. get { return validatingReader == null ? sourceReader.Settings : validatingReader.Settings; }
  259. }
  260. #endif
  261. [MonoTODO ("We decided not to support XDR schema that spec is obsolete.")]
  262. public ValidationType ValidationType {
  263. get { return validationType; }
  264. set {
  265. if (ReadState != ReadState.Initial)
  266. throw new InvalidOperationException ("ValidationType cannot be set after the first call to Read method.");
  267. switch (validationType) {
  268. case ValidationType.Auto:
  269. case ValidationType.DTD:
  270. case ValidationType.None:
  271. case ValidationType.Schema:
  272. validationType = value;
  273. break;
  274. case ValidationType.XDR:
  275. throw new NotSupportedException ();
  276. }
  277. }
  278. }
  279. public override string Value {
  280. get { return validatingReader == null ? String.Empty : validatingReader.Value; }
  281. }
  282. public override string XmlLang {
  283. get { return validatingReader == null ? String.Empty : validatingReader.XmlLang; }
  284. }
  285. public XmlResolver XmlResolver {
  286. set {
  287. resolverSpecified = true;
  288. resolver = value;
  289. if (xmlTextReader != null)
  290. xmlTextReader.XmlResolver = value;
  291. XsdValidatingReader xsvr = validatingReader as XsdValidatingReader;
  292. if (xsvr != null)
  293. xsvr.XmlResolver = value;
  294. DTDValidatingReader dvr = validatingReader as DTDValidatingReader;
  295. if (dvr != null)
  296. dvr.XmlResolver = value;
  297. }
  298. }
  299. public override XmlSpace XmlSpace {
  300. get { return validatingReader == null ? XmlSpace.None : validatingReader.XmlSpace; }
  301. }
  302. #endregion // Properties
  303. #region Methods
  304. public override void Close ()
  305. {
  306. if (validatingReader == null)
  307. sourceReader.Close ();
  308. else
  309. validatingReader.Close ();
  310. }
  311. public override string GetAttribute (int i)
  312. {
  313. if (validatingReader == null)
  314. throw new IndexOutOfRangeException ("Reader is not started.");
  315. return validatingReader [i];
  316. }
  317. public override string GetAttribute (string name)
  318. {
  319. return validatingReader == null ? null : validatingReader [name];
  320. }
  321. public override string GetAttribute (string localName, string namespaceName)
  322. {
  323. return validatingReader == null ? null : validatingReader [localName, namespaceName];
  324. }
  325. XmlParserContext IHasXmlParserContext.ParserContext {
  326. get { return dtdReader != null ? dtdReader.ParserContext : null; }
  327. }
  328. #if NET_2_0
  329. IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  330. {
  331. return ((IHasXmlParserContext) this).ParserContext.NamespaceManager.GetNamespacesInScope (scope);
  332. }
  333. #endif
  334. #if NET_2_0
  335. public bool HasLineInfo ()
  336. #else
  337. bool IXmlLineInfo.HasLineInfo ()
  338. #endif
  339. {
  340. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  341. return info != null ? info.HasLineInfo () : false;
  342. }
  343. public override string LookupNamespace (string prefix)
  344. {
  345. if (validatingReader != null)
  346. return validatingReader.LookupNamespace (prefix);
  347. else
  348. return sourceReader.LookupNamespace (prefix);
  349. }
  350. #if NET_2_0
  351. string IXmlNamespaceResolver.LookupPrefix (string ns)
  352. {
  353. IXmlNamespaceResolver res = null;
  354. if (validatingReader != null)
  355. res = sourceReader as IXmlNamespaceResolver;
  356. else
  357. res = validatingReader as IXmlNamespaceResolver;
  358. return res != null ?
  359. res.LookupNamespace (ns) :
  360. null;
  361. }
  362. #endif
  363. public override void MoveToAttribute (int i)
  364. {
  365. if (validatingReader == null)
  366. throw new IndexOutOfRangeException ("Reader is not started.");
  367. else
  368. validatingReader.MoveToAttribute (i);
  369. }
  370. public override bool MoveToAttribute (string name)
  371. {
  372. if (validatingReader == null)
  373. return false;
  374. return validatingReader.MoveToAttribute (name);
  375. }
  376. public override bool MoveToAttribute (string localName, string namespaceName)
  377. {
  378. if (validatingReader == null)
  379. return false;
  380. return validatingReader.MoveToAttribute (localName, namespaceName);
  381. }
  382. public override bool MoveToElement ()
  383. {
  384. if (validatingReader == null)
  385. return false;
  386. return validatingReader.MoveToElement ();
  387. }
  388. public override bool MoveToFirstAttribute ()
  389. {
  390. if (validatingReader == null)
  391. return false;
  392. return validatingReader.MoveToFirstAttribute ();
  393. }
  394. public override bool MoveToNextAttribute ()
  395. {
  396. if (validatingReader == null)
  397. return false;
  398. return validatingReader.MoveToNextAttribute ();
  399. }
  400. [MonoTODO ("We decided not to support XDR schema that spec is obsolete.")]
  401. public override bool Read ()
  402. {
  403. if (ReadState == ReadState.Initial) {
  404. switch (ValidationType) {
  405. case ValidationType.Auto:
  406. case ValidationType.None:
  407. goto case ValidationType.Schema; // might be specified by xsi:schemaLocation.
  408. case ValidationType.DTD:
  409. validatingReader = dtdReader = new DTDValidatingReader (sourceReader, this);
  410. dtdReader.XmlResolver = Resolver;
  411. break;
  412. case ValidationType.Schema:
  413. dtdReader = new DTDValidatingReader (sourceReader, this);
  414. XsdValidatingReader xsvr = new XsdValidatingReader (dtdReader);
  415. xsvr.ValidationEventHandler +=
  416. new ValidationEventHandler (
  417. OnValidationEvent);
  418. xsvr.ValidationType = ValidationType;
  419. xsvr.Schemas = Schemas.SchemaSet;
  420. xsvr.XmlResolver = Resolver;
  421. validatingReader = xsvr;
  422. dtdReader.XmlResolver = Resolver;
  423. break;
  424. case ValidationType.XDR:
  425. throw new NotSupportedException ();
  426. }
  427. schemaInfo = validatingReader as IHasXmlSchemaInfo;
  428. }
  429. return validatingReader.Read ();
  430. }
  431. public override bool ReadAttributeValue ()
  432. {
  433. if (validatingReader == null)
  434. return false;
  435. return validatingReader.ReadAttributeValue ();
  436. }
  437. #if NET_1_0
  438. // LAMESPEC: MS.NET 1.0 has critical bug here.
  439. // After calling these methods, validation does not work anymore!
  440. public override string ReadInnerXml ()
  441. {
  442. if (validatingReader == null)
  443. return "";
  444. return validatingReader.ReadInnerXml ();
  445. }
  446. public override string ReadOuterXml ()
  447. {
  448. if (validatingReader == null)
  449. return "";
  450. return validatingReader.ReadOuterXml ();
  451. }
  452. #endif
  453. #if NET_1_0
  454. public override string ReadString ()
  455. {
  456. return base.ReadStringInternal ();
  457. }
  458. #else
  459. public override string ReadString ()
  460. {
  461. return base.ReadString ();
  462. }
  463. #endif
  464. #if NET_2_0
  465. [Obsolete]
  466. public override object ReadTypedValue ()
  467. #else
  468. public object ReadTypedValue ()
  469. #endif
  470. {
  471. if (dtdReader == null)
  472. return null;
  473. XmlSchemaDatatype dt = schemaInfo.SchemaType as XmlSchemaDatatype;
  474. if (dt == null)
  475. return null;
  476. switch (NodeType) {
  477. case XmlNodeType.Element:
  478. if (IsEmptyElement)
  479. return null;
  480. storedCharacters.Length = 0;
  481. bool loop = true;
  482. do {
  483. Read ();
  484. switch (NodeType) {
  485. case XmlNodeType.SignificantWhitespace:
  486. case XmlNodeType.Text:
  487. case XmlNodeType.CDATA:
  488. storedCharacters.Append (Value);
  489. break;
  490. case XmlNodeType.Comment:
  491. break;
  492. default:
  493. loop = false;
  494. break;
  495. }
  496. } while (loop && !EOF);
  497. return dt.ParseValue (storedCharacters.ToString (), NameTable, dtdReader.ParserContext.NamespaceManager);
  498. case XmlNodeType.Attribute:
  499. return dt.ParseValue (Value, NameTable, dtdReader.ParserContext.NamespaceManager);
  500. }
  501. return null;
  502. }
  503. public override void ResolveEntity ()
  504. {
  505. validatingReader.ResolveEntity ();
  506. }
  507. // It should be "protected" as usual "event model"
  508. // methods are, but validation event is not exposed,
  509. // so it is no other way to make it "internal".
  510. internal void OnValidationEvent (object o, ValidationEventArgs e)
  511. {
  512. if (ValidationEventHandler != null)
  513. ValidationEventHandler (o, e);
  514. else if (ValidationType != ValidationType.None && e.Severity == XmlSeverityType.Error)
  515. throw e.Exception;
  516. }
  517. #if NET_2_0
  518. [MonoTODO ("Check how expanded entity is handled here.")]
  519. public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
  520. {
  521. if (validatingReader != null)
  522. return validatingReader.ReadContentAsBase64 (buffer, offset, length);
  523. else
  524. return sourceReader.ReadContentAsBase64 (buffer, offset, length);
  525. }
  526. [MonoTODO ("Check how expanded entity is handled here.")]
  527. public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
  528. {
  529. if (validatingReader != null)
  530. return validatingReader.ReadContentAsBinHex (buffer, offset, length);
  531. else
  532. return sourceReader.ReadContentAsBinHex (buffer, offset, length);
  533. }
  534. [MonoTODO ("Check how expanded entity is handled here.")]
  535. public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
  536. {
  537. if (validatingReader != null)
  538. return validatingReader.ReadElementContentAsBase64 (buffer, offset, length);
  539. else
  540. return sourceReader.ReadElementContentAsBase64 (buffer, offset, length);
  541. }
  542. [MonoTODO ("Check how expanded entity is handled here.")]
  543. public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
  544. {
  545. if (validatingReader != null)
  546. return validatingReader.ReadElementContentAsBinHex (buffer, offset, length);
  547. else
  548. return sourceReader.ReadElementContentAsBinHex (buffer, offset, length);
  549. }
  550. #endif
  551. #endregion // Methods
  552. #region Events and Delegates
  553. public event ValidationEventHandler ValidationEventHandler;
  554. #endregion // Events and Delegates
  555. }
  556. }