XmlDocumentNavigator.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. //
  2. // System.Xml.XmlDocumentNavigator
  3. //
  4. // Author:
  5. // Jason Diamond <[email protected]>
  6. //
  7. // (C) 2002 Jason Diamond
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Xml;
  12. using System.Xml.XPath;
  13. namespace System.Xml
  14. {
  15. internal class XmlDocumentNavigator : XPathNavigator
  16. {
  17. #region Constructors
  18. [MonoTODO]
  19. internal XmlDocumentNavigator(XmlNode node)
  20. {
  21. this.node = node;
  22. }
  23. #endregion
  24. #region Fields
  25. private XmlNode node;
  26. private IEnumerator attributesEnumerator;
  27. #endregion
  28. #region Properties
  29. [MonoTODO]
  30. public override string BaseURI {
  31. get {
  32. throw new NotImplementedException ();
  33. }
  34. }
  35. public override bool HasAttributes {
  36. get {
  37. if (node.Attributes != null)
  38. foreach (XmlAttribute attribute in node.Attributes)
  39. if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
  40. return true;
  41. return false;
  42. }
  43. }
  44. public override bool HasChildren {
  45. get {
  46. XPathNodeType nodeType = NodeType;
  47. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  48. return canHaveChildren && node.FirstChild != null;
  49. }
  50. }
  51. public override bool IsEmptyElement {
  52. get {
  53. return node.NodeType == XmlNodeType.Element && !HasChildren;
  54. }
  55. }
  56. public override string LocalName {
  57. get {
  58. XPathNodeType nodeType = NodeType;
  59. bool canHaveName =
  60. nodeType == XPathNodeType.Element ||
  61. nodeType == XPathNodeType.Attribute ||
  62. nodeType == XPathNodeType.ProcessingInstruction ||
  63. nodeType == XPathNodeType.Namespace;
  64. return canHaveName ? node.LocalName : String.Empty;
  65. }
  66. }
  67. public override string Name {
  68. get {
  69. XPathNodeType nodeType = NodeType;
  70. bool canHaveName =
  71. nodeType == XPathNodeType.Element ||
  72. nodeType == XPathNodeType.Attribute ||
  73. nodeType == XPathNodeType.ProcessingInstruction ||
  74. nodeType == XPathNodeType.Namespace;
  75. return canHaveName ? node.Name : String.Empty;
  76. }
  77. }
  78. public override string NamespaceURI {
  79. get {
  80. return node.NamespaceURI;
  81. }
  82. }
  83. [MonoTODO]
  84. public override XmlNameTable NameTable {
  85. get {
  86. throw new NotImplementedException ();
  87. }
  88. }
  89. public override XPathNodeType NodeType {
  90. get {
  91. switch (node.NodeType) {
  92. case XmlNodeType.Document:
  93. return XPathNodeType.Root;
  94. case XmlNodeType.Element:
  95. return XPathNodeType.Element;
  96. case XmlNodeType.Attribute:
  97. return XPathNodeType.Attribute;
  98. case XmlNodeType.Text:
  99. return XPathNodeType.Text;
  100. case XmlNodeType.Whitespace:
  101. return XPathNodeType.Whitespace;
  102. case XmlNodeType.SignificantWhitespace:
  103. return XPathNodeType.SignificantWhitespace;
  104. case XmlNodeType.Comment:
  105. return XPathNodeType.Comment;
  106. case XmlNodeType.ProcessingInstruction:
  107. return XPathNodeType.ProcessingInstruction;
  108. }
  109. throw new InvalidOperationException ();
  110. }
  111. }
  112. public override string Prefix {
  113. get {
  114. return node.Prefix;
  115. }
  116. }
  117. public override string Value {
  118. get {
  119. switch (NodeType) {
  120. case XPathNodeType.Attribute:
  121. return node.Value;
  122. case XPathNodeType.Element:
  123. return node.InnerText;
  124. case XPathNodeType.Comment:
  125. return node.Value;
  126. case XPathNodeType.ProcessingInstruction:
  127. return node.Value;
  128. case XPathNodeType.Text:
  129. return node.Value;
  130. case XPathNodeType.Whitespace:
  131. return node.Value;
  132. case XPathNodeType.SignificantWhitespace:
  133. return node.Value;
  134. case XPathNodeType.Root:
  135. return node.InnerText;
  136. }
  137. return String.Empty;
  138. }
  139. }
  140. [MonoTODO]
  141. public override string XmlLang {
  142. get {
  143. throw new NotImplementedException ();
  144. }
  145. }
  146. #endregion
  147. #region Methods
  148. [MonoTODO]
  149. public override XPathNavigator Clone ()
  150. {
  151. throw new NotImplementedException ();
  152. }
  153. [MonoTODO]
  154. public override string GetAttribute (string localName, string namespaceURI)
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. [MonoTODO]
  159. public override string GetNamespace (string name)
  160. {
  161. throw new NotImplementedException ();
  162. }
  163. public override bool IsSamePosition (XPathNavigator other)
  164. {
  165. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  166. if (otherDocumentNavigator != null)
  167. return node == otherDocumentNavigator.node;
  168. return false;
  169. }
  170. public override bool MoveTo (XPathNavigator other)
  171. {
  172. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  173. if (otherDocumentNavigator != null) {
  174. if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
  175. node = otherDocumentNavigator.node;
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. [MonoTODO]
  182. public override bool MoveToAttribute (string localName, string namespaceURI)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. public override bool MoveToFirst ()
  187. {
  188. if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  189. node = node.ParentNode.FirstChild;
  190. return true;
  191. }
  192. return false;
  193. }
  194. public override bool MoveToFirstAttribute ()
  195. {
  196. if (NodeType == XPathNodeType.Element) {
  197. attributesEnumerator = node.Attributes.GetEnumerator ();
  198. return MoveToNextAttribute ();
  199. }
  200. return false;
  201. }
  202. public override bool MoveToFirstChild ()
  203. {
  204. if (HasChildren) {
  205. node = node.FirstChild;
  206. return true;
  207. }
  208. return false;
  209. }
  210. [MonoTODO]
  211. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. [MonoTODO]
  216. public override bool MoveToId (string id)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. [MonoTODO]
  221. public override bool MoveToNamespace (string name)
  222. {
  223. throw new NotImplementedException ();
  224. }
  225. public override bool MoveToNext ()
  226. {
  227. if (node.NextSibling != null) {
  228. node = node.NextSibling;
  229. return true;
  230. }
  231. return false;
  232. }
  233. public override bool MoveToNextAttribute ()
  234. {
  235. if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
  236. node = attributesEnumerator.Current as XmlAttribute;
  237. return true;
  238. }
  239. return false;
  240. }
  241. [MonoTODO]
  242. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  243. {
  244. throw new NotImplementedException ();
  245. }
  246. public override bool MoveToParent ()
  247. {
  248. if (node.ParentNode != null) {
  249. node = node.ParentNode;
  250. return true;
  251. }
  252. return false;
  253. }
  254. public override bool MoveToPrevious ()
  255. {
  256. if (node.PreviousSibling != null) {
  257. node = node.PreviousSibling;
  258. return true;
  259. }
  260. return false;
  261. }
  262. public override void MoveToRoot ()
  263. {
  264. if (node.NodeType != XmlNodeType.Document)
  265. node = node.OwnerDocument;
  266. }
  267. #endregion
  268. }
  269. }