XmlReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. namespace System.Xml
  38. {
  39. #if NET_2_0
  40. public abstract class XmlReader : IDisposable, IXmlDataEvidence
  41. #else
  42. public abstract class XmlReader
  43. #endif
  44. {
  45. private StringBuilder readStringBuffer;
  46. private Evidence [] evidences;
  47. #if NET_2_0
  48. private XmlReaderSettings settings;
  49. #endif
  50. #region Constructor
  51. protected XmlReader ()
  52. {
  53. }
  54. #endregion
  55. #region Properties
  56. public abstract int AttributeCount { get; }
  57. public abstract string BaseURI { get; }
  58. public virtual bool CanResolveEntity
  59. {
  60. get { return false; }
  61. }
  62. public abstract int Depth { get; }
  63. public abstract bool EOF { get; }
  64. #if NET_2_0
  65. [MonoTODO]
  66. public virtual Evidence [] Evidence {
  67. get { return evidences; }
  68. }
  69. #endif
  70. public virtual bool HasAttributes
  71. {
  72. get { return AttributeCount > 0; }
  73. }
  74. public abstract bool HasValue { get; }
  75. public abstract bool IsDefault { get; }
  76. public abstract bool IsEmptyElement { get; }
  77. public abstract string this[int i] { get; }
  78. public abstract string this[string name] { get; }
  79. public abstract string this[
  80. string localName,
  81. string namespaceName]
  82. { get; }
  83. public abstract string LocalName { get; }
  84. public abstract string Name { get; }
  85. public abstract string NamespaceURI { get; }
  86. public abstract XmlNameTable NameTable { get; }
  87. public abstract XmlNodeType NodeType { get; }
  88. public abstract string Prefix { get; }
  89. public abstract char QuoteChar { get; }
  90. public abstract ReadState ReadState { get; }
  91. #if NET_2_0
  92. [MonoTODO]
  93. public virtual IXmlSchemaInfo SchemaInfo {
  94. get {
  95. throw new NotImplementedException ();
  96. }
  97. }
  98. public virtual XmlReaderSettings Settings {
  99. get { return settings; }
  100. }
  101. #endif
  102. public abstract string Value { get; }
  103. public abstract string XmlLang { get; }
  104. public abstract XmlSpace XmlSpace { get; }
  105. #endregion
  106. #region Methods
  107. public abstract void Close ();
  108. #if NET_2_0
  109. [MonoTODO]
  110. public virtual void Dispose ()
  111. {
  112. Close ();
  113. }
  114. #endif
  115. public abstract string GetAttribute (int i);
  116. public abstract string GetAttribute (string name);
  117. public abstract string GetAttribute (
  118. string localName,
  119. string namespaceName);
  120. public static bool IsName (string s)
  121. {
  122. return s != null && XmlChar.IsName (s);
  123. }
  124. public static bool IsNameToken (string s)
  125. {
  126. return s != null && XmlChar.IsNmToken (s);
  127. }
  128. public virtual bool IsStartElement ()
  129. {
  130. return (MoveToContent () == XmlNodeType.Element);
  131. }
  132. public virtual bool IsStartElement (string name)
  133. {
  134. if (!IsStartElement ())
  135. return false;
  136. return (Name == name);
  137. }
  138. public virtual bool IsStartElement (string localName, string namespaceName)
  139. {
  140. if (!IsStartElement ())
  141. return false;
  142. return (LocalName == localName && NamespaceURI == namespaceName);
  143. }
  144. public abstract string LookupNamespace (string prefix);
  145. public abstract void MoveToAttribute (int i);
  146. public abstract bool MoveToAttribute (string name);
  147. public abstract bool MoveToAttribute (
  148. string localName,
  149. string namespaceName);
  150. private bool IsContent (XmlNodeType nodeType)
  151. {
  152. /* MS doc says:
  153. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  154. */
  155. switch (nodeType) {
  156. case XmlNodeType.Text:
  157. return true;
  158. case XmlNodeType.CDATA:
  159. return true;
  160. case XmlNodeType.Element:
  161. return true;
  162. case XmlNodeType.EndElement:
  163. return true;
  164. case XmlNodeType.EntityReference:
  165. return true;
  166. case XmlNodeType.EndEntity:
  167. return true;
  168. }
  169. return false;
  170. }
  171. public virtual XmlNodeType MoveToContent ()
  172. {
  173. if (NodeType == XmlNodeType.Attribute)
  174. MoveToElement ();
  175. do {
  176. if (IsContent (NodeType))
  177. return NodeType;
  178. Read ();
  179. } while (!EOF);
  180. return XmlNodeType.None;
  181. }
  182. public abstract bool MoveToElement ();
  183. public abstract bool MoveToFirstAttribute ();
  184. public abstract bool MoveToNextAttribute ();
  185. public abstract bool Read ();
  186. public abstract bool ReadAttributeValue ();
  187. public virtual string ReadElementString ()
  188. {
  189. if (MoveToContent () != XmlNodeType.Element) {
  190. string error = String.Format ("'{0}' is an invalid node type.",
  191. NodeType.ToString ());
  192. throw new XmlException (this as IXmlLineInfo, error);
  193. }
  194. string result = String.Empty;
  195. if (!IsEmptyElement) {
  196. Read ();
  197. result = ReadString ();
  198. if (NodeType != XmlNodeType.EndElement) {
  199. string error = String.Format ("'{0}' is an invalid node type.",
  200. NodeType.ToString ());
  201. throw new XmlException (this as IXmlLineInfo, error);
  202. }
  203. }
  204. Read ();
  205. return result;
  206. }
  207. public virtual string ReadElementString (string name)
  208. {
  209. if (MoveToContent () != XmlNodeType.Element) {
  210. string error = String.Format ("'{0}' is an invalid node type.",
  211. NodeType.ToString ());
  212. throw new XmlException (this as IXmlLineInfo, error);
  213. }
  214. if (name != Name) {
  215. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  216. Name, NamespaceURI);
  217. throw new XmlException (this as IXmlLineInfo, error);
  218. }
  219. string result = String.Empty;
  220. if (!IsEmptyElement) {
  221. Read ();
  222. result = ReadString ();
  223. if (NodeType != XmlNodeType.EndElement) {
  224. string error = String.Format ("'{0}' is an invalid node type.",
  225. NodeType.ToString ());
  226. throw new XmlException (this as IXmlLineInfo, error);
  227. }
  228. }
  229. Read ();
  230. return result;
  231. }
  232. public virtual string ReadElementString (string localName, string namespaceName)
  233. {
  234. if (MoveToContent () != XmlNodeType.Element) {
  235. string error = String.Format ("'{0}' is an invalid node type.",
  236. NodeType.ToString ());
  237. throw new XmlException (this as IXmlLineInfo, error);
  238. }
  239. if (localName != LocalName || NamespaceURI != namespaceName) {
  240. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  241. LocalName, NamespaceURI);
  242. throw new XmlException (this as IXmlLineInfo, error);
  243. }
  244. string result = String.Empty;
  245. if (!IsEmptyElement) {
  246. Read ();
  247. result = ReadString ();
  248. if (NodeType != XmlNodeType.EndElement) {
  249. string error = String.Format ("'{0}' is an invalid node type.",
  250. NodeType.ToString ());
  251. throw new XmlException (this as IXmlLineInfo, error);
  252. }
  253. }
  254. Read ();
  255. return result;
  256. }
  257. public virtual void ReadEndElement ()
  258. {
  259. if (MoveToContent () != XmlNodeType.EndElement) {
  260. string error = String.Format ("'{0}' is an invalid node type.",
  261. NodeType.ToString ());
  262. throw new XmlException (this as IXmlLineInfo, error);
  263. }
  264. Read ();
  265. }
  266. #if NET_1_0
  267. public abstract string ReadInnerXml ();
  268. public abstract string ReadOuterXml ();
  269. #else
  270. public virtual string ReadInnerXml ()
  271. {
  272. return ReadInnerXmlInternal ();
  273. }
  274. public virtual string ReadOuterXml ()
  275. {
  276. return ReadOuterXmlInternal ();
  277. }
  278. #endif
  279. internal string ReadInnerXmlInternal ()
  280. {
  281. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  282. return String.Empty;
  283. StringWriter sw = new StringWriter ();
  284. XmlTextWriter xtw = new XmlTextWriter (sw);
  285. if (NodeType == XmlNodeType.Element) {
  286. if (IsEmptyElement) {
  287. Read ();
  288. return String.Empty;
  289. }
  290. int startDepth = Depth;
  291. Read ();
  292. while (startDepth < Depth) {
  293. if (ReadState != ReadState.Interactive)
  294. throw new XmlException ("Unexpected end of the XML reader.");
  295. xtw.WriteNode (this, false);
  296. }
  297. // reader is now end element, then proceed once more.
  298. Read ();
  299. }
  300. else
  301. xtw.WriteNode (this, false);
  302. return sw.ToString ();
  303. }
  304. internal string ReadOuterXmlInternal ()
  305. {
  306. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  307. return String.Empty;
  308. StringWriter sw = new StringWriter ();
  309. XmlTextWriter xtw = new XmlTextWriter (sw);
  310. xtw.WriteNode (this, false);
  311. return sw.ToString ();
  312. }
  313. public virtual void ReadStartElement ()
  314. {
  315. if (MoveToContent () != XmlNodeType.Element) {
  316. string error = String.Format ("'{0}' is an invalid node type.",
  317. NodeType.ToString ());
  318. throw new XmlException (this as IXmlLineInfo, error);
  319. }
  320. Read ();
  321. }
  322. public virtual void ReadStartElement (string name)
  323. {
  324. if (MoveToContent () != XmlNodeType.Element) {
  325. string error = String.Format ("'{0}' is an invalid node type.",
  326. NodeType.ToString ());
  327. throw new XmlException (this as IXmlLineInfo, error);
  328. }
  329. if (name != Name) {
  330. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  331. Name, NamespaceURI);
  332. throw new XmlException (this as IXmlLineInfo, error);
  333. }
  334. Read ();
  335. }
  336. public virtual void ReadStartElement (string localName, string namespaceName)
  337. {
  338. if (MoveToContent () != XmlNodeType.Element) {
  339. string error = String.Format ("'{0}' is an invalid node type.",
  340. NodeType.ToString ());
  341. throw new XmlException (this as IXmlLineInfo, error);
  342. }
  343. if (localName != LocalName || NamespaceURI != namespaceName) {
  344. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  345. localName, namespaceName,
  346. LocalName, NamespaceURI);
  347. throw new XmlException (this as IXmlLineInfo, error);
  348. }
  349. Read ();
  350. }
  351. #if NET_1_0
  352. public abstract string ReadString ();
  353. #else
  354. public virtual string ReadString ()
  355. {
  356. return ReadStringInternal ();
  357. }
  358. #endif
  359. internal string ReadStringInternal ()
  360. {
  361. if (readStringBuffer == null)
  362. readStringBuffer = new StringBuilder ();
  363. readStringBuffer.Length = 0;
  364. MoveToElement ();
  365. switch (NodeType) {
  366. default:
  367. return String.Empty;
  368. case XmlNodeType.Element:
  369. if (IsEmptyElement)
  370. return String.Empty;
  371. do {
  372. Read ();
  373. switch (NodeType) {
  374. case XmlNodeType.Text:
  375. case XmlNodeType.CDATA:
  376. case XmlNodeType.Whitespace:
  377. case XmlNodeType.SignificantWhitespace:
  378. readStringBuffer.Append (Value);
  379. continue;
  380. }
  381. break;
  382. } while (true);
  383. break;
  384. case XmlNodeType.Text:
  385. case XmlNodeType.CDATA:
  386. case XmlNodeType.Whitespace:
  387. case XmlNodeType.SignificantWhitespace:
  388. do {
  389. switch (NodeType) {
  390. case XmlNodeType.Text:
  391. case XmlNodeType.CDATA:
  392. case XmlNodeType.Whitespace:
  393. case XmlNodeType.SignificantWhitespace:
  394. readStringBuffer.Append (Value);
  395. Read ();
  396. continue;
  397. }
  398. break;
  399. } while (true);
  400. break;
  401. }
  402. string ret = readStringBuffer.ToString ();
  403. readStringBuffer.Length = 0;
  404. return ret;
  405. }
  406. public abstract void ResolveEntity ();
  407. public virtual void Skip ()
  408. {
  409. if (ReadState != ReadState.Interactive)
  410. return;
  411. MoveToElement ();
  412. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  413. Read ();
  414. return;
  415. }
  416. int depth = Depth;
  417. while (Read() && depth < Depth);
  418. if (NodeType == XmlNodeType.EndElement)
  419. Read ();
  420. }
  421. #endregion
  422. }
  423. }