XmlValidatingReader.cs 15 KB

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