XmlNodeReader.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. //
  2. // System.Xml.XmlNodeReader.cs
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. // (C) Atsushi Enomoto
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml;
  34. using System.Text;
  35. namespace System.Xml
  36. {
  37. public class XmlNodeReader : XmlReader
  38. {
  39. XmlDocument document;
  40. XmlNode startNode;
  41. XmlNode current;
  42. ReadState state = ReadState.Initial;
  43. int depth;
  44. bool isEndElement;
  45. bool nextIsEndElement; // used for ReadString()
  46. bool alreadyRead;
  47. StringBuilder valueBuilder = new StringBuilder ();
  48. XmlNamespaceManager defaultNsmgr;
  49. Stack entityReaderStack = new Stack ();
  50. XmlTextReader entityReader;
  51. private XmlNode ownerLinkedNode {
  52. get {
  53. if (current.ParentNode != null && current.ParentNode.NodeType == XmlNodeType.Attribute)
  54. return ((XmlAttribute) current.ParentNode).OwnerElement;
  55. else if (current.NodeType == XmlNodeType.Attribute)
  56. return ((XmlAttribute) current).OwnerElement;
  57. else
  58. return current;
  59. }
  60. }
  61. #region Constructor
  62. public XmlNodeReader (XmlNode node)
  63. {
  64. startNode = node;
  65. document = startNode.NodeType == XmlNodeType.Document ?
  66. startNode as XmlDocument : startNode.OwnerDocument;
  67. if (node.NodeType != XmlNodeType.Document
  68. && node.NodeType != XmlNodeType.DocumentFragment)
  69. alreadyRead = true;
  70. defaultNsmgr = new XmlNamespaceManager (this.NameTable);
  71. }
  72. #endregion
  73. #region Properties
  74. public override int AttributeCount {
  75. get {
  76. if (entityReader != null)
  77. return entityReader.ReadState == ReadState.Interactive ?
  78. entityReader.AttributeCount : 0;
  79. if (isEndElement || current == null)
  80. return 0;
  81. XmlNode n = ownerLinkedNode;
  82. return n.Attributes != null ? n.Attributes.Count : 0;
  83. }
  84. }
  85. public override string BaseURI {
  86. get {
  87. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  88. return entityReader.BaseURI;
  89. if (current == null)
  90. return String.Empty;
  91. return current.BaseURI;
  92. }
  93. }
  94. public override bool CanResolveEntity {
  95. get {
  96. return true;
  97. }
  98. }
  99. public override int Depth {
  100. get {
  101. if (entityReader != null && entityReader.ReadState == ReadState.Interactive)
  102. return entityReader.Depth + depth + entityReaderStack.Count + 1;
  103. if (current == null)
  104. return 0;
  105. if (current.NodeType == XmlNodeType.Attribute)
  106. return depth + 1;
  107. if (current.ParentNode != null && current.ParentNode.NodeType == XmlNodeType.Attribute)
  108. return depth + 2;
  109. return depth;
  110. }
  111. }
  112. public override bool EOF {
  113. get {
  114. return this.ReadState == ReadState.EndOfFile
  115. || this.ReadState == ReadState.Error;
  116. }
  117. }
  118. public override bool HasAttributes {
  119. get {
  120. if (entityReader != null)
  121. return entityReader.ReadState == ReadState.Interactive ?
  122. entityReader.HasAttributes : false;
  123. if (isEndElement || current == null)
  124. return false;
  125. // MS BUG: inconsistent return value between XmlTextReader and XmlNodeReader.
  126. // As for attribute and its descendants, XmlReader returns element's HasAttributes.
  127. XmlNode n = ownerLinkedNode;
  128. if (n.Attributes == null ||
  129. n.Attributes.Count == 0)
  130. return false;
  131. else
  132. return true;
  133. }
  134. }
  135. public override bool HasValue {
  136. get {
  137. if (entityReader != null)
  138. return entityReader.ReadState == ReadState.Interactive ?
  139. entityReader.IsDefault : false;
  140. if (current == null)
  141. return false;
  142. switch (current.NodeType) {
  143. case XmlNodeType.Element:
  144. case XmlNodeType.EntityReference:
  145. case XmlNodeType.Document:
  146. case XmlNodeType.DocumentFragment:
  147. case XmlNodeType.Notation:
  148. case XmlNodeType.EndElement:
  149. case XmlNodeType.EndEntity:
  150. return false;
  151. default:
  152. return true;
  153. }
  154. }
  155. }
  156. public override bool IsDefault {
  157. get {
  158. if (entityReader != null)
  159. return entityReader.ReadState == ReadState.Interactive ?
  160. entityReader.IsDefault : false;
  161. if (current == null)
  162. return false;
  163. if (current.NodeType != XmlNodeType.Attribute)
  164. return false;
  165. else
  166. {
  167. return ((XmlAttribute) current).isDefault;
  168. }
  169. }
  170. }
  171. public override bool IsEmptyElement {
  172. get {
  173. if (entityReader != null)
  174. return entityReader.ReadState == ReadState.Interactive ?
  175. entityReader.IsDefault : false;
  176. if (current == null)
  177. return false;
  178. if(current.NodeType == XmlNodeType.Element)
  179. return ((XmlElement) current).IsEmpty;
  180. else
  181. return false;
  182. }
  183. }
  184. #if NET_2_0
  185. #else
  186. public override string this [int i] {
  187. get { return GetAttribute (i); }
  188. }
  189. public override string this [string name] {
  190. get { return GetAttribute (name); }
  191. }
  192. public override string this [string name, string namespaceURI] {
  193. get { return GetAttribute (name, namespaceURI); }
  194. }
  195. #endif
  196. public override string LocalName {
  197. get {
  198. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  199. return entityReader.LocalName;
  200. if (current == null)
  201. return String.Empty;
  202. switch (current.NodeType) {
  203. case XmlNodeType.Attribute:
  204. case XmlNodeType.DocumentType:
  205. case XmlNodeType.Element:
  206. case XmlNodeType.EntityReference:
  207. case XmlNodeType.ProcessingInstruction:
  208. case XmlNodeType.XmlDeclaration:
  209. return current.LocalName;
  210. }
  211. return String.Empty;
  212. }
  213. }
  214. public override string Name {
  215. get {
  216. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  217. return entityReader.Name;
  218. if (current == null)
  219. return String.Empty;
  220. switch (current.NodeType) {
  221. case XmlNodeType.Attribute:
  222. case XmlNodeType.DocumentType:
  223. case XmlNodeType.Element:
  224. case XmlNodeType.EntityReference:
  225. case XmlNodeType.ProcessingInstruction:
  226. case XmlNodeType.XmlDeclaration:
  227. return current.Name;
  228. }
  229. return String.Empty;
  230. }
  231. }
  232. public override string NamespaceURI {
  233. get {
  234. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  235. return entityReader.NamespaceURI;
  236. if (current == null)
  237. return String.Empty;
  238. return current.NamespaceURI;
  239. }
  240. }
  241. public override XmlNameTable NameTable {
  242. get { return document.NameTable; }
  243. }
  244. public override XmlNodeType NodeType {
  245. get {
  246. if (entityReader != null)
  247. switch (entityReader.ReadState) {
  248. case ReadState.Interactive:
  249. return entityReader.NodeType;
  250. case ReadState.Initial:
  251. return XmlNodeType.EntityReference;
  252. case ReadState.EndOfFile:
  253. return XmlNodeType.EndEntity;
  254. }
  255. if (current == null)
  256. return XmlNodeType.None;
  257. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  258. }
  259. }
  260. public override string Prefix {
  261. get {
  262. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  263. return entityReader.Prefix;
  264. if (current == null)
  265. return String.Empty;
  266. // if (current.NodeType == XmlNodeType.Attribute)
  267. // return current.Prefix != String.Empty ? current.Prefix : null;
  268. // else
  269. return current.Prefix;
  270. }
  271. }
  272. #if NET_2_0
  273. #else
  274. public override char QuoteChar {
  275. get {
  276. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  277. return entityReader.QuoteChar;
  278. return '"';
  279. }
  280. }
  281. #endif
  282. public override ReadState ReadState {
  283. get { return state; }
  284. }
  285. public override string Value {
  286. get {
  287. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  288. return entityReader.Value;
  289. if (NodeType == XmlNodeType.DocumentType)
  290. return ((XmlDocumentType) current).InternalSubset;
  291. else
  292. return HasValue ? current.Value : String.Empty;
  293. }
  294. }
  295. public override string XmlLang {
  296. get {
  297. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  298. return entityReader.XmlLang;
  299. if (current == null)
  300. return String.Empty;
  301. return current.XmlLang;
  302. }
  303. }
  304. public override XmlSpace XmlSpace {
  305. get {
  306. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  307. return entityReader.XmlSpace;
  308. if (current == null)
  309. return XmlSpace.None;
  310. return current.XmlSpace;
  311. }
  312. }
  313. #endregion
  314. #region Methods
  315. // If current entityReference is a child of an attribute,
  316. // then MoveToAttribute simply means that we no more need this entity reader.
  317. // Otherwise, this invokation means that
  318. // it is expected to move to resolved (maybe) element's attribute.
  319. //
  320. // This rule applies to many methods like MoveTo*Attribute().
  321. private bool CheckAndResetEntityReaderOnMoveToAttribute ()
  322. {
  323. if (entityReader == null)
  324. return false;
  325. if (current != null && current.ParentNode != null &&
  326. current.ParentNode.NodeType == XmlNodeType.Attribute) {
  327. entityReader.Close ();
  328. entityReader = entityReaderStack.Count > 0 ?
  329. entityReaderStack.Pop () as XmlTextReader : null;
  330. return true;
  331. }
  332. else
  333. return false;
  334. }
  335. public override void Close ()
  336. {
  337. if (entityReader != null)
  338. entityReader.Close ();
  339. while (entityReaderStack.Count > 0)
  340. ((XmlTextReader) entityReaderStack.Pop ()).Close ();
  341. current = null;
  342. state = ReadState.Closed;
  343. }
  344. public override string GetAttribute (int attributeIndex)
  345. {
  346. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  347. return entityReader.GetAttribute (attributeIndex);
  348. if (NodeType == XmlNodeType.XmlDeclaration) {
  349. XmlDeclaration decl = current as XmlDeclaration;
  350. if (attributeIndex == 0)
  351. return decl.Version;
  352. else if (attributeIndex == 1) {
  353. if (decl.Encoding != String.Empty)
  354. return decl.Encoding;
  355. else if (decl.Standalone != String.Empty)
  356. return decl.Standalone;
  357. }
  358. else if (attributeIndex == 2 &&
  359. decl.Encoding != String.Empty && decl.Standalone != null)
  360. return decl.Standalone;
  361. throw new ArgumentOutOfRangeException ("Index out of range.");
  362. } else if (NodeType == XmlNodeType.DocumentType) {
  363. XmlDocumentType doctype = current as XmlDocumentType;
  364. if (attributeIndex == 0) {
  365. if (doctype.PublicId != "")
  366. return doctype.PublicId;
  367. else if (doctype.SystemId != "")
  368. return doctype.SystemId;
  369. } else if (attributeIndex == 1)
  370. if (doctype.PublicId == "" && doctype.SystemId != "")
  371. return doctype.SystemId;
  372. throw new ArgumentOutOfRangeException ("Index out of range.");
  373. }
  374. // This is MS.NET bug which returns attributes in spite of EndElement.
  375. if (isEndElement || current == null)
  376. return null;
  377. if (attributeIndex < 0 || attributeIndex > AttributeCount)
  378. throw new ArgumentOutOfRangeException ("Index out of range.");
  379. return ownerLinkedNode.Attributes [attributeIndex].Value;
  380. }
  381. public override string GetAttribute (string name)
  382. {
  383. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  384. return entityReader.GetAttribute (name);
  385. // This is MS.NET bug which returns attributes in spite of EndElement.
  386. if (isEndElement || current == null)
  387. return null;
  388. if (NodeType == XmlNodeType.XmlDeclaration)
  389. return GetXmlDeclarationAttribute (name);
  390. else if (NodeType == XmlNodeType.DocumentType)
  391. return GetDocumentTypeAttribute (name);
  392. if (ownerLinkedNode.Attributes == null)
  393. return null;
  394. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  395. if (attr == null)
  396. return null;
  397. else
  398. return attr.Value;
  399. }
  400. public override string GetAttribute (string name, string namespaceURI)
  401. {
  402. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  403. return entityReader.GetAttribute (name, namespaceURI);
  404. // This is MS.NET bug which returns attributes in spite of EndElement.
  405. if (isEndElement || current == null)
  406. return null;
  407. if (NodeType == XmlNodeType.XmlDeclaration)
  408. return GetXmlDeclarationAttribute (name);
  409. else if (NodeType == XmlNodeType.DocumentType)
  410. return GetDocumentTypeAttribute (name);
  411. if (ownerLinkedNode.Attributes == null)
  412. return null;
  413. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  414. if (attr == null)
  415. return null; // In fact MS.NET returns null instead of String.Empty.
  416. else
  417. return attr.Value;
  418. }
  419. private string GetXmlDeclarationAttribute (string name)
  420. {
  421. XmlDeclaration decl = current as XmlDeclaration;
  422. switch (name) {
  423. case "version":
  424. return decl.Version;
  425. case "encoding":
  426. // This is MS.NET bug that XmlNodeReturns in case of string.empty.
  427. return decl.Encoding != String.Empty ? decl.Encoding : null;
  428. case "standalone":
  429. return decl.Standalone;
  430. }
  431. return null;
  432. }
  433. private string GetDocumentTypeAttribute (string name)
  434. {
  435. XmlDocumentType doctype = current as XmlDocumentType;
  436. switch (name) {
  437. case "PUBLIC":
  438. return doctype.PublicId;
  439. case "SYSTEM":
  440. return doctype.SystemId;
  441. }
  442. return null;
  443. }
  444. internal XmlParserContext GetInternalParserContext ()
  445. {
  446. if (entityReader != null)
  447. return entityReader.GetInternalParserContext ();
  448. else
  449. return new XmlParserContext (document.NameTable,
  450. current.ConstructNamespaceManager (),
  451. document.DocumentType != null ? document.DocumentType.DTD : null,
  452. current.BaseURI, XmlLang, XmlSpace, Encoding.Unicode);
  453. }
  454. public override string LookupNamespace (string prefix)
  455. {
  456. if (entityReader != null && entityReader.ReadState != ReadState.Initial)
  457. return entityReader.LookupNamespace (prefix);
  458. if (current == null)
  459. return null;
  460. XmlAttribute curAttr = current as XmlAttribute;
  461. XmlNode target = curAttr != null ? curAttr.OwnerElement : current;
  462. if (prefix == "") {
  463. do {
  464. XmlAttribute attr = target.Attributes ["xmlns"];
  465. if (attr != null)
  466. return attr.Value;
  467. target = target.ParentNode;
  468. } while (target.NodeType != XmlNodeType.Document);
  469. } else {
  470. string name = "xmlns:" + prefix;
  471. do {
  472. XmlAttribute attr = target.Attributes [name];
  473. if (attr != null)
  474. return attr.Value;
  475. target = target.ParentNode;
  476. } while (target.NodeType != XmlNodeType.Document);
  477. }
  478. return defaultNsmgr.LookupNamespace (prefix, false);
  479. }
  480. public override void MoveToAttribute (int attributeIndex)
  481. {
  482. if (entityReader != null) {
  483. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ()) {
  484. entityReader.MoveToAttribute (attributeIndex);
  485. return;
  486. }
  487. // And in case of abondoning entityReader, go on...
  488. }
  489. if (isEndElement || attributeIndex < 0 || attributeIndex > AttributeCount)
  490. throw new ArgumentOutOfRangeException ();
  491. state = ReadState.Interactive;
  492. current = ownerLinkedNode.Attributes [attributeIndex];
  493. }
  494. public override bool MoveToAttribute (string name)
  495. {
  496. if (entityReader != null) {
  497. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ())
  498. return entityReader.MoveToAttribute (name);
  499. // And in case of abondoning entityReader, go on...
  500. }
  501. if (isEndElement || current == null)
  502. return false;
  503. XmlNode tmpCurrent = current;
  504. if (current.ParentNode.NodeType == XmlNodeType.Attribute)
  505. current = current.ParentNode;
  506. if (ownerLinkedNode.Attributes == null)
  507. return false;
  508. XmlAttribute attr = ownerLinkedNode.Attributes [name];
  509. if (attr == null) {
  510. current = tmpCurrent;
  511. return false;
  512. }
  513. else {
  514. current = attr;
  515. return true;
  516. }
  517. }
  518. public override bool MoveToAttribute (string name, string namespaceURI)
  519. {
  520. if (entityReader != null) {
  521. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ())
  522. return entityReader.MoveToAttribute (name, namespaceURI);
  523. // And in case of abondoning entityReader, go on...
  524. }
  525. if (isEndElement || current == null)
  526. return false;
  527. if (ownerLinkedNode.Attributes == null)
  528. return false;
  529. XmlAttribute attr = ownerLinkedNode.Attributes [name, namespaceURI];
  530. if (attr == null)
  531. return false;
  532. else {
  533. current = attr;
  534. return true;
  535. }
  536. }
  537. private void MoveToParentElement ()
  538. {
  539. // This is buggy. It is not only the case when EndElement = true.
  540. isEndElement = true;
  541. depth--;
  542. current = current.ParentNode;
  543. }
  544. public override bool MoveToElement ()
  545. {
  546. if (entityReader != null) {
  547. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ())
  548. return entityReader.MoveToElement ();
  549. // And in case of abondoning entityReader, go on...
  550. }
  551. if (current == null)
  552. return false;
  553. XmlNode n = ownerLinkedNode;
  554. if (current != n) {
  555. current = n;
  556. return true;
  557. } else
  558. return false;
  559. }
  560. public override bool MoveToFirstAttribute ()
  561. {
  562. if (entityReader != null) {
  563. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ())
  564. return entityReader.MoveToFirstAttribute ();
  565. // And in case of abondoning entityReader, go on...
  566. }
  567. if (current == null)
  568. return false;
  569. if (ownerLinkedNode.Attributes == null)
  570. return false;
  571. if(ownerLinkedNode.Attributes.Count > 0)
  572. {
  573. current = ownerLinkedNode.Attributes [0];
  574. return true;
  575. }
  576. else
  577. return false;
  578. }
  579. public override bool MoveToNextAttribute ()
  580. {
  581. if (entityReader != null) {
  582. if (!this.CheckAndResetEntityReaderOnMoveToAttribute ())
  583. return entityReader.MoveToNextAttribute ();
  584. // And in case of abondoning entityReader, go on...
  585. }
  586. if (current == null)
  587. return false;
  588. if (current.NodeType != XmlNodeType.Attribute)
  589. return MoveToFirstAttribute ();
  590. else
  591. {
  592. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  593. for (int i=0; i<ac.Count-1; i++)
  594. {
  595. XmlAttribute attr = ac [i];
  596. if (attr == current)
  597. {
  598. i++;
  599. if (i == ac.Count)
  600. return false;
  601. current = ac [i];
  602. return true;
  603. }
  604. }
  605. return false;
  606. }
  607. }
  608. private bool MoveToNextSibling ()
  609. {
  610. if (nextIsEndElement) {
  611. // nextIsEndElement is set only by ReadString.
  612. nextIsEndElement = false;
  613. MoveToParentElement ();
  614. } else if (alreadyRead) {
  615. alreadyRead = false;
  616. return current != null;
  617. }
  618. if (current.NextSibling != null) {
  619. isEndElement = false;
  620. current = current.NextSibling;
  621. } else {
  622. MoveToParentElement ();
  623. }
  624. if (current == null) {
  625. state = ReadState.EndOfFile;
  626. return false;
  627. }
  628. else
  629. return true;
  630. }
  631. public override bool Read ()
  632. {
  633. if (EOF)
  634. return false;
  635. this.CheckAndResetEntityReaderOnMoveToAttribute ();
  636. if (entityReader != null) {
  637. // Read finalizes entity reader.
  638. switch (entityReader.ReadState) {
  639. case ReadState.Interactive:
  640. case ReadState.Initial:
  641. // If it is ended, then other properties/methods will take care.
  642. entityReader.Read ();
  643. return true;
  644. default:
  645. entityReader = entityReaderStack.Count > 0 ?
  646. entityReaderStack.Pop () as XmlTextReader : null;
  647. return Read ();
  648. }
  649. // and go on ...
  650. }
  651. if (ReadState == ReadState.Initial) {
  652. current = startNode;
  653. state = ReadState.Interactive;
  654. // when startNode is document or fragment
  655. if (!alreadyRead)
  656. current = startNode.FirstChild;
  657. else
  658. alreadyRead = false;
  659. if (current == null) {
  660. state = ReadState.Error;
  661. return false;
  662. } else
  663. return true;
  664. }
  665. MoveToElement ();
  666. if (IsEmptyElement || isEndElement) {
  667. // Then go up and move to next.
  668. // If no more nodes, then set EOF.
  669. isEndElement = false;
  670. if (current.ParentNode == null
  671. || current.ParentNode.NodeType == XmlNodeType.Document
  672. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  673. current = null;
  674. state = ReadState.EndOfFile;
  675. return false;
  676. } else if (current.NextSibling == null) {
  677. depth--;
  678. current = current.ParentNode;
  679. isEndElement = true;
  680. return true;
  681. } else {
  682. current = current.NextSibling;
  683. return true;
  684. }
  685. } else if (nextIsEndElement) {
  686. // nextIsEndElement is set only by ReadString.
  687. nextIsEndElement = false;
  688. isEndElement = true;
  689. return current != null;
  690. } else if (alreadyRead) {
  691. alreadyRead = false;
  692. return current != null;
  693. }
  694. if (!isEndElement && current.FirstChild != null && current.NodeType != XmlNodeType.EntityReference) {
  695. isEndElement = false;
  696. current = current.FirstChild;
  697. depth++;
  698. } else if (current.NodeType == XmlNodeType.Element) {
  699. isEndElement = true;
  700. if (current.FirstChild != null)
  701. depth--;
  702. } else
  703. MoveToNextSibling ();
  704. return current != null;
  705. }
  706. public override bool ReadAttributeValue ()
  707. {
  708. if (entityReader != null) {
  709. switch (entityReader.ReadState) {
  710. case ReadState.Interactive:
  711. case ReadState.Initial:
  712. // If it is ended, then other properties/methods will take care.
  713. return entityReader.ReadAttributeValue ();
  714. default:
  715. entityReader = entityReaderStack.Count > 0 ?
  716. entityReaderStack.Pop () as XmlTextReader : null;
  717. // and go on ...
  718. return ReadAttributeValue ();
  719. }
  720. }
  721. if (current.NodeType == XmlNodeType.Attribute) {
  722. if (current.FirstChild == null)
  723. return false;
  724. current = current.FirstChild;
  725. return true;
  726. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  727. if (current.NextSibling == null)
  728. return false;
  729. current = current.NextSibling;
  730. return true;
  731. } else
  732. return false;
  733. }
  734. #if NET_1_0
  735. // Its traversal behavior is almost same as Read().
  736. public override string ReadInnerXml ()
  737. {
  738. if (entityReader != null) {
  739. if (entityReader.EOF) {
  740. entityReader = entityReaderStack.Count > 0 ?
  741. entityReaderStack.Pop () as XmlTextReader : null;
  742. return ReadInnerXml ();
  743. } else
  744. return entityReader.ReadInnerXml ();
  745. }
  746. if (this.state != ReadState.Interactive)
  747. return String.Empty;
  748. XmlNode initial = current;
  749. // Almost copied from XmlTextReader.
  750. switch (NodeType) {
  751. case XmlNodeType.Attribute:
  752. return Value;
  753. case XmlNodeType.Element:
  754. if (IsEmptyElement)
  755. return String.Empty;
  756. int startDepth = depth;
  757. bool loop = true;
  758. do {
  759. Read ();
  760. if (NodeType ==XmlNodeType.None)
  761. throw new XmlException ("unexpected end of xml.");
  762. else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
  763. loop = false;
  764. Read ();
  765. }
  766. } while (loop);
  767. return initial.InnerXml;
  768. case XmlNodeType.None:
  769. return String.Empty;
  770. default:
  771. Read ();
  772. return String.Empty;
  773. }
  774. }
  775. // Its traversal behavior is almost same as Read().
  776. public override string ReadOuterXml ()
  777. {
  778. if (entityReader != null) {
  779. if (entityReader.EOF) {
  780. entityReader = entityReaderStack.Count > 0 ?
  781. entityReaderStack.Pop () as XmlTextReader : null;
  782. return ReadOuterXml ();
  783. } else
  784. return entityReader.ReadOuterXml ();
  785. }
  786. if (NodeType == XmlNodeType.EndElement)
  787. return String.Empty;
  788. XmlNode initial = current;
  789. switch (NodeType) {
  790. case XmlNodeType.Attribute:
  791. return current.OuterXml;
  792. case XmlNodeType.Element:
  793. if (NodeType == XmlNodeType.Element && !IsEmptyElement)
  794. ReadInnerXml ();
  795. else
  796. Read ();
  797. return initial.OuterXml;
  798. case XmlNodeType.None:
  799. return String.Empty;
  800. default:
  801. Read ();
  802. return String.Empty;
  803. }
  804. }
  805. #endif
  806. public override string ReadString ()
  807. {
  808. return ReadStringInternal ();
  809. }
  810. public override void ResolveEntity ()
  811. {
  812. if (NodeType != XmlNodeType.EntityReference)
  813. throw new InvalidOperationException ("The current node is not an Entity Reference");
  814. // FIXME: Now that XmlEntityReference holds the target
  815. // entity's child nodes, we don't have to use
  816. // XmlTextReader and simply use those nodes directly.
  817. string replacementText = current.InnerXml;
  818. XmlNodeType xmlReaderNodeType =
  819. (current.ParentNode != null && current.ParentNode.NodeType == XmlNodeType.Attribute) ?
  820. XmlNodeType.Attribute : XmlNodeType.Element;
  821. XmlParserContext ctx = null;
  822. if (entityReader != null) {
  823. entityReaderStack.Push (entityReader);
  824. ctx = entityReader.GetInternalParserContext ();
  825. }
  826. if (ctx == null) {
  827. ctx = new XmlParserContext (document.NameTable,
  828. current.ConstructNamespaceManager (),
  829. document.DocumentType != null ? document.DocumentType.DTD : null,
  830. BaseURI, XmlLang, XmlSpace, Encoding.Unicode);
  831. }
  832. entityReader = new XmlTextReader (replacementText, xmlReaderNodeType, ctx);
  833. entityReader.XmlResolver = document.Resolver;
  834. entityReader.SkipTextDeclaration ();
  835. }
  836. public override void Skip ()
  837. {
  838. // Why is this overriden? Such skipping might raise
  839. // (or ignore) unexpected validation error.
  840. base.Skip ();
  841. }
  842. #endregion
  843. }
  844. }