XmlReader.cs 30 KB

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