XmlValidatingReader.cs 16 KB

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