XmlNodeReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  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. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. using System.Text;
  15. namespace System.Xml
  16. {
  17. public class XmlNodeReader : XmlReader
  18. {
  19. XmlDocument document;
  20. XmlNode startNode;
  21. XmlNode current;
  22. ReadState state = ReadState.Initial;
  23. int depth;
  24. bool isEndElement;
  25. bool isEndEntity;
  26. bool nextIsEndElement; // used for ReadString()
  27. bool alreadyRead;
  28. StringBuilder valueBuilder = new StringBuilder ();
  29. XmlNamespaceManager defaultNsmgr;
  30. private XmlNode ownerElement {
  31. get {
  32. return (current.NodeType == XmlNodeType.Attribute) ? ((XmlAttribute)current).OwnerElement : current;
  33. }
  34. }
  35. #region Constructor
  36. public XmlNodeReader (XmlNode node)
  37. {
  38. startNode = node;
  39. document = startNode.NodeType == XmlNodeType.Document ?
  40. startNode as XmlDocument : startNode.OwnerDocument;
  41. if (node.NodeType != XmlNodeType.Document
  42. && node.NodeType != XmlNodeType.DocumentFragment)
  43. alreadyRead = true;
  44. defaultNsmgr = new XmlNamespaceManager (this.NameTable);
  45. }
  46. #endregion
  47. #region Properties
  48. public override int AttributeCount {
  49. get {
  50. if (isEndElement || current == null || current.Attributes == null)
  51. return 0;
  52. return ownerElement.Attributes.Count;
  53. }
  54. }
  55. public override string BaseURI {
  56. get {
  57. if (current == null)
  58. return String.Empty;
  59. return current.BaseURI;
  60. }
  61. }
  62. [MonoTODO("wait for XML resolver")]
  63. public override bool CanResolveEntity {
  64. get {
  65. throw new NotImplementedException ();
  66. }
  67. }
  68. public override int Depth {
  69. get { return depth; }
  70. }
  71. public override bool EOF {
  72. get {
  73. return this.ReadState == ReadState.EndOfFile
  74. || this.ReadState == ReadState.Error;
  75. }
  76. }
  77. public override bool HasAttributes {
  78. get {
  79. if (isEndElement || current == null)
  80. return false;
  81. if (current.Attributes == null ||
  82. current.Attributes.Count == 0)
  83. return false;
  84. else
  85. return true;
  86. }
  87. }
  88. public override bool HasValue {
  89. get {
  90. if (current == null)
  91. return false;
  92. if (current.NodeType == XmlNodeType.Element ||
  93. current.NodeType == XmlNodeType.EntityReference ||
  94. current.NodeType == XmlNodeType.Document ||
  95. current.NodeType == XmlNodeType.DocumentFragment ||
  96. current.NodeType == XmlNodeType.Notation ||
  97. current.NodeType == XmlNodeType.EndElement ||
  98. current.NodeType == XmlNodeType.EndEntity)
  99. return false;
  100. else
  101. return true;
  102. }
  103. }
  104. [MonoTODO("waiting for DTD implementation")]
  105. public override bool IsDefault {
  106. get {
  107. if (current == null)
  108. return false;
  109. if (current.NodeType != XmlNodeType.Attribute)
  110. return false;
  111. else
  112. {
  113. return ((XmlAttribute) current).isDefault;
  114. }
  115. }
  116. }
  117. public override bool IsEmptyElement {
  118. get {
  119. if (current == null)
  120. return false;
  121. if(current.NodeType == XmlNodeType.Element)
  122. return ((XmlElement) current).IsEmpty;
  123. else
  124. return false;
  125. }
  126. }
  127. public override string this [int i] {
  128. get {
  129. // This is MS.NET bug which returns attributes in spite of EndElement.
  130. if (isEndElement || current == null)
  131. return null;
  132. if (NodeType == XmlNodeType.XmlDeclaration) {
  133. XmlDeclaration decl = current as XmlDeclaration;
  134. switch (i) {
  135. case 0:
  136. return decl.Version;
  137. case 1:
  138. if (decl.Encoding != String.Empty)
  139. return decl.Encoding;
  140. else if (decl.Standalone != String.Empty)
  141. return decl.Standalone;
  142. else
  143. throw new ArgumentOutOfRangeException ("Index out of range.");
  144. case 2:
  145. if (decl.Encoding != String.Empty && decl.Standalone != null)
  146. return decl.Standalone;
  147. else
  148. throw new ArgumentOutOfRangeException ("Index out of range.");
  149. }
  150. }
  151. if (i < 0 || i > AttributeCount)
  152. throw new ArgumentOutOfRangeException ("Index out of range.");
  153. return ownerElement.Attributes [i].Value;
  154. }
  155. }
  156. private string GetXmlDeclarationAttribute (string name)
  157. {
  158. XmlDeclaration decl = current as XmlDeclaration;
  159. switch (name) {
  160. case "version":
  161. return decl.Version;
  162. case "encoding":
  163. return decl.Encoding;
  164. case "standalone":
  165. return decl.Standalone;
  166. }
  167. return null;
  168. }
  169. public override string this [string name] {
  170. get {
  171. // This is MS.NET bug which returns attributes in spite of EndElement.
  172. if (isEndElement || current == null)
  173. return null;
  174. if (NodeType == XmlNodeType.XmlDeclaration)
  175. return GetXmlDeclarationAttribute (name);
  176. XmlAttribute attr = ownerElement.Attributes [name];
  177. if (attr == null)
  178. return null;
  179. else
  180. return attr.Value;
  181. }
  182. }
  183. public override string this [string name, string namespaceURI] {
  184. get {
  185. // This is MS.NET bug which returns attributes in spite of EndElement.
  186. if (isEndElement || current == null)
  187. return null;
  188. if (NodeType == XmlNodeType.XmlDeclaration)
  189. return GetXmlDeclarationAttribute (name);
  190. XmlAttribute attr = ownerElement.Attributes [name, namespaceURI];
  191. if (attr == null)
  192. return null; // In fact MS.NET returns null instead of String.Empty.
  193. else
  194. return attr.Value;
  195. }
  196. }
  197. public override string LocalName {
  198. get {
  199. if (current == null)
  200. return String.Empty;
  201. switch (current.NodeType) {
  202. case XmlNodeType.Attribute:
  203. case XmlNodeType.DocumentType:
  204. case XmlNodeType.Element:
  205. case XmlNodeType.EntityReference:
  206. case XmlNodeType.ProcessingInstruction:
  207. case XmlNodeType.XmlDeclaration:
  208. return current.LocalName;
  209. }
  210. return String.Empty;
  211. }
  212. }
  213. public override string Name {
  214. get {
  215. if (current == null)
  216. return String.Empty;
  217. switch (current.NodeType) {
  218. case XmlNodeType.Attribute:
  219. case XmlNodeType.DocumentType:
  220. case XmlNodeType.Element:
  221. case XmlNodeType.EntityReference:
  222. case XmlNodeType.ProcessingInstruction:
  223. case XmlNodeType.XmlDeclaration:
  224. return current.Name;
  225. }
  226. return String.Empty;
  227. }
  228. }
  229. public override string NamespaceURI {
  230. get {
  231. if (current == null)
  232. return String.Empty;
  233. return current.NamespaceURI;
  234. }
  235. }
  236. public override XmlNameTable NameTable {
  237. get { return document.NameTable; }
  238. }
  239. public override XmlNodeType NodeType {
  240. get {
  241. if (current == null)
  242. return XmlNodeType.None;
  243. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  244. }
  245. }
  246. public override string Prefix {
  247. get {
  248. if (current == null)
  249. return String.Empty;
  250. return current.Prefix;
  251. }
  252. }
  253. public override char QuoteChar {
  254. get { return '"'; }
  255. }
  256. public override ReadState ReadState {
  257. get { return state; }
  258. }
  259. public override string Value {
  260. get {
  261. return HasValue ? current.Value : String.Empty;
  262. }
  263. }
  264. public override string XmlLang {
  265. get {
  266. if (current == null)
  267. return String.Empty;
  268. return current.XmlLang;
  269. }
  270. }
  271. public override XmlSpace XmlSpace {
  272. get {
  273. if (current == null)
  274. return XmlSpace.None;
  275. return current.XmlSpace;
  276. }
  277. }
  278. #endregion
  279. #region Methods
  280. public override void Close ()
  281. {
  282. current = null;
  283. state = ReadState.Closed;
  284. }
  285. public override string GetAttribute (int attributeIndex)
  286. {
  287. return this [attributeIndex];
  288. }
  289. public override string GetAttribute (string name)
  290. {
  291. return this [name];
  292. }
  293. public override string GetAttribute (string name, string namespaceURI)
  294. {
  295. return this [name, namespaceURI];
  296. }
  297. public override string LookupNamespace (string prefix)
  298. {
  299. if (current == null)
  300. return null;
  301. XmlAttribute curAttr = current as XmlAttribute;
  302. XmlNode target = curAttr != null ? curAttr.OwnerElement : current;
  303. if (prefix == "") {
  304. do {
  305. XmlAttribute attr = target.Attributes ["xmlns"];
  306. if (attr != null)
  307. return attr.Value;
  308. target = target.ParentNode;
  309. } while (target.NodeType != XmlNodeType.Document);
  310. } else {
  311. string name = "xmlns:" + prefix;
  312. do {
  313. XmlAttribute attr = target.Attributes [name];
  314. if (attr != null)
  315. return attr.Value;
  316. target = target.ParentNode;
  317. } while (target.NodeType != XmlNodeType.Document);
  318. }
  319. return defaultNsmgr.LookupNamespace (prefix);
  320. }
  321. public override void MoveToAttribute (int attributeIndex)
  322. {
  323. if (isEndElement || attributeIndex < 0 || attributeIndex > AttributeCount)
  324. throw new ArgumentOutOfRangeException ();
  325. state = ReadState.Interactive;
  326. current = ownerElement.Attributes [attributeIndex];
  327. }
  328. public override bool MoveToAttribute (string name)
  329. {
  330. if (isEndElement || current == null)
  331. return false;
  332. XmlAttribute attr = ownerElement.Attributes [name];
  333. if (attr == null)
  334. return false;
  335. else {
  336. current = attr;
  337. return true;
  338. }
  339. }
  340. public override bool MoveToAttribute (string name, string namespaceURI)
  341. {
  342. if (isEndElement || current == null)
  343. return false;
  344. XmlAttribute attr = ownerElement.Attributes [name, namespaceURI];
  345. if (attr == null)
  346. return false;
  347. else {
  348. current = attr;
  349. return true;
  350. }
  351. }
  352. private void MoveToParentElement ()
  353. {
  354. // This is buggy. It is not only the case when EndElement = true.
  355. isEndElement = true;
  356. depth--;
  357. current = current.ParentNode;
  358. }
  359. public override bool MoveToElement ()
  360. {
  361. if (current == null)
  362. return false;
  363. if (current.NodeType == XmlNodeType.Attribute) {
  364. current = ((XmlAttribute) current).OwnerElement;
  365. return true;
  366. } else
  367. return false;
  368. }
  369. public override bool MoveToFirstAttribute ()
  370. {
  371. if (current == null)
  372. return false;
  373. if(ownerElement.Attributes.Count > 0)
  374. {
  375. current = ownerElement.Attributes [0];
  376. return true;
  377. }
  378. else
  379. return false;
  380. }
  381. public override bool MoveToNextAttribute ()
  382. {
  383. if (current == null)
  384. return false;
  385. if (current.NodeType != XmlNodeType.Attribute)
  386. return MoveToFirstAttribute ();
  387. else
  388. {
  389. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  390. for (int i=0; i<ac.Count-1; i++)
  391. {
  392. XmlAttribute attr = ac [i];
  393. if (attr == current)
  394. {
  395. current = ac [i+1];
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. }
  402. private bool MoveToNextSibling ()
  403. {
  404. if (nextIsEndElement) {
  405. // nextIsEndElement is set only by ReadString.
  406. nextIsEndElement = false;
  407. MoveToParentElement ();
  408. } else if (alreadyRead) {
  409. alreadyRead = false;
  410. return current != null;
  411. }
  412. if (current.NextSibling != null) {
  413. isEndElement = false;
  414. current = current.NextSibling;
  415. } else {
  416. MoveToParentElement ();
  417. }
  418. if (current == null) {
  419. state = ReadState.EndOfFile;
  420. return false;
  421. }
  422. else
  423. return true;
  424. }
  425. [MonoTODO("Entity handling is not supported.")]
  426. public override bool Read ()
  427. {
  428. if (EOF)
  429. return false;
  430. if (ReadState == ReadState.Initial) {
  431. current = startNode;
  432. state = ReadState.Interactive;
  433. // when startNode is document or fragment
  434. if (!alreadyRead)
  435. current = startNode.FirstChild;
  436. else
  437. alreadyRead = false;
  438. if (current == null) {
  439. state = ReadState.Error;
  440. return false;
  441. } else
  442. return true;
  443. }
  444. MoveToElement ();
  445. isEndEntity = false;
  446. if (IsEmptyElement || isEndElement) {
  447. // Then go up and move to next.
  448. // If no more nodes, then set EOF.
  449. isEndElement = false;
  450. if (current.ParentNode == null
  451. || current.ParentNode.NodeType == XmlNodeType.Document
  452. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  453. current = null;
  454. state = ReadState.EndOfFile;
  455. return false;
  456. } else if (current.NextSibling == null) {
  457. depth--;
  458. current = current.ParentNode;
  459. isEndElement = true;
  460. return true;
  461. } else {
  462. current = current.NextSibling;
  463. return true;
  464. }
  465. } else if (nextIsEndElement) {
  466. // nextIsEndElement is set only by ReadString.
  467. nextIsEndElement = false;
  468. isEndElement = true;
  469. return current != null;
  470. } else if (alreadyRead) {
  471. alreadyRead = false;
  472. return current != null;
  473. }
  474. if (!isEndElement && current.FirstChild != null) {
  475. isEndElement = false;
  476. current = current.FirstChild;
  477. depth++;
  478. } else if (current.NodeType == XmlNodeType.Element) {
  479. isEndElement = true;
  480. if (current.FirstChild != null)
  481. depth--;
  482. } else
  483. MoveToNextSibling ();
  484. return current != null;
  485. }
  486. public override bool ReadAttributeValue ()
  487. {
  488. if (current.NodeType == XmlNodeType.Attribute) {
  489. current = current.FirstChild;
  490. return current != null;
  491. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  492. current = current.NextSibling;
  493. return current != null;
  494. } else
  495. return false;
  496. }
  497. // Its traversal behavior is almost same as Read().
  498. public override string ReadInnerXml ()
  499. {
  500. XmlNode initial = current;
  501. // Almost copied from XmlTextReader.
  502. switch (NodeType) {
  503. case XmlNodeType.Attribute:
  504. return Value;
  505. case XmlNodeType.Element:
  506. if (IsEmptyElement)
  507. return String.Empty;
  508. int startDepth = depth;
  509. bool loop = true;
  510. do {
  511. Read ();
  512. if (NodeType ==XmlNodeType.None)
  513. throw new XmlException ("unexpected end of xml.");
  514. else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
  515. loop = false;
  516. Read ();
  517. }
  518. } while (loop);
  519. return initial.InnerXml;
  520. case XmlNodeType.None:
  521. return String.Empty;
  522. default:
  523. Read ();
  524. return String.Empty;
  525. }
  526. }
  527. [MonoTODO("Need to move to next content.")]
  528. // Its traversal behavior is almost same as Read().
  529. public override string ReadOuterXml ()
  530. {
  531. if (NodeType == XmlNodeType.EndElement)
  532. return String.Empty;
  533. /*
  534. if (current.NodeType != XmlNodeType.Attribute &&
  535. current.NodeType != XmlNodeType.Element)
  536. return String.Empty;
  537. else
  538. return current.OuterXml;
  539. */
  540. XmlNode initial = current;
  541. switch (NodeType) {
  542. case XmlNodeType.Attribute:
  543. return current.OuterXml;
  544. case XmlNodeType.Element:
  545. if (NodeType == XmlNodeType.Element && !IsEmptyElement)
  546. ReadInnerXml ();
  547. else
  548. Read ();
  549. return initial.OuterXml;
  550. case XmlNodeType.None:
  551. return String.Empty;
  552. default:
  553. Read ();
  554. return String.Empty;
  555. }
  556. }
  557. public override string ReadString ()
  558. {
  559. return ReadStringInternal ();
  560. }
  561. [MonoTODO]
  562. public override void ResolveEntity ()
  563. {
  564. throw new NotImplementedException ();
  565. // if (current.NodeType != XmlNodeType.EntityReference)
  566. // throw new InvalidOperationException ("The current node is not an Entity Reference");
  567. }
  568. [MonoTODO("test it.")]
  569. public override void Skip ()
  570. {
  571. // Why is this overriden? Such skipping might raise
  572. // (or ignore) unexpected validation error.
  573. base.Skip ();
  574. }
  575. #endregion
  576. }
  577. }