XmlDocumentNavigator.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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, IHasXmlNode
  16. {
  17. #region Constructors
  18. internal XmlDocumentNavigator(XmlNode node)
  19. {
  20. this.node = node;
  21. this.document = node.NodeType == XmlNodeType.Document ?
  22. node as XmlDocument : node.OwnerDocument;
  23. }
  24. #endregion
  25. #region Fields
  26. private XmlNode node;
  27. private XmlDocument document;
  28. private IEnumerator attributesEnumerator;
  29. #endregion
  30. #region Properties
  31. public override string BaseURI {
  32. get {
  33. return node.BaseURI;
  34. }
  35. }
  36. public override bool HasAttributes {
  37. get {
  38. if (node.Attributes != null)
  39. foreach (XmlAttribute attribute in node.Attributes)
  40. if (attribute.NamespaceURI != "http://www.w3.org/2000/xmlns/")
  41. return true;
  42. return false;
  43. }
  44. }
  45. public override bool HasChildren {
  46. get {
  47. XPathNodeType nodeType = NodeType;
  48. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  49. return canHaveChildren && node.FirstChild != null;
  50. }
  51. }
  52. public override bool IsEmptyElement {
  53. get {
  54. return node.NodeType == XmlNodeType.Element && !HasChildren;
  55. }
  56. }
  57. public override string LocalName {
  58. get {
  59. XPathNodeType nodeType = NodeType;
  60. bool canHaveName =
  61. nodeType == XPathNodeType.Element ||
  62. nodeType == XPathNodeType.Attribute ||
  63. nodeType == XPathNodeType.ProcessingInstruction ||
  64. nodeType == XPathNodeType.Namespace;
  65. return canHaveName ? node.LocalName : String.Empty;
  66. }
  67. }
  68. public override string Name {
  69. get {
  70. XPathNodeType nodeType = NodeType;
  71. bool canHaveName =
  72. nodeType == XPathNodeType.Element ||
  73. nodeType == XPathNodeType.Attribute ||
  74. nodeType == XPathNodeType.ProcessingInstruction ||
  75. nodeType == XPathNodeType.Namespace;
  76. return canHaveName ? node.Name : String.Empty;
  77. }
  78. }
  79. public override string NamespaceURI {
  80. get {
  81. return node.NamespaceURI;
  82. }
  83. }
  84. public override XmlNameTable NameTable {
  85. get {
  86. return document.NameTable;
  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. public override string XmlLang {
  117. get {
  118. return node.XmlLang;
  119. }
  120. }
  121. #endregion
  122. #region Methods
  123. public override XPathNavigator Clone ()
  124. {
  125. return new XmlDocumentNavigator (node);
  126. }
  127. public override string GetAttribute (string localName, string namespaceURI)
  128. {
  129. XmlElement el = Node as XmlElement;
  130. return (el != null) ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  131. }
  132. public override string GetNamespace (string name)
  133. {
  134. // MSDN says "String.Empty if a matching namespace
  135. // node is not found or if the navigator is not
  136. // positioned on an element node", but in fact it
  137. // returns actual namespace for the other nodes.
  138. return Node.GetNamespaceOfPrefix (name);
  139. }
  140. public override bool IsSamePosition (XPathNavigator other)
  141. {
  142. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  143. if (otherDocumentNavigator != null)
  144. return node == otherDocumentNavigator.node;
  145. return false;
  146. }
  147. public override bool MoveTo (XPathNavigator other)
  148. {
  149. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  150. if (otherDocumentNavigator != null) {
  151. if (document == otherDocumentNavigator.document) {
  152. node = otherDocumentNavigator.node;
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. public override bool MoveToAttribute (string localName, string namespaceURI)
  159. {
  160. attributesEnumerator = node.Attributes.GetEnumerator ();
  161. while (attributesEnumerator.MoveNext ()) {
  162. XmlAttribute attr = attributesEnumerator.Current as XmlAttribute;
  163. if (attr.LocalName == localName && attr.NamespaceURI == namespaceURI) {
  164. node = attr;
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. public override bool MoveToFirst ()
  171. {
  172. if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  173. node = node.ParentNode.FirstChild;
  174. return true;
  175. }
  176. return false;
  177. }
  178. public override bool MoveToFirstAttribute ()
  179. {
  180. if (NodeType == XPathNodeType.Element) {
  181. attributesEnumerator = node.Attributes.GetEnumerator ();
  182. return MoveToNextAttribute ();
  183. }
  184. return false;
  185. }
  186. public override bool MoveToFirstChild ()
  187. {
  188. if (HasChildren) {
  189. if (node == document) {
  190. XmlNode n = node.FirstChild;
  191. if (n == null)
  192. return false;
  193. bool loop = true;
  194. do {
  195. switch (n.NodeType) {
  196. case XmlNodeType.XmlDeclaration:
  197. case XmlNodeType.DocumentType:
  198. n = n.NextSibling;
  199. if (n == null)
  200. return false;
  201. break;
  202. default:
  203. loop = false;
  204. break;
  205. }
  206. } while (loop);
  207. node = n;
  208. }
  209. else
  210. node = node.FirstChild;
  211. return true;
  212. }
  213. return false;
  214. }
  215. [MonoTODO]
  216. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. public override bool MoveToId (string id)
  221. {
  222. XmlElement eltNew = document.GetElementById (id);
  223. if (eltNew == null)
  224. return false;
  225. node = eltNew;
  226. return true;
  227. }
  228. [MonoTODO]
  229. public override bool MoveToNamespace (string name)
  230. {
  231. throw new NotImplementedException ();
  232. }
  233. public override bool MoveToNext ()
  234. {
  235. if (node.NextSibling != null) {
  236. node = node.NextSibling;
  237. return true;
  238. }
  239. return false;
  240. }
  241. public override bool MoveToNextAttribute ()
  242. {
  243. if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
  244. node = attributesEnumerator.Current as XmlAttribute;
  245. return true;
  246. }
  247. return false;
  248. }
  249. [MonoTODO]
  250. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  251. {
  252. throw new NotImplementedException ();
  253. }
  254. public override bool MoveToParent ()
  255. {
  256. if (node.NodeType == XmlNodeType.Attribute) {
  257. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  258. if (ownerElement != null) {
  259. node = ownerElement;
  260. return true;
  261. }
  262. } else if (node.ParentNode != null) {
  263. node = node.ParentNode;
  264. return true;
  265. }
  266. return false;
  267. }
  268. public override bool MoveToPrevious ()
  269. {
  270. if (node.PreviousSibling != null) {
  271. node = node.PreviousSibling;
  272. return true;
  273. }
  274. return false;
  275. }
  276. public override void MoveToRoot ()
  277. {
  278. node = document;
  279. }
  280. internal XmlNode Node { get { return node; } }
  281. XmlNode IHasXmlNode.GetNode ()
  282. {
  283. return node;
  284. }
  285. #endregion
  286. }
  287. }