XmlValidatingReader.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 [] Evidences {
  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. public object SchemaType {
  229. get { return schemaInfo.SchemaType; }
  230. }
  231. [MonoTODO ("We decided not to support XDR schema that spec is obsolete.")]
  232. public ValidationType ValidationType {
  233. get { return validationType; }
  234. set {
  235. if (ReadState != ReadState.Initial)
  236. throw new InvalidOperationException ("ValidationType cannot be set after the first call to Read method.");
  237. switch (validationType) {
  238. case ValidationType.Auto:
  239. case ValidationType.DTD:
  240. case ValidationType.None:
  241. case ValidationType.Schema:
  242. validationType = value;
  243. break;
  244. case ValidationType.XDR:
  245. throw new NotSupportedException ();
  246. }
  247. }
  248. }
  249. public override string Value {
  250. get { return validatingReader == null ? String.Empty : validatingReader.Value; }
  251. }
  252. public override string XmlLang {
  253. get { return validatingReader == null ? String.Empty : validatingReader.XmlLang; }
  254. }
  255. public XmlResolver XmlResolver {
  256. set {
  257. specifiedResolver = true;
  258. if (xmlTextReader != null)
  259. xmlTextReader.XmlResolver = value;
  260. else
  261. resolver = value;
  262. XsdValidatingReader xsvr = validatingReader as XsdValidatingReader;
  263. if (xsvr != null)
  264. xsvr.XmlResolver = value;
  265. DTDValidatingReader dvr = validatingReader as DTDValidatingReader;
  266. if (dvr != null)
  267. dvr.XmlResolver = value;
  268. }
  269. }
  270. public override XmlSpace XmlSpace {
  271. get { return validatingReader == null ? XmlSpace.None : validatingReader.XmlSpace; }
  272. }
  273. #endregion // Properties
  274. #region Methods
  275. public override void Close ()
  276. {
  277. if (validatingReader == null)
  278. sourceReader.Close ();
  279. else
  280. validatingReader.Close ();
  281. }
  282. public override string GetAttribute (int i)
  283. {
  284. return this [i];
  285. }
  286. public override string GetAttribute (string name)
  287. {
  288. return this [name];
  289. }
  290. public override string GetAttribute (string localName, string namespaceName)
  291. {
  292. return this [localName, namespaceName];
  293. }
  294. internal XmlParserContext GetInternalParserContext ()
  295. {
  296. return dtdReader != null ?
  297. dtdReader.ParserContext : null;
  298. }
  299. bool IXmlLineInfo.HasLineInfo ()
  300. {
  301. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  302. return info != null ? info.HasLineInfo () : false;
  303. }
  304. public override string LookupNamespace (string prefix)
  305. {
  306. if (validatingReader != null)
  307. return sourceReader.LookupNamespace (prefix);
  308. else
  309. return validatingReader.LookupNamespace (prefix);
  310. }
  311. public override void MoveToAttribute (int i)
  312. {
  313. if (validatingReader == null)
  314. throw new IndexOutOfRangeException ("Reader is not started.");
  315. else
  316. validatingReader.MoveToAttribute (i);
  317. }
  318. public override bool MoveToAttribute (string name)
  319. {
  320. if (validatingReader == null)
  321. return false;
  322. return validatingReader.MoveToAttribute (name);
  323. }
  324. public override bool MoveToAttribute (string localName, string namespaceName)
  325. {
  326. if (validatingReader == null)
  327. return false;
  328. return validatingReader.MoveToAttribute (localName, namespaceName);
  329. }
  330. public override bool MoveToElement ()
  331. {
  332. if (validatingReader == null)
  333. return false;
  334. return validatingReader.MoveToElement ();
  335. }
  336. public override bool MoveToFirstAttribute ()
  337. {
  338. if (validatingReader == null)
  339. return false;
  340. return validatingReader.MoveToFirstAttribute ();
  341. }
  342. public override bool MoveToNextAttribute ()
  343. {
  344. if (validatingReader == null)
  345. return false;
  346. return validatingReader.MoveToNextAttribute ();
  347. }
  348. [MonoTODO ("We decided not to support XDR schema that spec is obsolete.")]
  349. public override bool Read ()
  350. {
  351. if (ReadState == ReadState.Initial) {
  352. switch (ValidationType) {
  353. case ValidationType.Auto:
  354. case ValidationType.None:
  355. goto case ValidationType.Schema; // might be specified by xsi:schemaLocation.
  356. case ValidationType.DTD:
  357. validatingReader = dtdReader = new DTDValidatingReader (sourceReader, this);
  358. dtdReader.XmlResolver = Resolver;
  359. break;
  360. case ValidationType.Schema:
  361. dtdReader = new DTDValidatingReader (sourceReader, this);
  362. XsdValidatingReader xsvr = new XsdValidatingReader (dtdReader, this);
  363. foreach (XmlSchema schema in Schemas)
  364. xsvr.Schemas.Add (schema);
  365. validatingReader = xsvr;
  366. xsvr.XmlResolver = Resolver;
  367. dtdReader.XmlResolver = Resolver;
  368. break;
  369. case ValidationType.XDR:
  370. throw new NotSupportedException ();
  371. }
  372. schemaInfo = validatingReader as IHasXmlSchemaInfo;
  373. }
  374. return validatingReader.Read ();
  375. }
  376. public override bool ReadAttributeValue ()
  377. {
  378. if (validatingReader == null)
  379. return false;
  380. return validatingReader.ReadAttributeValue ();
  381. }
  382. #if NET_1_0
  383. // LAMESPEC: MS.NET 1.0 has critical bug here.
  384. // After calling these methods, validation does not work anymore!
  385. public override string ReadInnerXml ()
  386. {
  387. if (validatingReader == null)
  388. return "";
  389. return validatingReader.ReadInnerXml ();
  390. }
  391. public override string ReadOuterXml ()
  392. {
  393. if (validatingReader == null)
  394. return "";
  395. return validatingReader.ReadOuterXml ();
  396. }
  397. #endif
  398. #if NET_1_0
  399. public override string ReadString ()
  400. {
  401. return base.ReadStringInternal ();
  402. }
  403. #else
  404. public override string ReadString ()
  405. {
  406. return base.ReadString ();
  407. }
  408. #endif
  409. public object ReadTypedValue ()
  410. {
  411. if (dtdReader == null)
  412. return null;
  413. XmlSchemaDatatype dt = schemaInfo.SchemaType as XmlSchemaDatatype;
  414. if (dt == null)
  415. return null;
  416. switch (NodeType) {
  417. case XmlNodeType.Element:
  418. if (IsEmptyElement)
  419. return null;
  420. storedCharacters.Length = 0;
  421. bool loop = true;
  422. do {
  423. Read ();
  424. switch (NodeType) {
  425. case XmlNodeType.SignificantWhitespace:
  426. case XmlNodeType.Text:
  427. case XmlNodeType.CDATA:
  428. storedCharacters.Append (Value);
  429. break;
  430. case XmlNodeType.Comment:
  431. break;
  432. default:
  433. loop = false;
  434. break;
  435. }
  436. } while (loop && !EOF);
  437. return dt.ParseValue (storedCharacters.ToString (), NameTable, dtdReader.ParserContext.NamespaceManager);
  438. case XmlNodeType.Attribute:
  439. return dt.ParseValue (Value, NameTable, dtdReader.ParserContext.NamespaceManager);
  440. }
  441. return null;
  442. }
  443. public override void ResolveEntity ()
  444. {
  445. validatingReader.ResolveEntity ();
  446. }
  447. // It should be "protected" as usual "event model"
  448. // methods are, but validation event is not exposed,
  449. // so it is no other way to make it "internal".
  450. internal void OnValidationEvent (object o, ValidationEventArgs e)
  451. {
  452. if (ValidationEventHandler != null)
  453. ValidationEventHandler (o, e);
  454. else if (ValidationType != ValidationType.None && e.Severity == XmlSeverityType.Error)
  455. throw e.Exception;
  456. }
  457. #endregion // Methods
  458. #region Events and Delegates
  459. public event ValidationEventHandler ValidationEventHandler;
  460. #endregion // Events and Delegates
  461. }
  462. }