XmlReader.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. XmlNodeType nodeType = NodeType;
  135. if (IsEmptyElement)
  136. return nodeType;
  137. if (nodeType == XmlNodeType.Attribute) {
  138. MoveToElement ();
  139. return nodeType;
  140. }
  141. do {
  142. Read ();
  143. nodeType = NodeType;
  144. if (IsContent (nodeType))
  145. return nodeType;
  146. } while (ReadState != ReadState.EndOfFile);
  147. return XmlNodeType.None;
  148. }
  149. public abstract bool MoveToElement ();
  150. public abstract bool MoveToFirstAttribute ();
  151. public abstract bool MoveToNextAttribute ();
  152. public abstract bool Read ();
  153. public abstract bool ReadAttributeValue ();
  154. public virtual string ReadElementString ()
  155. {
  156. if (MoveToContent () != XmlNodeType.Element) {
  157. string error = String.Format ("'{0}' is an invalid node type.",
  158. NodeType.ToString ());
  159. throw new XmlException (this as IXmlLineInfo, error);
  160. }
  161. string result = String.Empty;
  162. if (!IsEmptyElement) {
  163. Read ();
  164. result = ReadString ();
  165. if (NodeType != XmlNodeType.EndElement) {
  166. string error = String.Format ("'{0}' is an invalid node type.",
  167. NodeType.ToString ());
  168. throw new XmlException (this as IXmlLineInfo, error);
  169. }
  170. }
  171. Read ();
  172. return result;
  173. }
  174. public virtual string ReadElementString (string name)
  175. {
  176. if (MoveToContent () != XmlNodeType.Element) {
  177. string error = String.Format ("'{0}' is an invalid node type.",
  178. NodeType.ToString ());
  179. throw new XmlException (this as IXmlLineInfo, error);
  180. }
  181. if (name != Name) {
  182. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  183. Name, NamespaceURI);
  184. throw new XmlException (this as IXmlLineInfo, error);
  185. }
  186. string result = String.Empty;
  187. if (!IsEmptyElement) {
  188. Read ();
  189. result = ReadString ();
  190. if (NodeType != XmlNodeType.EndElement) {
  191. string error = String.Format ("'{0}' is an invalid node type.",
  192. NodeType.ToString ());
  193. throw new XmlException (this as IXmlLineInfo, error);
  194. }
  195. }
  196. Read ();
  197. return result;
  198. }
  199. public virtual string ReadElementString (string localName, string namespaceName)
  200. {
  201. if (MoveToContent () != XmlNodeType.Element) {
  202. string error = String.Format ("'{0}' is an invalid node type.",
  203. NodeType.ToString ());
  204. throw new XmlException (this as IXmlLineInfo, error);
  205. }
  206. if (localName != LocalName || NamespaceURI != namespaceName) {
  207. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  208. LocalName, NamespaceURI);
  209. throw new XmlException (this as IXmlLineInfo, error);
  210. }
  211. string result = String.Empty;
  212. if (!IsEmptyElement) {
  213. Read ();
  214. result = ReadString ();
  215. if (NodeType != XmlNodeType.EndElement) {
  216. string error = String.Format ("'{0}' is an invalid node type.",
  217. NodeType.ToString ());
  218. throw new XmlException (this as IXmlLineInfo, error);
  219. }
  220. }
  221. Read ();
  222. return result;
  223. }
  224. public virtual void ReadEndElement ()
  225. {
  226. if (MoveToContent () != XmlNodeType.EndElement) {
  227. string error = String.Format ("'{0}' is an invalid node type.",
  228. NodeType.ToString ());
  229. throw new XmlException (this as IXmlLineInfo, error);
  230. }
  231. Read ();
  232. }
  233. public abstract string ReadInnerXml ();
  234. public abstract string ReadOuterXml ();
  235. public virtual void ReadStartElement ()
  236. {
  237. if (MoveToContent () != XmlNodeType.Element) {
  238. string error = String.Format ("'{0}' is an invalid node type.",
  239. NodeType.ToString ());
  240. throw new XmlException (this as IXmlLineInfo, error);
  241. }
  242. Read ();
  243. }
  244. public virtual void ReadStartElement (string name)
  245. {
  246. if (MoveToContent () != XmlNodeType.Element) {
  247. string error = String.Format ("'{0}' is an invalid node type.",
  248. NodeType.ToString ());
  249. throw new XmlException (this as IXmlLineInfo, error);
  250. }
  251. if (name != Name) {
  252. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  253. Name, NamespaceURI);
  254. throw new XmlException (this as IXmlLineInfo, error);
  255. }
  256. Read ();
  257. }
  258. public virtual void ReadStartElement (string localName, string namespaceName)
  259. {
  260. if (MoveToContent () != XmlNodeType.Element) {
  261. string error = String.Format ("'{0}' is an invalid node type.",
  262. NodeType.ToString ());
  263. throw new XmlException (this as IXmlLineInfo, error);
  264. }
  265. if (localName != LocalName || NamespaceURI != namespaceName) {
  266. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  267. LocalName, NamespaceURI);
  268. throw new XmlException (this as IXmlLineInfo, error);
  269. }
  270. Read ();
  271. }
  272. public abstract string ReadString ();
  273. public abstract void ResolveEntity ();
  274. public virtual void Skip ()
  275. {
  276. if (ReadState != ReadState.Interactive)
  277. return;
  278. MoveToElement ();
  279. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  280. Read ();
  281. return;
  282. }
  283. int depth = Depth;
  284. while (Read() && depth < Depth);
  285. if (NodeType == XmlNodeType.EndElement)
  286. Read ();
  287. }
  288. #endregion
  289. }
  290. }