XPathNavigator.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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.Xml;
  34. using System.Xml.Schema;
  35. using Mono.Xml.XPath;
  36. namespace System.Xml.XPath
  37. {
  38. public abstract class XPathNavigator : ICloneable
  39. {
  40. #region Constructor
  41. protected XPathNavigator ()
  42. {
  43. }
  44. #endregion
  45. #region Properties
  46. public abstract string BaseURI { get; }
  47. public abstract bool HasAttributes { get; }
  48. public abstract bool HasChildren { get; }
  49. public abstract bool IsEmptyElement { get; }
  50. public abstract string LocalName { get; }
  51. public abstract string Name { get; }
  52. public abstract string NamespaceURI { get; }
  53. public abstract XmlNameTable NameTable { get; }
  54. public abstract XPathNodeType NodeType { get; }
  55. public abstract string Prefix { get; }
  56. public abstract string Value { get; }
  57. public abstract string XmlLang { get; }
  58. int Depth
  59. {
  60. get
  61. {
  62. int cLevels = 0;
  63. XPathNavigator nav = Clone ();
  64. while (nav.MoveToParent ())
  65. cLevels ++;
  66. return cLevels;
  67. }
  68. }
  69. #endregion
  70. #region Methods
  71. public abstract XPathNavigator Clone ();
  72. public virtual XmlNodeOrder ComparePosition (XPathNavigator nav)
  73. {
  74. if (IsSamePosition (nav))
  75. return XmlNodeOrder.Same;
  76. XPathNavigator nav1 = Clone ();
  77. XPathNavigator nav2 = nav.Clone ();
  78. int nDepth1 = nav1.Depth;
  79. int nDepth2 = nav2.Depth;
  80. if (nDepth1 > nDepth2)
  81. {
  82. while (nDepth1 > nDepth2)
  83. {
  84. if (!nav1.MoveToParent ())
  85. break;
  86. nDepth1 --;
  87. }
  88. if (nav1.IsSamePosition (nav2))
  89. return XmlNodeOrder.After;
  90. }
  91. else if (nDepth1 < nDepth2)
  92. {
  93. while (nDepth1 < nDepth2)
  94. {
  95. if (!nav2.MoveToParent ())
  96. break;
  97. nDepth2 --;
  98. }
  99. if (nav1.IsSamePosition (nav2))
  100. return XmlNodeOrder.Before;
  101. }
  102. XPathNavigator parent1 = nav1.Clone ();
  103. XPathNavigator parent2 = nav2.Clone ();
  104. while (parent1.MoveToParent () && parent2.MoveToParent ())
  105. {
  106. if (parent1.IsSamePosition (parent2))
  107. {
  108. // the ordering is namespace, attribute, children
  109. // assume that nav1 is before nav2, find counter-example
  110. if (nav1.NodeType == XPathNodeType.Namespace)
  111. {
  112. if (nav2.NodeType == XPathNodeType.Namespace)
  113. {
  114. // match namespaces
  115. while (nav2.MoveToNextNamespace ())
  116. if (nav2.IsSamePosition (nav1))
  117. return XmlNodeOrder.After;
  118. }
  119. }
  120. else if (nav1.NodeType == XPathNodeType.Attribute)
  121. {
  122. if (nav2.NodeType == XPathNodeType.Namespace)
  123. return XmlNodeOrder.After;
  124. else if (nav2.NodeType == XPathNodeType.Attribute)
  125. {
  126. // match attributes
  127. while (nav2.MoveToNextAttribute ())
  128. if (nav2.IsSamePosition (nav1))
  129. return XmlNodeOrder.After;
  130. }
  131. }
  132. else
  133. {
  134. switch (nav2.NodeType) {
  135. case XPathNodeType.Namespace:
  136. case XPathNodeType.Attribute:
  137. return XmlNodeOrder.After;
  138. }
  139. // match children
  140. while (nav2.MoveToNext ())
  141. if (nav2.IsSamePosition (nav1))
  142. return XmlNodeOrder.After;
  143. }
  144. return XmlNodeOrder.Before;
  145. }
  146. nav1.MoveToParent ();
  147. nav2.MoveToParent ();
  148. }
  149. return XmlNodeOrder.Unknown;
  150. }
  151. public virtual XPathExpression Compile (string xpath)
  152. {
  153. XPathParser parser = new XPathParser ();
  154. return new CompiledExpression (parser.Compile (xpath));
  155. }
  156. internal virtual XPathExpression Compile (string xpath, System.Xml.Xsl.IStaticXsltContext ctx)
  157. {
  158. XPathParser parser = new XPathParser (ctx);
  159. return new CompiledExpression (parser.Compile (xpath));
  160. }
  161. public virtual object Evaluate (string xpath)
  162. {
  163. return Evaluate (Compile (xpath));
  164. }
  165. public virtual object Evaluate (XPathExpression expr)
  166. {
  167. return Evaluate (expr, null);
  168. }
  169. public virtual object Evaluate (XPathExpression expr, XPathNodeIterator context)
  170. {
  171. return Evaluate (expr, context, null);
  172. }
  173. internal virtual object Evaluate (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  174. {
  175. CompiledExpression cexpr = (CompiledExpression) expr;
  176. if (ctx == null)
  177. ctx = cexpr.NamespaceManager;
  178. if (context == null)
  179. context = new NullIterator (this, ctx);
  180. BaseIterator iterContext = (BaseIterator) context;
  181. iterContext.NamespaceManager = ctx;
  182. return cexpr.Evaluate (iterContext);
  183. }
  184. internal XPathNodeIterator EvaluateNodeSet (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  185. {
  186. CompiledExpression cexpr = (CompiledExpression) expr;
  187. if (ctx == null)
  188. ctx = cexpr.NamespaceManager;
  189. if (context == null)
  190. context = new NullIterator (this, cexpr.NamespaceManager);
  191. BaseIterator iterContext = (BaseIterator) context;
  192. iterContext.NamespaceManager = ctx;
  193. return cexpr.EvaluateNodeSet (iterContext);
  194. }
  195. internal string EvaluateString (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  196. {
  197. CompiledExpression cexpr = (CompiledExpression) expr;
  198. if (ctx == null)
  199. ctx = cexpr.NamespaceManager;
  200. if (context == null)
  201. context = new NullIterator (this, cexpr.NamespaceManager);
  202. BaseIterator iterContext = (BaseIterator) context;
  203. iterContext.NamespaceManager = ctx;
  204. return cexpr.EvaluateString (iterContext);
  205. }
  206. internal double EvaluateNumber (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  207. {
  208. CompiledExpression cexpr = (CompiledExpression) expr;
  209. if (ctx == null)
  210. ctx = cexpr.NamespaceManager;
  211. if (context == null)
  212. context = new NullIterator (this, cexpr.NamespaceManager);
  213. BaseIterator iterContext = (BaseIterator) context;
  214. iterContext.NamespaceManager = ctx;
  215. return cexpr.EvaluateNumber (iterContext);
  216. }
  217. internal bool EvaluateBoolean (XPathExpression expr, XPathNodeIterator context, XmlNamespaceManager ctx)
  218. {
  219. CompiledExpression cexpr = (CompiledExpression) expr;
  220. if (ctx == null)
  221. ctx = cexpr.NamespaceManager;
  222. if (context == null)
  223. context = new NullIterator (this, cexpr.NamespaceManager);
  224. BaseIterator iterContext = (BaseIterator) context;
  225. iterContext.NamespaceManager = ctx;
  226. return cexpr.EvaluateBoolean (iterContext);
  227. }
  228. public abstract string GetAttribute (string localName, string namespaceURI);
  229. public abstract string GetNamespace (string name);
  230. object ICloneable.Clone ()
  231. {
  232. return Clone ();
  233. }
  234. public virtual bool IsDescendant (XPathNavigator nav)
  235. {
  236. if (nav != null)
  237. {
  238. nav = nav.Clone ();
  239. while (nav.MoveToParent ())
  240. {
  241. if (IsSamePosition (nav))
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. public abstract bool IsSamePosition (XPathNavigator other);
  248. public virtual bool Matches (string xpath)
  249. {
  250. return Matches (Compile (xpath));
  251. }
  252. public virtual bool Matches (XPathExpression expr)
  253. {
  254. Expression e = ((CompiledExpression) expr).ExpressionNode;
  255. if (e is ExprRoot)
  256. return NodeType == XPathNodeType.Root;
  257. NodeTest nt = e as NodeTest;
  258. if (nt != null) {
  259. switch (nt.Axis.Axis) {
  260. case Axes.Child:
  261. case Axes.Attribute:
  262. break;
  263. default:
  264. throw new XPathException ("Only child and attribute pattern are allowed for a pattern.");
  265. }
  266. return nt.Match (((CompiledExpression)expr).NamespaceManager, this);
  267. }
  268. if (e is ExprFilter) {
  269. do {
  270. e = ((ExprFilter) e).LeftHandSide;
  271. } while (e is ExprFilter);
  272. if (e is NodeTest && !((NodeTest) e).Match (((CompiledExpression) expr).NamespaceManager, this))
  273. return false;
  274. }
  275. XPathResultType resultType = e.ReturnType;
  276. switch (resultType) {
  277. case XPathResultType.Any:
  278. case XPathResultType.NodeSet:
  279. break;
  280. default:
  281. return false;
  282. }
  283. switch (e.EvaluatedNodeType) {
  284. case XPathNodeType.Attribute:
  285. case XPathNodeType.Namespace:
  286. if (NodeType != e.EvaluatedNodeType)
  287. return false;
  288. break;
  289. }
  290. XPathNodeIterator nodes;
  291. nodes = this.Select (expr);
  292. while (nodes.MoveNext ()) {
  293. if (IsSamePosition (nodes.Current))
  294. return true;
  295. }
  296. // ancestors might select this node.
  297. XPathNavigator navigator = Clone ();
  298. while (navigator.MoveToParent ()) {
  299. nodes = navigator.Select (expr);
  300. while (nodes.MoveNext ()) {
  301. if (IsSamePosition (nodes.Current))
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307. public abstract bool MoveTo (XPathNavigator other);
  308. public abstract bool MoveToAttribute (string localName, string namespaceURI);
  309. public abstract bool MoveToFirst ();
  310. public abstract bool MoveToFirstAttribute ();
  311. public abstract bool MoveToFirstChild ();
  312. public bool MoveToFirstNamespace ()
  313. {
  314. return MoveToFirstNamespace (XPathNamespaceScope.All);
  315. }
  316. public abstract bool MoveToFirstNamespace (XPathNamespaceScope namespaceScope);
  317. public abstract bool MoveToId (string id);
  318. public abstract bool MoveToNamespace (string name);
  319. public abstract bool MoveToNext ();
  320. public abstract bool MoveToNextAttribute ();
  321. public bool MoveToNextNamespace ()
  322. {
  323. return MoveToNextNamespace (XPathNamespaceScope.All);
  324. }
  325. public abstract bool MoveToNextNamespace (XPathNamespaceScope namespaceScope);
  326. public abstract bool MoveToParent ();
  327. public abstract bool MoveToPrevious ();
  328. public abstract void MoveToRoot ();
  329. public virtual XPathNodeIterator Select (string xpath)
  330. {
  331. return Select (Compile (xpath));
  332. }
  333. public virtual XPathNodeIterator Select (XPathExpression expr)
  334. {
  335. return Select (expr, null);
  336. }
  337. internal virtual XPathNodeIterator Select (XPathExpression expr, XmlNamespaceManager ctx)
  338. {
  339. CompiledExpression cexpr = (CompiledExpression) expr;
  340. if (ctx == null)
  341. ctx = cexpr.NamespaceManager;
  342. BaseIterator iter = new NullIterator (this, ctx);
  343. return cexpr.EvaluateNodeSet (iter);
  344. }
  345. public virtual XPathNodeIterator SelectAncestors (XPathNodeType type, bool matchSelf)
  346. {
  347. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  348. return SelectTest (new NodeTypeTest (axis, type));
  349. }
  350. public virtual XPathNodeIterator SelectAncestors (string name, string namespaceURI, bool matchSelf)
  351. {
  352. if (name == null)
  353. throw new ArgumentNullException ("name");
  354. if (namespaceURI == null)
  355. throw new ArgumentNullException ("namespaceURI");
  356. Axes axis = (matchSelf) ? Axes.AncestorOrSelf : Axes.Ancestor;
  357. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  358. return SelectTest (new NodeNameTest (axis, qname, true));
  359. }
  360. public virtual XPathNodeIterator SelectChildren (XPathNodeType type)
  361. {
  362. return SelectTest (new NodeTypeTest (Axes.Child, type));
  363. }
  364. public virtual XPathNodeIterator SelectChildren (string name, string namespaceURI)
  365. {
  366. if (name == null)
  367. throw new ArgumentNullException ("name");
  368. if (namespaceURI == null)
  369. throw new ArgumentNullException ("namespaceURI");
  370. Axes axis = Axes.Child;
  371. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  372. return SelectTest (new NodeNameTest (axis, qname, true));
  373. }
  374. public virtual XPathNodeIterator SelectDescendants (XPathNodeType type, bool matchSelf)
  375. {
  376. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  377. return SelectTest (new NodeTypeTest (axis, type));
  378. }
  379. public virtual XPathNodeIterator SelectDescendants (string name, string namespaceURI, bool matchSelf)
  380. {
  381. if (name == null)
  382. throw new ArgumentNullException ("name");
  383. if (namespaceURI == null)
  384. throw new ArgumentNullException ("namespaceURI");
  385. Axes axis = (matchSelf) ? Axes.DescendantOrSelf : Axes.Descendant;
  386. XmlQualifiedName qname = new XmlQualifiedName (name, namespaceURI);
  387. return SelectTest (new NodeNameTest (axis, qname, true));
  388. }
  389. internal XPathNodeIterator SelectTest (NodeTest test)
  390. {
  391. return test.EvaluateNodeSet (new NullIterator (this));
  392. }
  393. public override string ToString ()
  394. {
  395. return Value;
  396. }
  397. #endregion
  398. #if NET_2_0
  399. [MonoTODO]
  400. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler)
  401. {
  402. throw new NotImplementedException ();
  403. }
  404. [MonoTODO]
  405. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler, XmlSchemaAttribute attribute)
  406. {
  407. throw new NotImplementedException ();
  408. }
  409. [MonoTODO]
  410. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler, XmlSchemaElement element)
  411. {
  412. throw new NotImplementedException ();
  413. }
  414. [MonoTODO]
  415. public virtual bool CheckValidity (XmlSchemaSet schemas, ValidationEventHandler handler, XmlSchemaType schemaType)
  416. {
  417. throw new NotImplementedException ();
  418. }
  419. [MonoTODO]
  420. public virtual object CopyAsObject (Type targetType)
  421. {
  422. throw new NotImplementedException ();
  423. }
  424. [MonoTODO]
  425. public virtual XPathNavigator CreateNavigator ()
  426. {
  427. return Clone ();
  428. }
  429. [MonoTODO]
  430. public virtual object Evaluate (string xpath, IXmlNamespaceResolver nsResolver)
  431. {
  432. throw new NotImplementedException ();
  433. }
  434. [MonoTODO]
  435. public virtual IDictionary GetNamespacesInScope (XmlNamespaceScope scope)
  436. {
  437. throw new NotImplementedException ();
  438. }
  439. public virtual string LookupNamespace (string prefix)
  440. {
  441. return LookupNamespace (prefix, false);
  442. }
  443. [MonoTODO]
  444. public virtual string LookupNamespace (string prefix, bool atomizedNames)
  445. {
  446. throw new NotImplementedException ();
  447. }
  448. public virtual string LookupPrefix (string namespaceUri)
  449. {
  450. return LookupPrefix (namespaceUri, false);
  451. }
  452. [MonoTODO]
  453. public virtual string LookupPrefix (string namespaceUri, bool atomizedNames)
  454. {
  455. throw new NotImplementedException ();
  456. }
  457. [MonoTODO]
  458. public virtual XmlReader ReadSubtree ()
  459. {
  460. throw new NotImplementedException ();
  461. }
  462. public virtual XPathNavigator SelectSingleNode (string xpath)
  463. {
  464. return SelectSingleNode (xpath, null);
  465. }
  466. [MonoTODO]
  467. public virtual XPathNavigator SelectSingleNode (string xpath, IXmlNamespaceResolver nsResolver)
  468. {
  469. throw new NotImplementedException ();
  470. }
  471. public virtual object ValueAs (Type type)
  472. {
  473. return ValueAs (type, null);
  474. }
  475. [MonoTODO]
  476. public virtual object ValueAs (Type type, IXmlNamespaceResolver nsResolver)
  477. {
  478. throw new NotImplementedException ();
  479. }
  480. [MonoTODO]
  481. public virtual XmlWriter WriteSubtree ()
  482. {
  483. throw new NotImplementedException ();
  484. }
  485. [MonoTODO]
  486. public bool HasNamespaceResolver {
  487. get { throw new NotImplementedException (); }
  488. }
  489. [MonoTODO]
  490. public virtual string InnerXml {
  491. get { throw new NotImplementedException (); }
  492. }
  493. public virtual bool IsNode {
  494. get { throw new NotImplementedException (); }
  495. }
  496. /* FIXME: It should be member, but requires new Collection type.
  497. [MonoTODO]
  498. public virtual IKeyComparer NavigatorComparer {
  499. get { throw new NotImplementedException (); }
  500. }
  501. */
  502. [MonoTODO]
  503. public virtual string OuterXml {
  504. get { throw new NotImplementedException (); }
  505. }
  506. [MonoTODO]
  507. public virtual IXmlSchemaInfo SchemaInfo {
  508. get { throw new NotImplementedException (); }
  509. }
  510. [MonoTODO]
  511. public virtual object TypedValue {
  512. get { throw new NotImplementedException (); }
  513. }
  514. [MonoTODO]
  515. public virtual object UnderlyingObject {
  516. get { throw new NotImplementedException (); }
  517. }
  518. public virtual bool ValueAsBoolean {
  519. get { throw new NotImplementedException (); }
  520. }
  521. public virtual DateTime ValueAsDateTime {
  522. get { throw new NotImplementedException (); }
  523. }
  524. public virtual decimal ValueAsDecimal {
  525. get { throw new NotImplementedException (); }
  526. }
  527. public virtual double ValueAsDouble {
  528. get { throw new NotImplementedException (); }
  529. }
  530. public virtual int ValueAsInt32 {
  531. get { throw new NotImplementedException (); }
  532. }
  533. public virtual long ValueAsInt64 {
  534. get { throw new NotImplementedException (); }
  535. }
  536. public virtual ICollection ValueAsList {
  537. get { throw new NotImplementedException (); }
  538. }
  539. public virtual float ValueAsSingle {
  540. get { throw new NotImplementedException (); }
  541. }
  542. public virtual Type ValueType {
  543. get { throw new NotImplementedException (); }
  544. }
  545. public virtual XmlSchemaType XmlType {
  546. get { throw new NotImplementedException (); }
  547. }
  548. protected XmlReader GetValidatingReader (XmlSchemaSet schemas, ValidationEventHandler handler)
  549. {
  550. throw new NotImplementedException ();
  551. }
  552. #endif
  553. }
  554. }