XmlReader.cs 30 KB

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