XmlNodeReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. XmlNode tmpCurrent = current;
  333. if (current.ParentNode.NodeType == XmlNodeType.Attribute)
  334. current = current.ParentNode;
  335. XmlAttribute attr = ownerElement.Attributes [name];
  336. if (attr == null) {
  337. current = tmpCurrent;
  338. return false;
  339. }
  340. else {
  341. current = attr;
  342. return true;
  343. }
  344. }
  345. public override bool MoveToAttribute (string name, string namespaceURI)
  346. {
  347. if (isEndElement || current == null)
  348. return false;
  349. XmlAttribute attr = ownerElement.Attributes [name, namespaceURI];
  350. if (attr == null)
  351. return false;
  352. else {
  353. current = attr;
  354. return true;
  355. }
  356. }
  357. private void MoveToParentElement ()
  358. {
  359. // This is buggy. It is not only the case when EndElement = true.
  360. isEndElement = true;
  361. depth--;
  362. current = current.ParentNode;
  363. }
  364. public override bool MoveToElement ()
  365. {
  366. if (current == null)
  367. return false;
  368. if (current.NodeType == XmlNodeType.Attribute) {
  369. current = ((XmlAttribute) current).OwnerElement;
  370. return true;
  371. } else
  372. return false;
  373. }
  374. public override bool MoveToFirstAttribute ()
  375. {
  376. if (current == null)
  377. return false;
  378. if(ownerElement.Attributes.Count > 0)
  379. {
  380. current = ownerElement.Attributes [0];
  381. return true;
  382. }
  383. else
  384. return false;
  385. }
  386. public override bool MoveToNextAttribute ()
  387. {
  388. if (current == null)
  389. return false;
  390. if (current.NodeType != XmlNodeType.Attribute)
  391. return MoveToFirstAttribute ();
  392. else
  393. {
  394. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  395. for (int i=0; i<ac.Count-1; i++)
  396. {
  397. XmlAttribute attr = ac [i];
  398. if (attr == current)
  399. {
  400. i++;
  401. if (i == ac.Count)
  402. return false;
  403. current = ac [i];
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409. }
  410. private bool MoveToNextSibling ()
  411. {
  412. if (nextIsEndElement) {
  413. // nextIsEndElement is set only by ReadString.
  414. nextIsEndElement = false;
  415. MoveToParentElement ();
  416. } else if (alreadyRead) {
  417. alreadyRead = false;
  418. return current != null;
  419. }
  420. if (current.NextSibling != null) {
  421. isEndElement = false;
  422. current = current.NextSibling;
  423. } else {
  424. MoveToParentElement ();
  425. }
  426. if (current == null) {
  427. state = ReadState.EndOfFile;
  428. return false;
  429. }
  430. else
  431. return true;
  432. }
  433. [MonoTODO("Entity handling is not supported.")]
  434. public override bool Read ()
  435. {
  436. if (EOF)
  437. return false;
  438. if (ReadState == ReadState.Initial) {
  439. current = startNode;
  440. state = ReadState.Interactive;
  441. // when startNode is document or fragment
  442. if (!alreadyRead)
  443. current = startNode.FirstChild;
  444. else
  445. alreadyRead = false;
  446. if (current == null) {
  447. state = ReadState.Error;
  448. return false;
  449. } else
  450. return true;
  451. }
  452. MoveToElement ();
  453. isEndEntity = false;
  454. if (IsEmptyElement || isEndElement) {
  455. // Then go up and move to next.
  456. // If no more nodes, then set EOF.
  457. isEndElement = false;
  458. if (current.ParentNode == null
  459. || current.ParentNode.NodeType == XmlNodeType.Document
  460. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  461. current = null;
  462. state = ReadState.EndOfFile;
  463. return false;
  464. } else if (current.NextSibling == null) {
  465. depth--;
  466. current = current.ParentNode;
  467. isEndElement = true;
  468. return true;
  469. } else {
  470. current = current.NextSibling;
  471. return true;
  472. }
  473. } else if (nextIsEndElement) {
  474. // nextIsEndElement is set only by ReadString.
  475. nextIsEndElement = false;
  476. isEndElement = true;
  477. return current != null;
  478. } else if (alreadyRead) {
  479. alreadyRead = false;
  480. return current != null;
  481. }
  482. if (!isEndElement && current.FirstChild != null) {
  483. isEndElement = false;
  484. current = current.FirstChild;
  485. depth++;
  486. } else if (current.NodeType == XmlNodeType.Element) {
  487. isEndElement = true;
  488. if (current.FirstChild != null)
  489. depth--;
  490. } else
  491. MoveToNextSibling ();
  492. return current != null;
  493. }
  494. public override bool ReadAttributeValue ()
  495. {
  496. if (current.NodeType == XmlNodeType.Attribute) {
  497. current = current.FirstChild;
  498. return current != null;
  499. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  500. if (current.NextSibling == null)
  501. return false;
  502. current = current.NextSibling;
  503. return true;
  504. } else
  505. return false;
  506. }
  507. // Its traversal behavior is almost same as Read().
  508. public override string ReadInnerXml ()
  509. {
  510. XmlNode initial = current;
  511. // Almost copied from XmlTextReader.
  512. switch (NodeType) {
  513. case XmlNodeType.Attribute:
  514. return Value;
  515. case XmlNodeType.Element:
  516. if (IsEmptyElement)
  517. return String.Empty;
  518. int startDepth = depth;
  519. bool loop = true;
  520. do {
  521. Read ();
  522. if (NodeType ==XmlNodeType.None)
  523. throw new XmlException ("unexpected end of xml.");
  524. else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
  525. loop = false;
  526. Read ();
  527. }
  528. } while (loop);
  529. return initial.InnerXml;
  530. case XmlNodeType.None:
  531. return String.Empty;
  532. default:
  533. Read ();
  534. return String.Empty;
  535. }
  536. }
  537. [MonoTODO("Need to move to next content.")]
  538. // Its traversal behavior is almost same as Read().
  539. public override string ReadOuterXml ()
  540. {
  541. if (NodeType == XmlNodeType.EndElement)
  542. return String.Empty;
  543. /*
  544. if (current.NodeType != XmlNodeType.Attribute &&
  545. current.NodeType != XmlNodeType.Element)
  546. return String.Empty;
  547. else
  548. return current.OuterXml;
  549. */
  550. XmlNode initial = current;
  551. switch (NodeType) {
  552. case XmlNodeType.Attribute:
  553. return current.OuterXml;
  554. case XmlNodeType.Element:
  555. if (NodeType == XmlNodeType.Element && !IsEmptyElement)
  556. ReadInnerXml ();
  557. else
  558. Read ();
  559. return initial.OuterXml;
  560. case XmlNodeType.None:
  561. return String.Empty;
  562. default:
  563. Read ();
  564. return String.Empty;
  565. }
  566. }
  567. public override string ReadString ()
  568. {
  569. return ReadStringInternal ();
  570. }
  571. [MonoTODO]
  572. public override void ResolveEntity ()
  573. {
  574. throw new NotImplementedException ();
  575. // if (current.NodeType != XmlNodeType.EntityReference)
  576. // throw new InvalidOperationException ("The current node is not an Entity Reference");
  577. }
  578. [MonoTODO("test it.")]
  579. public override void Skip ()
  580. {
  581. // Why is this overriden? Such skipping might raise
  582. // (or ignore) unexpected validation error.
  583. base.Skip ();
  584. }
  585. #endregion
  586. }
  587. }