XmlReader.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //
  2. // XmlReader.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. namespace System.Xml
  12. {
  13. public abstract class XmlReader
  14. {
  15. #region Constructor
  16. protected XmlReader ()
  17. {
  18. }
  19. #endregion
  20. #region Properties
  21. public abstract int AttributeCount { get; }
  22. public abstract string BaseURI { get; }
  23. public virtual bool CanResolveEntity
  24. {
  25. get { return false; }
  26. }
  27. public abstract int Depth { get; }
  28. public abstract bool EOF { get; }
  29. public virtual bool HasAttributes
  30. {
  31. get { return AttributeCount > 0; }
  32. }
  33. public abstract bool HasValue { get; }
  34. public abstract bool IsDefault { get; }
  35. public abstract bool IsEmptyElement { get; }
  36. public abstract string this[int i] { get; }
  37. public abstract string this[string name] { get; }
  38. public abstract string this[
  39. string localName,
  40. string namespaceName]
  41. { get; }
  42. public abstract string LocalName { get; }
  43. public abstract string Name { get; }
  44. public abstract string NamespaceURI { get; }
  45. public abstract XmlNameTable NameTable { get; }
  46. public abstract XmlNodeType NodeType { get; }
  47. public abstract string Prefix { get; }
  48. public abstract char QuoteChar { get; }
  49. public abstract ReadState ReadState { get; }
  50. public abstract string Value { get; }
  51. public abstract string XmlLang { get; }
  52. public abstract XmlSpace XmlSpace { get; }
  53. #endregion
  54. #region Methods
  55. public abstract void Close ();
  56. public abstract string GetAttribute (int i);
  57. public abstract string GetAttribute (string name);
  58. public abstract string GetAttribute (
  59. string localName,
  60. string namespaceName);
  61. public static bool IsName (string s)
  62. {
  63. bool result = false;
  64. if (s != null && s.Length > 0) {
  65. char[] chars = s.ToCharArray ();
  66. if (XmlConstructs.IsNameStart (chars[0])) {
  67. int i = 1;
  68. int n = chars.Length;
  69. while (i < n && XmlConstructs.IsName (chars[i]))
  70. ++i;
  71. result = i == n;
  72. }
  73. }
  74. return result;
  75. }
  76. public static bool IsNameToken (string s)
  77. {
  78. bool result = false;
  79. if (s != null && s.Length > 0) {
  80. char[] chars = s.ToCharArray ();
  81. int i = 0;
  82. int n = chars.Length;
  83. while (i < n && XmlConstructs.IsName (chars[i]))
  84. ++i;
  85. result = i == n;
  86. }
  87. return result;
  88. }
  89. public virtual bool IsStartElement ()
  90. {
  91. return (MoveToContent () == XmlNodeType.Element);
  92. }
  93. public virtual bool IsStartElement (string name)
  94. {
  95. if (!IsStartElement ())
  96. return false;
  97. return (Name == name);
  98. }
  99. public virtual bool IsStartElement (string localName, string namespaceName)
  100. {
  101. if (!IsStartElement ())
  102. return false;
  103. return (LocalName == localName && NamespaceURI == namespaceName);
  104. }
  105. public abstract string LookupNamespace (string prefix);
  106. public abstract void MoveToAttribute (int i);
  107. public abstract bool MoveToAttribute (string name);
  108. public abstract bool MoveToAttribute (
  109. string localName,
  110. string namespaceName);
  111. private bool IsContent (XmlNodeType nodeType)
  112. {
  113. /* MS doc says:
  114. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  115. */
  116. switch (nodeType) {
  117. case XmlNodeType.Text:
  118. return true;
  119. case XmlNodeType.CDATA:
  120. return true;
  121. case XmlNodeType.Element:
  122. return true;
  123. case XmlNodeType.EndElement:
  124. return true;
  125. case XmlNodeType.EntityReference:
  126. return true;
  127. case XmlNodeType.EndEntity:
  128. return true;
  129. }
  130. return false;
  131. }
  132. public virtual XmlNodeType MoveToContent ()
  133. {
  134. if (NodeType == XmlNodeType.Attribute)
  135. MoveToElement ();
  136. do {
  137. if (IsContent (NodeType))
  138. return NodeType;
  139. Read ();
  140. } while (!EOF);
  141. return XmlNodeType.None;
  142. }
  143. public abstract bool MoveToElement ();
  144. public abstract bool MoveToFirstAttribute ();
  145. public abstract bool MoveToNextAttribute ();
  146. public abstract bool Read ();
  147. public abstract bool ReadAttributeValue ();
  148. public virtual string ReadElementString ()
  149. {
  150. if (MoveToContent () != XmlNodeType.Element) {
  151. string error = String.Format ("'{0}' is an invalid node type.",
  152. NodeType.ToString ());
  153. throw new XmlException (this as IXmlLineInfo, error);
  154. }
  155. string result = String.Empty;
  156. if (!IsEmptyElement) {
  157. Read ();
  158. result = ReadString ();
  159. if (NodeType != XmlNodeType.EndElement) {
  160. string error = String.Format ("'{0}' is an invalid node type.",
  161. NodeType.ToString ());
  162. throw new XmlException (this as IXmlLineInfo, error);
  163. }
  164. }
  165. Read ();
  166. return result;
  167. }
  168. public virtual string ReadElementString (string name)
  169. {
  170. if (MoveToContent () != XmlNodeType.Element) {
  171. string error = String.Format ("'{0}' is an invalid node type.",
  172. NodeType.ToString ());
  173. throw new XmlException (this as IXmlLineInfo, error);
  174. }
  175. if (name != Name) {
  176. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  177. Name, NamespaceURI);
  178. throw new XmlException (this as IXmlLineInfo, error);
  179. }
  180. string result = String.Empty;
  181. if (!IsEmptyElement) {
  182. Read ();
  183. result = ReadString ();
  184. if (NodeType != XmlNodeType.EndElement) {
  185. string error = String.Format ("'{0}' is an invalid node type.",
  186. NodeType.ToString ());
  187. throw new XmlException (this as IXmlLineInfo, error);
  188. }
  189. }
  190. Read ();
  191. return result;
  192. }
  193. public virtual string ReadElementString (string localName, string namespaceName)
  194. {
  195. if (MoveToContent () != XmlNodeType.Element) {
  196. string error = String.Format ("'{0}' is an invalid node type.",
  197. NodeType.ToString ());
  198. throw new XmlException (this as IXmlLineInfo, error);
  199. }
  200. if (localName != LocalName || NamespaceURI != namespaceName) {
  201. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  202. LocalName, NamespaceURI);
  203. throw new XmlException (this as IXmlLineInfo, error);
  204. }
  205. string result = String.Empty;
  206. if (!IsEmptyElement) {
  207. Read ();
  208. result = ReadString ();
  209. if (NodeType != XmlNodeType.EndElement) {
  210. string error = String.Format ("'{0}' is an invalid node type.",
  211. NodeType.ToString ());
  212. throw new XmlException (this as IXmlLineInfo, error);
  213. }
  214. }
  215. Read ();
  216. return result;
  217. }
  218. public virtual void ReadEndElement ()
  219. {
  220. if (MoveToContent () != XmlNodeType.EndElement) {
  221. string error = String.Format ("'{0}' is an invalid node type.",
  222. NodeType.ToString ());
  223. throw new XmlException (this as IXmlLineInfo, error);
  224. }
  225. Read ();
  226. }
  227. public abstract string ReadInnerXml ();
  228. public abstract string ReadOuterXml ();
  229. public virtual void ReadStartElement ()
  230. {
  231. if (MoveToContent () != XmlNodeType.Element) {
  232. string error = String.Format ("'{0}' is an invalid node type.",
  233. NodeType.ToString ());
  234. throw new XmlException (this as IXmlLineInfo, error);
  235. }
  236. Read ();
  237. }
  238. public virtual void ReadStartElement (string name)
  239. {
  240. if (MoveToContent () != XmlNodeType.Element) {
  241. string error = String.Format ("'{0}' is an invalid node type.",
  242. NodeType.ToString ());
  243. throw new XmlException (this as IXmlLineInfo, error);
  244. }
  245. if (name != Name) {
  246. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  247. Name, NamespaceURI);
  248. throw new XmlException (this as IXmlLineInfo, error);
  249. }
  250. Read ();
  251. }
  252. public virtual void ReadStartElement (string localName, string namespaceName)
  253. {
  254. if (MoveToContent () != XmlNodeType.Element) {
  255. string error = String.Format ("'{0}' is an invalid node type.",
  256. NodeType.ToString ());
  257. throw new XmlException (this as IXmlLineInfo, error);
  258. }
  259. if (localName != LocalName || NamespaceURI != namespaceName) {
  260. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  261. LocalName, NamespaceURI);
  262. throw new XmlException (this as IXmlLineInfo, error);
  263. }
  264. Read ();
  265. }
  266. public abstract string ReadString ();
  267. public abstract void ResolveEntity ();
  268. public virtual void Skip ()
  269. {
  270. if (ReadState != ReadState.Interactive)
  271. return;
  272. MoveToElement ();
  273. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  274. Read ();
  275. return;
  276. }
  277. int depth = Depth;
  278. while (Read() && depth < Depth);
  279. if (NodeType == XmlNodeType.EndElement)
  280. Read ();
  281. }
  282. #endregion
  283. }
  284. }