XmlDocumentNavigator.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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
  55. && ((XmlElement) node).IsEmpty;
  56. }
  57. }
  58. public override string LocalName {
  59. get {
  60. XPathNodeType nodeType = NodeType;
  61. bool canHaveName =
  62. nodeType == XPathNodeType.Element ||
  63. nodeType == XPathNodeType.Attribute ||
  64. nodeType == XPathNodeType.ProcessingInstruction ||
  65. nodeType == XPathNodeType.Namespace;
  66. return canHaveName ? node.LocalName : String.Empty;
  67. }
  68. }
  69. public override string Name {
  70. get {
  71. XPathNodeType nodeType = NodeType;
  72. bool canHaveName =
  73. nodeType == XPathNodeType.Element ||
  74. nodeType == XPathNodeType.Attribute ||
  75. nodeType == XPathNodeType.ProcessingInstruction ||
  76. nodeType == XPathNodeType.Namespace;
  77. return canHaveName ? node.Name : String.Empty;
  78. }
  79. }
  80. public override string NamespaceURI {
  81. get {
  82. return node.NamespaceURI;
  83. }
  84. }
  85. public override XmlNameTable NameTable {
  86. get {
  87. return document.NameTable;
  88. }
  89. }
  90. public override XPathNodeType NodeType {
  91. get {
  92. return node.XPathNodeType;
  93. }
  94. }
  95. public override string Prefix {
  96. get {
  97. return node.Prefix;
  98. }
  99. }
  100. public override string Value {
  101. get {
  102. switch (NodeType) {
  103. case XPathNodeType.Attribute:
  104. case XPathNodeType.Comment:
  105. case XPathNodeType.ProcessingInstruction:
  106. case XPathNodeType.Text:
  107. case XPathNodeType.Whitespace:
  108. case XPathNodeType.SignificantWhitespace:
  109. return node.Value;
  110. case XPathNodeType.Element:
  111. case XPathNodeType.Root:
  112. return node.InnerText;
  113. }
  114. return String.Empty;
  115. }
  116. }
  117. public override string XmlLang {
  118. get {
  119. return node.XmlLang;
  120. }
  121. }
  122. #endregion
  123. #region Methods
  124. public override XPathNavigator Clone ()
  125. {
  126. return new XmlDocumentNavigator (node);
  127. }
  128. public override string GetAttribute (string localName, string namespaceURI)
  129. {
  130. XmlElement el = Node as XmlElement;
  131. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  132. }
  133. public override string GetNamespace (string name)
  134. {
  135. // MSDN says "String.Empty if a matching namespace
  136. // node is not found or if the navigator is not
  137. // positioned on an element node", but in fact it
  138. // returns actual namespace for the other nodes.
  139. return Node.GetNamespaceOfPrefix (name);
  140. }
  141. public override bool IsSamePosition (XPathNavigator other)
  142. {
  143. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  144. if (otherDocumentNavigator != null)
  145. return node == otherDocumentNavigator.node;
  146. return false;
  147. }
  148. public override bool MoveTo (XPathNavigator other)
  149. {
  150. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  151. if (otherDocumentNavigator != null) {
  152. if (document == otherDocumentNavigator.document) {
  153. node = otherDocumentNavigator.node;
  154. return true;
  155. }
  156. }
  157. return false;
  158. }
  159. public override bool MoveToAttribute (string localName, string namespaceURI)
  160. {
  161. attributesEnumerator = node.Attributes.GetEnumerator ();
  162. while (attributesEnumerator.MoveNext ()) {
  163. XmlAttribute attr = attributesEnumerator.Current as XmlAttribute;
  164. if (attr.LocalName == localName && attr.NamespaceURI == namespaceURI) {
  165. node = attr;
  166. return true;
  167. }
  168. }
  169. return false;
  170. }
  171. public override bool MoveToFirst ()
  172. {
  173. if (node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  174. MoveToParent ();
  175. // Follow these 2 steps so that we can skip
  176. // some types of nodes .
  177. MoveToFirstChild ();
  178. return true;
  179. }
  180. return false;
  181. }
  182. public override bool MoveToFirstAttribute ()
  183. {
  184. if (NodeType == XPathNodeType.Element) {
  185. attributesEnumerator = node.Attributes.GetEnumerator ();
  186. return MoveToNextAttribute ();
  187. }
  188. return false;
  189. }
  190. public override bool MoveToFirstChild ()
  191. {
  192. if (HasChildren) {
  193. if (node == document) {
  194. XmlNode n = node.FirstChild;
  195. if (n == null)
  196. return false;
  197. bool loop = true;
  198. do {
  199. switch (n.NodeType) {
  200. case XmlNodeType.XmlDeclaration:
  201. case XmlNodeType.DocumentType:
  202. n = n.NextSibling;
  203. if (n == null)
  204. return false;
  205. break;
  206. default:
  207. loop = false;
  208. break;
  209. }
  210. } while (loop);
  211. node = n;
  212. }
  213. else
  214. node = node.FirstChild;
  215. return true;
  216. }
  217. return false;
  218. }
  219. [MonoTODO]
  220. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  221. {
  222. throw new NotImplementedException ();
  223. }
  224. public override bool MoveToId (string id)
  225. {
  226. XmlElement eltNew = document.GetElementById (id);
  227. if (eltNew == null)
  228. return false;
  229. node = eltNew;
  230. return true;
  231. }
  232. [MonoTODO]
  233. public override bool MoveToNamespace (string name)
  234. {
  235. throw new NotImplementedException ();
  236. }
  237. public override bool MoveToNext ()
  238. {
  239. if (node.NextSibling != null) {
  240. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  241. XmlNode n = node.NextSibling;
  242. while (n != null) {
  243. switch (n.NodeType) {
  244. case XmlNodeType.DocumentType:
  245. case XmlNodeType.XmlDeclaration:
  246. n = n.NextSibling;
  247. continue;
  248. }
  249. break;
  250. }
  251. if (n != null)
  252. node = n;
  253. else
  254. return false;
  255. }
  256. else
  257. node = node.NextSibling;
  258. return true;
  259. }
  260. else
  261. return false;
  262. }
  263. public override bool MoveToNextAttribute ()
  264. {
  265. if (attributesEnumerator != null && attributesEnumerator.MoveNext ()) {
  266. node = attributesEnumerator.Current as XmlAttribute;
  267. return true;
  268. }
  269. return false;
  270. }
  271. [MonoTODO]
  272. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  273. {
  274. throw new NotImplementedException ();
  275. }
  276. public override bool MoveToParent ()
  277. {
  278. if (node.NodeType == XmlNodeType.Attribute) {
  279. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  280. if (ownerElement != null) {
  281. node = ownerElement;
  282. return true;
  283. }
  284. } else if (node.ParentNode != null) {
  285. node = node.ParentNode;
  286. return true;
  287. }
  288. return false;
  289. }
  290. public override bool MoveToPrevious ()
  291. {
  292. if (node.PreviousSibling != null) {
  293. node = node.PreviousSibling;
  294. return true;
  295. }
  296. return false;
  297. }
  298. public override void MoveToRoot ()
  299. {
  300. node = document;
  301. }
  302. internal XmlNode Node { get { return node; } }
  303. XmlNode IHasXmlNode.GetNode ()
  304. {
  305. return node;
  306. }
  307. #endregion
  308. }
  309. }