XmlReader.cs 30 KB

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