XmlValidatingReader.cs 12 KB

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