XmlReader.cs 29 KB

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