XmlValidatingReader.cs 16 KB

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