XmlReader.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. //
  2. // XmlReader.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. // (C) 2003 Atsushi Enomoto
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.IO;
  34. using System.Security.Policy;
  35. using System.Text;
  36. using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
  37. using Mono.Xml; // only required for NET_2_0 (XmlFilterReader)
  38. namespace System.Xml
  39. {
  40. #if NET_2_0
  41. public abstract class XmlReader : IDisposable, IXmlDataEvidence
  42. #else
  43. public abstract class XmlReader
  44. #endif
  45. {
  46. private StringBuilder readStringBuffer;
  47. private Evidence evidence;
  48. #if NET_2_0
  49. private XmlReaderSettings settings;
  50. #endif
  51. #region Constructor
  52. protected XmlReader ()
  53. {
  54. }
  55. #endregion
  56. #region Properties
  57. public abstract int AttributeCount { get; }
  58. public abstract string BaseURI { get; }
  59. public virtual bool CanResolveEntity
  60. {
  61. get { return false; }
  62. }
  63. public abstract int Depth { get; }
  64. public abstract bool EOF { get; }
  65. #if NET_2_0
  66. [MonoTODO]
  67. public virtual Evidence Evidence {
  68. get { return evidence; }
  69. }
  70. #endif
  71. public virtual bool HasAttributes
  72. {
  73. get { return AttributeCount > 0; }
  74. }
  75. public abstract bool HasValue { get; }
  76. public abstract bool IsDefault { get; }
  77. public abstract bool IsEmptyElement { get; }
  78. #if NET_2_0
  79. public virtual string this [int i] {
  80. get { return GetAttribute (i); }
  81. }
  82. public virtual string this [string name] {
  83. get { return GetAttribute (name); }
  84. }
  85. public virtual string this [string name, string namespaceURI] {
  86. get { return GetAttribute (name, namespaceURI); }
  87. }
  88. #else
  89. public abstract string this [int i] { get; }
  90. public abstract string this [string name] { get; }
  91. public abstract string this [string localName, string namespaceName] { get; }
  92. #endif
  93. public abstract string LocalName { get; }
  94. public abstract string Name { get; }
  95. public abstract string NamespaceURI { get; }
  96. public abstract XmlNameTable NameTable { get; }
  97. public abstract XmlNodeType NodeType { get; }
  98. public abstract string Prefix { get; }
  99. #if NET_2_0
  100. public virtual char QuoteChar {
  101. get { return '\"'; }
  102. }
  103. #else
  104. public abstract char QuoteChar { get; }
  105. #endif
  106. public abstract ReadState ReadState { get; }
  107. #if NET_2_0
  108. [MonoTODO]
  109. public virtual IXmlSchemaInfo SchemaInfo {
  110. get {
  111. throw new NotImplementedException ();
  112. }
  113. }
  114. public virtual XmlReaderSettings Settings {
  115. get { return settings; }
  116. }
  117. #endif
  118. public abstract string Value { get; }
  119. public abstract string XmlLang { get; }
  120. public abstract XmlSpace XmlSpace { get; }
  121. #endregion
  122. #region Methods
  123. public abstract void Close ();
  124. #if NET_2_0
  125. public static XmlReader Create (Stream stream)
  126. {
  127. return Create (stream, null, null, new XmlUrlResolver (), null);
  128. }
  129. public static XmlReader Create (string url)
  130. {
  131. return Create (url, null);
  132. }
  133. public static XmlReader Create (TextReader reader)
  134. {
  135. return Create (reader, null, new XmlUrlResolver (), null);
  136. }
  137. public static XmlReader Create (string url, XmlReaderSettings settings)
  138. {
  139. return Create (url, new XmlUrlResolver (), settings);
  140. }
  141. public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
  142. {
  143. return Create (reader, new XmlUrlResolver (), settings);
  144. }
  145. [MonoTODO ("CheckCharacters, ConformanceLevel, IgnoreSchemaXXX etc.")]
  146. public static XmlReader Create (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  147. {
  148. return CreateFilteredXmlReader (reader, resolver, settings);
  149. }
  150. [MonoTODO ("CheckCharacters, ConformanceLevel, IgnoreSchemaXXX etc.")]
  151. public static XmlReader Create (string url, XmlResolver resolver, XmlReaderSettings settings)
  152. {
  153. return CreateCustomizedTextReader (new XmlTextReader (url), resolver, settings);
  154. }
  155. [MonoTODO ("CheckCharacters, ConformanceLevel, IgnoreSchemaXXX etc.")]
  156. public static XmlReader Create (TextReader reader, string baseUri, XmlResolver resolver, XmlReaderSettings settings)
  157. {
  158. return CreateCustomizedTextReader (new XmlTextReader (baseUri, reader), resolver, settings);
  159. }
  160. [MonoTODO ("CheckCharacters, ConformanceLevel, IgnoreSchemaXXX etc.")]
  161. public static XmlReader Create (Stream stream, string baseUri, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
  162. {
  163. return CreateCustomizedTextReader (encoding == null ? new XmlTextReader (baseUri, stream) : new XmlTextReader (baseUri, new StreamReader (stream, encoding)), resolver, settings);
  164. }
  165. private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlResolver resolver, XmlReaderSettings settings)
  166. {
  167. reader.XmlResolver = resolver;
  168. if (settings == null)
  169. settings = new XmlReaderSettings ();
  170. if (settings.ProhibitDtd)
  171. reader.ProhibitDtd = true;
  172. if (!settings.CheckCharacters)
  173. throw new NotImplementedException ();
  174. // I guess it might be changed in 2.0 RTM to set true
  175. // as default, or just disappear. It goes against
  176. // XmlTextReader's default usage and users will have
  177. // to close input manually (that's annoying). Moreover,
  178. // MS XmlTextReader consumes text input more than
  179. // actually read and users can acquire those extra
  180. // consumption by GetRemainder() that returns different
  181. // TextReader.
  182. reader.CloseInput = settings.CloseInput;
  183. // I would like to support it in detail later;
  184. // MSDN description looks source of confusion. We don't
  185. // need examples, but precise list of how it works.
  186. reader.Conformance = settings.ConformanceLevel;
  187. reader.AdjustLineInfoOffset (settings.LineNumberOffset,
  188. settings.LinePositionOffset);
  189. // FIXME: maybe we had better create XmlParserContext.
  190. if (settings.NameTable != null)
  191. reader.SetNameTable (settings.NameTable);
  192. return CreateFilteredXmlReader (reader, resolver, settings);
  193. }
  194. private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  195. {
  196. reader = CreateValidatingXmlReader (reader, settings);
  197. if (reader.Settings != null ||
  198. settings.IgnoreComments ||
  199. settings.IgnoreProcessingInstructions ||
  200. settings.IgnoreWhitespace)
  201. return new XmlFilterReader (reader, settings);
  202. else {
  203. reader.settings = settings;
  204. return reader;
  205. }
  206. }
  207. private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlReaderSettings settings)
  208. {
  209. XmlValidatingReader xvr = null;
  210. if (settings.DtdValidate) {
  211. xvr = new XmlValidatingReader (reader);
  212. if (!settings.XsdValidate)
  213. xvr.ValidationType = ValidationType.DTD;
  214. // otherwise .Auto by default.
  215. } else if (settings.XsdValidate) {
  216. xvr = new XmlValidatingReader (reader);
  217. xvr.ValidationType = ValidationType.Schema;
  218. }
  219. if (xvr != null)
  220. xvr.SetSchemas (settings.Schemas);
  221. if (settings.IgnoreIdentityConstraints)
  222. throw new NotImplementedException ();
  223. if (!settings.IgnoreInlineSchema)
  224. throw new NotImplementedException ();
  225. if (!settings.IgnoreSchemaLocation)
  226. throw new NotImplementedException ();
  227. if (!settings.IgnoreValidationWarnings)
  228. throw new NotImplementedException ();
  229. return xvr != null ? xvr : reader;
  230. }
  231. #endif
  232. #if NET_2_0
  233. public virtual void Dispose ()
  234. {
  235. if (ReadState != ReadState.Closed)
  236. Close ();
  237. }
  238. #endif
  239. public abstract string GetAttribute (int i);
  240. public abstract string GetAttribute (string name);
  241. public abstract string GetAttribute (
  242. string localName,
  243. string namespaceName);
  244. public static bool IsName (string s)
  245. {
  246. return s != null && XmlChar.IsName (s);
  247. }
  248. public static bool IsNameToken (string s)
  249. {
  250. return s != null && XmlChar.IsNmToken (s);
  251. }
  252. public virtual bool IsStartElement ()
  253. {
  254. return (MoveToContent () == XmlNodeType.Element);
  255. }
  256. public virtual bool IsStartElement (string name)
  257. {
  258. if (!IsStartElement ())
  259. return false;
  260. return (Name == name);
  261. }
  262. public virtual bool IsStartElement (string localName, string namespaceName)
  263. {
  264. if (!IsStartElement ())
  265. return false;
  266. return (LocalName == localName && NamespaceURI == namespaceName);
  267. }
  268. public abstract string LookupNamespace (string prefix);
  269. public abstract void MoveToAttribute (int i);
  270. public abstract bool MoveToAttribute (string name);
  271. public abstract bool MoveToAttribute (
  272. string localName,
  273. string namespaceName);
  274. private bool IsContent (XmlNodeType nodeType)
  275. {
  276. /* MS doc says:
  277. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  278. */
  279. switch (nodeType) {
  280. case XmlNodeType.Text:
  281. return true;
  282. case XmlNodeType.CDATA:
  283. return true;
  284. case XmlNodeType.Element:
  285. return true;
  286. case XmlNodeType.EndElement:
  287. return true;
  288. case XmlNodeType.EntityReference:
  289. return true;
  290. case XmlNodeType.EndEntity:
  291. return true;
  292. }
  293. return false;
  294. }
  295. public virtual XmlNodeType MoveToContent ()
  296. {
  297. if (NodeType == XmlNodeType.Attribute)
  298. MoveToElement ();
  299. do {
  300. if (IsContent (NodeType))
  301. return NodeType;
  302. Read ();
  303. } while (!EOF);
  304. return XmlNodeType.None;
  305. }
  306. public abstract bool MoveToElement ();
  307. public abstract bool MoveToFirstAttribute ();
  308. public abstract bool MoveToNextAttribute ();
  309. public abstract bool Read ();
  310. public abstract bool ReadAttributeValue ();
  311. public virtual string ReadElementString ()
  312. {
  313. if (MoveToContent () != XmlNodeType.Element) {
  314. string error = String.Format ("'{0}' is an invalid node type.",
  315. NodeType.ToString ());
  316. throw new XmlException (this as IXmlLineInfo, error);
  317. }
  318. string result = String.Empty;
  319. if (!IsEmptyElement) {
  320. Read ();
  321. result = ReadString ();
  322. if (NodeType != XmlNodeType.EndElement) {
  323. string error = String.Format ("'{0}' is an invalid node type.",
  324. NodeType.ToString ());
  325. throw new XmlException (this as IXmlLineInfo, error);
  326. }
  327. }
  328. Read ();
  329. return result;
  330. }
  331. public virtual string ReadElementString (string name)
  332. {
  333. if (MoveToContent () != XmlNodeType.Element) {
  334. string error = String.Format ("'{0}' is an invalid node type.",
  335. NodeType.ToString ());
  336. throw new XmlException (this as IXmlLineInfo, error);
  337. }
  338. if (name != Name) {
  339. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  340. Name, NamespaceURI);
  341. throw new XmlException (this as IXmlLineInfo, error);
  342. }
  343. string result = String.Empty;
  344. if (!IsEmptyElement) {
  345. Read ();
  346. result = ReadString ();
  347. if (NodeType != XmlNodeType.EndElement) {
  348. string error = String.Format ("'{0}' is an invalid node type.",
  349. NodeType.ToString ());
  350. throw new XmlException (this as IXmlLineInfo, error);
  351. }
  352. }
  353. Read ();
  354. return result;
  355. }
  356. public virtual string ReadElementString (string localName, string namespaceName)
  357. {
  358. if (MoveToContent () != XmlNodeType.Element) {
  359. string error = String.Format ("'{0}' is an invalid node type.",
  360. NodeType.ToString ());
  361. throw new XmlException (this as IXmlLineInfo, error);
  362. }
  363. if (localName != LocalName || NamespaceURI != namespaceName) {
  364. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  365. LocalName, NamespaceURI);
  366. throw new XmlException (this as IXmlLineInfo, error);
  367. }
  368. string result = String.Empty;
  369. if (!IsEmptyElement) {
  370. Read ();
  371. result = ReadString ();
  372. if (NodeType != XmlNodeType.EndElement) {
  373. string error = String.Format ("'{0}' is an invalid node type.",
  374. NodeType.ToString ());
  375. throw new XmlException (this as IXmlLineInfo, error);
  376. }
  377. }
  378. Read ();
  379. return result;
  380. }
  381. public virtual void ReadEndElement ()
  382. {
  383. if (MoveToContent () != XmlNodeType.EndElement) {
  384. string error = String.Format ("'{0}' is an invalid node type.",
  385. NodeType.ToString ());
  386. throw new XmlException (this as IXmlLineInfo, error);
  387. }
  388. Read ();
  389. }
  390. #if NET_1_0
  391. public abstract string ReadInnerXml ();
  392. public abstract string ReadOuterXml ();
  393. #else
  394. public virtual string ReadInnerXml ()
  395. {
  396. return ReadInnerXmlInternal ();
  397. }
  398. public virtual string ReadOuterXml ()
  399. {
  400. return ReadOuterXmlInternal ();
  401. }
  402. #endif
  403. internal string ReadInnerXmlInternal ()
  404. {
  405. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  406. return String.Empty;
  407. StringWriter sw = new StringWriter ();
  408. XmlTextWriter xtw = new XmlTextWriter (sw);
  409. if (NodeType == XmlNodeType.Element) {
  410. if (IsEmptyElement) {
  411. Read ();
  412. return String.Empty;
  413. }
  414. int startDepth = Depth;
  415. Read ();
  416. while (startDepth < Depth) {
  417. if (ReadState != ReadState.Interactive)
  418. throw new XmlException ("Unexpected end of the XML reader.");
  419. xtw.WriteNode (this, false);
  420. }
  421. // reader is now end element, then proceed once more.
  422. Read ();
  423. }
  424. else
  425. xtw.WriteNode (this, false);
  426. return sw.ToString ();
  427. }
  428. internal string ReadOuterXmlInternal ()
  429. {
  430. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  431. return String.Empty;
  432. StringWriter sw = new StringWriter ();
  433. XmlTextWriter xtw = new XmlTextWriter (sw);
  434. xtw.WriteNode (this, false);
  435. return sw.ToString ();
  436. }
  437. public virtual void ReadStartElement ()
  438. {
  439. if (MoveToContent () != XmlNodeType.Element) {
  440. string error = String.Format ("'{0}' is an invalid node type.",
  441. NodeType.ToString ());
  442. throw new XmlException (this as IXmlLineInfo, error);
  443. }
  444. Read ();
  445. }
  446. public virtual void ReadStartElement (string name)
  447. {
  448. if (MoveToContent () != XmlNodeType.Element) {
  449. string error = String.Format ("'{0}' is an invalid node type.",
  450. NodeType.ToString ());
  451. throw new XmlException (this as IXmlLineInfo, error);
  452. }
  453. if (name != Name) {
  454. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  455. Name, NamespaceURI);
  456. throw new XmlException (this as IXmlLineInfo, error);
  457. }
  458. Read ();
  459. }
  460. public virtual void ReadStartElement (string localName, string namespaceName)
  461. {
  462. if (MoveToContent () != XmlNodeType.Element) {
  463. string error = String.Format ("'{0}' is an invalid node type.",
  464. NodeType.ToString ());
  465. throw new XmlException (this as IXmlLineInfo, error);
  466. }
  467. if (localName != LocalName || NamespaceURI != namespaceName) {
  468. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  469. localName, namespaceName,
  470. LocalName, NamespaceURI);
  471. throw new XmlException (this as IXmlLineInfo, error);
  472. }
  473. Read ();
  474. }
  475. #if NET_1_0
  476. public abstract string ReadString ();
  477. #else
  478. public virtual string ReadString ()
  479. {
  480. return ReadStringInternal ();
  481. }
  482. #endif
  483. internal string ReadStringInternal ()
  484. {
  485. if (readStringBuffer == null)
  486. readStringBuffer = new StringBuilder ();
  487. readStringBuffer.Length = 0;
  488. MoveToElement ();
  489. switch (NodeType) {
  490. default:
  491. return String.Empty;
  492. case XmlNodeType.Element:
  493. if (IsEmptyElement)
  494. return String.Empty;
  495. do {
  496. Read ();
  497. switch (NodeType) {
  498. case XmlNodeType.Text:
  499. case XmlNodeType.CDATA:
  500. case XmlNodeType.Whitespace:
  501. case XmlNodeType.SignificantWhitespace:
  502. readStringBuffer.Append (Value);
  503. continue;
  504. }
  505. break;
  506. } while (true);
  507. break;
  508. case XmlNodeType.Text:
  509. case XmlNodeType.CDATA:
  510. case XmlNodeType.Whitespace:
  511. case XmlNodeType.SignificantWhitespace:
  512. do {
  513. switch (NodeType) {
  514. case XmlNodeType.Text:
  515. case XmlNodeType.CDATA:
  516. case XmlNodeType.Whitespace:
  517. case XmlNodeType.SignificantWhitespace:
  518. readStringBuffer.Append (Value);
  519. Read ();
  520. continue;
  521. }
  522. break;
  523. } while (true);
  524. break;
  525. }
  526. string ret = readStringBuffer.ToString ();
  527. readStringBuffer.Length = 0;
  528. return ret;
  529. }
  530. public abstract void ResolveEntity ();
  531. public virtual void Skip ()
  532. {
  533. if (ReadState != ReadState.Interactive)
  534. return;
  535. MoveToElement ();
  536. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  537. Read ();
  538. return;
  539. }
  540. int depth = Depth;
  541. while (Read() && depth < Depth);
  542. if (NodeType == XmlNodeType.EndElement)
  543. Read ();
  544. }
  545. #endregion
  546. }
  547. }