XPathNavigator.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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. XPathParser parser = new XPathParser ();
  188. return new CompiledExpression (xpath, parser.Compile (xpath));
  189. }
  190. internal virtual XPathExpression Compile (string xpath, System.Xml.Xsl.IStaticXsltContext ctx)
  191. {
  192. XPathParser parser = new XPathParser (ctx);
  193. return new CompiledExpression (xpath, parser.Compile (xpath));
  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 = nav.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 = nav.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 GetNamespacesInScope (XmlNamespaceScope scope)
  520. {
  521. Hashtable table = new Hashtable ();
  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. return LookupNamespace (prefix, false);
  541. }
  542. [Obsolete]
  543. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  544. {
  545. XPathNavigator nav = Clone ();
  546. if (nav.NodeType != XPathNodeType.Element)
  547. nav.MoveToParent ();
  548. if (nav.MoveToNamespace (prefix)) {
  549. if (atomizedNames)
  550. return nav.NameTable.Add (nav.Value);
  551. else
  552. return nav.Value;
  553. }
  554. return null;
  555. }
  556. public virtual string LookupPrefix (string namespaceUri)
  557. {
  558. return LookupPrefix (namespaceUri, false);
  559. }
  560. [Obsolete]
  561. public virtual string LookupPrefix (string namespaceUri, bool atomizedNames)
  562. {
  563. XPathNavigator nav = Clone ();
  564. if (nav.NodeType != XPathNodeType.Element)
  565. nav.MoveToParent ();
  566. if (!nav.MoveToFirstNamespace ())
  567. return null;
  568. do {
  569. if (atomizedNames) {
  570. if (Object.ReferenceEquals (nav.Value, namespaceUri))
  571. return nav.Name;
  572. } else {
  573. if (nav.Value == namespaceUri)
  574. return nav.Name;
  575. }
  576. } while (nav.MoveToNextNamespace ());
  577. return null;
  578. }
  579. [Obsolete]
  580. public virtual bool MoveToAttribute (string localName, string namespaceURI, bool atomizedNames)
  581. {
  582. return MoveToAttribute (localName, namespaceURI);
  583. }
  584. private bool MoveTo (XPathNodeIterator iter)
  585. {
  586. if (iter.MoveNext ()) {
  587. MoveTo (iter.Current);
  588. return true;
  589. }
  590. else
  591. return false;
  592. }
  593. public virtual bool MoveToChild (XPathNodeType type)
  594. {
  595. return MoveTo (SelectChildren (type));
  596. }
  597. public virtual bool MoveToChild (string localName, string namespaceURI)
  598. {
  599. return MoveTo (SelectChildren (localName, namespaceURI));
  600. }
  601. [Obsolete]
  602. public virtual bool MoveToChild (string localName, string namespaceURI, bool atomizedNames)
  603. {
  604. return MoveToChild (localName, namespaceURI);
  605. }
  606. [Obsolete]
  607. public virtual bool MoveToDescendant (XPathNodeType type)
  608. {
  609. return MoveTo (SelectDescendants (type, false));
  610. }
  611. [Obsolete]
  612. public virtual bool MoveToDescendant (string localName, string namespaceURI)
  613. {
  614. return MoveTo (SelectDescendants (localName, namespaceURI, false));
  615. }
  616. [Obsolete]
  617. public virtual bool MoveToDescendant (string localName, string namespaceURI, bool atomizedNames)
  618. {
  619. return MoveToDescendant (localName, namespaceURI);
  620. }
  621. public virtual bool MoveToNext (string localName, string namespaceURI)
  622. {
  623. XPathNavigator nav = Clone ();
  624. while (nav.MoveToNext ()) {
  625. if (nav.LocalName == localName &&
  626. nav.NamespaceURI == namespaceURI) {
  627. MoveTo (nav);
  628. return true;
  629. }
  630. }
  631. return false;
  632. }
  633. [Obsolete]
  634. public virtual bool MoveToNext (string localName, string namespaceURI, bool atomizedNames)
  635. {
  636. return MoveToNext (localName, namespaceURI);
  637. }
  638. public virtual bool MoveToNext (XPathNodeType type)
  639. {
  640. XPathNavigator nav = Clone ();
  641. while (nav.MoveToNext ()) {
  642. if (nav.NodeType == type) {
  643. MoveTo (nav);
  644. return true;
  645. }
  646. }
  647. return false;
  648. }
  649. [MonoTODO]
  650. public virtual bool MoveToFollowing (string localName,
  651. string namespaceURI)
  652. {
  653. return MoveToFollowing (localName, namespaceURI, null);
  654. }
  655. [MonoTODO]
  656. public virtual bool MoveToFollowing (string localName,
  657. string namespaceURI, XPathNavigator end)
  658. {
  659. XPathNavigator nav = Clone ();
  660. bool skip = false;
  661. do {
  662. if (!skip && nav.MoveToDescendant (localName,
  663. namespaceURI)) {
  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 bool MoveToFollowing (XPathNodeType type)
  686. {
  687. return MoveToFollowing (type, null);
  688. }
  689. [MonoTODO]
  690. public virtual bool MoveToFollowing (XPathNodeType type,
  691. XPathNavigator end)
  692. {
  693. XPathNavigator nav = Clone ();
  694. bool skip = false;
  695. do {
  696. if (!skip && nav.MoveToDescendant (type)) {
  697. if (end != null) {
  698. switch (nav.ComparePosition (end)) {
  699. case XmlNodeOrder.After:
  700. case XmlNodeOrder.Unknown:
  701. return false;
  702. }
  703. }
  704. MoveTo (nav);
  705. return true;
  706. }
  707. else
  708. skip = false;
  709. if (!nav.MoveToNext ()) {
  710. if (!nav.MoveToParent ())
  711. break;
  712. skip = true;
  713. }
  714. } while (true);
  715. return false;
  716. }
  717. [MonoTODO]
  718. public virtual XmlReader ReadSubtree ()
  719. {
  720. return new XPathNavigatorReader (this);
  721. }
  722. public virtual XPathNodeIterator Select (string xpath, IXmlNamespaceResolver nsResolver)
  723. {
  724. return Select (Compile (xpath), nsResolver);
  725. }
  726. public virtual XPathNavigator SelectSingleNode (string xpath)
  727. {
  728. return SelectSingleNode (xpath, null);
  729. }
  730. public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
  731. {
  732. XPathExpression expr = Compile (xpath);
  733. expr.SetContext (nsResolver);
  734. return SelectSingleNode (expr);
  735. }
  736. public XPathNavigator SelectSingleNode (XPathExpression expression)
  737. {
  738. XPathNodeIterator iter = Select (expression);
  739. if (iter.MoveNext ())
  740. return iter.Current;
  741. else
  742. return null;
  743. }
  744. [MonoTODO]
  745. public override object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
  746. {
  747. throw new NotImplementedException ();
  748. }
  749. [MonoTODO]
  750. public virtual void WriteSubtree (XmlWriter writer)
  751. {
  752. XmlReader st = ReadSubtree ();
  753. writer.WriteNode (st, false);
  754. }
  755. [MonoTODO]
  756. public virtual string InnerXml {
  757. get {
  758. XmlReader r = ReadSubtree ();
  759. r.Read (); // start
  760. // skip the element itself (or will reach to
  761. // EOF if other than element) unless writing
  762. // doc itself
  763. int depth = r.Depth;
  764. if (NodeType != XPathNodeType.Root)
  765. r.Read ();
  766. StringWriter sw = new StringWriter ();
  767. XmlWriter xtw = XmlWriter.Create (sw);
  768. while (!r.EOF && r.Depth > depth)
  769. xtw.WriteNode (r, false);
  770. return sw.ToString ();
  771. }
  772. set {
  773. DeleteChildren ();
  774. if (NodeType == XPathNodeType.Attribute) {
  775. SetValue (value);
  776. return;
  777. }
  778. AppendChild (value);
  779. }
  780. }
  781. [MonoTODO]
  782. public override bool IsNode {
  783. get { return true; }
  784. }
  785. [MonoTODO]
  786. public virtual IKeyComparer NavigatorComparer {
  787. get { throw new NotImplementedException (); }
  788. }
  789. [MonoTODO]
  790. public virtual string OuterXml {
  791. get {
  792. StringWriter sw = new StringWriter ();
  793. XmlTextWriter xtw = new XmlTextWriter (sw);
  794. WriteSubtree (xtw);
  795. xtw.Close ();
  796. return sw.ToString ();
  797. }
  798. set {
  799. switch (NodeType) {
  800. case XPathNodeType.Root:
  801. case XPathNodeType.Attribute:
  802. case XPathNodeType.Namespace:
  803. throw new XmlException ("Setting OuterXml Root, Attribute and Namespace is not supported.");
  804. }
  805. DeleteSelf ();
  806. AppendChild (value);
  807. MoveToFirstChild ();
  808. }
  809. }
  810. [MonoTODO]
  811. public virtual IXmlSchemaInfo SchemaInfo {
  812. get {
  813. return null;
  814. }
  815. }
  816. [MonoTODO]
  817. public override object TypedValue {
  818. get {
  819. switch (NodeType) {
  820. case XPathNodeType.Element:
  821. case XPathNodeType.Attribute:
  822. if (XmlType == null)
  823. break;
  824. XmlSchemaDatatype dt = XmlType.Datatype;
  825. if (dt == null)
  826. break;
  827. return dt.ParseValue (Value, NameTable, this as IXmlNamespaceResolver);
  828. }
  829. return Value;
  830. }
  831. }
  832. [MonoTODO]
  833. public virtual object UnderlyingObject {
  834. get { throw new NotImplementedException (); }
  835. }
  836. [MonoTODO]
  837. public override bool ValueAsBoolean {
  838. get { return XQueryConvert.StringToBoolean (Value); }
  839. }
  840. [MonoTODO]
  841. public override DateTime ValueAsDateTime {
  842. get { return XmlConvert.ToDateTime (Value); }
  843. }
  844. [MonoTODO]
  845. public override decimal ValueAsDecimal {
  846. get { return XQueryConvert.StringToDecimal (Value); }
  847. }
  848. [MonoTODO]
  849. public override double ValueAsDouble {
  850. get { return XQueryConvert.StringToDouble (Value); }
  851. }
  852. [MonoTODO]
  853. public override int ValueAsInt32 {
  854. get { return XQueryConvert.StringToInt (Value); }
  855. }
  856. [MonoTODO]
  857. public override long ValueAsInt64 {
  858. get { return XQueryConvert.StringToInteger (Value); }
  859. }
  860. [MonoTODO]
  861. public override ICollection ValueAsList {
  862. get { throw new NotImplementedException (); }
  863. }
  864. [MonoTODO]
  865. public override float ValueAsSingle {
  866. get { return XQueryConvert.StringToFloat (Value); }
  867. }
  868. [MonoTODO]
  869. public override Type ValueType {
  870. get {
  871. return SchemaInfo != null &&
  872. SchemaInfo.SchemaType != null &&
  873. SchemaInfo.SchemaType.Datatype != null ?
  874. SchemaInfo.SchemaType.Datatype.ValueType
  875. : null;
  876. }
  877. }
  878. [MonoTODO]
  879. public override XmlSchemaType XmlType {
  880. get {
  881. if (SchemaInfo != null)
  882. return SchemaInfo.SchemaType;
  883. return null;
  884. }
  885. }
  886. [MonoTODO]
  887. protected XmlReader GetValidatingReader (XmlSchemaSet schemas, ValidationEventHandler handler, XmlSchemaType schemaType)
  888. {
  889. throw new NotImplementedException ();
  890. }
  891. private XmlReader CreateFragmentReader (string fragment)
  892. {
  893. return new XmlTextReader (fragment, XmlNodeType.Element, new XmlParserContext (NameTable, null, null, XmlSpace.None));
  894. }
  895. public virtual XmlWriter AppendChild ()
  896. {
  897. throw new NotSupportedException ();
  898. }
  899. [MonoTODO]
  900. public virtual XPathNavigator AppendChild (
  901. string xmlFragments)
  902. {
  903. // FIXME: should XmlParserContext be something?
  904. return AppendChild (CreateFragmentReader (xmlFragments));
  905. }
  906. [MonoTODO]
  907. public virtual XPathNavigator AppendChild (
  908. XmlReader reader)
  909. {
  910. XmlWriter w = AppendChild ();
  911. while (!reader.EOF)
  912. w.WriteNode (reader, false);
  913. w.Close ();
  914. XPathNavigator nav = Clone ();
  915. nav.MoveToFirstChild ();
  916. while (nav.MoveToNext ())
  917. ;
  918. return nav;
  919. }
  920. [MonoTODO]
  921. public virtual XPathNavigator AppendChild (
  922. XPathNavigator nav)
  923. {
  924. return AppendChild (new XPathNavigatorReader (nav));
  925. }
  926. public void AppendChildElement (string prefix, string name, string ns, string value)
  927. {
  928. XmlWriter xw = AppendChild ();
  929. xw.WriteStartElement (prefix, name, ns);
  930. xw.WriteString (value);
  931. xw.WriteEndElement ();
  932. xw.Close ();
  933. }
  934. public virtual void CreateAttribute (string prefix, string localName, string namespaceURI, string value)
  935. {
  936. using (XmlWriter w = CreateAttributes ()) {
  937. w.WriteAttributeString (prefix, localName, namespaceURI, value);
  938. }
  939. }
  940. public virtual XmlWriter CreateAttributes ()
  941. {
  942. throw new NotSupportedException ();
  943. }
  944. public virtual bool DeleteSelf ()
  945. {
  946. throw new NotSupportedException ();
  947. }
  948. public virtual XmlWriter InsertAfter ()
  949. {
  950. XPathNavigator nav = Clone ();
  951. if (nav.MoveToNext ())
  952. return nav.InsertBefore ();
  953. else
  954. return AppendChild ();
  955. }
  956. public virtual XPathNavigator InsertAfter (string xmlFragments)
  957. {
  958. return InsertAfter (CreateFragmentReader (xmlFragments));
  959. }
  960. [MonoTODO]
  961. public virtual XPathNavigator InsertAfter (XmlReader reader)
  962. {
  963. using (XmlWriter w = InsertAfter ()) {
  964. w.WriteNode (reader, false);
  965. }
  966. XPathNavigator nav = Clone ();
  967. nav.MoveToNext ();
  968. return nav;
  969. }
  970. [MonoTODO]
  971. public virtual XPathNavigator InsertAfter (XPathNavigator nav)
  972. {
  973. return InsertAfter (new XPathNavigatorReader (nav));
  974. }
  975. public virtual XmlWriter InsertBefore ()
  976. {
  977. throw new NotSupportedException ();
  978. }
  979. public virtual XPathNavigator InsertBefore (string xmlFragments)
  980. {
  981. return InsertBefore (CreateFragmentReader (xmlFragments));
  982. }
  983. [MonoTODO]
  984. public virtual XPathNavigator InsertBefore (XmlReader reader)
  985. {
  986. using (XmlWriter w = InsertBefore ()) {
  987. w.WriteNode (reader, false);
  988. }
  989. XPathNavigator nav = Clone ();
  990. nav.MoveToPrevious ();
  991. return nav;
  992. }
  993. [MonoTODO]
  994. public virtual XPathNavigator InsertBefore (XPathNavigator nav)
  995. {
  996. return InsertBefore (new XPathNavigatorReader (nav));
  997. }
  998. public virtual void InsertElementAfter (string prefix,
  999. string localName, string namespaceURI, string value)
  1000. {
  1001. using (XmlWriter w = InsertAfter ()) {
  1002. w.WriteElementString (prefix, localName, namespaceURI, value);
  1003. }
  1004. }
  1005. public virtual void InsertElementBefore (string prefix,
  1006. string localName, string namespaceURI, string value)
  1007. {
  1008. using (XmlWriter w = InsertBefore ()) {
  1009. w.WriteElementString (prefix, localName, namespaceURI, value);
  1010. }
  1011. }
  1012. public virtual XmlWriter PrependChild ()
  1013. {
  1014. XPathNavigator nav = Clone ();
  1015. if (nav.MoveToFirstChild ())
  1016. return nav.InsertBefore ();
  1017. else
  1018. return InsertBefore ();
  1019. }
  1020. public virtual XPathNavigator PrependChild (string xmlFragments)
  1021. {
  1022. return PrependChild (CreateFragmentReader (xmlFragments));
  1023. }
  1024. [MonoTODO]
  1025. public virtual XPathNavigator PrependChild (XmlReader reader)
  1026. {
  1027. using (XmlWriter w = PrependChild ()) {
  1028. w.WriteNode (reader, false);
  1029. }
  1030. XPathNavigator nav = Clone ();
  1031. nav.MoveToFirstChild ();
  1032. return nav;
  1033. }
  1034. [MonoTODO]
  1035. public virtual XPathNavigator PrependChild (XPathNavigator nav)
  1036. {
  1037. return PrependChild (new XPathNavigatorReader (nav));
  1038. }
  1039. public virtual void PrependChildElement (string prefix,
  1040. string localName, string namespaceURI, string value)
  1041. {
  1042. using (XmlWriter w = PrependChild ()) {
  1043. w.WriteElementString (prefix, localName, namespaceURI, value);
  1044. }
  1045. }
  1046. [MonoTODO]
  1047. public virtual bool ReplaceSelf (string xmlFragment)
  1048. {
  1049. return ReplaceSelf (XmlReader.Create (new StringReader (xmlFragment)));
  1050. }
  1051. [MonoTODO]
  1052. public virtual bool ReplaceSelf (XmlReader reader)
  1053. {
  1054. InsertBefore (reader);
  1055. return DeleteSelf ();
  1056. }
  1057. [MonoTODO]
  1058. public virtual bool ReplaceSelf (XPathNavigator navigator)
  1059. {
  1060. return ReplaceSelf (new XPathNavigatorReader (navigator));
  1061. }
  1062. // Dunno the exact purpose, but maybe internal editor use
  1063. [MonoTODO]
  1064. public virtual void SetTypedValue (object value)
  1065. {
  1066. throw new NotSupportedException ();
  1067. }
  1068. public virtual void SetValue (string value)
  1069. {
  1070. throw new NotSupportedException ();
  1071. }
  1072. [MonoTODO]
  1073. private void DeleteChildren ()
  1074. {
  1075. switch (NodeType) {
  1076. case XPathNodeType.Namespace:
  1077. throw new InvalidOperationException ("Removing namespace node content is not supported.");
  1078. case XPathNodeType.Attribute:
  1079. return;
  1080. case XPathNodeType.Text:
  1081. case XPathNodeType.SignificantWhitespace:
  1082. case XPathNodeType.Whitespace:
  1083. case XPathNodeType.ProcessingInstruction:
  1084. case XPathNodeType.Comment:
  1085. DeleteSelf ();
  1086. return;
  1087. }
  1088. if (!HasChildren)
  1089. return;
  1090. XPathNavigator nav = Clone ();
  1091. nav.MoveToFirstChild ();
  1092. while (!nav.IsSamePosition (this))
  1093. nav.DeleteSelf ();
  1094. }
  1095. #endif
  1096. }
  1097. }