XmlDocumentNavigator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. for (int i = 0; i < node.Attributes.Count; i++)
  47. if (node.Attributes [i].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. for (int i = 0; i < node.Attributes.Count; i++) {
  185. XmlAttribute attr = node.Attributes [i];
  186. if (attr.LocalName == localName
  187. && attr.NamespaceURI == namespaceURI) {
  188. node = attr;
  189. nsNode = null;
  190. return true;
  191. }
  192. }
  193. }
  194. return false;
  195. }
  196. public override bool MoveToFirst ()
  197. {
  198. if (nsNode == null && node.NodeType != XmlNodeType.Attribute && node.ParentNode != null) {
  199. MoveToParent ();
  200. // Follow these 2 steps so that we can skip
  201. // some types of nodes .
  202. MoveToFirstChild ();
  203. return true;
  204. }
  205. return false;
  206. }
  207. public override bool MoveToFirstAttribute ()
  208. {
  209. if (node.Attributes == null)
  210. return false;
  211. if (NodeType == XPathNodeType.Element) {
  212. for (int i = 0; i < node.Attributes.Count; i++) {
  213. XmlAttribute attr = node.Attributes [i];
  214. if (attr.NamespaceURI != Xmlns) {
  215. node = attr;
  216. nsNode = null;
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. public override bool MoveToFirstChild ()
  224. {
  225. if (HasChildren) {
  226. if (node == document) {
  227. XmlNode n = node.FirstChild;
  228. if (n == null)
  229. return false;
  230. bool loop = true;
  231. do {
  232. switch (n.NodeType) {
  233. case XmlNodeType.XmlDeclaration:
  234. case XmlNodeType.DocumentType:
  235. n = n.NextSibling;
  236. if (n == null)
  237. return false;
  238. break;
  239. default:
  240. loop = false;
  241. break;
  242. }
  243. } while (loop);
  244. node = n;
  245. } else {
  246. do {
  247. node = node.FirstChild;
  248. if (node.NodeType != XmlNodeType.EntityReference)
  249. break;
  250. node = node.NextSibling;
  251. } while (node != null);
  252. if (node == null)
  253. return false;
  254. }
  255. return true;
  256. }
  257. return false;
  258. }
  259. public override bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope)
  260. {
  261. if (NodeType != XPathNodeType.Element)
  262. return false;
  263. XmlElement el = node as XmlElement;
  264. if (node.Attributes != null) {
  265. do {
  266. for (int i = 0; i < el.Attributes.Count; i++) {
  267. XmlAttribute attr = el.Attributes [i];
  268. if (attr.NamespaceURI == Xmlns) {
  269. nsNode = attr;
  270. return true;
  271. }
  272. }
  273. if (namespaceScope == XPathNamespaceScope.Local)
  274. return false;
  275. el = el.ParentNode as XmlElement;
  276. } while (el != null);
  277. }
  278. if (namespaceScope == XPathNamespaceScope.All) {
  279. nsNode = document;
  280. return true;
  281. }
  282. else
  283. return false;
  284. }
  285. public override bool MoveToId (string id)
  286. {
  287. XmlElement eltNew = document.GetElementById (id);
  288. if (eltNew == null)
  289. return false;
  290. node = eltNew;
  291. return true;
  292. }
  293. public override bool MoveToNamespace (string name)
  294. {
  295. if (name == "xml") {
  296. nsNode = document;
  297. return true;
  298. }
  299. if (NodeType != XPathNodeType.Element)
  300. return false;
  301. XmlElement el = node as XmlElement;
  302. if (node.Attributes != null) {
  303. do {
  304. for (int i = 0; i < el.Attributes.Count; i++) {
  305. XmlAttribute attr = el.Attributes [i];
  306. if (attr.NamespaceURI == Xmlns && attr.Name == name) {
  307. nsNode = attr;
  308. return true;
  309. }
  310. }
  311. el = node.ParentNode as XmlElement;
  312. } while (el != null);
  313. }
  314. return false;
  315. }
  316. public override bool MoveToNext ()
  317. {
  318. if (nsNode != null)
  319. return false;
  320. if (node.NextSibling != null) {
  321. XmlNode n = node.NextSibling;
  322. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  323. while (n != null) {
  324. switch (n.NodeType) {
  325. case XmlNodeType.DocumentType:
  326. case XmlNodeType.XmlDeclaration:
  327. n = n.NextSibling;
  328. continue;
  329. }
  330. break;
  331. }
  332. if (n != null)
  333. node = n;
  334. else
  335. return false;
  336. } else {
  337. while (n != null) {
  338. if (n.NodeType != XmlNodeType.EntityReference)
  339. break;
  340. n = n.NextSibling;
  341. }
  342. if (n != null)
  343. node = n;
  344. else
  345. return false;
  346. }
  347. return true;
  348. }
  349. else
  350. return false;
  351. }
  352. public override bool MoveToNextAttribute ()
  353. {
  354. if (node == null)
  355. return false;
  356. if (NodeType != XPathNodeType.Attribute)
  357. return false;
  358. // Find current attribute.
  359. int pos = 0;
  360. XmlElement owner = ((XmlAttribute) node).OwnerElement;
  361. if (owner == null)
  362. return false;
  363. int count = owner.Attributes.Count;
  364. for(; pos < count; pos++)
  365. if (owner.Attributes [pos] == node)
  366. break;
  367. if (pos == count)
  368. return false; // Where is current attribute? Maybe removed.
  369. // Find next attribute.
  370. for(pos++; pos < count; pos++) {
  371. if (owner.Attributes [pos].NamespaceURI != Xmlns) {
  372. node = owner.Attributes [pos];
  373. nsNode = null;
  374. return true;
  375. }
  376. }
  377. return false;
  378. }
  379. public override bool MoveToNextNamespace (XPathNamespaceScope namespaceScope)
  380. {
  381. if (nsNode == document)
  382. // Current namespace is "xml", so there should be no more namespace nodes.
  383. return false;
  384. if (nsNode == null)
  385. return false;
  386. // Get current attribute's position.
  387. int pos = 0;
  388. XmlElement owner = ((XmlAttribute) nsNode).OwnerElement;
  389. if (owner == null)
  390. return false;
  391. int count = owner.Attributes.Count;
  392. for(; pos < count; pos++)
  393. if (owner.Attributes [pos] == nsNode)
  394. break;
  395. if (pos == count)
  396. return false; // Where is current attribute? Maybe removed.
  397. // Find next namespace from the same element as current ns node.
  398. for(pos++; pos < count; pos++) {
  399. if (owner.Attributes [pos].NamespaceURI == Xmlns) {
  400. nsNode = owner.Attributes [pos];
  401. return true;
  402. }
  403. }
  404. // If not found more, then find from ancestors.
  405. // But if scope is Local, then it returns false here.
  406. if (namespaceScope == XPathNamespaceScope.Local)
  407. return false;
  408. owner = owner.ParentNode as XmlElement;
  409. while (owner != null) {
  410. for (int i = 0; i < owner.Attributes.Count; i++) {
  411. XmlAttribute attr = owner.Attributes [i];
  412. if (attr.NamespaceURI == Xmlns) {
  413. nsNode = attr;
  414. return true;
  415. }
  416. }
  417. owner = owner.ParentNode as XmlElement;
  418. }
  419. if (namespaceScope == XPathNamespaceScope.All) {
  420. nsNode = document;
  421. return true;
  422. }
  423. else
  424. return false;
  425. }
  426. public override bool MoveToParent ()
  427. {
  428. if (nsNode != null) {
  429. nsNode = null;
  430. return true;
  431. }
  432. else if (node.NodeType == XmlNodeType.Attribute) {
  433. XmlElement ownerElement = ((XmlAttribute)node).OwnerElement;
  434. if (ownerElement != null) {
  435. node = ownerElement;
  436. nsNode = null;
  437. return true;
  438. }
  439. } else if (node.ParentNode != null) {
  440. node = node.ParentNode;
  441. nsNode = null;
  442. return true;
  443. }
  444. return false;
  445. }
  446. public override bool MoveToPrevious ()
  447. {
  448. if (nsNode != null)
  449. return false;
  450. if (node.PreviousSibling != null) {
  451. if (node.ParentNode != null && node.ParentNode.NodeType == XmlNodeType.Document) {
  452. XmlNode n = node.PreviousSibling;
  453. while (n != null) {
  454. switch (n.NodeType) {
  455. case XmlNodeType.DocumentType:
  456. case XmlNodeType.XmlDeclaration:
  457. n = n.PreviousSibling;
  458. continue;
  459. }
  460. break;
  461. }
  462. if (n != null)
  463. node = n;
  464. else
  465. return false;
  466. }
  467. else
  468. node = node.PreviousSibling;
  469. return true;
  470. }
  471. else
  472. return false;
  473. }
  474. public override void MoveToRoot ()
  475. {
  476. XmlAttribute attr = node as XmlAttribute;
  477. XmlNode tmp = attr != null ? attr.OwnerElement : node;
  478. while (tmp.ParentNode != null)
  479. tmp = tmp.ParentNode;
  480. node = tmp;
  481. nsNode = null;
  482. }
  483. internal XmlNode Node { get { return node; } }
  484. XmlNode IHasXmlNode.GetNode ()
  485. {
  486. return node;
  487. }
  488. #endregion
  489. }
  490. }