XmlReader.cs 13 KB

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