XPathNavigator.cs 28 KB

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