XmlReader.cs 12 KB

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