XPathNavigator.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. bool MoveToDescendant (XPathNodeType type)
  608. {
  609. return MoveTo (SelectDescendants (type, false));
  610. }
  611. bool MoveToDescendant (string localName, string namespaceURI)
  612. {
  613. return MoveTo (SelectDescendants (localName, namespaceURI, false));
  614. }
  615. public virtual bool MoveToNext (string localName, string namespaceURI)
  616. {
  617. XPathNavigator nav = Clone ();
  618. while (nav.MoveToNext ()) {
  619. if (nav.LocalName == localName &&
  620. nav.NamespaceURI == namespaceURI) {
  621. MoveTo (nav);
  622. return true;
  623. }
  624. }
  625. return false;
  626. }
  627. public virtual bool MoveToNext (XPathNodeType type)
  628. {
  629. XPathNavigator nav = Clone ();
  630. while (nav.MoveToNext ()) {
  631. if (nav.NodeType == type) {
  632. MoveTo (nav);
  633. return true;
  634. }
  635. }
  636. return false;
  637. }
  638. [MonoTODO]
  639. public virtual bool MoveToFollowing (string localName,
  640. string namespaceURI)
  641. {
  642. return MoveToFollowing (localName, namespaceURI, null);
  643. }
  644. [MonoTODO]
  645. public virtual bool MoveToFollowing (string localName,
  646. string namespaceURI, XPathNavigator end)
  647. {
  648. XPathNavigator nav = Clone ();
  649. bool skip = false;
  650. do {
  651. if (!skip && nav.MoveToDescendant (localName,
  652. namespaceURI)) {
  653. if (end != null) {
  654. switch (nav.ComparePosition (end)) {
  655. case XmlNodeOrder.After:
  656. case XmlNodeOrder.Unknown:
  657. return false;
  658. }
  659. }
  660. MoveTo (nav);
  661. return true;
  662. }
  663. else
  664. skip = false;
  665. if (!nav.MoveToNext ()) {
  666. if (!nav.MoveToParent ())
  667. break;
  668. skip = true;
  669. }
  670. } while (true);
  671. return false;
  672. }
  673. [MonoTODO]
  674. public virtual bool MoveToFollowing (XPathNodeType type)
  675. {
  676. return MoveToFollowing (type, null);
  677. }
  678. [MonoTODO]
  679. public virtual bool MoveToFollowing (XPathNodeType type,
  680. XPathNavigator end)
  681. {
  682. XPathNavigator nav = Clone ();
  683. bool skip = false;
  684. do {
  685. if (!skip && nav.MoveToDescendant (type)) {
  686. if (end != null) {
  687. switch (nav.ComparePosition (end)) {
  688. case XmlNodeOrder.After:
  689. case XmlNodeOrder.Unknown:
  690. return false;
  691. }
  692. }
  693. MoveTo (nav);
  694. return true;
  695. }
  696. else
  697. skip = false;
  698. if (!nav.MoveToNext ()) {
  699. if (!nav.MoveToParent ())
  700. break;
  701. skip = true;
  702. }
  703. } while (true);
  704. return false;
  705. }
  706. [MonoTODO]
  707. public virtual XmlReader ReadSubtree ()
  708. {
  709. return new XPathNavigatorReader (this);
  710. }
  711. public virtual XPathNodeIterator Select (string xpath, IXmlNamespaceResolver nsResolver)
  712. {
  713. return Select (Compile (xpath), nsResolver);
  714. }
  715. public virtual XPathNavigator SelectSingleNode (string xpath)
  716. {
  717. return SelectSingleNode (xpath, null);
  718. }
  719. public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
  720. {
  721. XPathExpression expr = Compile (xpath);
  722. expr.SetContext (nsResolver);
  723. return SelectSingleNode (expr);
  724. }
  725. public virtual XPathNavigator SelectSingleNode (XPathExpression expression)
  726. {
  727. XPathNodeIterator iter = Select (expression);
  728. if (iter.MoveNext ())
  729. return iter.Current;
  730. else
  731. return null;
  732. }
  733. [MonoTODO]
  734. public override object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
  735. {
  736. throw new NotImplementedException ();
  737. }
  738. [MonoTODO]
  739. public virtual void WriteSubtree (XmlWriter writer)
  740. {
  741. XmlReader st = ReadSubtree ();
  742. writer.WriteNode (st, false);
  743. }
  744. [MonoTODO]
  745. public virtual string InnerXml {
  746. get {
  747. XmlReader r = ReadSubtree ();
  748. r.Read (); // start
  749. // skip the element itself (or will reach to
  750. // EOF if other than element) unless writing
  751. // doc itself
  752. int depth = r.Depth;
  753. if (NodeType != XPathNodeType.Root)
  754. r.Read ();
  755. StringWriter sw = new StringWriter ();
  756. XmlWriter xtw = XmlWriter.Create (sw);
  757. while (!r.EOF && r.Depth > depth)
  758. xtw.WriteNode (r, false);
  759. return sw.ToString ();
  760. }
  761. set {
  762. DeleteChildren ();
  763. if (NodeType == XPathNodeType.Attribute) {
  764. SetValue (value);
  765. return;
  766. }
  767. AppendChild (value);
  768. }
  769. }
  770. [MonoTODO]
  771. public override bool IsNode {
  772. get { return true; }
  773. }
  774. [MonoTODO]
  775. public virtual string OuterXml {
  776. get {
  777. StringWriter sw = new StringWriter ();
  778. XmlTextWriter xtw = new XmlTextWriter (sw);
  779. WriteSubtree (xtw);
  780. xtw.Close ();
  781. return sw.ToString ();
  782. }
  783. set {
  784. switch (NodeType) {
  785. case XPathNodeType.Root:
  786. case XPathNodeType.Attribute:
  787. case XPathNodeType.Namespace:
  788. throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
  789. }
  790. DeleteSelf ();
  791. AppendChild (value);
  792. MoveToFirstChild ();
  793. }
  794. }
  795. [MonoTODO]
  796. public virtual IXmlSchemaInfo SchemaInfo {
  797. get {
  798. return null;
  799. }
  800. }
  801. [MonoTODO]
  802. public override object TypedValue {
  803. get {
  804. switch (NodeType) {
  805. case XPathNodeType.Element:
  806. case XPathNodeType.Attribute:
  807. if (XmlType == null)
  808. break;
  809. XmlSchemaDatatype dt = XmlType.Datatype;
  810. if (dt == null)
  811. break;
  812. return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
  813. }
  814. return Value;
  815. }
  816. }
  817. [MonoTODO]
  818. public virtual object UnderlyingObject {
  819. get { throw new NotImplementedException (); }
  820. }
  821. [MonoTODO]
  822. public override bool ValueAsBoolean {
  823. get { return XQueryConvert.StringToBoolean (Value); }
  824. }
  825. [MonoTODO]
  826. public override DateTime ValueAsDateTime {
  827. get { return XmlConvert.ToDateTime (Value); }
  828. }
  829. [MonoTODO]
  830. public override double ValueAsDouble {
  831. get { return XQueryConvert.StringToDouble (Value); }
  832. }
  833. [MonoTODO]
  834. public override int ValueAsInt {
  835. get { return XQueryConvert.StringToInt (Value); }
  836. }
  837. [MonoTODO]
  838. public override long ValueAsLong {
  839. get { return XQueryConvert.StringToInteger (Value); }
  840. }
  841. [MonoTODO]
  842. public override Type ValueType {
  843. get {
  844. return SchemaInfo != null &&
  845. SchemaInfo.SchemaType != null &&
  846. SchemaInfo.SchemaType.Datatype != null ?
  847. SchemaInfo.SchemaType.Datatype.ValueType
  848. : null;
  849. }
  850. }
  851. [MonoTODO]
  852. public override XmlSchemaType XmlType {
  853. get {
  854. if (SchemaInfo != null)
  855. return SchemaInfo.SchemaType;
  856. return null;
  857. }
  858. }
  859. private XmlReader CreateFragmentReader (string fragment)
  860. {
  861. return new XmlTextReader (fragment, XmlNodeType.Element, new XmlParserContext (NameTable, null, null, XmlSpace.None));
  862. }
  863. public virtual XmlWriter AppendChild ()
  864. {
  865. throw new NotSupportedException ();
  866. }
  867. [MonoTODO]
  868. public virtual void AppendChild (
  869. string xmlFragments)
  870. {
  871. // FIXME: should XmlParserContext be something?
  872. AppendChild (CreateFragmentReader (xmlFragments));
  873. }
  874. [MonoTODO]
  875. public virtual void AppendChild (
  876. XmlReader reader)
  877. {
  878. XmlWriter w = AppendChild ();
  879. while (!reader.EOF)
  880. w.WriteNode (reader, false);
  881. w.Close ();
  882. }
  883. [MonoTODO]
  884. public virtual void AppendChild (
  885. XPathNavigator nav)
  886. {
  887. AppendChild (new XPathNavigatorReader (nav));
  888. }
  889. public virtual void AppendChildElement (string prefix, string name, string ns, string value)
  890. {
  891. XmlWriter xw = AppendChild ();
  892. xw.WriteStartElement (prefix, name, ns);
  893. xw.WriteString (value);
  894. xw.WriteEndElement ();
  895. xw.Close ();
  896. }
  897. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  898. {
  899. using (XmlWriter w = CreateAttributes ()) {
  900. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  901. }
  902. }
  903. public virtual XmlWriter CreateAttributes ()
  904. {
  905. throw new NotSupportedException ();
  906. }
  907. public virtual void DeleteSelf ()
  908. {
  909. throw new NotSupportedException ();
  910. }
  911. [MonoTODO ("no concrete implementation yet")]
  912. public virtual void DeleteRange (XPathNavigator nav)
  913. {
  914. throw new NotSupportedException ();
  915. }
  916. [MonoTODO ("no concrete implementation yet")]
  917. public virtual XmlWriter ReplaceRange (XPathNavigator nav)
  918. {
  919. throw new NotSupportedException ();
  920. }
  921. public virtual XmlWriter InsertAfter ()
  922. {
  923. XPathNavigator nav = Clone ();
  924. if (nav.MoveToNext ())
  925. return nav.InsertBefore ();
  926. else
  927. return AppendChild ();
  928. }
  929. public virtual void InsertAfter (string xmlFragments)
  930. {
  931. InsertAfter (CreateFragmentReader (xmlFragments));
  932. }
  933. [MonoTODO]
  934. public virtual void InsertAfter (XmlReader reader)
  935. {
  936. using (XmlWriter w = InsertAfter ()) {
  937. w.WriteNode (reader, false);
  938. }
  939. }
  940. [MonoTODO]
  941. public virtual void InsertAfter (XPathNavigator nav)
  942. {
  943. InsertAfter (new XPathNavigatorReader (nav));
  944. }
  945. public virtual XmlWriter InsertBefore ()
  946. {
  947. throw new NotSupportedException ();
  948. }
  949. public virtual void InsertBefore (string xmlFragments)
  950. {
  951. InsertBefore (CreateFragmentReader (xmlFragments));
  952. }
  953. [MonoTODO]
  954. public virtual void InsertBefore (XmlReader reader)
  955. {
  956. using (XmlWriter w = InsertBefore ()) {
  957. w.WriteNode (reader, false);
  958. }
  959. }
  960. [MonoTODO]
  961. public virtual void InsertBefore (XPathNavigator nav)
  962. {
  963. InsertBefore (new XPathNavigatorReader (nav));
  964. }
  965. public virtual void InsertElementAfter (string prefix,
  966. string localName, string namespaceURI, string value)
  967. {
  968. using (XmlWriter w = InsertAfter ()) {
  969. w.WriteElementString (prefix, localName, namespaceURI, value);
  970. }
  971. }
  972. public virtual void InsertElementBefore (string prefix,
  973. string localName, string namespaceURI, string value)
  974. {
  975. using (XmlWriter w = InsertBefore ()) {
  976. w.WriteElementString (prefix, localName, namespaceURI, value);
  977. }
  978. }
  979. public virtual XmlWriter PrependChild ()
  980. {
  981. XPathNavigator nav = Clone ();
  982. if (nav.MoveToFirstChild ())
  983. return nav.InsertBefore ();
  984. else
  985. return InsertBefore ();
  986. }
  987. public virtual void PrependChild (string xmlFragments)
  988. {
  989. PrependChild (CreateFragmentReader (xmlFragments));
  990. }
  991. [MonoTODO]
  992. public virtual void PrependChild (XmlReader reader)
  993. {
  994. using (XmlWriter w = PrependChild ()) {
  995. w.WriteNode (reader, false);
  996. }
  997. }
  998. [MonoTODO]
  999. public virtual void PrependChild (XPathNavigator nav)
  1000. {
  1001. PrependChild (new XPathNavigatorReader (nav));
  1002. }
  1003. public virtual void PrependChildElement (string prefix,
  1004. string localName, string namespaceURI, string value)
  1005. {
  1006. using (XmlWriter w = PrependChild ()) {
  1007. w.WriteElementString (prefix, localName, namespaceURI, value);
  1008. }
  1009. }
  1010. [MonoTODO]
  1011. public virtual void ReplaceSelf (string xmlFragment)
  1012. {
  1013. ReplaceSelf (XmlReader.Create (new StringReader (xmlFragment)));
  1014. }
  1015. [MonoTODO]
  1016. public virtual void ReplaceSelf (XmlReader reader)
  1017. {
  1018. InsertBefore (reader);
  1019. DeleteSelf ();
  1020. }
  1021. [MonoTODO]
  1022. public virtual void ReplaceSelf (XPathNavigator navigator)
  1023. {
  1024. ReplaceSelf (new XPathNavigatorReader (navigator));
  1025. }
  1026. // Dunno the exact purpose, but maybe internal editor use
  1027. [MonoTODO]
  1028. public virtual void SetTypedValue (object value)
  1029. {
  1030. throw new NotSupportedException ();
  1031. }
  1032. public virtual void SetValue (string value)
  1033. {
  1034. throw new NotSupportedException ();
  1035. }
  1036. [MonoTODO]
  1037. private void DeleteChildren ()
  1038. {
  1039. switch (NodeType) {
  1040. case XPathNodeType.Namespace:
  1041. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  1042. case XPathNodeType.Attribute:
  1043. return;
  1044. case XPathNodeType.Text:
  1045. case XPathNodeType.SignificantWhitespace:
  1046. case XPathNodeType.Whitespace:
  1047. case XPathNodeType.ProcessingInstruction:
  1048. case XPathNodeType.Comment:
  1049. DeleteSelf ();
  1050. return;
  1051. }
  1052. if (!HasChildren)
  1053. return;
  1054. XPathNavigator nav = Clone ();
  1055. nav.MoveToFirstChild ();
  1056. while (!nav.IsSamePosition (this))
  1057. nav.DeleteSelf ();
  1058. }
  1059. #endif
  1060. }
  1061. }