2
0

XmlDocumentNavigator.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. return node.XPathNodeType;
  92. }
  93. }
  94. public override string Prefix {
  95. get {
  96. return node.Prefix;
  97. }
  98. }
  99. public override string Value {
  100. get {
  101. switch (NodeType) {
  102. case XPathNodeType.Attribute:
  103. case XPathNodeType.Comment:
  104. case XPathNodeType.ProcessingInstruction:
  105. case XPathNodeType.Text:
  106. case XPathNodeType.Whitespace:
  107. case XPathNodeType.SignificantWhitespace:
  108. return node.Value;
  109. case XPathNodeType.Element:
  110. case XPathNodeType.Root:
  111. return node.InnerText;
  112. }
  113. return String.Empty;
  114. }
  115. }
  116. [MonoTODO]
  117. public override string XmlLang {
  118. get {
  119. throw new NotImplementedException ();
  120. }
  121. }
  122. #endregion
  123. #region Methods
  124. public override XPathNavigator Clone ()
  125. {
  126. return new XmlDocumentNavigator (node);
  127. }
  128. [MonoTODO]
  129. public override string GetAttribute (string localName, string namespaceURI)
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. [MonoTODO]
  134. public override string GetNamespace (string name)
  135. {
  136. throw new NotImplementedException ();
  137. }
  138. public override bool IsSamePosition (XPathNavigator other)
  139. {
  140. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  141. if (otherDocumentNavigator != null)
  142. return node == otherDocumentNavigator.node;
  143. return false;
  144. }
  145. public override bool MoveTo (XPathNavigator other)
  146. {
  147. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  148. if (otherDocumentNavigator != null) {
  149. if (node.OwnerDocument == otherDocumentNavigator.node.OwnerDocument) {
  150. node = otherDocumentNavigator.node;
  151. return true;
  152. }
  153. }
  154. return false;
  155. }
  156. [MonoTODO]
  157. public override bool MoveToAttribute (string localName, string namespaceURI)
  158. {
  159. throw new NotImplementedException ();
  160. }
  161. public override bool MoveToFirst ()
  162. {
  163. if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  164. node = node.ParentNode.FirstChild;
  165. return true;
  166. }
  167. return false;
  168. }
  169. public override bool MoveToFirstAttribute ()
  170. {
  171. if (NodeType == XPathNodeType.Element) {
  172. attributesEnumerator = node.Attributes.GetEnumerator ();
  173. return MoveToNextAttribute ();
  174. }
  175. return false;
  176. }
  177. public override bool MoveToFirstChild ()
  178. {
  179. if (HasChildren) {
  180. node = node.FirstChild;
  181. return true;
  182. }
  183. return false;
  184. }
  185. [MonoTODO]
  186. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  187. {
  188. throw new NotImplementedException ();
  189. }
  190. public override bool MoveToId (string id)
  191. {
  192. XmlDocument doc;
  193. if (node.NodeType == XmlNodeType.Document)
  194. doc = (XmlDocument) node;
  195. else
  196. doc = node.OwnerDocument;
  197. XmlElement eltNew = doc.GetElementById (id);
  198. if (eltNew == null)
  199. return false;
  200. node = eltNew;
  201. return true;
  202. }
  203. [MonoTODO]
  204. public override bool MoveToNamespace (string name)
  205. {
  206. throw new NotImplementedException ();
  207. }
  208. public override bool MoveToNext ()
  209. {
  210. if (node.NextSibling != null) {
  211. node = node.NextSibling;
  212. return true;
  213. }
  214. return false;
  215. }
  216. public override bool MoveToNextAttribute ()
  217. {
  218. if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
  219. node = attributesEnumerator.Current as XmlAttribute;
  220. return true;
  221. }
  222. return false;
  223. }
  224. [MonoTODO]
  225. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. public override bool MoveToParent ()
  230. {
  231. if (node.NodeType == XmlNodeType.Attribute) {
  232. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  233. if (ownerElement != null) {
  234. node = ownerElement;
  235. return true;
  236. }
  237. } else if (node.ParentNode != null) {
  238. node = node.ParentNode;
  239. return true;
  240. }
  241. return false;
  242. }
  243. public override bool MoveToPrevious ()
  244. {
  245. if (node.PreviousSibling != null) {
  246. node = node.PreviousSibling;
  247. return true;
  248. }
  249. return false;
  250. }
  251. public override void MoveToRoot ()
  252. {
  253. if (node.NodeType != XmlNodeType.Document)
  254. node = node.OwnerDocument;
  255. }
  256. internal XmlNode Node { get { return node; } }
  257. #endregion
  258. }
  259. }