XPathNavigator.cs 29 KB

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