XmlValidatingReader.cs 14 KB

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