XmlDocumentNavigator.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. //
  2. // System.Xml.XmlDocumentNavigator
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Atsushi Enomoto <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond
  9. // (C) 2003 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml;
  14. using System.Xml.XPath;
  15. namespace System.Xml
  16. {
  17. internal class XmlDocumentNavigator : XPathNavigator, IHasXmlNode
  18. {
  19. #region Constructors
  20. internal XmlDocumentNavigator(XmlNode node)
  21. {
  22. this.node = node;
  23. this.document = node.NodeType == XmlNodeType.Document ?
  24. node as XmlDocument : node.OwnerDocument;
  25. }
  26. #endregion
  27. #region Fields
  28. private const string Xmlns = "http://www.w3.org/2000/xmlns/";
  29. private const string XmlnsXML = "http://www.w3.org/XML/1998/namespace";
  30. private XmlNode node;
  31. private XmlDocument document;
  32. // Current namespace node (ancestor's attribute of current node).
  33. private XmlNode nsNode;
  34. #endregion
  35. #region Properties
  36. public override string BaseURI {
  37. get {
  38. return node.BaseURI;
  39. }
  40. }
  41. public override bool HasAttributes {
  42. get {
  43. if (nsNode != null)
  44. return false;
  45. if (node.Attributes != null)
  46. foreach (XmlAttribute attribute in node.Attributes)
  47. if (attribute.NamespaceURI != Xmlns)
  48. return true;
  49. return false;
  50. }
  51. }
  52. public override bool HasChildren {
  53. get {
  54. if (nsNode != null)
  55. return false;
  56. XPathNodeType nodeType = NodeType;
  57. bool canHaveChildren = nodeType == XPathNodeType.Root || nodeType == XPathNodeType.Element;
  58. return canHaveChildren && node.FirstChild != null;
  59. }
  60. }
  61. public override bool IsEmptyElement {
  62. get {
  63. if (nsNode != null)
  64. return false;
  65. return node.NodeType == XmlNodeType.Element
  66. && ((XmlElement) node).IsEmpty;
  67. }
  68. }
  69. public override string LocalName {
  70. get {
  71. if (nsNode != null) {
  72. if (nsNode == document)
  73. return "xml";
  74. else
  75. return (nsNode.Name == "xmlns") ? String.Empty : nsNode.LocalName;
  76. }
  77. XPathNodeType nodeType = NodeType;
  78. bool canHaveName =
  79. nodeType == XPathNodeType.Element ||
  80. nodeType == XPathNodeType.Attribute ||
  81. nodeType == XPathNodeType.ProcessingInstruction ||
  82. nodeType == XPathNodeType.Namespace;
  83. return canHaveName ? node.LocalName : String.Empty;
  84. }
  85. }
  86. public override string Name {
  87. get {
  88. if (nsNode != null)
  89. return LocalName;
  90. XPathNodeType nodeType = NodeType;
  91. bool canHaveName =
  92. nodeType == XPathNodeType.Element ||
  93. nodeType == XPathNodeType.Attribute ||
  94. nodeType == XPathNodeType.ProcessingInstruction ||
  95. nodeType == XPathNodeType.Namespace;
  96. return canHaveName ? node.Name : String.Empty;
  97. }
  98. }
  99. public override string NamespaceURI {
  100. get { return (nsNode != null) ? String.Empty : node.NamespaceURI; }
  101. }
  102. public override XmlNameTable NameTable {
  103. get {
  104. return document.NameTable;
  105. }
  106. }
  107. public override XPathNodeType NodeType {
  108. get { return (nsNode != null) ? XPathNodeType.Namespace : node.XPathNodeType; }
  109. }
  110. public override string Prefix {
  111. get { return (nsNode != null) ? String.Empty : node.Prefix; }
  112. }
  113. public override string Value {
  114. get {
  115. switch (NodeType) {
  116. case XPathNodeType.Attribute:
  117. case XPathNodeType.Comment:
  118. case XPathNodeType.ProcessingInstruction:
  119. case XPathNodeType.Text:
  120. case XPathNodeType.Whitespace:
  121. case XPathNodeType.SignificantWhitespace:
  122. return node.Value;
  123. case XPathNodeType.Element:
  124. case XPathNodeType.Root:
  125. return node.InnerText;
  126. case XPathNodeType.Namespace:
  127. return nsNode == document ? XmlnsXML : nsNode.Value;
  128. }
  129. return String.Empty;
  130. }
  131. }
  132. public override string XmlLang {
  133. get {
  134. return node.XmlLang;
  135. }
  136. }
  137. #endregion
  138. #region Methods
  139. public override XPathNavigator Clone ()
  140. {
  141. XmlDocumentNavigator clone = new XmlDocumentNavigator (node);
  142. clone.nsNode = nsNode;
  143. return clone;
  144. }
  145. public override string GetAttribute (string localName, string namespaceURI)
  146. {
  147. if (HasAttributes) {
  148. XmlElement el = Node as XmlElement;
  149. return el != null ? el.GetAttribute (localName, namespaceURI) : String.Empty;
  150. }
  151. return String.Empty;
  152. }
  153. public override string GetNamespace (string name)
  154. {
  155. // MSDN says "String.Empty if a matching namespace
  156. // node is not found or if the navigator is not
  157. // positioned on an element node", but in fact it
  158. // returns actual namespace for the other nodes.
  159. return Node.GetNamespaceOfPrefix (name);
  160. }
  161. public override bool IsSamePosition (XPathNavigator other)
  162. {
  163. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  164. if (otherDocumentNavigator != null)
  165. return node == otherDocumentNavigator.node
  166. && nsNode == otherDocumentNavigator.nsNode;
  167. return false;
  168. }
  169. public override bool MoveTo (XPathNavigator other)
  170. {
  171. XmlDocumentNavigator otherDocumentNavigator = other as XmlDocumentNavigator;
  172. if (otherDocumentNavigator != null) {
  173. if (document == otherDocumentNavigator.document) {
  174. node = otherDocumentNavigator.node;
  175. nsNode = otherDocumentNavigator.nsNode;
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. public override bool MoveToAttribute (string localName, string namespaceURI)
  182. {
  183. if (node.Attributes != null) {
  184. foreach (XmlAttribute attr in node.Attributes) {
  185. if (attr.LocalName == localName
  186. && attr.NamespaceURI == namespaceURI) {
  187. node = attr;
  188. nsNode = null;
  189. return true;
  190. }
  191. }
  192. }
  193. return false;
  194. }
  195. public override bool MoveToFirst ()
  196. {
  197. if (nsNode == null && node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  198. MoveToParent ();
  199. // Follow these 2 steps so that we can skip
  200. // some types of nodes .
  201. MoveToFirstChild ();
  202. return true;
  203. }
  204. return false;
  205. }
  206. public override bool MoveToFirstAttribute ()
  207. {
  208. if (node.Attributes == null)
  209. return false;
  210. if (NodeType == XPathNodeType.Element) {
  211. foreach (XmlAttribute attr in node.Attributes) {
  212. if (attr.NamespaceURI != Xmlns) {
  213. node = attr;
  214. nsNode = null;
  215. return true;
  216. }
  217. }
  218. }
  219. return false;
  220. }
  221. public override bool MoveToFirstChild ()
  222. {
  223. if (HasChildren) {
  224. if (node == document) {
  225. XmlNode n = node.FirstChild;
  226. if (n == null)
  227. return false;
  228. bool loop = true;
  229. do {
  230. switch (n.NodeType) {
  231. case XmlNodeType.XmlDeclaration:
  232. case XmlNodeType.DocumentType:
  233. n = n.NextSibling;
  234. if (n == null)
  235. return false;
  236. break;
  237. default:
  238. loop = false;
  239. break;
  240. }
  241. } while (loop);
  242. node = n;
  243. }
  244. else
  245. node = node.FirstChild;
  246. return true;
  247. }
  248. return false;
  249. }
  250. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  251. {
  252. if (NodeType != XPathNodeType.Element)
  253. return false;
  254. XmlElement el = node as XmlElement;
  255. if (node.Attributes != null) {
  256. do {
  257. foreach (XmlAttribute attr in el.Attributes) {
  258. if (attr.NamespaceURI == Xmlns) {
  259. nsNode = attr;
  260. return true;
  261. }
  262. }
  263. if (namespaceScope == XPathNamespaceScope.Local)
  264. return false;
  265. el = el.ParentNode as XmlElement;
  266. } while (el != null);
  267. }
  268. if (namespaceScope == XPathNamespaceScope.All) {
  269. nsNode = document;
  270. return true;
  271. }
  272. else
  273. return false;
  274. }
  275. public override bool MoveToId (string id)
  276. {
  277. XmlElement eltNew = document.GetElementById (id);
  278. if (eltNew == null)
  279. return false;
  280. node = eltNew;
  281. return true;
  282. }
  283. public override bool MoveToNamespace (string name)
  284. {
  285. if (name == "xml") {
  286. nsNode = document;
  287. return true;
  288. }
  289. if (NodeType != XPathNodeType.Element)
  290. return false;
  291. XmlElement el = node as XmlElement;
  292. if (node.Attributes != null) {
  293. do {
  294. foreach (XmlAttribute attr in el.Attributes) {
  295. if (attr.NamespaceURI == Xmlns && Name == name) {
  296. nsNode = attr;
  297. return true;
  298. }
  299. }
  300. el = node.ParentNode as XmlElement;
  301. } while (el != null);
  302. }
  303. return false;
  304. }
  305. public override bool MoveToNext ()
  306. {
  307. if (nsNode != null)
  308. return false;
  309. if (node.NextSibling != null) {
  310. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  311. XmlNode n = node.NextSibling;
  312. while (n != null) {
  313. switch (n.NodeType) {
  314. case XmlNodeType.DocumentType:
  315. case XmlNodeType.XmlDeclaration:
  316. n = n.NextSibling;
  317. continue;
  318. }
  319. break;
  320. }
  321. if (n != null)
  322. node = n;
  323. else
  324. return false;
  325. }
  326. else
  327. node = node.NextSibling;
  328. return true;
  329. }
  330. else
  331. return false;
  332. }
  333. public override bool MoveToNextAttribute ()
  334. {
  335. if (NodeType != XPathNodeType.Attribute)
  336. return false;
  337. // Find current attribute.
  338. int pos = 0;
  339. XmlElement owner = ((XmlAttribute) node).OwnerElement;
  340. int count = owner.Attributes.Count;
  341. for(; pos < count; pos++)
  342. if (owner.Attributes [pos] == node)
  343. break;
  344. if (pos == count)
  345. return false; // Where is current attribute? Maybe removed.
  346. // Find next attribute.
  347. for(pos++; pos < count; pos++) {
  348. if (owner.Attributes [pos].NamespaceURI != Xmlns) {
  349. node = owner.Attributes [pos];
  350. nsNode = null;
  351. return true;
  352. }
  353. }
  354. return false;
  355. }
  356. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  357. {
  358. if (nsNode == document)
  359. // Current namespace is "xml", so there should be no more namespace nodes.
  360. return false;
  361. if (nsNode == null)
  362. return false;
  363. // Get current attribute's position.
  364. int pos = 0;
  365. XmlElement owner = ((XmlAttribute) nsNode).OwnerElement;
  366. int count = owner.Attributes.Count;
  367. for(; pos < count; pos++)
  368. if (owner.Attributes [pos] == nsNode)
  369. break;
  370. if (pos == count)
  371. return false; // Where is current attribute? Maybe removed.
  372. // Find next namespace from the same element as current ns node.
  373. for(pos++; pos < count; pos++) {
  374. if (owner.Attributes [pos].NamespaceURI == Xmlns) {
  375. nsNode = owner.Attributes [pos];
  376. return true;
  377. }
  378. }
  379. // If not found more, then find from ancestors.
  380. // But if scope is Local, then it returns false here.
  381. if (namespaceScope == XPathNamespaceScope.Local)
  382. return false;
  383. owner = owner.ParentNode as XmlElement;
  384. while (owner != null) {
  385. foreach (XmlAttribute attr in owner.Attributes) {
  386. if (attr.NamespaceURI == Xmlns) {
  387. nsNode = attr;
  388. return true;
  389. }
  390. }
  391. owner = owner.ParentNode as XmlElement;
  392. }
  393. if (namespaceScope == XPathNamespaceScope.All) {
  394. nsNode = document;
  395. return true;
  396. }
  397. else
  398. return false;
  399. }
  400. public override bool MoveToParent ()
  401. {
  402. if (nsNode != null) {
  403. nsNode = null;
  404. return true;
  405. }
  406. else if (node.NodeType == XmlNodeType.Attribute) {
  407. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  408. if (ownerElement != null) {
  409. node = ownerElement;
  410. nsNode = null;
  411. return true;
  412. }
  413. } else if (node.ParentNode != null) {
  414. node = node.ParentNode;
  415. nsNode = null;
  416. return true;
  417. }
  418. return false;
  419. }
  420. public override bool MoveToPrevious ()
  421. {
  422. if (nsNode != null)
  423. return false;
  424. if (node.PreviousSibling != null) {
  425. node = node.PreviousSibling;
  426. return true;
  427. }
  428. return false;
  429. }
  430. public override void MoveToRoot ()
  431. {
  432. node = document;
  433. nsNode = null;
  434. }
  435. internal XmlNode Node { get { return node; } }
  436. XmlNode IHasXmlNode.GetNode ()
  437. {
  438. return node;
  439. }
  440. #endregion
  441. }
  442. }