XmlValidatingReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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]
  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 NotImplementedException ();
  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. XsdValidatingReader xsvr = validatingReader as XsdValidatingReader;
  209. if (xsvr != null)
  210. xsvr.XmlResolver = value;
  211. DTDValidatingReader dvr = validatingReader as DTDValidatingReader;
  212. if (dvr != null)
  213. dvr.XmlResolver = value;
  214. }
  215. }
  216. public override XmlSpace XmlSpace {
  217. get { return validatingReader == null ? XmlSpace.None : validatingReader.XmlSpace; }
  218. }
  219. #endregion // Properties
  220. #region Methods
  221. public override void Close ()
  222. {
  223. if (validatingReader == null)
  224. sourceReader.Close ();
  225. else
  226. validatingReader.Close ();
  227. }
  228. public override string GetAttribute (int i)
  229. {
  230. return this [i];
  231. }
  232. public override string GetAttribute (string name)
  233. {
  234. return this [name];
  235. }
  236. public override string GetAttribute (string localName, string namespaceName)
  237. {
  238. return this [localName, namespaceName];
  239. }
  240. internal XmlParserContext GetInternalParserContext ()
  241. {
  242. return dtdReader != null ?
  243. dtdReader.ParserContext : null;
  244. }
  245. bool IXmlLineInfo.HasLineInfo ()
  246. {
  247. IXmlLineInfo info = validatingReader as IXmlLineInfo;
  248. return info != null ? info.HasLineInfo () : false;
  249. }
  250. public override string LookupNamespace (string prefix)
  251. {
  252. if (validatingReader != null)
  253. return sourceReader.LookupNamespace (prefix);
  254. else
  255. return validatingReader.LookupNamespace (prefix);
  256. }
  257. public override void MoveToAttribute (int i)
  258. {
  259. if (validatingReader == null)
  260. throw new IndexOutOfRangeException ("Reader is not started.");
  261. else
  262. validatingReader.MoveToAttribute (i);
  263. }
  264. public override bool MoveToAttribute (string name)
  265. {
  266. if (validatingReader == null)
  267. return false;
  268. return validatingReader.MoveToAttribute (name);
  269. }
  270. public override bool MoveToAttribute (string localName, string namespaceName)
  271. {
  272. if (validatingReader == null)
  273. return false;
  274. return validatingReader.MoveToAttribute (localName, namespaceName);
  275. }
  276. public override bool MoveToElement ()
  277. {
  278. if (validatingReader == null)
  279. return false;
  280. return validatingReader.MoveToElement ();
  281. }
  282. public override bool MoveToFirstAttribute ()
  283. {
  284. if (validatingReader == null)
  285. return false;
  286. return validatingReader.MoveToFirstAttribute ();
  287. }
  288. public override bool MoveToNextAttribute ()
  289. {
  290. if (validatingReader == null)
  291. return false;
  292. return validatingReader.MoveToNextAttribute ();
  293. }
  294. [MonoTODO]
  295. public override bool Read ()
  296. {
  297. if (ReadState == ReadState.Initial) {
  298. switch (ValidationType) {
  299. case ValidationType.Auto:
  300. case ValidationType.None:
  301. // if (schemas.Count > 0)
  302. goto case ValidationType.Schema; // might be specified by xsi:schemaLocation.
  303. // else
  304. // goto case ValidationType.DTD;
  305. case ValidationType.DTD:
  306. validatingReader = dtdReader = new DTDValidatingReader (sourceReader, this);
  307. if (specifiedResolver)
  308. dtdReader.XmlResolver = resolver;
  309. break;
  310. case ValidationType.Schema:
  311. dtdReader = new DTDValidatingReader (sourceReader, this);
  312. XsdValidatingReader xsvr = new XsdValidatingReader (dtdReader, this);
  313. foreach (XmlSchema schema in Schemas)
  314. xsvr.Schemas.Add (schema);
  315. validatingReader = xsvr;
  316. if (specifiedResolver) {
  317. xsvr.XmlResolver = resolver;
  318. dtdReader.XmlResolver = resolver;
  319. }
  320. break;
  321. case ValidationType.XDR:
  322. throw new NotImplementedException ();
  323. }
  324. schemaInfo = validatingReader as IHasXmlSchemaInfo;
  325. }
  326. return validatingReader.Read ();
  327. }
  328. public override bool ReadAttributeValue ()
  329. {
  330. if (validatingReader == null)
  331. return false;
  332. return validatingReader.ReadAttributeValue ();
  333. }
  334. #if NET_1_0
  335. // LAMESPEC: MS.NET 1.0 has critical bug here.
  336. // After calling these methods, validation does not work anymore!
  337. public override string ReadInnerXml ()
  338. {
  339. if (validatingReader == null)
  340. return "";
  341. return validatingReader.ReadInnerXml ();
  342. }
  343. public override string ReadOuterXml ()
  344. {
  345. if (validatingReader == null)
  346. return "";
  347. return validatingReader.ReadOuterXml ();
  348. }
  349. #endif
  350. #if NET_1_0
  351. public override string ReadString ()
  352. {
  353. return base.ReadStringInternal ();
  354. }
  355. #else
  356. public override string ReadString ()
  357. {
  358. return base.ReadString ();
  359. }
  360. #endif
  361. public object ReadTypedValue ()
  362. {
  363. if (dtdReader == null)
  364. return null;
  365. XmlSchemaDatatype dt = schemaInfo.SchemaType as XmlSchemaDatatype;
  366. if (dt == null)
  367. return null;
  368. switch (NodeType) {
  369. case XmlNodeType.Element:
  370. if (IsEmptyElement)
  371. return null;
  372. storedCharacters.Length = 0;
  373. bool loop = true;
  374. do {
  375. Read ();
  376. switch (NodeType) {
  377. case XmlNodeType.SignificantWhitespace:
  378. case XmlNodeType.Text:
  379. case XmlNodeType.CDATA:
  380. storedCharacters.Append (Value);
  381. break;
  382. case XmlNodeType.Comment:
  383. break;
  384. default:
  385. loop = false;
  386. break;
  387. }
  388. } while (loop && !EOF);
  389. return dt.ParseValue (storedCharacters.ToString (), NameTable, dtdReader.ParserContext.NamespaceManager);
  390. case XmlNodeType.Attribute:
  391. return dt.ParseValue (Value, NameTable, dtdReader.ParserContext.NamespaceManager);
  392. }
  393. return null;
  394. }
  395. public override void ResolveEntity ()
  396. {
  397. validatingReader.ResolveEntity ();
  398. }
  399. // It should be "protected" as usual "event model"
  400. // methods are, but validation event is not exposed,
  401. // so it is no other way to make it "internal".
  402. internal void OnValidationEvent (object o, ValidationEventArgs e)
  403. {
  404. if (ValidationEventHandler != null)
  405. ValidationEventHandler (o, e);
  406. else if (ValidationType != ValidationType.None)
  407. throw e.Exception;
  408. }
  409. #endregion // Methods
  410. #region Events and Delegates
  411. public event ValidationEventHandler ValidationEventHandler;
  412. #endregion // Events and Delegates
  413. }
  414. }