XmlNodeReader.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. private string GetDocumentTypeAttribute (string name)
  170. {
  171. XmlDocumentType doctype = current as XmlDocumentType;
  172. switch (name) {
  173. case "PUBLIC":
  174. return doctype.PublicId;
  175. case "SYSTEM":
  176. return doctype.SystemId;
  177. }
  178. return null;
  179. }
  180. public override string this [string name] {
  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. else if (NodeType == XmlNodeType.DocumentType)
  188. return GetDocumentTypeAttribute (name);
  189. XmlAttribute attr = ownerElement.Attributes [name];
  190. if (attr == null)
  191. return null;
  192. else
  193. return attr.Value;
  194. }
  195. }
  196. public override string this [string name, string namespaceURI] {
  197. get {
  198. // This is MS.NET bug which returns attributes in spite of EndElement.
  199. if (isEndElement || current == null)
  200. return null;
  201. if (NodeType == XmlNodeType.XmlDeclaration)
  202. return GetXmlDeclarationAttribute (name);
  203. XmlAttribute attr = ownerElement.Attributes [name, namespaceURI];
  204. if (attr == null)
  205. return null; // In fact MS.NET returns null instead of String.Empty.
  206. else
  207. return attr.Value;
  208. }
  209. }
  210. public override string LocalName {
  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.LocalName;
  222. }
  223. return String.Empty;
  224. }
  225. }
  226. public override string Name {
  227. get {
  228. if (current == null)
  229. return String.Empty;
  230. switch (current.NodeType) {
  231. case XmlNodeType.Attribute:
  232. case XmlNodeType.DocumentType:
  233. case XmlNodeType.Element:
  234. case XmlNodeType.EntityReference:
  235. case XmlNodeType.ProcessingInstruction:
  236. case XmlNodeType.XmlDeclaration:
  237. return current.Name;
  238. }
  239. return String.Empty;
  240. }
  241. }
  242. public override string NamespaceURI {
  243. get {
  244. if (current == null)
  245. return String.Empty;
  246. return current.NamespaceURI;
  247. }
  248. }
  249. public override XmlNameTable NameTable {
  250. get { return document.NameTable; }
  251. }
  252. public override XmlNodeType NodeType {
  253. get {
  254. if (current == null)
  255. return XmlNodeType.None;
  256. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  257. }
  258. }
  259. public override string Prefix {
  260. get {
  261. if (current == null)
  262. return String.Empty;
  263. return current.Prefix;
  264. }
  265. }
  266. public override char QuoteChar {
  267. get { return '"'; }
  268. }
  269. public override ReadState ReadState {
  270. get { return state; }
  271. }
  272. public override string Value {
  273. get {
  274. if (NodeType == XmlNodeType.DocumentType)
  275. return ((XmlDocumentType) current).InternalSubset;
  276. else
  277. return HasValue ? current.Value : String.Empty;
  278. }
  279. }
  280. public override string XmlLang {
  281. get {
  282. if (current == null)
  283. return String.Empty;
  284. return current.XmlLang;
  285. }
  286. }
  287. public override XmlSpace XmlSpace {
  288. get {
  289. if (current == null)
  290. return XmlSpace.None;
  291. return current.XmlSpace;
  292. }
  293. }
  294. #endregion
  295. #region Methods
  296. public override void Close ()
  297. {
  298. current = null;
  299. state = ReadState.Closed;
  300. }
  301. public override string GetAttribute (int attributeIndex)
  302. {
  303. return this [attributeIndex];
  304. }
  305. public override string GetAttribute (string name)
  306. {
  307. return this [name];
  308. }
  309. public override string GetAttribute (string name, string namespaceURI)
  310. {
  311. return this [name, namespaceURI];
  312. }
  313. public override string LookupNamespace (string prefix)
  314. {
  315. if (current == null)
  316. return null;
  317. XmlAttribute curAttr = current as XmlAttribute;
  318. XmlNode target = curAttr != null ? curAttr.OwnerElement : current;
  319. if (prefix == "") {
  320. do {
  321. XmlAttribute attr = target.Attributes ["xmlns"];
  322. if (attr != null)
  323. return attr.Value;
  324. target = target.ParentNode;
  325. } while (target.NodeType != XmlNodeType.Document);
  326. } else {
  327. string name = "xmlns:" + prefix;
  328. do {
  329. XmlAttribute attr = target.Attributes [name];
  330. if (attr != null)
  331. return attr.Value;
  332. target = target.ParentNode;
  333. } while (target.NodeType != XmlNodeType.Document);
  334. }
  335. return defaultNsmgr.LookupNamespace (prefix);
  336. }
  337. public override void MoveToAttribute (int attributeIndex)
  338. {
  339. if (isEndElement || attributeIndex < 0 || attributeIndex > AttributeCount)
  340. throw new ArgumentOutOfRangeException ();
  341. state = ReadState.Interactive;
  342. current = ownerElement.Attributes [attributeIndex];
  343. }
  344. public override bool MoveToAttribute (string name)
  345. {
  346. if (isEndElement || current == null)
  347. return false;
  348. XmlNode tmpCurrent = current;
  349. if (current.ParentNode.NodeType == XmlNodeType.Attribute)
  350. current = current.ParentNode;
  351. XmlAttribute attr = ownerElement.Attributes [name];
  352. if (attr == null) {
  353. current = tmpCurrent;
  354. return false;
  355. }
  356. else {
  357. current = attr;
  358. return true;
  359. }
  360. }
  361. public override bool MoveToAttribute (string name, string namespaceURI)
  362. {
  363. if (isEndElement || current == null)
  364. return false;
  365. XmlAttribute attr = ownerElement.Attributes [name, namespaceURI];
  366. if (attr == null)
  367. return false;
  368. else {
  369. current = attr;
  370. return true;
  371. }
  372. }
  373. private void MoveToParentElement ()
  374. {
  375. // This is buggy. It is not only the case when EndElement = true.
  376. isEndElement = true;
  377. depth--;
  378. current = current.ParentNode;
  379. }
  380. public override bool MoveToElement ()
  381. {
  382. if (current == null)
  383. return false;
  384. if (current.NodeType == XmlNodeType.Attribute) {
  385. current = ((XmlAttribute) current).OwnerElement;
  386. return true;
  387. } else
  388. return false;
  389. }
  390. public override bool MoveToFirstAttribute ()
  391. {
  392. if (current == null)
  393. return false;
  394. if(ownerElement.Attributes.Count > 0)
  395. {
  396. current = ownerElement.Attributes [0];
  397. return true;
  398. }
  399. else
  400. return false;
  401. }
  402. public override bool MoveToNextAttribute ()
  403. {
  404. if (current == null)
  405. return false;
  406. if (current.NodeType != XmlNodeType.Attribute)
  407. return MoveToFirstAttribute ();
  408. else
  409. {
  410. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  411. for (int i=0; i<ac.Count-1; i++)
  412. {
  413. XmlAttribute attr = ac [i];
  414. if (attr == current)
  415. {
  416. i++;
  417. if (i == ac.Count)
  418. return false;
  419. current = ac [i];
  420. return true;
  421. }
  422. }
  423. return false;
  424. }
  425. }
  426. private bool MoveToNextSibling ()
  427. {
  428. if (nextIsEndElement) {
  429. // nextIsEndElement is set only by ReadString.
  430. nextIsEndElement = false;
  431. MoveToParentElement ();
  432. } else if (alreadyRead) {
  433. alreadyRead = false;
  434. return current != null;
  435. }
  436. if (current.NextSibling != null) {
  437. isEndElement = false;
  438. current = current.NextSibling;
  439. } else {
  440. MoveToParentElement ();
  441. }
  442. if (current == null) {
  443. state = ReadState.EndOfFile;
  444. return false;
  445. }
  446. else
  447. return true;
  448. }
  449. [MonoTODO("Entity handling is not supported.")]
  450. public override bool Read ()
  451. {
  452. if (EOF)
  453. return false;
  454. if (ReadState == ReadState.Initial) {
  455. current = startNode;
  456. state = ReadState.Interactive;
  457. // when startNode is document or fragment
  458. if (!alreadyRead)
  459. current = startNode.FirstChild;
  460. else
  461. alreadyRead = false;
  462. if (current == null) {
  463. state = ReadState.Error;
  464. return false;
  465. } else
  466. return true;
  467. }
  468. MoveToElement ();
  469. isEndEntity = false;
  470. if (IsEmptyElement || isEndElement) {
  471. // Then go up and move to next.
  472. // If no more nodes, then set EOF.
  473. isEndElement = false;
  474. if (current.ParentNode == null
  475. || current.ParentNode.NodeType == XmlNodeType.Document
  476. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  477. current = null;
  478. state = ReadState.EndOfFile;
  479. return false;
  480. } else if (current.NextSibling == null) {
  481. depth--;
  482. current = current.ParentNode;
  483. isEndElement = true;
  484. return true;
  485. } else {
  486. current = current.NextSibling;
  487. return true;
  488. }
  489. } else if (nextIsEndElement) {
  490. // nextIsEndElement is set only by ReadString.
  491. nextIsEndElement = false;
  492. isEndElement = true;
  493. return current != null;
  494. } else if (alreadyRead) {
  495. alreadyRead = false;
  496. return current != null;
  497. }
  498. if (!isEndElement && current.FirstChild != null) {
  499. isEndElement = false;
  500. current = current.FirstChild;
  501. depth++;
  502. } else if (current.NodeType == XmlNodeType.Element) {
  503. isEndElement = true;
  504. if (current.FirstChild != null)
  505. depth--;
  506. } else
  507. MoveToNextSibling ();
  508. return current != null;
  509. }
  510. public override bool ReadAttributeValue ()
  511. {
  512. if (current.NodeType == XmlNodeType.Attribute) {
  513. current = current.FirstChild;
  514. return current != null;
  515. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  516. if (current.NextSibling == null)
  517. return false;
  518. current = current.NextSibling;
  519. return true;
  520. } else
  521. return false;
  522. }
  523. // Its traversal behavior is almost same as Read().
  524. public override string ReadInnerXml ()
  525. {
  526. if (this.state != ReadState.Interactive)
  527. return String.Empty;
  528. XmlNode initial = current;
  529. // Almost copied from XmlTextReader.
  530. switch (NodeType) {
  531. case XmlNodeType.Attribute:
  532. return Value;
  533. case XmlNodeType.Element:
  534. if (IsEmptyElement)
  535. return String.Empty;
  536. int startDepth = depth;
  537. bool loop = true;
  538. do {
  539. Read ();
  540. if (NodeType ==XmlNodeType.None)
  541. throw new XmlException ("unexpected end of xml.");
  542. else if (NodeType == XmlNodeType.EndElement && depth == startDepth) {
  543. loop = false;
  544. Read ();
  545. }
  546. } while (loop);
  547. return initial.InnerXml;
  548. case XmlNodeType.None:
  549. return String.Empty;
  550. default:
  551. Read ();
  552. return String.Empty;
  553. }
  554. }
  555. [MonoTODO("Need to move to next content.")]
  556. // Its traversal behavior is almost same as Read().
  557. public override string ReadOuterXml ()
  558. {
  559. if (NodeType == XmlNodeType.EndElement)
  560. return String.Empty;
  561. XmlNode initial = current;
  562. switch (NodeType) {
  563. case XmlNodeType.Attribute:
  564. return current.OuterXml;
  565. case XmlNodeType.Element:
  566. if (NodeType == XmlNodeType.Element && !IsEmptyElement)
  567. ReadInnerXml ();
  568. else
  569. Read ();
  570. return initial.OuterXml;
  571. case XmlNodeType.None:
  572. return String.Empty;
  573. default:
  574. Read ();
  575. return String.Empty;
  576. }
  577. }
  578. public override string ReadString ()
  579. {
  580. return ReadStringInternal ();
  581. }
  582. [MonoTODO]
  583. public override void ResolveEntity ()
  584. {
  585. throw new NotImplementedException ();
  586. // if (current.NodeType != XmlNodeType.EntityReference)
  587. // throw new InvalidOperationException ("The current node is not an Entity Reference");
  588. }
  589. [MonoTODO("test it.")]
  590. public override void Skip ()
  591. {
  592. // Why is this overriden? Such skipping might raise
  593. // (or ignore) unexpected validation error.
  594. base.Skip ();
  595. }
  596. #endregion
  597. }
  598. }