XPathNavigator.cs 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. public virtual void WriteSubtree (XmlWriter writer)
  742. {
  743. writer.WriteNode (this, false);
  744. }
  745. [MonoTODO]
  746. public virtual string InnerXml {
  747. get {
  748. XmlReader r = ReadSubtree ();
  749. r.Read (); // start
  750. // skip the element itself (or will reach to
  751. // EOF if other than element) unless writing
  752. // doc itself
  753. int depth = r.Depth;
  754. if (NodeType != XPathNodeType.Root)
  755. r.Read ();
  756. StringWriter sw = new StringWriter ();
  757. XmlWriter xtw = XmlWriter.Create (sw);
  758. while (!r.EOF && r.Depth > depth)
  759. xtw.WriteNode (r, false);
  760. return sw.ToString ();
  761. }
  762. set {
  763. DeleteChildren ();
  764. if (NodeType == XPathNodeType.Attribute) {
  765. SetValue (value);
  766. return;
  767. }
  768. AppendChild (value);
  769. }
  770. }
  771. public override sealed 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. public virtual object UnderlyingObject {
  818. get { return null; }
  819. }
  820. public override bool ValueAsBoolean {
  821. get { return XQueryConvert.StringToBoolean (Value); }
  822. }
  823. public override DateTime ValueAsDateTime {
  824. get { return XmlConvert.ToDateTime (Value); }
  825. }
  826. public override double ValueAsDouble {
  827. get { return XQueryConvert.StringToDouble (Value); }
  828. }
  829. public override int ValueAsInt {
  830. get { return XQueryConvert.StringToInt (Value); }
  831. }
  832. public override long ValueAsLong {
  833. get { return XQueryConvert.StringToInteger (Value); }
  834. }
  835. public override Type ValueType {
  836. get {
  837. return SchemaInfo != null &&
  838. SchemaInfo.SchemaType != null &&
  839. SchemaInfo.SchemaType.Datatype != null ?
  840. SchemaInfo.SchemaType.Datatype.ValueType
  841. : null;
  842. }
  843. }
  844. [MonoTODO]
  845. public override XmlSchemaType XmlType {
  846. get {
  847. if (SchemaInfo != null)
  848. return SchemaInfo.SchemaType;
  849. return null;
  850. }
  851. }
  852. private XmlReader CreateFragmentReader (string fragment)
  853. {
  854. XmlReaderSettings settings = new XmlReaderSettings ();
  855. settings.ConformanceLevel = ConformanceLevel.Fragment;
  856. XmlNamespaceManager nsmgr = new XmlNamespaceManager (NameTable);
  857. foreach (KeyValuePair<string,string> nss in GetNamespacesInScope (XmlNamespaceScope.All))
  858. nsmgr.AddNamespace (nss.Key, nss.Value);
  859. return XmlReader.Create (
  860. new StringReader (fragment),
  861. settings,
  862. new XmlParserContext (NameTable, nsmgr, null, XmlSpace.None));
  863. }
  864. // must override it.
  865. public virtual XmlWriter AppendChild ()
  866. {
  867. throw new NotSupportedException ();
  868. }
  869. public virtual void AppendChild (
  870. string xmlFragments)
  871. {
  872. AppendChild (CreateFragmentReader (xmlFragments));
  873. }
  874. public virtual void AppendChild (
  875. XmlReader reader)
  876. {
  877. XmlWriter w = AppendChild ();
  878. while (!reader.EOF)
  879. w.WriteNode (reader, false);
  880. w.Close ();
  881. }
  882. [MonoTODO]
  883. public virtual void AppendChild (
  884. XPathNavigator nav)
  885. {
  886. AppendChild (new XPathNavigatorReader (nav));
  887. }
  888. public virtual void AppendChildElement (string prefix, string name, string ns, string value)
  889. {
  890. XmlWriter xw = AppendChild ();
  891. xw.WriteStartElement (prefix, name, ns);
  892. xw.WriteString (value);
  893. xw.WriteEndElement ();
  894. xw.Close ();
  895. }
  896. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  897. {
  898. using (XmlWriter w = CreateAttributes ()) {
  899. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  900. }
  901. }
  902. // must override it.
  903. [MonoTODO ("needs tests")]
  904. public virtual XmlWriter CreateAttributes ()
  905. {
  906. throw new NotSupportedException ();
  907. }
  908. // must override it.
  909. public virtual void DeleteSelf ()
  910. {
  911. throw new NotSupportedException ();
  912. }
  913. // must override it.
  914. public virtual void DeleteRange (XPathNavigator nav)
  915. {
  916. throw new NotSupportedException ();
  917. }
  918. public virtual XmlWriter ReplaceRange (XPathNavigator nav)
  919. {
  920. throw new NotSupportedException ();
  921. }
  922. public virtual XmlWriter InsertAfter ()
  923. {
  924. switch (NodeType) {
  925. case XPathNodeType.Root:
  926. case XPathNodeType.Attribute:
  927. case XPathNodeType.Namespace:
  928. throw new InvalidOperationException (String.Format ("Insertion after {0} is not allowed.", NodeType));
  929. }
  930. XPathNavigator nav = Clone ();
  931. if (nav.MoveToNext ())
  932. return nav.InsertBefore ();
  933. else if (nav.MoveToParent ())
  934. return nav.AppendChild ();
  935. else
  936. throw new InvalidOperationException ("Could not move to parent to insert sibling node");
  937. }
  938. public virtual void InsertAfter (string xmlFragments)
  939. {
  940. InsertAfter (CreateFragmentReader (xmlFragments));
  941. }
  942. public virtual void InsertAfter (XmlReader reader)
  943. {
  944. using (XmlWriter w = InsertAfter ()) {
  945. w.WriteNode (reader, false);
  946. }
  947. }
  948. [MonoTODO]
  949. public virtual void InsertAfter (XPathNavigator nav)
  950. {
  951. InsertAfter (new XPathNavigatorReader (nav));
  952. }
  953. public virtual XmlWriter InsertBefore ()
  954. {
  955. throw new NotSupportedException ();
  956. }
  957. public virtual void InsertBefore (string xmlFragments)
  958. {
  959. InsertBefore (CreateFragmentReader (xmlFragments));
  960. }
  961. public virtual void InsertBefore (XmlReader reader)
  962. {
  963. using (XmlWriter w = InsertBefore ()) {
  964. w.WriteNode (reader, false);
  965. }
  966. }
  967. [MonoTODO]
  968. public virtual void InsertBefore (XPathNavigator nav)
  969. {
  970. InsertBefore (new XPathNavigatorReader (nav));
  971. }
  972. public virtual void InsertElementAfter (string prefix,
  973. string localName, string namespaceURI, string value)
  974. {
  975. using (XmlWriter w = InsertAfter ()) {
  976. w.WriteElementString (prefix, localName, namespaceURI, value);
  977. }
  978. }
  979. public virtual void InsertElementBefore (string prefix,
  980. string localName, string namespaceURI, string value)
  981. {
  982. using (XmlWriter w = InsertBefore ()) {
  983. w.WriteElementString (prefix, localName, namespaceURI, value);
  984. }
  985. }
  986. public virtual XmlWriter PrependChild ()
  987. {
  988. XPathNavigator nav = Clone ();
  989. if (nav.MoveToFirstChild ())
  990. return nav.InsertBefore ();
  991. else
  992. return AppendChild ();
  993. }
  994. public virtual void PrependChild (string xmlFragments)
  995. {
  996. PrependChild (CreateFragmentReader (xmlFragments));
  997. }
  998. public virtual void PrependChild (XmlReader reader)
  999. {
  1000. using (XmlWriter w = PrependChild ()) {
  1001. w.WriteNode (reader, false);
  1002. }
  1003. }
  1004. [MonoTODO]
  1005. public virtual void PrependChild (XPathNavigator nav)
  1006. {
  1007. PrependChild (new XPathNavigatorReader (nav));
  1008. }
  1009. public virtual void PrependChildElement (string prefix,
  1010. string localName, string namespaceURI, string value)
  1011. {
  1012. using (XmlWriter w = PrependChild ()) {
  1013. w.WriteElementString (prefix, localName, namespaceURI, value);
  1014. }
  1015. }
  1016. public virtual void ReplaceSelf (string xmlFragment)
  1017. {
  1018. ReplaceSelf (CreateFragmentReader (xmlFragment));
  1019. }
  1020. // must override it.
  1021. public virtual void ReplaceSelf (XmlReader reader)
  1022. {
  1023. throw new NotSupportedException ();
  1024. }
  1025. [MonoTODO]
  1026. public virtual void ReplaceSelf (XPathNavigator navigator)
  1027. {
  1028. ReplaceSelf (new XPathNavigatorReader (navigator));
  1029. }
  1030. // Dunno the exact purpose, but maybe internal editor use
  1031. [MonoTODO]
  1032. public virtual void SetTypedValue (object value)
  1033. {
  1034. throw new NotSupportedException ();
  1035. }
  1036. public virtual void SetValue (string value)
  1037. {
  1038. throw new NotSupportedException ();
  1039. }
  1040. [MonoTODO]
  1041. private void DeleteChildren ()
  1042. {
  1043. switch (NodeType) {
  1044. case XPathNodeType.Namespace:
  1045. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  1046. case XPathNodeType.Attribute:
  1047. return;
  1048. case XPathNodeType.Text:
  1049. case XPathNodeType.SignificantWhitespace:
  1050. case XPathNodeType.Whitespace:
  1051. case XPathNodeType.ProcessingInstruction:
  1052. case XPathNodeType.Comment:
  1053. DeleteSelf ();
  1054. return;
  1055. }
  1056. if (!HasChildren)
  1057. return;
  1058. XPathNavigator nav = Clone ();
  1059. nav.MoveToFirstChild ();
  1060. while (!nav.IsSamePosition (this))
  1061. nav.DeleteSelf ();
  1062. }
  1063. #endif
  1064. }
  1065. }