XmlReader.cs 30 KB

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