Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JaxenXPath.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*--
  2. $Id: JaxenXPath.java,v 1.20 2007/11/10 05:29:02 jhunter Exp $
  3. Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions, and the disclaimer that follows
  12. these conditions in the documentation and/or other materials
  13. provided with the distribution.
  14. 3. The name "JDOM" must not be used to endorse or promote products
  15. derived from this software without prior written permission. For
  16. written permission, please contact <request_AT_jdom_DOT_org>.
  17. 4. Products derived from this software may not be called "JDOM", nor
  18. may "JDOM" appear in their name, without prior written permission
  19. from the JDOM Project Management <request_AT_jdom_DOT_org>.
  20. In addition, we request (but do not require) that you include in the
  21. end-user documentation provided with the redistribution and/or in the
  22. software itself an acknowledgement equivalent to the following:
  23. "This product includes software developed by the
  24. JDOM Project (http://www.jdom.org/)."
  25. Alternatively, the acknowledgment may be graphical using the logos
  26. available at http://www.jdom.org/images/logos.
  27. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  28. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
  31. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  33. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  34. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. SUCH DAMAGE.
  39. This software consists of voluntary contributions made by many
  40. individuals on behalf of the JDOM Project and was originally
  41. created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
  42. Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
  43. on the JDOM Project, please see <http://www.jdom.org/>.
  44. */
  45. package org.jdom.xpath;
  46. import java.util.*;
  47. import org.jaxen.*;
  48. import org.jaxen.jdom.*;
  49. import org.jdom.*;
  50. /**
  51. * A non-public concrete XPath implementation for Jaxen.
  52. *
  53. * @version $Revision: 1.20 $, $Date: 2007/11/10 05:29:02 $
  54. * @author Laurent Bihanic
  55. */
  56. class JaxenXPath extends XPath { // package protected
  57. private static final String CVS_ID =
  58. "@(#) $RCSfile: JaxenXPath.java,v $ $Revision: 1.20 $ $Date: 2007/11/10 05:29:02 $ $Name: jdom_1_1 $";
  59. /**
  60. * The compiled XPath object to select nodes. This attribute can
  61. * not be made final as it needs to be set upon object
  62. * deserialization.
  63. */
  64. private transient JDOMXPath xPath;
  65. /**
  66. * The current context for XPath expression evaluation.
  67. */
  68. private Object currentContext;
  69. /**
  70. * Creates a new XPath wrapper object, compiling the specified
  71. * XPath expression.
  72. *
  73. * @param expr the XPath expression to wrap.
  74. *
  75. * @throws JDOMException if the XPath expression is invalid.
  76. */
  77. public JaxenXPath(String expr) throws JDOMException {
  78. setXPath(expr);
  79. }
  80. /**
  81. * Evaluates the wrapped XPath expression and returns the list
  82. * of selected items.
  83. *
  84. * @param context the node to use as context for evaluating
  85. * the XPath expression.
  86. *
  87. * @return the list of selected items, which may be of types: {@link Element},
  88. * {@link Attribute}, {@link Text}, {@link CDATA},
  89. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  90. * Double, or String.
  91. *
  92. * @throws JDOMException if the evaluation of the XPath
  93. * expression on the specified context
  94. * failed.
  95. */
  96. public List selectNodes(Object context) throws JDOMException {
  97. try {
  98. currentContext = context;
  99. return xPath.selectNodes(context);
  100. }
  101. catch (JaxenException ex1) {
  102. throw new JDOMException("XPath error while evaluating \"" +
  103. xPath.toString() + "\": " + ex1.getMessage(), ex1);
  104. }
  105. finally {
  106. currentContext = null;
  107. }
  108. }
  109. /**
  110. * Evaluates the wrapped XPath expression and returns the first
  111. * entry in the list of selected nodes (or atomics).
  112. *
  113. * @param context the node to use as context for evaluating
  114. * the XPath expression.
  115. *
  116. * @return the first selected item, which may be of types: {@link Element},
  117. * {@link Attribute}, {@link Text}, {@link CDATA},
  118. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  119. * Double, String, or <code>null</code> if no item was selected.
  120. *
  121. * @throws JDOMException if the evaluation of the XPath
  122. * expression on the specified context
  123. * failed.
  124. */
  125. public Object selectSingleNode(Object context) throws JDOMException {
  126. try {
  127. currentContext = context;
  128. return xPath.selectSingleNode(context);
  129. }
  130. catch (JaxenException ex1) {
  131. throw new JDOMException("XPath error while evaluating \"" +
  132. xPath.toString() + "\": " + ex1.getMessage(), ex1);
  133. }
  134. finally {
  135. currentContext = null;
  136. }
  137. }
  138. /**
  139. * Returns the string value of the first node selected by applying
  140. * the wrapped XPath expression to the given context.
  141. *
  142. * @param context the element to use as context for evaluating
  143. * the XPath expression.
  144. *
  145. * @return the string value of the first node selected by applying
  146. * the wrapped XPath expression to the given context.
  147. *
  148. * @throws JDOMException if the XPath expression is invalid or
  149. * its evaluation on the specified context
  150. * failed.
  151. */
  152. public String valueOf(Object context) throws JDOMException {
  153. try {
  154. currentContext = context;
  155. return xPath.stringValueOf(context);
  156. }
  157. catch (JaxenException ex1) {
  158. throw new JDOMException("XPath error while evaluating \"" +
  159. xPath.toString() + "\": " + ex1.getMessage(), ex1);
  160. }
  161. finally {
  162. currentContext = null;
  163. }
  164. }
  165. /**
  166. * Returns the number value of the first item selected by applying
  167. * the wrapped XPath expression to the given context.
  168. *
  169. * @param context the element to use as context for evaluating
  170. * the XPath expression.
  171. *
  172. * @return the number value of the first item selected by applying
  173. * the wrapped XPath expression to the given context,
  174. * <code>null</code> if no node was selected or the
  175. * special value {@link java.lang.Double#NaN}
  176. * (Not-a-Number) if the selected value can not be
  177. * converted into a number value.
  178. *
  179. * @throws JDOMException if the XPath expression is invalid or
  180. * its evaluation on the specified context
  181. * failed.
  182. */
  183. public Number numberValueOf(Object context) throws JDOMException {
  184. try {
  185. currentContext = context;
  186. return xPath.numberValueOf(context);
  187. }
  188. catch (JaxenException ex1) {
  189. throw new JDOMException("XPath error while evaluating \"" +
  190. xPath.toString() + "\": " + ex1.getMessage(), ex1);
  191. }
  192. finally {
  193. currentContext = null;
  194. }
  195. }
  196. /**
  197. * Defines an XPath variable and sets its value.
  198. *
  199. * @param name the variable name.
  200. * @param value the variable value.
  201. *
  202. * @throws IllegalArgumentException if <code>name</code> is not
  203. * a valid XPath variable name
  204. * or if the value type is not
  205. * supported by the underlying
  206. * implementation
  207. */
  208. public void setVariable(String name, Object value)
  209. throws IllegalArgumentException {
  210. Object o = xPath.getVariableContext();
  211. if (o instanceof SimpleVariableContext) {
  212. ((SimpleVariableContext)o).setVariableValue(null, name, value);
  213. }
  214. }
  215. /**
  216. * Adds a namespace definition to the list of namespaces known of
  217. * this XPath expression.
  218. * <p>
  219. * <strong>Note</strong>: In XPath, there is no such thing as a
  220. * 'default namespace'. The empty prefix <b>always</b> resolves
  221. * to the empty namespace URI.</p>
  222. *
  223. * @param namespace the namespace.
  224. */
  225. public void addNamespace(Namespace namespace) {
  226. try {
  227. xPath.addNamespace(namespace.getPrefix(), namespace.getURI());
  228. }
  229. catch (JaxenException ex1) { /* Can't happen here. */ }
  230. }
  231. /**
  232. * Returns the wrapped XPath expression as a string.
  233. *
  234. * @return the wrapped XPath expression as a string.
  235. */
  236. public String getXPath() {
  237. return (xPath.toString());
  238. }
  239. /**
  240. * Compiles and sets the XPath expression wrapped by this object.
  241. *
  242. * @param expr the XPath expression to wrap.
  243. *
  244. * @throws JDOMException if the XPath expression is invalid.
  245. */
  246. private void setXPath(String expr) throws JDOMException {
  247. try {
  248. xPath = new JDOMXPath(expr);
  249. xPath.setNamespaceContext(new NSContext());
  250. }
  251. catch (Exception ex1) {
  252. throw new JDOMException(
  253. "Invalid XPath expression: \"" + expr + "\"", ex1);
  254. }
  255. }
  256. public String toString() {
  257. return (xPath.toString());
  258. }
  259. public boolean equals(Object o) {
  260. if (o instanceof JaxenXPath) {
  261. JaxenXPath x = (JaxenXPath)o;
  262. return (super.equals(o) &&
  263. xPath.toString().equals(x.xPath.toString()));
  264. }
  265. return false;
  266. }
  267. public int hashCode() {
  268. return xPath.hashCode();
  269. }
  270. private class NSContext extends SimpleNamespaceContext {
  271. public NSContext() {
  272. super();
  273. }
  274. /**
  275. * <i>[Jaxen NamespaceContext interface support]</i> Translates
  276. * the provided namespace prefix into the matching bound
  277. * namespace URI.
  278. *
  279. * @param prefix the namespace prefix to resolve.
  280. *
  281. * @return the namespace URI matching the prefix.
  282. */
  283. public String translateNamespacePrefixToUri(String prefix) {
  284. if ((prefix == null) || (prefix.length() == 0)) {
  285. return null;
  286. }
  287. String uri = super.translateNamespacePrefixToUri(prefix);
  288. if (uri == null) {
  289. Object ctx = currentContext;
  290. if (ctx != null) {
  291. Element elt = null;
  292. // Get closer element node
  293. if (ctx instanceof Element) {
  294. elt = (Element)ctx;
  295. } else if (ctx instanceof Attribute) {
  296. elt = ((Attribute)ctx).getParent();
  297. } else if (ctx instanceof Content) {
  298. elt = ((Content) ctx).getParentElement();
  299. } else if (ctx instanceof Document) {
  300. elt = ((Document)ctx).getRootElement();
  301. }
  302. if (elt != null) {
  303. Namespace ns = elt.getNamespace(prefix);
  304. if (ns != null) {
  305. uri = ns.getURI();
  306. }
  307. }
  308. }
  309. }
  310. return uri;
  311. }
  312. }
  313. }