XmlReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. bool result = false;
  88. if (s != null && s.Length > 0) {
  89. char[] chars = s.ToCharArray ();
  90. if (XmlChar.IsFirstNameChar (chars[0])) {
  91. int i = 1;
  92. int n = chars.Length;
  93. while (i < n && XmlChar.IsNameChar (chars[i]))
  94. ++i;
  95. result = i == n;
  96. }
  97. }
  98. return result;
  99. }
  100. public static bool IsNameToken (string s)
  101. {
  102. bool result = false;
  103. if (s != null && s.Length > 0) {
  104. char[] chars = s.ToCharArray ();
  105. int i = 0;
  106. int n = chars.Length;
  107. while (i < n && XmlChar.IsNameChar (chars[i]))
  108. ++i;
  109. result = i == n;
  110. }
  111. return result;
  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. if (ReadState != ReadState.Interactive)
  258. return String.Empty;
  259. StringWriter sw = new StringWriter ();
  260. XmlTextWriter xtw = new XmlTextWriter (sw);
  261. if (NodeType == XmlNodeType.Element) {
  262. if (IsEmptyElement)
  263. return String.Empty;
  264. int startDepth = Depth;
  265. Read ();
  266. do {
  267. if (ReadState != ReadState.Interactive)
  268. throw new XmlException ("Unexpected end of the XML reader.");
  269. xtw.WriteNode (this, false);
  270. } while (startDepth < Depth);
  271. // reader is now end element, then proceed once more.
  272. Read ();
  273. }
  274. else
  275. xtw.WriteNode (this, false);
  276. return sw.ToString ();
  277. }
  278. public virtual string ReadOuterXml ()
  279. {
  280. if (ReadState != ReadState.Interactive)
  281. return String.Empty;
  282. StringWriter sw = new StringWriter ();
  283. XmlTextWriter xtw = new XmlTextWriter (sw);
  284. xtw.WriteNode (this, false);
  285. return sw.ToString ();
  286. }
  287. #endif
  288. public virtual void ReadStartElement ()
  289. {
  290. if (MoveToContent () != XmlNodeType.Element) {
  291. string error = String.Format ("'{0}' is an invalid node type.",
  292. NodeType.ToString ());
  293. throw new XmlException (this as IXmlLineInfo, error);
  294. }
  295. Read ();
  296. }
  297. public virtual void ReadStartElement (string name)
  298. {
  299. if (MoveToContent () != XmlNodeType.Element) {
  300. string error = String.Format ("'{0}' is an invalid node type.",
  301. NodeType.ToString ());
  302. throw new XmlException (this as IXmlLineInfo, error);
  303. }
  304. if (name != Name) {
  305. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  306. Name, NamespaceURI);
  307. throw new XmlException (this as IXmlLineInfo, error);
  308. }
  309. Read ();
  310. }
  311. public virtual void ReadStartElement (string localName, string namespaceName)
  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. if (localName != LocalName || NamespaceURI != namespaceName) {
  319. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  320. localName, namespaceName,
  321. LocalName, NamespaceURI);
  322. throw new XmlException (this as IXmlLineInfo, error);
  323. }
  324. Read ();
  325. }
  326. #if NET_1_0
  327. public abstract string ReadString ();
  328. #else
  329. public virtual string ReadString ()
  330. {
  331. return ReadStringInternal ();
  332. }
  333. #endif
  334. internal string ReadStringInternal ()
  335. {
  336. if (readStringBuffer == null)
  337. readStringBuffer = new StringBuilder ();
  338. readStringBuffer.Length = 0;
  339. MoveToElement ();
  340. switch (NodeType) {
  341. default:
  342. return String.Empty;
  343. case XmlNodeType.Element:
  344. if (IsEmptyElement)
  345. return String.Empty;
  346. do {
  347. Read ();
  348. switch (NodeType) {
  349. case XmlNodeType.Text:
  350. case XmlNodeType.CDATA:
  351. case XmlNodeType.Whitespace:
  352. case XmlNodeType.SignificantWhitespace:
  353. readStringBuffer.Append (Value);
  354. continue;
  355. }
  356. break;
  357. } while (true);
  358. break;
  359. case XmlNodeType.Text:
  360. case XmlNodeType.CDATA:
  361. case XmlNodeType.Whitespace:
  362. case XmlNodeType.SignificantWhitespace:
  363. do {
  364. switch (NodeType) {
  365. case XmlNodeType.Text:
  366. case XmlNodeType.CDATA:
  367. case XmlNodeType.Whitespace:
  368. case XmlNodeType.SignificantWhitespace:
  369. readStringBuffer.Append (Value);
  370. Read ();
  371. continue;
  372. }
  373. break;
  374. } while (true);
  375. break;
  376. }
  377. string ret = readStringBuffer.ToString ();
  378. readStringBuffer.Length = 0;
  379. return ret;
  380. }
  381. public abstract void ResolveEntity ();
  382. public virtual void Skip ()
  383. {
  384. if (ReadState != ReadState.Interactive)
  385. return;
  386. MoveToElement ();
  387. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  388. Read ();
  389. return;
  390. }
  391. int depth = Depth;
  392. while (Read() && depth < Depth);
  393. if (NodeType == XmlNodeType.EndElement)
  394. Read ();
  395. }
  396. #endregion
  397. }
  398. }