XmlValidatingReader.cs 17 KB

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