XmlDocumentNavigator.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. public override string BaseURI {
  30. get {
  31. return node.BaseURI;
  32. }
  33. }
  34. public override bool HasAttributes {
  35. get {
  36. if (node.Attributes != null)
  37. foreach (XmlAttribute attribute in node.Attributes)
  38. if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
  39. return true;
  40. return false;
  41. }
  42. }
  43. public override bool HasChildren {
  44. get {
  45. XPathNodeType nodeType = NodeType;
  46. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  47. return canHaveChildren && node.FirstChild != null;
  48. }
  49. }
  50. public override bool IsEmptyElement {
  51. get {
  52. return node.NodeType == XmlNodeType.Element && !HasChildren;
  53. }
  54. }
  55. public override string LocalName {
  56. get {
  57. XPathNodeType nodeType = NodeType;
  58. bool canHaveName =
  59. nodeType == XPathNodeType.Element ||
  60. nodeType == XPathNodeType.Attribute ||
  61. nodeType == XPathNodeType.ProcessingInstruction ||
  62. nodeType == XPathNodeType.Namespace;
  63. return canHaveName ? node.LocalName : String.Empty;
  64. }
  65. }
  66. public override string Name {
  67. get {
  68. XPathNodeType nodeType = NodeType;
  69. bool canHaveName =
  70. nodeType == XPathNodeType.Element ||
  71. nodeType == XPathNodeType.Attribute ||
  72. nodeType == XPathNodeType.ProcessingInstruction ||
  73. nodeType == XPathNodeType.Namespace;
  74. return canHaveName ? node.Name : String.Empty;
  75. }
  76. }
  77. public override string NamespaceURI {
  78. get {
  79. return node.NamespaceURI;
  80. }
  81. }
  82. public override XmlNameTable NameTable {
  83. get {
  84. return node.OwnerDocument.NameTable;
  85. }
  86. }
  87. public override XPathNodeType NodeType {
  88. get {
  89. return node.XPathNodeType;
  90. }
  91. }
  92. public override string Prefix {
  93. get {
  94. return node.Prefix;
  95. }
  96. }
  97. public override string Value {
  98. get {
  99. switch (NodeType) {
  100. case XPathNodeType.Attribute:
  101. case XPathNodeType.Comment:
  102. case XPathNodeType.ProcessingInstruction:
  103. case XPathNodeType.Text:
  104. case XPathNodeType.Whitespace:
  105. case XPathNodeType.SignificantWhitespace:
  106. return node.Value;
  107. case XPathNodeType.Element:
  108. case XPathNodeType.Root:
  109. return node.InnerText;
  110. }
  111. return String.Empty;
  112. }
  113. }
  114. public override string XmlLang {
  115. get {
  116. return node.XmlLang;
  117. }
  118. }
  119. #endregion
  120. #region Methods
  121. public override XPathNavigator Clone ()
  122. {
  123. return new XmlDocumentNavigator (node);
  124. }
  125. public override string GetAttribute (string localName, string namespaceURI)
  126. {
  127. XmlElement el = Node as XmlElement;
  128. return (el != null) ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  129. }
  130. public override string GetNamespace (string name)
  131. {
  132. // MSDN says "String.Empty if a matching namespace
  133. // node is not found or if the navigator is not
  134. // positioned on an element node", but in fact it
  135. // returns actual namespace for the other nodes.
  136. return Node.GetNamespaceOfPrefix (name);
  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. public override bool MoveToAttribute (string localName, string namespaceURI)
  157. {
  158. attributesEnumerator = node.Attributes.GetEnumerator ();
  159. while (attributesEnumerator.MoveNext ()) {
  160. XmlAttribute attr = attributesEnumerator.Current as XmlAttribute;
  161. if (attr.LocalName == localName && attr.NamespaceURI == namespaceURI) {
  162. node = attr;
  163. return true;
  164. }
  165. }
  166. return false;
  167. }
  168. public override bool MoveToFirst ()
  169. {
  170. if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  171. node = node.ParentNode.FirstChild;
  172. return true;
  173. }
  174. return false;
  175. }
  176. public override bool MoveToFirstAttribute ()
  177. {
  178. if (NodeType == XPathNodeType.Element) {
  179. attributesEnumerator = node.Attributes.GetEnumerator ();
  180. return MoveToNextAttribute ();
  181. }
  182. return false;
  183. }
  184. public override bool MoveToFirstChild ()
  185. {
  186. if (HasChildren) {
  187. node = (NodeType == XPathNodeType.Root) ?
  188. ((XmlDocument) node).DocumentElement :
  189. node.FirstChild;
  190. return true;
  191. }
  192. return false;
  193. }
  194. [MonoTODO]
  195. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  196. {
  197. throw new NotImplementedException ();
  198. }
  199. public override bool MoveToId (string id)
  200. {
  201. XmlDocument doc;
  202. if (node.NodeType == XmlNodeType.Document)
  203. doc = (XmlDocument) node;
  204. else
  205. doc = node.OwnerDocument;
  206. XmlElement eltNew = doc.GetElementById (id);
  207. if (eltNew == null)
  208. return false;
  209. node = eltNew;
  210. return true;
  211. }
  212. [MonoTODO]
  213. public override bool MoveToNamespace (string name)
  214. {
  215. throw new NotImplementedException ();
  216. }
  217. public override bool MoveToNext ()
  218. {
  219. if (node.NextSibling != null) {
  220. node = node.NextSibling;
  221. return true;
  222. }
  223. return false;
  224. }
  225. public override bool MoveToNextAttribute ()
  226. {
  227. if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
  228. node = attributesEnumerator.Current as XmlAttribute;
  229. return true;
  230. }
  231. return false;
  232. }
  233. [MonoTODO]
  234. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  235. {
  236. throw new NotImplementedException ();
  237. }
  238. public override bool MoveToParent ()
  239. {
  240. if (node.NodeType == XmlNodeType.Attribute) {
  241. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  242. if (ownerElement != null) {
  243. node = ownerElement;
  244. return true;
  245. }
  246. } else if (node.ParentNode != null) {
  247. node = node.ParentNode;
  248. return true;
  249. }
  250. return false;
  251. }
  252. public override bool MoveToPrevious ()
  253. {
  254. if (node.PreviousSibling != null) {
  255. node = node.PreviousSibling;
  256. return true;
  257. }
  258. return false;
  259. }
  260. public override void MoveToRoot ()
  261. {
  262. if (node.NodeType != XmlNodeType.Document)
  263. node = node.OwnerDocument;
  264. }
  265. internal XmlNode Node { get { return node; } }
  266. #endregion
  267. }
  268. }