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