XmlReader.cs 11 KB

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