XPathNavigator.cs 30 KB

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