XmlValidatingReader.cs 14 KB

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