XPathNavigator.cs 28 KB

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