XmlNodeReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. return true;
  256. }
  257. public override bool MoveToAttribute (string name, string namespaceURI)
  258. {
  259. if (GetAttribute (name, namespaceURI) == null)
  260. return false;
  261. else
  262. return true;
  263. }
  264. public override bool MoveToElement ()
  265. {
  266. if (current.NodeType == XmlNodeType.Attribute) {
  267. current = ((XmlAttribute) current).OwnerElement;
  268. return true;
  269. } else
  270. return false;
  271. }
  272. public override bool MoveToFirstAttribute ()
  273. {
  274. if(current.Attributes.Count > 0)
  275. {
  276. current = current.Attributes [0];
  277. return true;
  278. }
  279. else
  280. return false;
  281. }
  282. public override bool MoveToNextAttribute ()
  283. {
  284. if (current.NodeType != XmlNodeType.Attribute)
  285. return MoveToFirstAttribute ();
  286. else
  287. {
  288. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  289. for (int i=0; i<ac.Count-1; i++)
  290. {
  291. XmlAttribute attr = ac [i];
  292. if (attr == current)
  293. {
  294. current = ac [i+1];
  295. return true;
  296. }
  297. }
  298. return false;
  299. }
  300. }
  301. [MonoTODO("Entity handling is not supported.")]
  302. public override bool Read ()
  303. {
  304. if (EOF)
  305. return false;
  306. if (ReadState == ReadState.Initial) {
  307. current = startNode;
  308. state = ReadState.Interactive;
  309. // when startNode is document or fragment
  310. if (!alreadyRead)
  311. current = startNode.FirstChild;
  312. else
  313. alreadyRead = false;
  314. if (current == null) {
  315. state = ReadState.Error;
  316. return false;
  317. } else
  318. return true;
  319. }
  320. if (current.NodeType == XmlNodeType.Attribute)
  321. current = ((XmlAttribute) current).OwnerElement;
  322. isEndEntity = false;
  323. if (isEndElement) {
  324. // Then go up and move to next.
  325. // If no more nodes, then set EOF.
  326. isEndElement = false;
  327. if (current.ParentNode == null
  328. || current.ParentNode.NodeType == XmlNodeType.Document
  329. || current.ParentNode.NodeType == XmlNodeType.DocumentFragment) {
  330. current = null;
  331. state = ReadState.EndOfFile;
  332. return false;
  333. } else if (current.ParentNode.NextSibling == null) {
  334. current = current.ParentNode;
  335. isEndElement = true;
  336. return true;
  337. } else {
  338. current = current.ParentNode.NextSibling;
  339. return true;
  340. }
  341. } else if (nextIsEndElement) {
  342. // nextIsEndElement is set only by ReadString.
  343. nextIsEndElement = false;
  344. isEndElement = true;
  345. return current != null;
  346. } else if (alreadyRead) {
  347. alreadyRead = false;
  348. return current != null;
  349. }
  350. // hmm... here may be unnecessary codes. plz check anyone ;)
  351. if (!isEndElement && current.FirstChild != null) {
  352. isEndElement = false;
  353. current = current.FirstChild;
  354. depth++;
  355. } else if (depth == 0) {
  356. state = ReadState.EndOfFile;
  357. return false;
  358. } else if (current.NextSibling != null) {
  359. isEndElement = false;
  360. current = current.NextSibling;
  361. } else {
  362. isEndElement = true;
  363. depth--;
  364. current = current.ParentNode;
  365. }
  366. return current != null;
  367. }
  368. public override bool ReadAttributeValue ()
  369. {
  370. if (current is XmlAttribute) {
  371. current = current.FirstChild;
  372. return current != null;
  373. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  374. current = current.NextSibling;
  375. return current != null;
  376. } else
  377. return false;
  378. }
  379. [MonoTODO("Need to move to next content.")]
  380. // Its traversal behavior is almost same as Read().
  381. public override string ReadInnerXml ()
  382. {
  383. if (ReadState == ReadState.Initial) {
  384. state = ReadState.Error;
  385. return String.Empty; // heh
  386. }
  387. if (current.NodeType != XmlNodeType.Attribute &&
  388. current.NodeType != XmlNodeType.Element)
  389. return String.Empty;
  390. else
  391. return current.InnerXml;
  392. }
  393. [MonoTODO("Need to move to next content.")]
  394. // Its traversal behavior is almost same as Read().
  395. public override string ReadOuterXml ()
  396. {
  397. if (NodeType == XmlNodeType.EndElement)
  398. return String.Empty;
  399. if (current.NodeType != XmlNodeType.Attribute &&
  400. current.NodeType != XmlNodeType.Element)
  401. return String.Empty;
  402. else
  403. return current.OuterXml;
  404. }
  405. public override string ReadString ()
  406. {
  407. if (NodeType == XmlNodeType.EndElement)
  408. return String.Empty;
  409. XmlNode original = current;
  410. StringBuilder builder = new StringBuilder();
  411. foreach (XmlNode child in current.ChildNodes) {
  412. if (child is XmlCharacterData && !(child is XmlComment))
  413. builder.Append (child.Value);
  414. else {
  415. depth++;
  416. current = child;
  417. break;
  418. }
  419. }
  420. alreadyRead = true;
  421. if (current == original) {
  422. nextIsEndElement = true;
  423. Read ();
  424. }
  425. return builder.ToString ();
  426. }
  427. [MonoTODO]
  428. public override void ResolveEntity ()
  429. {
  430. throw new NotImplementedException ();
  431. // if (current.NodeType != XmlNodeType.EntityReference)
  432. // throw new InvalidOperationException ("The current node is not an Entity Reference");
  433. }
  434. [MonoTODO("test it.")]
  435. public override void Skip ()
  436. {
  437. if (current.NodeType == XmlNodeType.Attribute)
  438. current = ((XmlAttribute) current).OwnerElement.NextSibling;
  439. else
  440. {
  441. if(current.ChildNodes.Count > 0) {
  442. current = current.FirstChild;
  443. depth++;
  444. } else if (current.NextSibling != null) {
  445. current = current.NextSibling;
  446. } else if (current.NodeType == XmlNodeType.Attribute) {
  447. current = current.ParentNode;
  448. } else {
  449. depth--;
  450. }
  451. }
  452. }
  453. #endregion
  454. }
  455. }