XmlNodeReader.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 current;
  21. ReadState state = ReadState.Initial;
  22. int depth;
  23. bool isEndElement;
  24. bool isEndEntity;
  25. bool nextIsEndElement;
  26. bool alreadyRead;
  27. public XmlNodeReader (XmlNode node)
  28. {
  29. current = node;
  30. if (node.NodeType != XmlNodeType.Document
  31. && node.NodeType != XmlNodeType.DocumentFragment)
  32. alreadyRead = true;
  33. }
  34. #endregion
  35. #region Properties
  36. public override int AttributeCount {
  37. get {
  38. return ((ICollection) current.Attributes).Count;
  39. }
  40. }
  41. public override string BaseURI {
  42. get { return current.BaseURI; }
  43. }
  44. public override bool CanResolveEntity {
  45. get { return false; }
  46. }
  47. public override int Depth {
  48. get { return depth; }
  49. }
  50. public override bool EOF {
  51. get { return this.ReadState == ReadState.EndOfFile; }
  52. }
  53. public override bool HasAttributes {
  54. get {
  55. if (current.Attributes == null)
  56. return false;
  57. else
  58. return true;
  59. }
  60. }
  61. public override bool HasValue {
  62. get {
  63. if (current.NodeType == XmlNodeType.Element ||
  64. current.NodeType == XmlNodeType.EntityReference ||
  65. current.NodeType == XmlNodeType.Document ||
  66. current.NodeType == XmlNodeType.DocumentFragment ||
  67. current.NodeType == XmlNodeType.Notation ||
  68. current.NodeType == XmlNodeType.EndElement ||
  69. current.NodeType == XmlNodeType.EndEntity)
  70. return false;
  71. else
  72. return true;
  73. }
  74. }
  75. [MonoTODO("waiting for DTD implementation")]
  76. public override bool IsDefault {
  77. get {
  78. if (current.NodeType != XmlNodeType.Attribute)
  79. return false;
  80. else
  81. {
  82. return ((XmlAttribute) current).isDefault;
  83. }
  84. }
  85. }
  86. [MonoTODO("test it.")]
  87. public override bool IsEmptyElement {
  88. get {
  89. if(current.NodeType == XmlNodeType.Element)
  90. return ((XmlElement) current).IsEmpty;
  91. else
  92. return false;
  93. }
  94. }
  95. public override string this [int i] {
  96. get {
  97. if (i < 0 || i > AttributeCount)
  98. throw new ArgumentOutOfRangeException ("i is out of range.");
  99. return current.Attributes [i].Value;
  100. }
  101. }
  102. public override string this [string name] {
  103. get {
  104. string ret = current.Attributes [name].Value;
  105. if (ret == null)
  106. return String.Empty;
  107. else
  108. return ret;
  109. }
  110. }
  111. public override string this [string name, string namespaceURI] {
  112. get {
  113. string ret = current.Attributes [name].Value;
  114. if (ret == null)
  115. return String.Empty;
  116. else
  117. return ret;
  118. }
  119. }
  120. public override string LocalName {
  121. get {
  122. if (current is XmlCharacterData)
  123. return String.Empty;
  124. else
  125. return current.LocalName;
  126. }
  127. }
  128. public override string Name {
  129. get { return current.Name; }
  130. }
  131. public override string NamespaceURI {
  132. get {
  133. return current.NamespaceURI;
  134. }
  135. }
  136. public override XmlNameTable NameTable {
  137. get {
  138. XmlDocument doc =
  139. current.NodeType == XmlNodeType.Document ?
  140. current as XmlDocument : current.OwnerDocument;
  141. return doc.NameTable;
  142. }
  143. }
  144. public override XmlNodeType NodeType {
  145. get {
  146. return isEndElement ? XmlNodeType.EndElement : current.NodeType;
  147. }
  148. }
  149. public override string Prefix {
  150. get {
  151. return current.Prefix;
  152. }
  153. }
  154. public override char QuoteChar {
  155. get { return '"'; }
  156. }
  157. public override ReadState ReadState {
  158. get { return state; }
  159. }
  160. public override string Value {
  161. get {
  162. return HasValue ? current.Value : String.Empty;
  163. }
  164. }
  165. public override string XmlLang {
  166. get { return current.XmlLang; }
  167. }
  168. public override XmlSpace XmlSpace {
  169. get { return current.XmlSpace; }
  170. }
  171. #endregion
  172. #region Methods
  173. public override void Close ()
  174. {
  175. current = null;
  176. state = ReadState.Closed;
  177. }
  178. public override string GetAttribute (int attributeIndex)
  179. {
  180. return this [attributeIndex];
  181. }
  182. public override string GetAttribute (string name)
  183. {
  184. return this [name];
  185. }
  186. public override string GetAttribute (string name, string namespaceURI)
  187. {
  188. return this [name, namespaceURI];
  189. }
  190. // FIXME: Its performance is not good.
  191. public override string LookupNamespace (string prefix)
  192. {
  193. XmlNamespaceManager nsmgr = current.ConstructNamespaceManager();
  194. return nsmgr.LookupNamespace (prefix);
  195. }
  196. public override void MoveToAttribute (int attributeIndex)
  197. {
  198. if (attributeIndex < 0 || attributeIndex > AttributeCount)
  199. throw new ArgumentOutOfRangeException ();
  200. state = ReadState.Interactive;
  201. current = current.Attributes [attributeIndex];
  202. }
  203. public override bool MoveToAttribute (string name)
  204. {
  205. if (GetAttribute (name) == null)
  206. return false;
  207. else
  208. return true;
  209. }
  210. public override bool MoveToAttribute (string name, string namespaceURI)
  211. {
  212. if (GetAttribute (name, namespaceURI) == null)
  213. return false;
  214. else
  215. return true;
  216. }
  217. public override bool MoveToElement ()
  218. {
  219. if (current.NodeType == XmlNodeType.Attribute) {
  220. current = ((XmlAttribute) current).OwnerElement;
  221. return true;
  222. } else
  223. return false;
  224. }
  225. public override bool MoveToFirstAttribute ()
  226. {
  227. if(current.Attributes.Count > 0)
  228. {
  229. current = current.Attributes [0];
  230. return true;
  231. }
  232. else
  233. return false;
  234. }
  235. public override bool MoveToNextAttribute ()
  236. {
  237. if (current.NodeType != XmlNodeType.Attribute)
  238. return MoveToFirstAttribute ();
  239. else
  240. {
  241. XmlAttributeCollection ac = ((XmlAttribute) current).OwnerElement.Attributes;
  242. for (int i=0; i<ac.Count-1; i++)
  243. {
  244. XmlAttribute attr = ac [i];
  245. if (attr == current)
  246. {
  247. current = ac [i+1];
  248. return true;
  249. }
  250. }
  251. return false;
  252. }
  253. }
  254. [MonoTODO("Entity handling is not supported.")]
  255. public override bool Read ()
  256. {
  257. state = ReadState.Interactive;
  258. isEndEntity = false;
  259. if (current.NodeType == XmlNodeType.Attribute)
  260. current = ((XmlAttribute) current).OwnerElement;
  261. if (nextIsEndElement) {
  262. nextIsEndElement = false;
  263. isEndElement = true;
  264. } else if (alreadyRead) {
  265. alreadyRead = false;
  266. return current != null;
  267. }
  268. if (!isEndElement && current.FirstChild != null) {
  269. isEndElement = false;
  270. current = current.FirstChild;
  271. depth++;
  272. } else if (depth == 0) {
  273. state = ReadState.EndOfFile;
  274. return false;
  275. } else if (current.NextSibling != null) {
  276. isEndElement = false;
  277. current = current.NextSibling;
  278. } else {
  279. isEndElement = true;
  280. depth--;
  281. current = current.ParentNode;
  282. }
  283. return current != null;
  284. }
  285. public override bool ReadAttributeValue ()
  286. {
  287. if (current is XmlAttribute) {
  288. current = current.FirstChild;
  289. return current != null;
  290. } else if (current.ParentNode.NodeType == XmlNodeType.Attribute) {
  291. current = current.NextSibling;
  292. return current != null;
  293. } else
  294. return false;
  295. }
  296. [MonoTODO("Need to move to next content.")]
  297. // Its traversal behavior is almost same as Read().
  298. public override string ReadInnerXml ()
  299. {
  300. if (current.NodeType != XmlNodeType.Attribute &&
  301. current.NodeType != XmlNodeType.Element)
  302. return String.Empty;
  303. else
  304. return current.InnerXml;
  305. }
  306. [MonoTODO("Need to move to next content.")]
  307. // Its traversal behavior is almost same as Read().
  308. public override string ReadOuterXml ()
  309. {
  310. if (current.NodeType != XmlNodeType.Attribute &&
  311. current.NodeType != XmlNodeType.Element)
  312. return String.Empty;
  313. else
  314. return current.OuterXml;
  315. }
  316. [MonoTODO("test it.")]
  317. public override string ReadString ()
  318. {
  319. XmlNode original = current;
  320. StringBuilder builder = new StringBuilder();
  321. foreach (XmlNode child in current.ChildNodes)
  322. {
  323. if (child is XmlCharacterData)
  324. builder.Append (child.Value);
  325. else {
  326. depth++;
  327. current = child;
  328. break;
  329. }
  330. }
  331. alreadyRead = true;
  332. if (current == original)
  333. nextIsEndElement = true;
  334. return builder.ToString ();
  335. }
  336. [MonoTODO]
  337. public override void ResolveEntity ()
  338. {
  339. if (current.NodeType != XmlNodeType.EntityReference)
  340. throw new InvalidOperationException ("The current node is not an Entity Reference");
  341. }
  342. [MonoTODO("test it.")]
  343. public override void Skip ()
  344. {
  345. if (current.NodeType == XmlNodeType.Attribute)
  346. current = ((XmlAttribute) current).OwnerElement.NextSibling;
  347. else
  348. {
  349. if(current.ChildNodes.Count > 0) {
  350. current = current.FirstChild;
  351. depth++;
  352. } else if (current.NextSibling != null) {
  353. current = current.NextSibling;
  354. } else if (current.NodeType == XmlNodeType.Attribute) {
  355. current = current.ParentNode;
  356. } else {
  357. depth--;
  358. }
  359. }
  360. }
  361. #endregion
  362. }
  363. }