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