XmlNodeReader.cs 25 KB

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