XPathNavigator.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. //
  2. // System.Xml.XPath.XPathNavigator
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2004 Novell Inc.
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. #if NET_2_0
  33. using System.Collections;
  34. using System.Collections.Generic;
  35. using System.Diagnostics;
  36. #endif
  37. using System.IO;
  38. using System.Xml;
  39. using System.Xml.Schema;
  40. using Mono.Xml.XPath;
  41. #if NET_2_0
  42. using NSResolver = System.Xml.IXmlNamespaceResolver;
  43. #else
  44. using NSResolver = System.Xml.XmlNamespaceManager;
  45. #endif
  46. namespace System.Xml.XPath
  47. {
  48. #if NET_2_0
  49. public abstract class XPathNavigator : XPathItem,
  50. ICloneable, IXPathNavigable, IXmlNamespaceResolver
  51. #else
  52. public abstract class XPathNavigator : ICloneable
  53. #endif
  54. {
  55. #region Static members
  56. #if NET_2_0
  57. public static IEqualityComparer NavigatorComparer {
  58. get { return XPathNavigatorComparer.Instance; }
  59. }
  60. #endif
  61. #endregion
  62. #region Constructor
  63. protected XPathNavigator ()
  64. {
  65. }
  66. #endregion
  67. #region Properties
  68. public abstract string BaseURI { get; }
  69. #if NET_2_0
  70. public virtual bool CanEdit {
  71. get { return false; }
  72. }
  73. public virtual bool HasAttributes {
  74. get {
  75. if (!MoveToFirstAttribute ())
  76. return false;
  77. MoveToParent ();
  78. return true;
  79. }
  80. }
  81. public virtual bool HasChildren {
  82. get {
  83. if (!MoveToFirstChild ())
  84. return false;
  85. MoveToParent ();
  86. return true;
  87. }
  88. }
  89. #else
  90. public abstract bool HasAttributes { get; }
  91. public abstract bool HasChildren { get; }
  92. #endif
  93. public abstract bool IsEmptyElement { get; }
  94. public abstract string LocalName { get; }
  95. public abstract string Name { get; }
  96. public abstract string NamespaceURI { get; }
  97. public abstract XmlNameTable NameTable { get; }
  98. public abstract XPathNodeType NodeType { get; }
  99. public abstract string Prefix { get; }
  100. #if NET_2_0
  101. public virtual string XmlLang {
  102. get {
  103. XPathNavigator nav = Clone ();
  104. switch (nav.NodeType) {
  105. case XPathNodeType.Attribute:
  106. case XPathNodeType.Namespace:
  107. nav.MoveToParent ();
  108. break;
  109. }
  110. do {
  111. if (nav.MoveToAttribute ("lang", "http://www.w3.org/XML/1998/namespace"))
  112. return nav.Value;
  113. } while (nav.MoveToParent ());
  114. return String.Empty;
  115. }
  116. }
  117. #else
  118. public abstract string Value { get; }
  119. public abstract string XmlLang { get; }
  120. #endif
  121. #endregion
  122. #region Methods
  123. public abstract XPathNavigator Clone ();
  124. public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
  125. {
  126. if (IsSamePosition (nav))
  127. return XmlNodeOrder.Same;
  128. // quick check for direct descendant
  129. if (IsDescendant (nav))
  130. return XmlNodeOrder.Before;
  131. // quick check for direct ancestor
  132. if (nav.IsDescendant (this))
  133. return XmlNodeOrder.After;
  134. XPathNavigator nav1 = Clone ();
  135. XPathNavigator nav2 = nav.Clone ();
  136. // check if document instance is the same.
  137. nav1.MoveToRoot ();
  138. nav2.MoveToRoot ();
  139. if (!nav1.IsSamePosition (nav2))
  140. return XmlNodeOrder.Unknown;
  141. nav1.MoveTo (this);
  142. nav2.MoveTo (nav);
  143. int depth1 = 0;
  144. while (nav1.MoveToParent ())
  145. depth1++;
  146. nav1.MoveTo (this);
  147. int depth2 = 0;
  148. while (nav2.MoveToParent ())
  149. depth2++;
  150. nav2.MoveTo (nav);
  151. // find common parent depth
  152. int common = depth1;
  153. for (;common > depth2; common--)
  154. nav1.MoveToParent ();
  155. for (int i = depth2; i > common; i--)
  156. nav2.MoveToParent ();
  157. while (!nav1.IsSamePosition (nav2)) {
  158. nav1.MoveToParent ();
  159. nav2.MoveToParent ();
  160. common--;
  161. }
  162. // For each this and target, move to the node that is
  163. // ancestor of the node and child of the common parent.
  164. nav1.MoveTo (this);
  165. for (int i = depth1; i > common + 1; i--)
  166. nav1.MoveToParent ();
  167. nav2.MoveTo (nav);
  168. for (int i = depth2; i > common + 1; i--)
  169. nav2.MoveToParent ();
  170. // Those children of common parent are comparable.
  171. // namespace nodes precede to attributes, and they
  172. // precede to other nodes.
  173. if (nav1.NodeType == XPathNodeType.Namespace) {
  174. if (nav2.NodeType != XPathNodeType.Namespace)
  175. return XmlNodeOrder.Before;
  176. while (nav1.MoveToNextNamespace ())
  177. if (nav1.IsSamePosition (nav2))
  178. return XmlNodeOrder.Before;
  179. return XmlNodeOrder.After;
  180. }
  181. if (nav2.NodeType == XPathNodeType.Namespace)
  182. return XmlNodeOrder.After;
  183. if (nav1.NodeType == XPathNodeType.Attribute) {
  184. if (nav2.NodeType != XPathNodeType.Attribute)
  185. return XmlNodeOrder.Before;
  186. while (nav1.MoveToNextAttribute ())
  187. if (nav1.IsSamePosition (nav2))
  188. return XmlNodeOrder.Before;
  189. return XmlNodeOrder.After;
  190. }
  191. while (nav1.MoveToNext ())
  192. if (nav1.IsSamePosition (nav2))
  193. return XmlNodeOrder.Before;
  194. return XmlNodeOrder.After;
  195. }
  196. public virtual XPathExpression Compile (string xpath)
  197. {
  198. return XPathExpression.Compile (xpath);
  199. }
  200. internal virtual XPathExpression Compile (string xpath, System.Xml.Xsl.IStaticXsltContext ctx)
  201. {
  202. return XPathExpression.Compile (xpath, null, ctx);
  203. }
  204. public virtual object Evaluate (string xpath)
  205. {
  206. return Evaluate (Compile (xpath));
  207. }
  208. public virtual object Evaluate (XPathExpression expr)
  209. {
  210. return Evaluate (expr, null);
  211. }
  212. public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
  213. {
  214. return Evaluate (expr, context, null);
  215. }
  216. internal virtual object Evaluate (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
  217. {
  218. CompiledExpression cexpr = (CompiledExpression) expr;
  219. if (ctx == null)
  220. ctx = cexpr.NamespaceManager;
  221. if (context == null)
  222. context = new NullIterator (this, ctx);
  223. BaseIterator iterContext = (BaseIterator) context;
  224. iterContext.NamespaceManager = ctx;
  225. return cexpr.Evaluate (iterContext);
  226. }
  227. internal XPathNodeIterator EvaluateNodeSet (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
  228. {
  229. CompiledExpression cexpr = (CompiledExpression) expr;
  230. if (ctx == null)
  231. ctx = cexpr.NamespaceManager;
  232. if (context == null)
  233. context = new NullIterator (this, cexpr.NamespaceManager);
  234. BaseIterator iterContext = (BaseIterator) context;
  235. iterContext.NamespaceManager = ctx;
  236. return cexpr.EvaluateNodeSet (iterContext);
  237. }
  238. internal string EvaluateString (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
  239. {
  240. CompiledExpression cexpr = (CompiledExpression) expr;
  241. if (ctx == null)
  242. ctx = cexpr.NamespaceManager;
  243. if (context == null)
  244. context = new NullIterator (this, cexpr.NamespaceManager);
  245. BaseIterator iterContext = (BaseIterator) context;
  246. iterContext.NamespaceManager = ctx;
  247. return cexpr.EvaluateString (iterContext);
  248. }
  249. internal double EvaluateNumber (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
  250. {
  251. CompiledExpression cexpr = (CompiledExpression) expr;
  252. if (ctx == null)
  253. ctx = cexpr.NamespaceManager;
  254. if (context == null)
  255. context = new NullIterator (this, cexpr.NamespaceManager);
  256. BaseIterator iterContext = (BaseIterator) context;
  257. iterContext.NamespaceManager = ctx;
  258. return cexpr.EvaluateNumber (iterContext);
  259. }
  260. internal bool EvaluateBoolean (XPathExpression expr, XPathNodeIterator context, NSResolver ctx)
  261. {
  262. CompiledExpression cexpr = (CompiledExpression) expr;
  263. if (ctx == null)
  264. ctx = cexpr.NamespaceManager;
  265. if (context == null)
  266. context = new NullIterator (this, cexpr.NamespaceManager);
  267. BaseIterator iterContext = (BaseIterator) context;
  268. iterContext.NamespaceManager = ctx;
  269. return cexpr.EvaluateBoolean (iterContext);
  270. }
  271. #if NET_2_0
  272. public virtual string GetAttribute (string localName, string namespaceURI)
  273. {
  274. if (!MoveToAttribute (localName, namespaceURI))
  275. return String.Empty;
  276. string value = Value;
  277. MoveToParent ();
  278. return value;
  279. }
  280. public virtual string GetNamespace (string name)
  281. {
  282. if (!MoveToNamespace (name))
  283. return String.Empty;
  284. string value = Value;
  285. MoveToParent ();
  286. return value;
  287. }
  288. #else
  289. public abstract string GetAttribute (string localName, string namespaceURI);
  290. public abstract string GetNamespace (string name);
  291. #endif
  292. object ICloneable.Clone ()
  293. {
  294. return Clone ();
  295. }
  296. public virtual bool IsDescendant (XPathNavigator nav)
  297. {
  298. if (nav != null)
  299. {
  300. nav = nav.Clone ();
  301. while (nav.MoveToParent ())
  302. {
  303. if (IsSamePosition (nav))
  304. return true;
  305. }
  306. }
  307. return false;
  308. }
  309. public abstract bool IsSamePosition (XPathNavigator other);
  310. public virtual bool Matches (string xpath)
  311. {
  312. return Matches (Compile (xpath));
  313. }
  314. public virtual bool Matches (XPathExpression expr)
  315. {
  316. Expression e = ((CompiledExpression) expr).ExpressionNode;
  317. if (e is ExprRoot)
  318. return NodeType == XPathNodeType.Root;
  319. NodeTest nt = e as NodeTest;
  320. if (nt != null) {
  321. switch (nt.Axis.Axis) {
  322. case Axes.Child:
  323. case Axes.Attribute:
  324. break;
  325. default:
  326. throw new XPathException ("Only child and attribute pattern are allowed for a pattern.");
  327. }
  328. return nt.Match (((CompiledExpression)expr).NamespaceManager, this);
  329. }
  330. if (e is ExprFilter) {
  331. do {
  332. e = ((ExprFilter) e).LeftHandSide;
  333. } while (e is ExprFilter);
  334. if (e is NodeTest && !((NodeTest) e).Match (((CompiledExpression) expr).NamespaceManager, this))
  335. return false;
  336. }
  337. XPathResultType resultType = e.ReturnType;
  338. switch (resultType) {
  339. case XPathResultType.Any:
  340. case XPathResultType.NodeSet:
  341. break;
  342. default:
  343. return false;
  344. }
  345. switch (e.EvaluatedNodeType) {
  346. case XPathNodeType.Attribute:
  347. case XPathNodeType.Namespace:
  348. if (NodeType != e.EvaluatedNodeType)
  349. return false;
  350. break;
  351. }
  352. XPathNodeIterator nodes;
  353. nodes = this.Select (expr);
  354. while (nodes.MoveNext ()) {
  355. if (IsSamePosition (nodes.Current))
  356. return true;
  357. }
  358. // ancestors might select this node.
  359. XPathNavigator navigator = Clone ();
  360. while (navigator.MoveToParent ()) {
  361. nodes = navigator.Select (expr);
  362. while (nodes.MoveNext ()) {
  363. if (IsSamePosition (nodes.Current))
  364. return true;
  365. }
  366. }
  367. return false;
  368. }
  369. public abstract bool MoveTo (XPathNavigator other);
  370. #if NET_2_0
  371. public virtual bool MoveToAttribute (string localName, string namespaceURI)
  372. {
  373. if (MoveToFirstAttribute ()) {
  374. do {
  375. if (LocalName == localName && NamespaceURI == namespaceURI)
  376. return true;
  377. } while (MoveToNextAttribute ());
  378. MoveToParent ();
  379. }
  380. return false;
  381. }
  382. public virtual bool MoveToNamespace (string name)
  383. {
  384. if (MoveToFirstNamespace ()) {
  385. do {
  386. if (LocalName == name)
  387. return true;
  388. } while (MoveToNextNamespace ());
  389. MoveToParent ();
  390. }
  391. return false;
  392. }
  393. /*
  394. public virtual bool MoveToFirst ()
  395. {
  396. if (MoveToPrevious ()) {
  397. // It would be able to invoke MoveToPrevious() until the end, but this way would be much faster
  398. MoveToParent ();
  399. MoveToFirstChild ();
  400. return true;
  401. }
  402. return false;
  403. }
  404. */
  405. public virtual bool MoveToFirst ()
  406. {
  407. return MoveToFirstImpl ();
  408. }
  409. public virtual void MoveToRoot ()
  410. {
  411. while (MoveToParent ())
  412. ;
  413. }
  414. #else
  415. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  416. public abstract bool MoveToNamespace (string name);
  417. public abstract bool MoveToFirst ();
  418. public abstract void MoveToRoot ();
  419. #endif
  420. internal bool MoveToFirstImpl ()
  421. {
  422. switch (NodeType) {
  423. case XPathNodeType.Attribute:
  424. case XPathNodeType.Namespace:
  425. return false;
  426. default:
  427. if (!MoveToParent ())
  428. return false;
  429. // Follow these 2 steps so that we can skip
  430. // some types of nodes .
  431. MoveToFirstChild ();
  432. return true;
  433. }
  434. }
  435. public abstract bool MoveToFirstAttribute ();
  436. public abstract bool MoveToFirstChild ();
  437. public bool MoveToFirstNamespace ()
  438. {
  439. return MoveToFirstNamespace (XPathNamespaceScope.All);
  440. }
  441. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  442. public abstract bool MoveToId (string id);
  443. public abstract bool MoveToNext ();
  444. public abstract bool MoveToNextAttribute ();
  445. public bool MoveToNextNamespace ()
  446. {
  447. return MoveToNextNamespace (XPathNamespaceScope.All);
  448. }
  449. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  450. public abstract bool MoveToParent ();
  451. public abstract bool MoveToPrevious ();
  452. public virtual XPathNodeIterator Select (string xpath)
  453. {
  454. return Select (Compile (xpath));
  455. }
  456. public virtual XPathNodeIterator Select (XPathExpression expr)
  457. {
  458. return Select (expr, null);
  459. }
  460. internal virtual XPathNodeIterator Select (XPathExpression expr, NSResolver ctx)
  461. {
  462. CompiledExpression cexpr = (CompiledExpression) expr;
  463. if (ctx == null)
  464. ctx = cexpr.NamespaceManager;
  465. BaseIterator iter = new NullIterator (this, ctx);
  466. return cexpr.EvaluateNodeSet (iter);
  467. }
  468. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  469. {
  470. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  471. return SelectTest (new NodeTypeTest (axis, type));
  472. }
  473. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  474. {
  475. if (name == null)
  476. throw new ArgumentNullException ("name");
  477. if (namespaceURI == null)
  478. throw new ArgumentNullException ("namespaceURI");
  479. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  480. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  481. return SelectTest (new NodeNameTest (axis, qname, true));
  482. }
  483. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  484. {
  485. return SelectTest (new NodeTypeTest (Axes.Child, type));
  486. }
  487. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  488. {
  489. if (name == null)
  490. throw new ArgumentNullException ("name");
  491. if (namespaceURI == null)
  492. throw new ArgumentNullException ("namespaceURI");
  493. Axes axis = Axes.Child;
  494. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  495. return SelectTest (new NodeNameTest (axis, qname, true));
  496. }
  497. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  498. {
  499. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  500. return SelectTest (new NodeTypeTest (axis, type));
  501. }
  502. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  503. {
  504. if (name == null)
  505. throw new ArgumentNullException ("name");
  506. if (namespaceURI == null)
  507. throw new ArgumentNullException ("namespaceURI");
  508. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  509. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  510. return SelectTest (new NodeNameTest (axis, qname, true));
  511. }
  512. internal XPathNodeIterator SelectTest (NodeTest test)
  513. {
  514. return test.EvaluateNodeSet (new NullIterator (this));
  515. }
  516. public override string ToString ()
  517. {
  518. return Value;
  519. }
  520. #endregion
  521. #if NET_2_0
  522. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler)
  523. {
  524. XmlReaderSettings settings = new XmlReaderSettings ();
  525. settings.NameTable = NameTable;
  526. settings.SetSchemas (schemas);
  527. settings.ValidationEventHandler += handler;
  528. settings.ValidationType = ValidationType.Schema;
  529. try {
  530. XmlReader r = XmlReader.Create (
  531. ReadSubtree (), settings);
  532. while (!r.EOF)
  533. r.Read ();
  534. } catch (XmlSchemaValidationException) {
  535. return false;
  536. }
  537. return true;
  538. }
  539. public virtual XPathNavigator CreateNavigator ()
  540. {
  541. return Clone ();
  542. }
  543. [MonoTODO]
  544. public virtual object Evaluate (string xpath, IXmlNamespaceResolver nsResolver)
  545. {
  546. return Evaluate (Compile (xpath), null, nsResolver);
  547. }
  548. [MonoTODO]
  549. public virtual IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
  550. {
  551. IDictionary<string, string> table = new Dictionary<string, string> ();
  552. XPathNamespaceScope xpscope =
  553. scope == XmlNamespaceScope.Local ?
  554. XPathNamespaceScope.Local :
  555. scope == XmlNamespaceScope.ExcludeXml ?
  556. XPathNamespaceScope.ExcludeXml :
  557. XPathNamespaceScope.All;
  558. XPathNavigator nav = Clone ();
  559. if (nav.NodeType != XPathNodeType.Element)
  560. nav.MoveToParent ();
  561. if (!nav.MoveToFirstNamespace (xpscope))
  562. return table;
  563. do {
  564. table.Add (nav.Name, nav.Value);
  565. } while (nav.MoveToNextNamespace (xpscope));
  566. return table;
  567. }
  568. public virtual string LookupNamespace (string prefix)
  569. {
  570. XPathNavigator nav = Clone ();
  571. if (nav.NodeType != XPathNodeType.Element)
  572. nav.MoveToParent ();
  573. if (nav.MoveToNamespace (prefix))
  574. return nav.Value;
  575. return null;
  576. }
  577. public virtual string LookupPrefix (string namespaceUri)
  578. {
  579. XPathNavigator nav = Clone ();
  580. if (nav.NodeType != XPathNodeType.Element)
  581. nav.MoveToParent ();
  582. if (!nav.MoveToFirstNamespace ())
  583. return null;
  584. do {
  585. if (nav.Value == namespaceUri)
  586. return nav.Name;
  587. } while (nav.MoveToNextNamespace ());
  588. return null;
  589. }
  590. private bool MoveTo (XPathNodeIterator iter)
  591. {
  592. if (iter.MoveNext ()) {
  593. MoveTo (iter.Current);
  594. return true;
  595. }
  596. else
  597. return false;
  598. }
  599. public virtual bool MoveToChild (XPathNodeType type)
  600. {
  601. return MoveTo (SelectChildren (type));
  602. }
  603. public virtual bool MoveToChild (string localName, string namespaceURI)
  604. {
  605. return MoveTo (SelectChildren (localName, namespaceURI));
  606. }
  607. public virtual bool MoveToNext (string localName, string namespaceURI)
  608. {
  609. XPathNavigator nav = Clone ();
  610. while (nav.MoveToNext ()) {
  611. if (nav.LocalName == localName &&
  612. nav.NamespaceURI == namespaceURI) {
  613. MoveTo (nav);
  614. return true;
  615. }
  616. }
  617. return false;
  618. }
  619. public virtual bool MoveToNext (XPathNodeType type)
  620. {
  621. XPathNavigator nav = Clone ();
  622. while (nav.MoveToNext ()) {
  623. if (nav.NodeType == type) {
  624. MoveTo (nav);
  625. return true;
  626. }
  627. }
  628. return false;
  629. }
  630. public virtual bool MoveToFollowing (string localName,
  631. string namespaceURI)
  632. {
  633. return MoveToFollowing (localName, namespaceURI, null);
  634. }
  635. public virtual bool MoveToFollowing (string localName,
  636. string namespaceURI, XPathNavigator end)
  637. {
  638. if (localName == null)
  639. throw new ArgumentNullException ("localName");
  640. if (namespaceURI == null)
  641. throw new ArgumentNullException ("namespaceURI");
  642. localName = NameTable.Get (localName);
  643. if (localName == null)
  644. return false;
  645. namespaceURI = NameTable.Get (namespaceURI);
  646. if (namespaceURI == null)
  647. return false;
  648. XPathNavigator nav = Clone ();
  649. switch (nav.NodeType) {
  650. case XPathNodeType.Attribute:
  651. case XPathNodeType.Namespace:
  652. nav.MoveToParent ();
  653. break;
  654. }
  655. do {
  656. if (!nav.MoveToFirstChild ()) {
  657. do {
  658. if (!nav.MoveToNext ()) {
  659. if (!nav.MoveToParent ())
  660. return false;
  661. }
  662. else
  663. break;
  664. } while (true);
  665. }
  666. if (end != null && end.IsSamePosition (nav))
  667. return false;
  668. if (object.ReferenceEquals (localName, nav.LocalName) &&
  669. object.ReferenceEquals (namespaceURI, nav.NamespaceURI)) {
  670. MoveTo (nav);
  671. return true;
  672. }
  673. } while (true);
  674. }
  675. public virtual bool MoveToFollowing (XPathNodeType type)
  676. {
  677. return MoveToFollowing (type, null);
  678. }
  679. public virtual bool MoveToFollowing (XPathNodeType type,
  680. XPathNavigator end)
  681. {
  682. if (type == XPathNodeType.Root)
  683. return false; // will never match
  684. XPathNavigator nav = Clone ();
  685. switch (nav.NodeType) {
  686. case XPathNodeType.Attribute:
  687. case XPathNodeType.Namespace:
  688. nav.MoveToParent ();
  689. break;
  690. }
  691. do {
  692. if (!nav.MoveToFirstChild ()) {
  693. do {
  694. if (!nav.MoveToNext ()) {
  695. if (!nav.MoveToParent ())
  696. return false;
  697. }
  698. else
  699. break;
  700. } while (true);
  701. }
  702. if (end != null && end.IsSamePosition (nav))
  703. return false;
  704. if (nav.NodeType == type) {
  705. MoveTo (nav);
  706. return true;
  707. }
  708. } while (true);
  709. }
  710. public virtual XmlReader ReadSubtree ()
  711. {
  712. return new XPathNavigatorReader (this);
  713. }
  714. public virtual XPathNodeIterator Select (string xpath, IXmlNamespaceResolver nsResolver)
  715. {
  716. return Select (Compile (xpath), nsResolver);
  717. }
  718. public virtual XPathNavigator SelectSingleNode (string xpath)
  719. {
  720. return SelectSingleNode (xpath, null);
  721. }
  722. public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
  723. {
  724. XPathExpression expr = Compile (xpath);
  725. expr.SetContext (nsResolver);
  726. return SelectSingleNode (expr);
  727. }
  728. public virtual XPathNavigator SelectSingleNode (XPathExpression expression)
  729. {
  730. XPathNodeIterator iter = Select (expression);
  731. if (iter.MoveNext ())
  732. return iter.Current;
  733. else
  734. return null;
  735. }
  736. [MonoTODO]
  737. public override object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
  738. {
  739. throw new NotImplementedException ();
  740. }
  741. [MonoTODO]
  742. public virtual void WriteSubtree (XmlWriter writer)
  743. {
  744. XmlReader st = ReadSubtree ();
  745. writer.WriteNode (st, false);
  746. }
  747. [MonoTODO]
  748. public virtual string InnerXml {
  749. get {
  750. XmlReader r = ReadSubtree ();
  751. r.Read (); // start
  752. // skip the element itself (or will reach to
  753. // EOF if other than element) unless writing
  754. // doc itself
  755. int depth = r.Depth;
  756. if (NodeType != XPathNodeType.Root)
  757. r.Read ();
  758. StringWriter sw = new StringWriter ();
  759. XmlWriter xtw = XmlWriter.Create (sw);
  760. while (!r.EOF && r.Depth > depth)
  761. xtw.WriteNode (r, false);
  762. return sw.ToString ();
  763. }
  764. set {
  765. DeleteChildren ();
  766. if (NodeType == XPathNodeType.Attribute) {
  767. SetValue (value);
  768. return;
  769. }
  770. AppendChild (value);
  771. }
  772. }
  773. public override bool IsNode {
  774. get { return true; }
  775. }
  776. [MonoTODO]
  777. public virtual string OuterXml {
  778. get {
  779. StringWriter sw = new StringWriter ();
  780. XmlTextWriter xtw = new XmlTextWriter (sw);
  781. WriteSubtree (xtw);
  782. xtw.Close ();
  783. return sw.ToString ();
  784. }
  785. set {
  786. switch (NodeType) {
  787. case XPathNodeType.Root:
  788. case XPathNodeType.Attribute:
  789. case XPathNodeType.Namespace:
  790. throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
  791. }
  792. DeleteSelf ();
  793. AppendChild (value);
  794. MoveToFirstChild ();
  795. }
  796. }
  797. [MonoTODO]
  798. public virtual IXmlSchemaInfo SchemaInfo {
  799. get {
  800. return null;
  801. }
  802. }
  803. [MonoTODO]
  804. public override object TypedValue {
  805. get {
  806. switch (NodeType) {
  807. case XPathNodeType.Element:
  808. case XPathNodeType.Attribute:
  809. if (XmlType == null)
  810. break;
  811. XmlSchemaDatatype dt = XmlType.Datatype;
  812. if (dt == null)
  813. break;
  814. return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
  815. }
  816. return Value;
  817. }
  818. }
  819. public virtual object UnderlyingObject {
  820. get { return null; }
  821. }
  822. public override bool ValueAsBoolean {
  823. get { return XQueryConvert.StringToBoolean (Value); }
  824. }
  825. public override DateTime ValueAsDateTime {
  826. get { return XmlConvert.ToDateTime (Value); }
  827. }
  828. public override double ValueAsDouble {
  829. get { return XQueryConvert.StringToDouble (Value); }
  830. }
  831. public override int ValueAsInt {
  832. get { return XQueryConvert.StringToInt (Value); }
  833. }
  834. public override long ValueAsLong {
  835. get { return XQueryConvert.StringToInteger (Value); }
  836. }
  837. public override Type ValueType {
  838. get {
  839. return SchemaInfo != null &&
  840. SchemaInfo.SchemaType != null &&
  841. SchemaInfo.SchemaType.Datatype != null ?
  842. SchemaInfo.SchemaType.Datatype.ValueType
  843. : null;
  844. }
  845. }
  846. [MonoTODO]
  847. public override XmlSchemaType XmlType {
  848. get {
  849. if (SchemaInfo != null)
  850. return SchemaInfo.SchemaType;
  851. return null;
  852. }
  853. }
  854. private XmlReader CreateFragmentReader (string fragment)
  855. {
  856. XmlReaderSettings settings = new XmlReaderSettings ();
  857. settings.ConformanceLevel = ConformanceLevel.Fragment;
  858. XmlNamespaceManager nsmgr = new XmlNamespaceManager (NameTable);
  859. foreach (KeyValuePair<string,string> nss in GetNamespacesInScope (XmlNamespaceScope.All))
  860. nsmgr.AddNamespace (nss.Key, nss.Value);
  861. return XmlReader.Create (
  862. new StringReader (fragment),
  863. settings,
  864. new XmlParserContext (NameTable, nsmgr, null, XmlSpace.None));
  865. }
  866. // must override it.
  867. public virtual XmlWriter AppendChild ()
  868. {
  869. throw new NotSupportedException ();
  870. }
  871. public virtual void AppendChild (
  872. string xmlFragments)
  873. {
  874. AppendChild (CreateFragmentReader (xmlFragments));
  875. }
  876. public virtual void AppendChild (
  877. XmlReader reader)
  878. {
  879. XmlWriter w = AppendChild ();
  880. while (!reader.EOF)
  881. w.WriteNode (reader, false);
  882. w.Close ();
  883. }
  884. [MonoTODO]
  885. public virtual void AppendChild (
  886. XPathNavigator nav)
  887. {
  888. AppendChild (new XPathNavigatorReader (nav));
  889. }
  890. public virtual void AppendChildElement (string prefix, string name, string ns, string value)
  891. {
  892. XmlWriter xw = AppendChild ();
  893. xw.WriteStartElement (prefix, name, ns);
  894. xw.WriteString (value);
  895. xw.WriteEndElement ();
  896. xw.Close ();
  897. }
  898. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  899. {
  900. using (XmlWriter w = CreateAttributes ()) {
  901. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  902. }
  903. }
  904. // must override it.
  905. [MonoTODO ("needs tests")]
  906. public virtual XmlWriter CreateAttributes ()
  907. {
  908. throw new NotSupportedException ();
  909. }
  910. // must override it.
  911. public virtual void DeleteSelf ()
  912. {
  913. throw new NotSupportedException ();
  914. }
  915. // must override it.
  916. public virtual void DeleteRange (XPathNavigator nav)
  917. {
  918. throw new NotSupportedException ();
  919. }
  920. public virtual XmlWriter ReplaceRange (XPathNavigator nav)
  921. {
  922. throw new NotSupportedException ();
  923. }
  924. public virtual XmlWriter InsertAfter ()
  925. {
  926. switch (NodeType) {
  927. case XPathNodeType.Root:
  928. case XPathNodeType.Attribute:
  929. case XPathNodeType.Namespace:
  930. throw new InvalidOperationException (String.Format ("Insertion after {0} is not allowed.", NodeType));
  931. }
  932. XPathNavigator nav = Clone ();
  933. if (nav.MoveToNext ())
  934. return nav.InsertBefore ();
  935. else if (nav.MoveToParent ())
  936. return nav.AppendChild ();
  937. else
  938. throw new InvalidOperationException ("Could not move to parent to insert sibling node");
  939. }
  940. public virtual void InsertAfter (string xmlFragments)
  941. {
  942. InsertAfter (CreateFragmentReader (xmlFragments));
  943. }
  944. public virtual void InsertAfter (XmlReader reader)
  945. {
  946. using (XmlWriter w = InsertAfter ()) {
  947. w.WriteNode (reader, false);
  948. }
  949. }
  950. [MonoTODO]
  951. public virtual void InsertAfter (XPathNavigator nav)
  952. {
  953. InsertAfter (new XPathNavigatorReader (nav));
  954. }
  955. public virtual XmlWriter InsertBefore ()
  956. {
  957. throw new NotSupportedException ();
  958. }
  959. public virtual void InsertBefore (string xmlFragments)
  960. {
  961. InsertBefore (CreateFragmentReader (xmlFragments));
  962. }
  963. public virtual void InsertBefore (XmlReader reader)
  964. {
  965. using (XmlWriter w = InsertBefore ()) {
  966. w.WriteNode (reader, false);
  967. }
  968. }
  969. [MonoTODO]
  970. public virtual void InsertBefore (XPathNavigator nav)
  971. {
  972. InsertBefore (new XPathNavigatorReader (nav));
  973. }
  974. public virtual void InsertElementAfter (string prefix,
  975. string localName, string namespaceURI, string value)
  976. {
  977. using (XmlWriter w = InsertAfter ()) {
  978. w.WriteElementString (prefix, localName, namespaceURI, value);
  979. }
  980. }
  981. public virtual void InsertElementBefore (string prefix,
  982. string localName, string namespaceURI, string value)
  983. {
  984. using (XmlWriter w = InsertBefore ()) {
  985. w.WriteElementString (prefix, localName, namespaceURI, value);
  986. }
  987. }
  988. public virtual XmlWriter PrependChild ()
  989. {
  990. XPathNavigator nav = Clone ();
  991. if (nav.MoveToFirstChild ())
  992. return nav.InsertBefore ();
  993. else
  994. return AppendChild ();
  995. }
  996. public virtual void PrependChild (string xmlFragments)
  997. {
  998. PrependChild (CreateFragmentReader (xmlFragments));
  999. }
  1000. public virtual void PrependChild (XmlReader reader)
  1001. {
  1002. using (XmlWriter w = PrependChild ()) {
  1003. w.WriteNode (reader, false);
  1004. }
  1005. }
  1006. [MonoTODO]
  1007. public virtual void PrependChild (XPathNavigator nav)
  1008. {
  1009. PrependChild (new XPathNavigatorReader (nav));
  1010. }
  1011. public virtual void PrependChildElement (string prefix,
  1012. string localName, string namespaceURI, string value)
  1013. {
  1014. using (XmlWriter w = PrependChild ()) {
  1015. w.WriteElementString (prefix, localName, namespaceURI, value);
  1016. }
  1017. }
  1018. public virtual void ReplaceSelf (string xmlFragment)
  1019. {
  1020. ReplaceSelf (CreateFragmentReader (xmlFragment));
  1021. }
  1022. // must override it.
  1023. public virtual void ReplaceSelf (XmlReader reader)
  1024. {
  1025. throw new NotSupportedException ();
  1026. }
  1027. [MonoTODO]
  1028. public virtual void ReplaceSelf (XPathNavigator navigator)
  1029. {
  1030. ReplaceSelf (new XPathNavigatorReader (navigator));
  1031. }
  1032. // Dunno the exact purpose, but maybe internal editor use
  1033. [MonoTODO]
  1034. public virtual void SetTypedValue (object value)
  1035. {
  1036. throw new NotSupportedException ();
  1037. }
  1038. public virtual void SetValue (string value)
  1039. {
  1040. throw new NotSupportedException ();
  1041. }
  1042. [MonoTODO]
  1043. private void DeleteChildren ()
  1044. {
  1045. switch (NodeType) {
  1046. case XPathNodeType.Namespace:
  1047. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  1048. case XPathNodeType.Attribute:
  1049. return;
  1050. case XPathNodeType.Text:
  1051. case XPathNodeType.SignificantWhitespace:
  1052. case XPathNodeType.Whitespace:
  1053. case XPathNodeType.ProcessingInstruction:
  1054. case XPathNodeType.Comment:
  1055. DeleteSelf ();
  1056. return;
  1057. }
  1058. if (!HasChildren)
  1059. return;
  1060. XPathNavigator nav = Clone ();
  1061. nav.MoveToFirstChild ();
  1062. while (!nav.IsSamePosition (this))
  1063. nav.DeleteSelf ();
  1064. }
  1065. #endif
  1066. }
  1067. }