XPathNavigator.cs 29 KB

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