XmlValidatingReader.cs 15 KB

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