XmlValidatingReader.cs 12 KB

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