XmlReader.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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.Collections;
  34. using System.IO;
  35. using System.Text;
  36. using System.Xml.Schema; // only required for NET_2_0 (SchemaInfo)
  37. using System.Xml.Serialization; // only required for NET_2_0 (SchemaInfo)
  38. using Mono.Xml; // only required for NET_2_0
  39. using Mono.Xml.Schema; // only required for NET_2_0
  40. namespace System.Xml
  41. {
  42. #if NET_2_0
  43. public abstract class XmlReader : IDisposable
  44. #else
  45. public abstract class XmlReader
  46. #endif
  47. {
  48. private StringBuilder readStringBuffer;
  49. private XmlReaderBinarySupport binary;
  50. #if NET_2_0
  51. private XmlReaderSettings settings;
  52. #endif
  53. #region Constructor
  54. protected XmlReader ()
  55. {
  56. }
  57. #endregion
  58. #region Properties
  59. public abstract int AttributeCount { get; }
  60. public abstract string BaseURI { get; }
  61. internal XmlReaderBinarySupport Binary {
  62. get { return binary; }
  63. }
  64. internal XmlReaderBinarySupport.CharGetter BinaryCharGetter {
  65. get { return binary != null ? binary.Getter : null; }
  66. set {
  67. if (binary == null)
  68. binary = new XmlReaderBinarySupport (this);
  69. binary.Getter = value;
  70. }
  71. }
  72. #if NET_2_0
  73. // To enable it internally in sys.xml, just insert these
  74. // two lines into Read():
  75. //
  76. // #if NET_2_0
  77. // if (Binary != null)
  78. // Binary.Reset ();
  79. // #endif
  80. //
  81. public virtual bool CanReadBinaryContent {
  82. get { return false; }
  83. }
  84. public virtual bool CanReadValueChunk {
  85. get { return false; }
  86. }
  87. #else
  88. internal virtual bool CanReadBinaryContent {
  89. get { return false; }
  90. }
  91. internal virtual bool CanReadValueChunk {
  92. get { return false; }
  93. }
  94. #endif
  95. public virtual bool CanResolveEntity
  96. {
  97. get { return false; }
  98. }
  99. public abstract int Depth { get; }
  100. public abstract bool EOF { get; }
  101. public virtual bool HasAttributes
  102. {
  103. get { return AttributeCount > 0; }
  104. }
  105. public abstract bool HasValue { get; }
  106. #if NET_2_0
  107. public virtual bool IsDefault {
  108. get { return false; }
  109. }
  110. public virtual bool IsEmptyElement {
  111. get { return false; }
  112. }
  113. public virtual string this [int i] {
  114. get { return GetAttribute (i); }
  115. }
  116. public virtual string this [string name] {
  117. get { return GetAttribute (name); }
  118. }
  119. public virtual string this [string name, string namespaceURI] {
  120. get { return GetAttribute (name, namespaceURI); }
  121. }
  122. #else
  123. public abstract bool IsDefault { get; }
  124. public abstract bool IsEmptyElement { get; }
  125. public abstract string this [int i] { get; }
  126. public abstract string this [string name] { get; }
  127. public abstract string this [string localName, string namespaceName] { get; }
  128. #endif
  129. public abstract string LocalName { get; }
  130. public abstract string Name { get; }
  131. public abstract string NamespaceURI { get; }
  132. public abstract XmlNameTable NameTable { get; }
  133. public abstract XmlNodeType NodeType { get; }
  134. public abstract string Prefix { get; }
  135. #if NET_2_0
  136. public virtual char QuoteChar {
  137. get { return '\"'; }
  138. }
  139. #else
  140. public abstract char QuoteChar { get; }
  141. #endif
  142. public abstract ReadState ReadState { get; }
  143. #if NET_2_0
  144. public virtual IXmlSchemaInfo SchemaInfo {
  145. get { return null; }
  146. }
  147. public virtual XmlReaderSettings Settings {
  148. get { return settings; }
  149. }
  150. #endif
  151. public abstract string Value { get; }
  152. #if NET_2_0
  153. public virtual string XmlLang {
  154. get { return String.Empty; }
  155. }
  156. public virtual XmlSpace XmlSpace {
  157. get { return XmlSpace.None; }
  158. }
  159. #else
  160. public abstract string XmlLang { get; }
  161. public abstract XmlSpace XmlSpace { get; }
  162. #endif
  163. #endregion
  164. #region Methods
  165. public abstract void Close ();
  166. #if NET_2_0
  167. private static XmlNameTable PopulateNameTable (
  168. XmlReaderSettings settings)
  169. {
  170. XmlNameTable nameTable = settings.NameTable;
  171. if (nameTable == null)
  172. nameTable = new NameTable ();
  173. return nameTable;
  174. }
  175. private static XmlNodeType GetNodeType (
  176. XmlReaderSettings settings)
  177. {
  178. ConformanceLevel level = settings != null ? settings.ConformanceLevel : ConformanceLevel.Auto;
  179. return
  180. level == ConformanceLevel.Fragment ?
  181. XmlNodeType.Element :
  182. XmlNodeType.Element;
  183. }
  184. public static XmlReader Create (Stream stream)
  185. {
  186. return Create (stream, null, null, new XmlUrlResolver (), null);
  187. }
  188. public static XmlReader Create (string url)
  189. {
  190. return Create (url, null);
  191. }
  192. public static XmlReader Create (TextReader reader)
  193. {
  194. return Create (reader, null, new XmlUrlResolver (), null);
  195. }
  196. public static XmlReader Create (string url, XmlReaderSettings settings)
  197. {
  198. return Create (url, null, new XmlUrlResolver (), settings);
  199. }
  200. public static XmlReader Create (XmlReader reader, XmlReaderSettings settings)
  201. {
  202. return Create (reader, new XmlUrlResolver (), settings);
  203. }
  204. [MonoTODO ("ConformanceLevel")]
  205. public static XmlReader Create (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  206. {
  207. if (settings == null) {
  208. settings = new XmlReaderSettings ();
  209. settings.XmlResolver = resolver;
  210. }
  211. XmlReader r = CreateFilteredXmlReader (reader, resolver, settings);
  212. r.settings = settings;
  213. return r;
  214. }
  215. [MonoTODO ("ConformanceLevel; Encoding")]
  216. public static XmlReader Create (string url, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
  217. {
  218. if (settings == null) {
  219. settings = new XmlReaderSettings ();
  220. settings.XmlResolver = resolver;
  221. }
  222. return CreateCustomizedTextReader (new XmlTextReader (url, PopulateNameTable (settings)), resolver, settings);
  223. }
  224. [MonoTODO ("ConformanceLevel")]
  225. public static XmlReader Create (Stream stream, XmlReaderSettings settings, XmlParserContext context)
  226. {
  227. if (settings == null)
  228. settings = new XmlReaderSettings ();
  229. return CreateCustomizedTextReader (new XmlTextReader (stream, GetNodeType (settings), context), settings.XmlResolver, settings);
  230. }
  231. [MonoTODO ("ConformanceLevel")]
  232. public static XmlReader Create (TextReader reader, string baseUri, XmlResolver resolver, XmlReaderSettings settings)
  233. {
  234. if (settings == null) {
  235. settings = new XmlReaderSettings ();
  236. settings.XmlResolver = resolver;
  237. }
  238. return CreateCustomizedTextReader (new XmlTextReader (baseUri, reader, PopulateNameTable (settings)), resolver, settings);
  239. }
  240. [MonoTODO ("ConformanceLevel")]
  241. public static XmlReader Create (Stream stream, string baseUri, Encoding encoding, XmlResolver resolver, XmlReaderSettings settings)
  242. {
  243. if (settings == null) {
  244. settings = new XmlReaderSettings ();
  245. settings.XmlResolver = resolver;
  246. }
  247. XmlNameTable nameTable = PopulateNameTable (settings);
  248. return CreateCustomizedTextReader (
  249. encoding == null ?
  250. new XmlTextReader (baseUri, stream, nameTable) :
  251. new XmlTextReader (baseUri, new StreamReader (stream, encoding), nameTable),
  252. resolver,
  253. settings);
  254. }
  255. private static XmlReader CreateCustomizedTextReader (XmlTextReader reader, XmlResolver resolver, XmlReaderSettings settings)
  256. {
  257. reader.XmlResolver = resolver;
  258. // Normalization is set true by default.
  259. reader.Normalization = true;
  260. if (settings.ProhibitDtd)
  261. reader.ProhibitDtd = true;
  262. if (!settings.CheckCharacters)
  263. reader.CharacterChecking = false;
  264. // I guess it might be changed in 2.0 RTM to set true
  265. // as default, or just disappear. It goes against
  266. // XmlTextReader's default usage and users will have
  267. // to close input manually (that's annoying). Moreover,
  268. // MS XmlTextReader consumes text input more than
  269. // actually read and users can acquire those extra
  270. // consumption by GetRemainder() that returns different
  271. // TextReader.
  272. reader.CloseInput = settings.CloseInput;
  273. // I would like to support it in detail later;
  274. // MSDN description looks source of confusion. We don't
  275. // need examples, but precise list of how it works.
  276. reader.Conformance = settings.ConformanceLevel;
  277. reader.AdjustLineInfoOffset (settings.LineNumberOffset,
  278. settings.LinePositionOffset);
  279. if (settings.NameTable != null)
  280. reader.SetNameTable (settings.NameTable);
  281. XmlReader r = CreateFilteredXmlReader (reader, resolver, settings);
  282. r.settings = settings;
  283. return r;
  284. }
  285. private static XmlReader CreateFilteredXmlReader (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  286. {
  287. reader = CreateValidatingXmlReader (reader, resolver, settings);
  288. if (reader.Settings != null ||
  289. settings.IgnoreComments ||
  290. settings.IgnoreProcessingInstructions ||
  291. settings.IgnoreWhitespace)
  292. return new XmlFilterReader (reader, settings);
  293. else {
  294. reader.settings = settings;
  295. return reader;
  296. }
  297. }
  298. private static XmlReader CreateValidatingXmlReader (XmlReader reader, XmlResolver resolver, XmlReaderSettings settings)
  299. {
  300. XmlValidatingReader xvr = null;
  301. switch (settings.ValidationType) {
  302. case ValidationType.None:
  303. return reader;
  304. case ValidationType.DTD:
  305. xvr = new XmlValidatingReader (reader);
  306. xvr.XmlResolver = resolver;
  307. xvr.ValidationType = ValidationType.DTD;
  308. break;
  309. case ValidationType.Schema:
  310. // xvr = new XmlValidatingReader (reader);
  311. // xvr.ValidationType = ValidationType.Schema;
  312. return new XmlSchemaValidatingReader (reader, settings);
  313. case ValidationType.Auto:
  314. xvr = new XmlValidatingReader (reader);
  315. xvr.ValidationType = ValidationType.DTD;
  316. reader = xvr;
  317. goto case ValidationType.Schema;
  318. case ValidationType.XDR:
  319. throw new NotSupportedException ();
  320. }
  321. if (xvr != null)
  322. xvr.SetSchemas (settings.Schemas);
  323. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreIdentityConstraints) != 0)
  324. throw new NotImplementedException ();
  325. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreInlineSchema) != 0)
  326. throw new NotImplementedException ();
  327. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreSchemaLocation) != 0)
  328. throw new NotImplementedException ();
  329. if ((settings.ValidationFlags & XmlSchemaValidationFlags.IgnoreValidationWarnings) == 0)
  330. throw new NotImplementedException ();
  331. return xvr != null ? xvr : reader;
  332. }
  333. #endif
  334. #if NET_2_0
  335. public virtual void Dispose ()
  336. {
  337. if (ReadState != ReadState.Closed)
  338. Close ();
  339. }
  340. #endif
  341. public abstract string GetAttribute (int i);
  342. public abstract string GetAttribute (string name);
  343. public abstract string GetAttribute (
  344. string localName,
  345. string namespaceName);
  346. public static bool IsName (string s)
  347. {
  348. return s != null && XmlChar.IsName (s);
  349. }
  350. public static bool IsNameToken (string s)
  351. {
  352. return s != null && XmlChar.IsNmToken (s);
  353. }
  354. public virtual bool IsStartElement ()
  355. {
  356. return (MoveToContent () == XmlNodeType.Element);
  357. }
  358. public virtual bool IsStartElement (string name)
  359. {
  360. if (!IsStartElement ())
  361. return false;
  362. return (Name == name);
  363. }
  364. public virtual bool IsStartElement (string localName, string namespaceName)
  365. {
  366. if (!IsStartElement ())
  367. return false;
  368. return (LocalName == localName && NamespaceURI == namespaceName);
  369. }
  370. public abstract string LookupNamespace (string prefix);
  371. #if NET_2_0
  372. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  373. #else
  374. internal virtual string LookupNamespace (string prefix, bool atomizedNames)
  375. #endif
  376. {
  377. return LookupNamespace (prefix);
  378. }
  379. public abstract void MoveToAttribute (int i);
  380. public abstract bool MoveToAttribute (string name);
  381. public abstract bool MoveToAttribute (
  382. string localName,
  383. string namespaceName);
  384. private bool IsContent (XmlNodeType nodeType)
  385. {
  386. /* MS doc says:
  387. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  388. */
  389. switch (nodeType) {
  390. case XmlNodeType.Text:
  391. return true;
  392. case XmlNodeType.CDATA:
  393. return true;
  394. case XmlNodeType.Element:
  395. return true;
  396. case XmlNodeType.EndElement:
  397. return true;
  398. case XmlNodeType.EntityReference:
  399. return true;
  400. case XmlNodeType.EndEntity:
  401. return true;
  402. }
  403. return false;
  404. }
  405. public virtual XmlNodeType MoveToContent ()
  406. {
  407. if (NodeType == XmlNodeType.Attribute)
  408. MoveToElement ();
  409. do {
  410. if (IsContent (NodeType))
  411. return NodeType;
  412. Read ();
  413. } while (!EOF);
  414. return XmlNodeType.None;
  415. }
  416. public abstract bool MoveToElement ();
  417. public abstract bool MoveToFirstAttribute ();
  418. public abstract bool MoveToNextAttribute ();
  419. public abstract bool Read ();
  420. public abstract bool ReadAttributeValue ();
  421. public virtual string ReadElementString ()
  422. {
  423. if (MoveToContent () != XmlNodeType.Element) {
  424. string error = String.Format ("'{0}' is an invalid node type.",
  425. NodeType.ToString ());
  426. throw XmlError (error);
  427. }
  428. string result = String.Empty;
  429. if (!IsEmptyElement) {
  430. Read ();
  431. result = ReadString ();
  432. if (NodeType != XmlNodeType.EndElement) {
  433. string error = String.Format ("'{0}' is an invalid node type.",
  434. NodeType.ToString ());
  435. throw XmlError (error);
  436. }
  437. }
  438. Read ();
  439. return result;
  440. }
  441. public virtual string ReadElementString (string name)
  442. {
  443. if (MoveToContent () != XmlNodeType.Element) {
  444. string error = String.Format ("'{0}' is an invalid node type.",
  445. NodeType.ToString ());
  446. throw XmlError (error);
  447. }
  448. if (name != Name) {
  449. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  450. Name, NamespaceURI);
  451. throw XmlError (error);
  452. }
  453. string result = String.Empty;
  454. if (!IsEmptyElement) {
  455. Read ();
  456. result = ReadString ();
  457. if (NodeType != XmlNodeType.EndElement) {
  458. string error = String.Format ("'{0}' is an invalid node type.",
  459. NodeType.ToString ());
  460. throw XmlError (error);
  461. }
  462. }
  463. Read ();
  464. return result;
  465. }
  466. public virtual string ReadElementString (string localName, string namespaceName)
  467. {
  468. if (MoveToContent () != XmlNodeType.Element) {
  469. string error = String.Format ("'{0}' is an invalid node type.",
  470. NodeType.ToString ());
  471. throw XmlError (error);
  472. }
  473. if (localName != LocalName || NamespaceURI != namespaceName) {
  474. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  475. LocalName, NamespaceURI);
  476. throw XmlError (error);
  477. }
  478. string result = String.Empty;
  479. if (!IsEmptyElement) {
  480. Read ();
  481. result = ReadString ();
  482. if (NodeType != XmlNodeType.EndElement) {
  483. string error = String.Format ("'{0}' is an invalid node type.",
  484. NodeType.ToString ());
  485. throw XmlError (error);
  486. }
  487. }
  488. Read ();
  489. return result;
  490. }
  491. public virtual void ReadEndElement ()
  492. {
  493. if (MoveToContent () != XmlNodeType.EndElement) {
  494. string error = String.Format ("'{0}' is an invalid node type.",
  495. NodeType.ToString ());
  496. throw XmlError (error);
  497. }
  498. Read ();
  499. }
  500. #if NET_1_0
  501. public abstract string ReadInnerXml ();
  502. public abstract string ReadOuterXml ();
  503. #else
  504. public virtual string ReadInnerXml ()
  505. {
  506. return ReadInnerXmlInternal ();
  507. }
  508. public virtual string ReadOuterXml ()
  509. {
  510. return ReadOuterXmlInternal ();
  511. }
  512. #endif
  513. internal string ReadInnerXmlInternal ()
  514. {
  515. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  516. return String.Empty;
  517. StringWriter sw = new StringWriter ();
  518. XmlTextWriter xtw = new XmlTextWriter (sw);
  519. if (NodeType == XmlNodeType.Element) {
  520. if (IsEmptyElement) {
  521. Read ();
  522. return String.Empty;
  523. }
  524. int startDepth = Depth;
  525. Read ();
  526. while (startDepth < Depth) {
  527. if (ReadState != ReadState.Interactive)
  528. throw XmlError ("Unexpected end of the XML reader.");
  529. xtw.WriteNode (this, false);
  530. }
  531. // reader is now end element, then proceed once more.
  532. Read ();
  533. }
  534. else
  535. xtw.WriteNode (this, false);
  536. return sw.ToString ();
  537. }
  538. internal string ReadOuterXmlInternal ()
  539. {
  540. if (ReadState != ReadState.Interactive || NodeType == XmlNodeType.EndElement)
  541. return String.Empty;
  542. StringWriter sw = new StringWriter ();
  543. XmlTextWriter xtw = new XmlTextWriter (sw);
  544. xtw.WriteNode (this, false);
  545. return sw.ToString ();
  546. }
  547. public virtual void ReadStartElement ()
  548. {
  549. if (MoveToContent () != XmlNodeType.Element) {
  550. string error = String.Format ("'{0}' is an invalid node type.",
  551. NodeType.ToString ());
  552. throw XmlError (error);
  553. }
  554. Read ();
  555. }
  556. public virtual void ReadStartElement (string name)
  557. {
  558. if (MoveToContent () != XmlNodeType.Element) {
  559. string error = String.Format ("'{0}' is an invalid node type.",
  560. NodeType.ToString ());
  561. throw XmlError (error);
  562. }
  563. if (name != Name) {
  564. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  565. Name, NamespaceURI);
  566. throw XmlError (error);
  567. }
  568. Read ();
  569. }
  570. public virtual void ReadStartElement (string localName, string namespaceName)
  571. {
  572. if (MoveToContent () != XmlNodeType.Element) {
  573. string error = String.Format ("'{0}' is an invalid node type.",
  574. NodeType.ToString ());
  575. throw XmlError (error);
  576. }
  577. if (localName != LocalName || NamespaceURI != namespaceName) {
  578. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  579. localName, namespaceName,
  580. LocalName, NamespaceURI);
  581. throw XmlError (error);
  582. }
  583. Read ();
  584. }
  585. #if NET_1_0
  586. public abstract string ReadString ();
  587. #else
  588. public virtual string ReadString ()
  589. {
  590. return ReadStringInternal ();
  591. }
  592. #endif
  593. internal string ReadStringInternal ()
  594. {
  595. if (readStringBuffer == null)
  596. readStringBuffer = new StringBuilder ();
  597. readStringBuffer.Length = 0;
  598. MoveToElement ();
  599. switch (NodeType) {
  600. default:
  601. return String.Empty;
  602. case XmlNodeType.Element:
  603. if (IsEmptyElement)
  604. return String.Empty;
  605. do {
  606. Read ();
  607. switch (NodeType) {
  608. case XmlNodeType.Text:
  609. case XmlNodeType.CDATA:
  610. case XmlNodeType.Whitespace:
  611. case XmlNodeType.SignificantWhitespace:
  612. readStringBuffer.Append (Value);
  613. continue;
  614. }
  615. break;
  616. } while (true);
  617. break;
  618. case XmlNodeType.Text:
  619. case XmlNodeType.CDATA:
  620. case XmlNodeType.Whitespace:
  621. case XmlNodeType.SignificantWhitespace:
  622. do {
  623. switch (NodeType) {
  624. case XmlNodeType.Text:
  625. case XmlNodeType.CDATA:
  626. case XmlNodeType.Whitespace:
  627. case XmlNodeType.SignificantWhitespace:
  628. readStringBuffer.Append (Value);
  629. Read ();
  630. continue;
  631. }
  632. break;
  633. } while (true);
  634. break;
  635. }
  636. string ret = readStringBuffer.ToString ();
  637. readStringBuffer.Length = 0;
  638. return ret;
  639. }
  640. #if NET_2_0
  641. public virtual Type ValueType {
  642. get { return typeof (string); }
  643. }
  644. [MonoTODO]
  645. public virtual bool ReadToDescendant (string name)
  646. {
  647. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  648. return false;
  649. int depth = Depth;
  650. for (Read (); depth < Depth; Read ())
  651. if (NodeType == XmlNodeType.Element && name == Name)
  652. return true;
  653. return false;
  654. }
  655. [MonoTODO]
  656. public virtual bool ReadToDescendant (string localName, string namespaceURI)
  657. {
  658. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  659. return false;
  660. int depth = Depth;
  661. for (Read (); depth < Depth; Read ())
  662. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  663. return true;
  664. return false;
  665. }
  666. [MonoTODO]
  667. public virtual bool ReadToFollowing (string name)
  668. {
  669. while (Read ())
  670. if (NodeType == XmlNodeType.Element && name == Name)
  671. return true;
  672. return false;
  673. }
  674. [MonoTODO]
  675. public virtual bool ReadToFollowing (string localName, string namespaceURI)
  676. {
  677. while (Read ())
  678. if (NodeType == XmlNodeType.Element && localName == Name && namespaceURI == NamespaceURI)
  679. return true;
  680. return false;
  681. }
  682. [MonoTODO]
  683. public virtual bool ReadToNextSibling (string name)
  684. {
  685. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  686. return false;
  687. int depth = Depth;
  688. for (Skip (); depth < Depth; Skip ())
  689. if (NodeType == XmlNodeType.Element && name == Name)
  690. return true;
  691. return false;
  692. }
  693. [MonoTODO]
  694. public virtual bool ReadToNextSibling (string localName, string namespaceURI)
  695. {
  696. if (NodeType != XmlNodeType.Element || IsEmptyElement)
  697. return false;
  698. int depth = Depth;
  699. for (Skip (); depth < Depth; Skip ())
  700. if (NodeType == XmlNodeType.Element && localName == LocalName && namespaceURI == NamespaceURI)
  701. return true;
  702. return false;
  703. }
  704. [MonoTODO]
  705. public virtual XmlReader ReadSubtree ()
  706. {
  707. return new SubtreeXmlReader (this);
  708. }
  709. [MonoTODO]
  710. [Obsolete]
  711. public virtual object ReadTypedValue ()
  712. {
  713. if (NodeType == XmlNodeType.Element)
  714. return ReadElementContentAs (ValueType, this as IXmlNamespaceResolver);
  715. else
  716. return ReadContentAs (ValueType, this as IXmlNamespaceResolver);
  717. }
  718. private string ReadContentString ()
  719. {
  720. switch (NodeType) {
  721. case XmlNodeType.Text:
  722. case XmlNodeType.CDATA:
  723. case XmlNodeType.SignificantWhitespace:
  724. case XmlNodeType.Whitespace:
  725. break;
  726. default:
  727. throw new InvalidOperationException (String.Format ("This method does not support node type {0}.", NodeType));
  728. }
  729. return ReadString ();
  730. }
  731. [MonoTODO]
  732. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver)
  733. {
  734. return ValueAs (ReadElementString (), type, resolver);
  735. }
  736. [MonoTODO]
  737. public virtual object ReadElementContentAs (Type type, IXmlNamespaceResolver resolver, string localName, string namespaceURI)
  738. {
  739. return ValueAs (ReadElementString (localName, namespaceURI), type, resolver);
  740. }
  741. [MonoTODO]
  742. public virtual object ReadContentAs (Type type, IXmlNamespaceResolver resolver)
  743. {
  744. return ValueAs (ReadContentString (), type, resolver);
  745. }
  746. private object ValueAs (string text, Type type, IXmlNamespaceResolver resolver)
  747. {
  748. try {
  749. if (type == typeof (XmlQualifiedName))
  750. return XmlQualifiedName.Parse (text, resolver);
  751. switch (Type.GetTypeCode (type)) {
  752. case TypeCode.Boolean:
  753. return ReadContentAsBoolean ();
  754. case TypeCode.DateTime:
  755. return ReadContentAsDateTime ();
  756. case TypeCode.Double:
  757. return ReadContentAsDouble ();
  758. case TypeCode.Int32:
  759. return ReadContentAsInt ();
  760. case TypeCode.String:
  761. return ReadContentAsString ();
  762. }
  763. } catch (Exception ex) {
  764. return new FormatException (String.Format ("Current text value '{0}' is not acceptable for specified type '{1}'.", text, type));
  765. }
  766. throw new ArgumentException (String.Format ("Specified type '{0}' is not supported.", type));
  767. }
  768. [MonoTODO]
  769. public virtual bool ReadElementContentAsBoolean ()
  770. {
  771. return XQueryConvert.StringToBoolean (ReadElementString ());
  772. }
  773. [MonoTODO]
  774. public virtual DateTime ReadElementContentAsDateTime ()
  775. {
  776. return XQueryConvert.StringToDateTime (ReadElementString ());
  777. }
  778. [MonoTODO]
  779. public virtual double ReadElementContentAsDouble ()
  780. {
  781. return XQueryConvert.StringToDouble (ReadElementString ());
  782. }
  783. [MonoTODO]
  784. public virtual int ReadElementContentAsInt ()
  785. {
  786. return XQueryConvert.StringToInt (ReadElementString ());
  787. }
  788. [MonoTODO]
  789. public virtual string ReadElementContentAsString ()
  790. {
  791. return ReadElementString ();
  792. }
  793. [MonoTODO]
  794. public virtual bool ReadElementContentAsBoolean (string localName, string namespaceURI)
  795. {
  796. return XQueryConvert.StringToBoolean (ReadElementString (localName, namespaceURI));
  797. }
  798. [MonoTODO]
  799. public virtual DateTime ReadElementContentAsDateTime (string localName, string namespaceURI)
  800. {
  801. return XQueryConvert.StringToDateTime (ReadElementString (localName, namespaceURI));
  802. }
  803. [MonoTODO]
  804. public virtual double ReadElementContentAsDouble (string localName, string namespaceURI)
  805. {
  806. return XQueryConvert.StringToDouble (ReadElementString (localName, namespaceURI));
  807. }
  808. [MonoTODO]
  809. public virtual int ReadElementContentAsInt (string localName, string namespaceURI)
  810. {
  811. return XQueryConvert.StringToInt (ReadElementString (localName, namespaceURI));
  812. }
  813. [MonoTODO]
  814. public virtual string ReadElementContentAsString (string localName, string namespaceURI)
  815. {
  816. return ReadElementString (localName, namespaceURI);
  817. }
  818. [MonoTODO]
  819. public virtual bool ReadContentAsBoolean ()
  820. {
  821. return XQueryConvert.StringToBoolean (ReadContentString ());
  822. }
  823. [MonoTODO]
  824. public virtual DateTime ReadContentAsDateTime ()
  825. {
  826. return XQueryConvert.StringToDateTime (ReadContentString ());
  827. }
  828. [MonoTODO]
  829. public virtual double ReadContentAsDouble ()
  830. {
  831. return XQueryConvert.StringToDouble (ReadContentString ());
  832. }
  833. [MonoTODO]
  834. public virtual int ReadContentAsInt ()
  835. {
  836. return XQueryConvert.StringToInt (ReadContentString ());
  837. }
  838. [MonoTODO]
  839. public virtual string ReadContentAsString ()
  840. {
  841. return ReadContentString ();
  842. }
  843. public virtual int ReadContentAsBase64 (
  844. byte [] buffer, int offset, int length)
  845. {
  846. CheckSupport ();
  847. return binary.ReadContentAsBase64 (
  848. buffer, offset, length);
  849. }
  850. public virtual int ReadContentAsBinHex (
  851. byte [] buffer, int offset, int length)
  852. {
  853. CheckSupport ();
  854. return binary.ReadContentAsBinHex (
  855. buffer, offset, length);
  856. }
  857. public virtual int ReadElementContentAsBase64 (
  858. byte [] buffer, int offset, int length)
  859. {
  860. CheckSupport ();
  861. return binary.ReadElementContentAsBase64 (
  862. buffer, offset, length);
  863. }
  864. public virtual int ReadElementContentAsBinHex (
  865. byte [] buffer, int offset, int length)
  866. {
  867. CheckSupport ();
  868. return binary.ReadElementContentAsBase64 (
  869. buffer, offset, length);
  870. }
  871. #endif
  872. #if NET_2_0
  873. public virtual int ReadValueChunk (
  874. char [] buffer, int offset, int length)
  875. #else
  876. internal virtual int ReadValueChunk (
  877. char [] buffer, int offset, int length)
  878. #endif
  879. {
  880. if (!CanReadValueChunk)
  881. throw new NotSupportedException ();
  882. if (binary == null)
  883. binary = new XmlReaderBinarySupport (this);
  884. return binary.ReadValueChunk (buffer, offset, length);
  885. }
  886. private void CheckSupport ()
  887. {
  888. // Default implementation expects both.
  889. if (!CanReadBinaryContent || !CanReadValueChunk)
  890. throw new NotSupportedException ();
  891. if (binary == null)
  892. binary = new XmlReaderBinarySupport (this);
  893. }
  894. public abstract void ResolveEntity ();
  895. public virtual void Skip ()
  896. {
  897. if (ReadState != ReadState.Interactive)
  898. return;
  899. MoveToElement ();
  900. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  901. Read ();
  902. return;
  903. }
  904. int depth = Depth;
  905. while (Read () && depth < Depth)
  906. ;
  907. if (NodeType == XmlNodeType.EndElement)
  908. Read ();
  909. }
  910. private XmlException XmlError (string message)
  911. {
  912. return new XmlException (this as IXmlLineInfo, BaseURI, message);
  913. }
  914. #endregion
  915. }
  916. }