XPathNavigator.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 IKeyComparer NavigatorComparer {
  744. get { throw new NotImplementedException (); }
  745. }
  746. [MonoTODO]
  747. public virtual string OuterXml {
  748. get {
  749. StringWriter sw = new StringWriter ();
  750. XmlTextWriter xtw = new XmlTextWriter (sw);
  751. WriteSubtree (xtw);
  752. xtw.Close ();
  753. return sw.ToString ();
  754. }
  755. set {
  756. switch (NodeType) {
  757. case XPathNodeType.Root:
  758. case XPathNodeType.Attribute:
  759. case XPathNodeType.Namespace:
  760. throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
  761. }
  762. DeleteSelf ();
  763. AppendChild (value);
  764. MoveToFirstChild ();
  765. }
  766. }
  767. [MonoTODO]
  768. public virtual IXmlSchemaInfo SchemaInfo {
  769. get {
  770. return null;
  771. }
  772. }
  773. [MonoTODO]
  774. public override object TypedValue {
  775. get {
  776. switch (NodeType) {
  777. case XPathNodeType.Element:
  778. case XPathNodeType.Attribute:
  779. if (XmlType == null)
  780. break;
  781. XmlSchemaDatatype dt = XmlType.Datatype;
  782. if (dt == null)
  783. break;
  784. return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
  785. }
  786. return Value;
  787. }
  788. }
  789. [MonoTODO]
  790. public virtual object UnderlyingObject {
  791. get { throw new NotImplementedException (); }
  792. }
  793. [MonoTODO]
  794. public override bool ValueAsBoolean {
  795. get { return XQueryConvert.StringToBoolean (Value); }
  796. }
  797. [MonoTODO]
  798. public override DateTime ValueAsDateTime {
  799. get { return XmlConvert.ToDateTime (Value); }
  800. }
  801. [MonoTODO]
  802. public override double ValueAsDouble {
  803. get { return XQueryConvert.StringToDouble (Value); }
  804. }
  805. [MonoTODO]
  806. public override int ValueAsInt {
  807. get { return XQueryConvert.StringToInt (Value); }
  808. }
  809. [MonoTODO]
  810. public override long ValueAsLong {
  811. get { return XQueryConvert.StringToInteger (Value); }
  812. }
  813. [MonoTODO]
  814. public override Type ValueType {
  815. get {
  816. return SchemaInfo != null &&
  817. SchemaInfo.SchemaType != null &&
  818. SchemaInfo.SchemaType.Datatype != null ?
  819. SchemaInfo.SchemaType.Datatype.ValueType
  820. : null;
  821. }
  822. }
  823. [MonoTODO]
  824. public override XmlSchemaType XmlType {
  825. get {
  826. if (SchemaInfo != null)
  827. return SchemaInfo.SchemaType;
  828. return null;
  829. }
  830. }
  831. private XmlReader CreateFragmentReader (string fragment)
  832. {
  833. return new XmlTextReader (fragment, XmlNodeType.Element, new XmlParserContext (NameTable, null, null, XmlSpace.None));
  834. }
  835. public virtual XmlWriter AppendChild ()
  836. {
  837. throw new NotSupportedException ();
  838. }
  839. [MonoTODO]
  840. public virtual void AppendChild (
  841. string xmlFragments)
  842. {
  843. // FIXME: should XmlParserContext be something?
  844. AppendChild (CreateFragmentReader (xmlFragments));
  845. }
  846. [MonoTODO]
  847. public virtual void AppendChild (
  848. XmlReader reader)
  849. {
  850. XmlWriter w = AppendChild ();
  851. while (!reader.EOF)
  852. w.WriteNode (reader, false);
  853. w.Close ();
  854. }
  855. [MonoTODO]
  856. public virtual void AppendChild (
  857. XPathNavigator nav)
  858. {
  859. AppendChild (new XPathNavigatorReader (nav));
  860. }
  861. public void AppendChildElement (string prefix, string name, string ns, string value)
  862. {
  863. XmlWriter xw = AppendChild ();
  864. xw.WriteStartElement (prefix, name, ns);
  865. xw.WriteString (value);
  866. xw.WriteEndElement ();
  867. xw.Close ();
  868. }
  869. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  870. {
  871. using (XmlWriter w = CreateAttributes ()) {
  872. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  873. }
  874. }
  875. public virtual XmlWriter CreateAttributes ()
  876. {
  877. throw new NotSupportedException ();
  878. }
  879. public virtual void DeleteSelf ()
  880. {
  881. throw new NotSupportedException ();
  882. }
  883. public virtual XmlWriter InsertAfter ()
  884. {
  885. XPathNavigator nav = Clone ();
  886. if (nav.MoveToNext ())
  887. return nav.InsertBefore ();
  888. else
  889. return AppendChild ();
  890. }
  891. public virtual void InsertAfter (string xmlFragments)
  892. {
  893. InsertAfter (CreateFragmentReader (xmlFragments));
  894. }
  895. [MonoTODO]
  896. public virtual void InsertAfter (XmlReader reader)
  897. {
  898. using (XmlWriter w = InsertAfter ()) {
  899. w.WriteNode (reader, false);
  900. }
  901. }
  902. [MonoTODO]
  903. public virtual void InsertAfter (XPathNavigator nav)
  904. {
  905. InsertAfter (new XPathNavigatorReader (nav));
  906. }
  907. public virtual XmlWriter InsertBefore ()
  908. {
  909. throw new NotSupportedException ();
  910. }
  911. public virtual void InsertBefore (string xmlFragments)
  912. {
  913. InsertBefore (CreateFragmentReader (xmlFragments));
  914. }
  915. [MonoTODO]
  916. public virtual void InsertBefore (XmlReader reader)
  917. {
  918. using (XmlWriter w = InsertBefore ()) {
  919. w.WriteNode (reader, false);
  920. }
  921. }
  922. [MonoTODO]
  923. public virtual void InsertBefore (XPathNavigator nav)
  924. {
  925. InsertBefore (new XPathNavigatorReader (nav));
  926. }
  927. public virtual void InsertElementAfter (string prefix,
  928. string localName, string namespaceURI, string value)
  929. {
  930. using (XmlWriter w = InsertAfter ()) {
  931. w.WriteElementString (prefix, localName, namespaceURI, value);
  932. }
  933. }
  934. public virtual void InsertElementBefore (string prefix,
  935. string localName, string namespaceURI, string value)
  936. {
  937. using (XmlWriter w = InsertBefore ()) {
  938. w.WriteElementString (prefix, localName, namespaceURI, value);
  939. }
  940. }
  941. public virtual XmlWriter PrependChild ()
  942. {
  943. XPathNavigator nav = Clone ();
  944. if (nav.MoveToFirstChild ())
  945. return nav.InsertBefore ();
  946. else
  947. return InsertBefore ();
  948. }
  949. public virtual void PrependChild (string xmlFragments)
  950. {
  951. PrependChild (CreateFragmentReader (xmlFragments));
  952. }
  953. [MonoTODO]
  954. public virtual void PrependChild (XmlReader reader)
  955. {
  956. using (XmlWriter w = PrependChild ()) {
  957. w.WriteNode (reader, false);
  958. }
  959. }
  960. [MonoTODO]
  961. public virtual void PrependChild (XPathNavigator nav)
  962. {
  963. PrependChild (new XPathNavigatorReader (nav));
  964. }
  965. public virtual void PrependChildElement (string prefix,
  966. string localName, string namespaceURI, string value)
  967. {
  968. using (XmlWriter w = PrependChild ()) {
  969. w.WriteElementString (prefix, localName, namespaceURI, value);
  970. }
  971. }
  972. [MonoTODO]
  973. public virtual void ReplaceSelf (string xmlFragment)
  974. {
  975. ReplaceSelf (XmlReader.Create (new StringReader (xmlFragment)));
  976. }
  977. [MonoTODO]
  978. public virtual void ReplaceSelf (XmlReader reader)
  979. {
  980. InsertBefore (reader);
  981. DeleteSelf ();
  982. }
  983. [MonoTODO]
  984. public virtual void ReplaceSelf (XPathNavigator navigator)
  985. {
  986. ReplaceSelf (new XPathNavigatorReader (navigator));
  987. }
  988. // Dunno the exact purpose, but maybe internal editor use
  989. [MonoTODO]
  990. public virtual void SetTypedValue (object value)
  991. {
  992. throw new NotSupportedException ();
  993. }
  994. public virtual void SetValue (string value)
  995. {
  996. throw new NotSupportedException ();
  997. }
  998. [MonoTODO]
  999. private void DeleteChildren ()
  1000. {
  1001. switch (NodeType) {
  1002. case XPathNodeType.Namespace:
  1003. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  1004. case XPathNodeType.Attribute:
  1005. return;
  1006. case XPathNodeType.Text:
  1007. case XPathNodeType.SignificantWhitespace:
  1008. case XPathNodeType.Whitespace:
  1009. case XPathNodeType.ProcessingInstruction:
  1010. case XPathNodeType.Comment:
  1011. DeleteSelf ();
  1012. return;
  1013. }
  1014. if (!HasChildren)
  1015. return;
  1016. XPathNavigator nav = Clone ();
  1017. nav.MoveToFirstChild ();
  1018. while (!nav.IsSamePosition (this))
  1019. nav.DeleteSelf ();
  1020. }
  1021. #endif
  1022. }
  1023. }