XmlValidatingReader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. XmlSchemaCollection 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 XmlSchemaCollection (NameTable);
  252. return schemas;
  253. }
  254. }
  255. public object SchemaType {
  256. get { return schemaInfo.SchemaType; }
  257. }
  258. #if NET_2_0
  259. [MonoTODO]
  260. public override XmlReaderSettings Settings {
  261. get { return validatingReader == null ? sourceReader.Settings : validatingReader.Settings; }
  262. }
  263. #endif
  264. [MonoTODO]
  265. // We decided not to support XDR schema; it is obsolete.
  266. public ValidationType ValidationType {
  267. get { return validationType; }
  268. set {
  269. if (ReadState != ReadState.Initial)
  270. throw new InvalidOperationException ("ValidationType cannot be set after the first call to Read method.");
  271. switch (validationType) {
  272. case ValidationType.Auto:
  273. case ValidationType.DTD:
  274. case ValidationType.None:
  275. case ValidationType.Schema:
  276. validationType = value;
  277. break;
  278. case ValidationType.XDR:
  279. throw new NotSupportedException ();
  280. }
  281. }
  282. }
  283. public override string Value {
  284. get { return validatingReader == null ? String.Empty : validatingReader.Value; }
  285. }
  286. public override string XmlLang {
  287. get { return validatingReader == null ? String.Empty : validatingReader.XmlLang; }
  288. }
  289. public XmlResolver XmlResolver {
  290. set {
  291. resolverSpecified = true;
  292. resolver = value;
  293. if (xmlTextReader != null)
  294. xmlTextReader.XmlResolver = value;
  295. XsdValidatingReader xsvr = validatingReader as XsdValidatingReader;
  296. if (xsvr != null)
  297. xsvr.XmlResolver = value;
  298. DTDValidatingReader dvr = validatingReader as DTDValidatingReader;
  299. if (dvr != null)
  300. dvr.XmlResolver = value;
  301. }
  302. }
  303. public override XmlSpace XmlSpace {
  304. get { return validatingReader == null ? XmlSpace.None : validatingReader.XmlSpace; }
  305. }
  306. #endregion // Properties
  307. #region Methods
  308. public override void Close ()
  309. {
  310. if (validatingReader == null)
  311. sourceReader.Close ();
  312. else
  313. validatingReader.Close ();
  314. }
  315. public override string GetAttribute (int i)
  316. {
  317. if (validatingReader == null)
  318. throw new IndexOutOfRangeException ("Reader is not started.");
  319. return validatingReader [i];
  320. }
  321. public override string GetAttribute (string name)
  322. {
  323. return validatingReader == null ? null : validatingReader [name];
  324. }
  325. public override string GetAttribute (string localName, string namespaceName)
  326. {
  327. return validatingReader == null ? null : validatingReader [localName, namespaceName];
  328. }
  329. XmlParserContext IHasXmlParserContext.ParserContext {
  330. get {
  331. if (dtdReader != null)
  332. return dtdReader.ParserContext;
  333. IHasXmlParserContext i = sourceReader as IHasXmlParserContext;
  334. return i != null ? i.ParserContext : null;
  335. }
  336. }
  337. #if NET_2_0
  338. IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope (XmlNamespaceScope scope)
  339. {
  340. return ((IHasXmlParserContext) this).ParserContext.NamespaceManager.GetNamespacesInScope (scope);
  341. }
  342. #endif
  343. #if NET_2_0
  344. public bool HasLineInfo ()
  345. #else
  346. bool IXmlLineInfo.HasLineInfo ()
  347. #endif
  348. {
  349. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  350. return info != null ? info.HasLineInfo () : false;
  351. }
  352. public override string LookupNamespace (string prefix)
  353. {
  354. if (validatingReader != null)
  355. return validatingReader.LookupNamespace (prefix);
  356. else
  357. return sourceReader.LookupNamespace (prefix);
  358. }
  359. #if NET_2_0
  360. string IXmlNamespaceResolver.LookupPrefix (string ns)
  361. {
  362. IXmlNamespaceResolver res = null;
  363. if (validatingReader != null)
  364. res = sourceReader as IXmlNamespaceResolver;
  365. else
  366. res = validatingReader as IXmlNamespaceResolver;
  367. return res != null ?
  368. res.LookupNamespace (ns) :
  369. null;
  370. }
  371. #endif
  372. public override void MoveToAttribute (int i)
  373. {
  374. if (validatingReader == null)
  375. throw new IndexOutOfRangeException ("Reader is not started.");
  376. else
  377. validatingReader.MoveToAttribute (i);
  378. }
  379. public override bool MoveToAttribute (string name)
  380. {
  381. if (validatingReader == null)
  382. return false;
  383. return validatingReader.MoveToAttribute (name);
  384. }
  385. public override bool MoveToAttribute (string localName, string namespaceName)
  386. {
  387. if (validatingReader == null)
  388. return false;
  389. return validatingReader.MoveToAttribute (localName, namespaceName);
  390. }
  391. public override bool MoveToElement ()
  392. {
  393. if (validatingReader == null)
  394. return false;
  395. return validatingReader.MoveToElement ();
  396. }
  397. public override bool MoveToFirstAttribute ()
  398. {
  399. if (validatingReader == null)
  400. return false;
  401. return validatingReader.MoveToFirstAttribute ();
  402. }
  403. public override bool MoveToNextAttribute ()
  404. {
  405. if (validatingReader == null)
  406. return false;
  407. return validatingReader.MoveToNextAttribute ();
  408. }
  409. [MonoTODO]
  410. // We decided not to support XDR schema; it is obsolete.
  411. public override bool Read ()
  412. {
  413. if (validatingReader == null) {
  414. switch (ValidationType) {
  415. case ValidationType.Auto:
  416. case ValidationType.None:
  417. goto case ValidationType.Schema; // might be specified by xsi:schemaLocation.
  418. case ValidationType.DTD:
  419. validatingReader = dtdReader = new DTDValidatingReader (sourceReader, this);
  420. dtdReader.XmlResolver = Resolver;
  421. break;
  422. case ValidationType.Schema:
  423. dtdReader = new DTDValidatingReader (sourceReader, this);
  424. XsdValidatingReader xsvr = new XsdValidatingReader (dtdReader);
  425. xsvr.ValidationEventHandler +=
  426. new ValidationEventHandler (
  427. OnValidationEvent);
  428. xsvr.ValidationType = ValidationType;
  429. xsvr.Schemas = Schemas.SchemaSet;
  430. xsvr.XmlResolver = Resolver;
  431. validatingReader = xsvr;
  432. dtdReader.XmlResolver = Resolver;
  433. break;
  434. case ValidationType.XDR:
  435. throw new NotSupportedException ();
  436. }
  437. schemaInfo = validatingReader as IHasXmlSchemaInfo;
  438. }
  439. return validatingReader.Read ();
  440. }
  441. public override bool ReadAttributeValue ()
  442. {
  443. if (validatingReader == null)
  444. return false;
  445. return validatingReader.ReadAttributeValue ();
  446. }
  447. public override string ReadString ()
  448. {
  449. return base.ReadString ();
  450. }
  451. public object ReadTypedValue ()
  452. {
  453. if (dtdReader == null)
  454. return null;
  455. XmlSchemaDatatype dt = schemaInfo.SchemaType as XmlSchemaDatatype;
  456. if (dt == null) {
  457. XmlSchemaType st = schemaInfo.SchemaType as XmlSchemaType;
  458. if (st != null)
  459. dt = st.Datatype;
  460. }
  461. if (dt == null)
  462. return null;
  463. switch (NodeType) {
  464. case XmlNodeType.Element:
  465. if (IsEmptyElement)
  466. return null;
  467. storedCharacters.Length = 0;
  468. bool loop = true;
  469. do {
  470. Read ();
  471. switch (NodeType) {
  472. case XmlNodeType.Whitespace:
  473. case XmlNodeType.SignificantWhitespace:
  474. case XmlNodeType.Text:
  475. case XmlNodeType.CDATA:
  476. storedCharacters.Append (Value);
  477. break;
  478. case XmlNodeType.Comment:
  479. break;
  480. default:
  481. loop = false;
  482. break;
  483. }
  484. } while (loop && !EOF);
  485. return dt.ParseValue (storedCharacters.ToString (), NameTable, dtdReader.ParserContext.NamespaceManager);
  486. case XmlNodeType.Attribute:
  487. return dt.ParseValue (Value, NameTable, dtdReader.ParserContext.NamespaceManager);
  488. }
  489. return null;
  490. }
  491. public override void ResolveEntity ()
  492. {
  493. validatingReader.ResolveEntity ();
  494. }
  495. // It should be "protected" as usual "event model"
  496. // methods are, but validation event is not exposed,
  497. // so it is no other way to make it "internal".
  498. internal void OnValidationEvent (object o, ValidationEventArgs e)
  499. {
  500. if (ValidationEventHandler != null)
  501. ValidationEventHandler (o, e);
  502. else if (ValidationType != ValidationType.None && e.Severity == XmlSeverityType.Error)
  503. throw e.Exception;
  504. }
  505. #if NET_2_0
  506. [MonoTODO] // FIXME: Check how expanded entity is handled here.
  507. public override int ReadContentAsBase64 (byte [] buffer, int offset, int length)
  508. {
  509. if (validatingReader != null)
  510. return validatingReader.ReadContentAsBase64 (buffer, offset, length);
  511. else
  512. return sourceReader.ReadContentAsBase64 (buffer, offset, length);
  513. }
  514. [MonoTODO] // FIXME: Check how expanded entity is handled here.
  515. public override int ReadContentAsBinHex (byte [] buffer, int offset, int length)
  516. {
  517. if (validatingReader != null)
  518. return validatingReader.ReadContentAsBinHex (buffer, offset, length);
  519. else
  520. return sourceReader.ReadContentAsBinHex (buffer, offset, length);
  521. }
  522. [MonoTODO] // FIXME: Check how expanded entity is handled here.
  523. public override int ReadElementContentAsBase64 (byte [] buffer, int offset, int length)
  524. {
  525. if (validatingReader != null)
  526. return validatingReader.ReadElementContentAsBase64 (buffer, offset, length);
  527. else
  528. return sourceReader.ReadElementContentAsBase64 (buffer, offset, length);
  529. }
  530. [MonoTODO] // FIXME: Check how expanded entity is handled here.
  531. public override int ReadElementContentAsBinHex (byte [] buffer, int offset, int length)
  532. {
  533. if (validatingReader != null)
  534. return validatingReader.ReadElementContentAsBinHex (buffer, offset, length);
  535. else
  536. return sourceReader.ReadElementContentAsBinHex (buffer, offset, length);
  537. }
  538. #endif
  539. #endregion // Methods
  540. #region Events and Delegates
  541. public event ValidationEventHandler ValidationEventHandler;
  542. #endregion // Events and Delegates
  543. }
  544. }