XPathNavigator.cs 29 KB

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