XmlNodeReader.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. #region Constructor
  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. public XmlNodeReader (XmlNode node)
  29. {
  30. startNode = node;
  31. if (node.NodeType != XmlNodeType.Document
  32. && node.NodeType != XmlNodeType.DocumentFragment)
  33. alreadyRead = true;
  34. }
  35. #endregion
  36. #region Properties
  37. public override int AttributeCount {
  38. get {
  39. if (current == null)
  40. return 0;
  41. return ((ICollection) current.Attributes).Count;
  42. }
  43. }
  44. public override string BaseURI {
  45. get {
  46. if (current == null)
  47. return String.Empty;
  48. return current.BaseURI;
  49. }
  50. }
  51. [MonoTODO("wait for XML resolver")]
  52. public override bool CanResolveEntity {
  53. get {
  54. throw new NotImplementedException ();
  55. }
  56. }
  57. public override int Depth {
  58. get { return depth; }
  59. }
  60. public override bool EOF {
  61. get {
  62. return this.ReadState == ReadState.EndOfFile
  63. || this.ReadState == ReadState.Error;
  64. }
  65. }
  66. public override bool HasAttributes {
  67. get {
  68. if (current == null)
  69. return false;
  70. if (current.Attributes == null)
  71. return false;
  72. else
  73. return true;
  74. }
  75. }
  76. public override bool HasValue {
  77. get {
  78. if (current == null)
  79. return false;
  80. if (current.NodeType == XmlNodeType.Element ||
  81. current.NodeType == XmlNodeType.EntityReference ||
  82. current.NodeType == XmlNodeType.Document ||
  83. current.NodeType == XmlNodeType.DocumentFragment ||
  84. current.NodeType == XmlNodeType.Notation ||
  85. current.NodeType == XmlNodeType.EndElement ||
  86. current.NodeType == XmlNodeType.EndEntity)
  87. return false;
  88. else
  89. return true;
  90. }
  91. }
  92. [MonoTODO("waiting for DTD implementation")]
  93. public override bool IsDefault {
  94. get {
  95. if (current == null)
  96. return false;
  97. if (current.NodeType != XmlNodeType.Attribute)
  98. return false;
  99. else
  100. {
  101. return ((XmlAttribute) current).isDefault;
  102. }
  103. }
  104. }
  105. [MonoTODO("test it.")]
  106. public override bool IsEmptyElement {
  107. get {
  108. if (current == null)
  109. return false;
  110. if(current.NodeType == XmlNodeType.Element)
  111. return ((XmlElement) current).IsEmpty;
  112. else
  113. return false;
  114. }
  115. }
  116. public override string this [int i] {
  117. get {
  118. if (current == null)
  119. return null;
  120. if (i < 0 || i > AttributeCount)
  121. throw new ArgumentOutOfRangeException ("i is out of range.");
  122. return current.Attributes [i].Value;
  123. }
  124. }
  125. public override string this [string name] {
  126. get {
  127. if (current == null)
  128. return null;
  129. string ret = current.Attributes [name].Value;
  130. if (ret == null)
  131. return String.Empty;
  132. else
  133. return ret;
  134. }
  135. }
  136. public override string this [string name, string namespaceURI] {
  137. get {
  138. if (current == null)
  139. return null;
  140. string ret = current.Attributes [name, namespaceURI].Value;
  141. if (ret == null)
  142. return String.Empty;
  143. else
  144. return ret;
  145. }
  146. }
  147. public override string LocalName {
  148. get {
  149. if (current == null)
  150. return String.Empty;
  151. if (current is XmlCharacterData)
  152. return String.Empty;
  153. else
  154. return current.LocalName;
  155. }
  156. }
  157. public override string Name {
  158. get {
  159. if (current == null)
  160. return String.Empty;
  161. return current.Name;
  162. }
  163. }
  164. public override string NamespaceURI {
  165. get {
  166. if (current == null)
  167. return String.Empty;
  168. return current.NamespaceURI;
  169. }
  170. }
  171. public override XmlNameTable NameTable {
  172. get {
  173. XmlDocument doc =
  174. current.NodeType == XmlNodeType.Document ?
  175. current as XmlDocument : current.OwnerDocument;
  176. return doc.NameTable;
  177. }
  178. }
  179. public override XmlNodeType NodeType {
  180. get {
  181. if (current == null)
  182. return XmlNodeType.None;
  183. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  184. }
  185. }
  186. public override string Prefix {
  187. get {
  188. if (current == null)
  189. return String.Empty;
  190. return current.Prefix;
  191. }
  192. }
  193. public override char QuoteChar {
  194. get { return '"'; }
  195. }
  196. public override ReadState ReadState {
  197. get { return state; }
  198. }
  199. public override string Value {
  200. get {
  201. return HasValue ? current.Value : String.Empty;
  202. }
  203. }
  204. public override string XmlLang {
  205. get {
  206. if (current == null)
  207. return String.Empty;
  208. return current.XmlLang;
  209. }
  210. }
  211. public override XmlSpace XmlSpace {
  212. get {
  213. if (current == null)
  214. return XmlSpace.None;
  215. return current.XmlSpace;
  216. }
  217. }
  218. #endregion
  219. #region Methods
  220. public override void Close ()
  221. {
  222. current = null;
  223. state = ReadState.Closed;
  224. }
  225. public override string GetAttribute (int attributeIndex)
  226. {
  227. return this [attributeIndex];
  228. }
  229. public override string GetAttribute (string name)
  230. {
  231. return this [name];
  232. }
  233. public override string GetAttribute (string name, string namespaceURI)
  234. {
  235. return this [name, namespaceURI];
  236. }
  237. // FIXME: Its performance is not good.
  238. public override string LookupNamespace (string prefix)
  239. {
  240. XmlNamespaceManager nsmgr = current.ConstructNamespaceManager();
  241. return nsmgr.LookupNamespace (prefix);
  242. }
  243. public override void MoveToAttribute (int attributeIndex)
  244. {
  245. if (attributeIndex < 0 || attributeIndex > AttributeCount)
  246. throw new ArgumentOutOfRangeException ();
  247. state = ReadState.Interactive;
  248. current = current.Attributes [attributeIndex];
  249. }
  250. public override bool MoveToAttribute (string name)
  251. {
  252. if (GetAttribute (name) == null)
  253. return false;
  254. else {
  255. current = current.Attributes [name];
  256. return true;
  257. }
  258. }
  259. public override bool MoveToAttribute (string name, string namespaceURI)
  260. {
  261. if (GetAttribute (name, namespaceURI) == null)
  262. return false;
  263. else {
  264. current = current.Attributes [name, namespaceURI];
  265. return true;
  266. }
  267. }
  268. private void MoveToEndElement ()
  269. {
  270. isEndElement = true;
  271. depth--;
  272. current = current.ParentNode;
  273. }
  274. public override bool MoveToElement ()
  275. {
  276. if (current == null)
  277. return false;
  278. if (current.NodeType == XmlNodeType.Attribute) {
  279. current = ((XmlAttribute) current).OwnerElement;
  280. return true;
  281. } else
  282. return false;
  283. }
  284. public override bool MoveToFirstAttribute ()
  285. {
  286. if(current.Attributes.Count > 0)
  287. {
  288. current = current.Attributes [0];
  289. return true;
  290. }
  291. else
  292. return false;
  293. }
  294. public override bool MoveToNextAttribute ()
  295. {
  296. if (current.NodeType != XmlNodeType.Attribute)
  297. return MoveToFirstAttribute ();
  298. else
  299. {
  300. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  301. for (int i=0; i<ac.Count-1; i++)
  302. {
  303. XmlAttribute attr = ac [i];
  304. if (attr == current)
  305. {
  306. current = ac [i+1];
  307. return true;
  308. }
  309. }
  310. return false;
  311. }
  312. }
  313. private bool MoveToNextSibling ()
  314. {
  315. if (nextIsEndElement) {
  316. // nextIsEndElement is set only by ReadString.
  317. nextIsEndElement = false;
  318. MoveToEndElement ();
  319. } else if (alreadyRead) {
  320. alreadyRead = false;
  321. return current != null;
  322. }
  323. if (current.NextSibling != null) {
  324. isEndElement = false;
  325. current = current.NextSibling;
  326. } else {
  327. MoveToEndElement ();
  328. }
  329. return current != null;
  330. }
  331. [MonoTODO("Entity handling is not supported.")]
  332. public override bool Read ()
  333. {
  334. if (EOF)
  335. return false;
  336. if (ReadState == ReadState.Initial) {
  337. current = startNode;
  338. state = ReadState.Interactive;
  339. // when startNode is document or fragment
  340. if (!alreadyRead)
  341. current = startNode.FirstChild;
  342. else
  343. alreadyRead = false;
  344. if (current == null) {
  345. state = ReadState.Error;
  346. return false;
  347. } else
  348. return true;
  349. }
  350. MoveToElement ();
  351. isEndEntity = false;
  352. if (isEndElement) {
  353. // Then go up and move to next.
  354. // If no more nodes, then set EOF.
  355. isEndElement = false;
  356. if (current.ParentNode == null
  357. || current.ParentNode.NodeType == XmlNodeType.Document
  358. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  359. current = null;
  360. state = ReadState.EndOfFile;
  361. return false;
  362. } else if (current.NextSibling == null) {
  363. depth--;
  364. current = current.ParentNode;
  365. isEndElement = true;
  366. return true;
  367. } else {
  368. current = current.NextSibling;
  369. return true;
  370. }
  371. } else if (nextIsEndElement) {
  372. // nextIsEndElement is set only by ReadString.
  373. nextIsEndElement = false;
  374. isEndElement = true;
  375. return current != null;
  376. } else if (alreadyRead) {
  377. alreadyRead = false;
  378. return current != null;
  379. }
  380. // hmm... here may be unnecessary codes. plz check anyone ;)
  381. if (!isEndElement && current.FirstChild != null) {
  382. isEndElement = false;
  383. current = current.FirstChild;
  384. depth++;
  385. } else if (depth == 0) {
  386. state = ReadState.EndOfFile;
  387. return false;
  388. } else
  389. MoveToNextSibling ();
  390. return current != null;
  391. }
  392. public override bool ReadAttributeValue ()
  393. {
  394. if (current.NodeType == XmlNodeType.Attribute) {
  395. current = current.FirstChild;
  396. return current != null;
  397. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  398. current = current.NextSibling;
  399. return current != null;
  400. } else
  401. return false;
  402. }
  403. [MonoTODO("Need to move to next content.")]
  404. // Its traversal behavior is almost same as Read().
  405. public override string ReadInnerXml ()
  406. {
  407. if (ReadState == ReadState.Initial) {
  408. state = ReadState.Error;
  409. return String.Empty; // heh
  410. }
  411. if (current.NodeType != XmlNodeType.Attribute &&
  412. current.NodeType != XmlNodeType.Element)
  413. return String.Empty;
  414. else
  415. return current.InnerXml;
  416. }
  417. [MonoTODO("Need to move to next content.")]
  418. // Its traversal behavior is almost same as Read().
  419. public override string ReadOuterXml ()
  420. {
  421. if (NodeType == XmlNodeType.EndElement)
  422. return String.Empty;
  423. if (current.NodeType != XmlNodeType.Attribute &&
  424. current.NodeType != XmlNodeType.Element)
  425. return String.Empty;
  426. else
  427. return current.OuterXml;
  428. }
  429. public override string ReadString ()
  430. {
  431. if (NodeType == XmlNodeType.EndElement)
  432. return String.Empty;
  433. XmlNode original = current;
  434. StringBuilder builder = new StringBuilder();
  435. if (NodeType == XmlNodeType.Element) {
  436. foreach (XmlNode child in current.ChildNodes) {
  437. if (child is XmlCharacterData && !(child is XmlComment))
  438. builder.Append (child.Value);
  439. else {
  440. depth++;
  441. current = child;
  442. break;
  443. }
  444. }
  445. alreadyRead = true;
  446. if (current == original) {
  447. nextIsEndElement = true;
  448. Read ();
  449. }
  450. } else {
  451. do {
  452. builder.Append (current.Value);
  453. if (current.NextSibling == null) {
  454. nextIsEndElement = true;
  455. break;
  456. } else if (current.NextSibling.NodeType == XmlNodeType.Comment)
  457. break;
  458. else
  459. current = current.NextSibling;
  460. } while (true);
  461. alreadyRead = true;
  462. if (current.NextSibling == null) {
  463. nextIsEndElement = true;
  464. Read ();
  465. }
  466. }
  467. return builder.ToString ();
  468. }
  469. [MonoTODO]
  470. public override void ResolveEntity ()
  471. {
  472. throw new NotImplementedException ();
  473. // if (current.NodeType != XmlNodeType.EntityReference)
  474. // throw new InvalidOperationException ("The current node is not an Entity Reference");
  475. }
  476. [MonoTODO("test it.")]
  477. public override void Skip ()
  478. {
  479. MoveToElement ();
  480. if(current.ChildNodes.Count > 0)
  481. MoveToNextSibling ();
  482. else
  483. Read ();
  484. }
  485. #endregion
  486. }
  487. }