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.

XPath.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*--
  2. $Id: XPath.java,v 1.17 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.io.*;
  47. import java.lang.reflect.*;
  48. import java.util.*;
  49. import org.jdom.*;
  50. /**
  51. * A utility class for performing XPath calls on JDOM nodes, with a factory
  52. * interface for obtaining a first XPath instance. Users operate against this
  53. * class while XPath vendors can plug-in implementations underneath. Users
  54. * can choose an implementation using either {@link #setXPathClass} or
  55. * the system property "org.jdom.xpath.class".
  56. *
  57. * @version $Revision: 1.17 $, $Date: 2007/11/10 05:29:02 $
  58. * @author Laurent Bihanic
  59. */
  60. public abstract class XPath implements Serializable {
  61. private static final String CVS_ID =
  62. "@(#) $RCSfile: XPath.java,v $ $Revision: 1.17 $ $Date: 2007/11/10 05:29:02 $ $Name: jdom_1_1 $";
  63. /**
  64. * The name of the system property from which to retrieve the
  65. * name of the implementation class to use.
  66. * <p>
  67. * The property name is:
  68. * "<code>org.jdom.xpath.class</code>".</p>
  69. */
  70. private final static String XPATH_CLASS_PROPERTY = "org.jdom.xpath.class";
  71. /**
  72. * The default implementation class to use if none was configured.
  73. */
  74. private final static String DEFAULT_XPATH_CLASS =
  75. "org.jdom.xpath.JaxenXPath";
  76. /**
  77. * The string passable to the JAXP 1.3 XPathFactory isObjectModelSupported()
  78. * method to query an XPath engine regarding its support for JDOM. Defined
  79. * to be the well-known URI "http://jdom.org/jaxp/xpath/jdom".
  80. */
  81. public final static String JDOM_OBJECT_MODEL_URI =
  82. "http://jdom.org/jaxp/xpath/jdom";
  83. /**
  84. * The constructor to instanciate a new XPath concrete
  85. * implementation.
  86. *
  87. * @see #newInstance
  88. */
  89. private static Constructor constructor = null;
  90. /**
  91. * Creates a new XPath wrapper object, compiling the specified
  92. * XPath expression.
  93. *
  94. * @param path the XPath expression to wrap.
  95. *
  96. * @throws JDOMException if the XPath expression is invalid.
  97. */
  98. public static XPath newInstance(String path) throws JDOMException {
  99. try {
  100. if (constructor == null) {
  101. // First call => Determine implementation.
  102. String className;
  103. try {
  104. className = System.getProperty(XPATH_CLASS_PROPERTY,
  105. DEFAULT_XPATH_CLASS);
  106. }
  107. catch (SecurityException ex1) {
  108. // Access to system property denied. => Use default impl.
  109. className = DEFAULT_XPATH_CLASS;
  110. }
  111. setXPathClass(Class.forName(className));
  112. }
  113. // Allocate and return new implementation instance.
  114. return (XPath)constructor.newInstance(new Object[] { path });
  115. }
  116. catch (JDOMException ex1) {
  117. throw ex1;
  118. }
  119. catch (InvocationTargetException ex2) {
  120. // Constructor threw an error on invocation.
  121. Throwable t = ex2.getTargetException();
  122. throw (t instanceof JDOMException)? (JDOMException)t:
  123. new JDOMException(t.toString(), t);
  124. }
  125. catch (Exception ex3) {
  126. // Any reflection error (probably due to a configuration mistake).
  127. throw new JDOMException(ex3.toString(), ex3);
  128. }
  129. }
  130. /**
  131. * Sets the concrete XPath subclass to use when allocating XPath
  132. * instances.
  133. *
  134. * @param aClass the concrete subclass of XPath.
  135. *
  136. * @throws IllegalArgumentException if <code>aClass</code> is
  137. * <code>null</code>.
  138. * @throws JDOMException if <code>aClass</code> is
  139. * not a concrete subclass
  140. * of XPath.
  141. */
  142. public static void setXPathClass(Class aClass) throws JDOMException {
  143. if (aClass == null) {
  144. throw new IllegalArgumentException("aClass");
  145. }
  146. try {
  147. if ((XPath.class.isAssignableFrom(aClass)) &&
  148. (Modifier.isAbstract(aClass.getModifiers()) == false)) {
  149. // Concrete subclass of XPath => Get constructor
  150. constructor = aClass.getConstructor(new Class[] { String.class });
  151. }
  152. else {
  153. throw new JDOMException(aClass.getName() +
  154. " is not a concrete JDOM XPath implementation");
  155. }
  156. }
  157. catch (JDOMException ex1) {
  158. throw ex1;
  159. }
  160. catch (Exception ex2) {
  161. // Any reflection error (probably due to a configuration mistake).
  162. throw new JDOMException(ex2.toString(), ex2);
  163. }
  164. }
  165. /**
  166. * Evaluates the wrapped XPath expression and returns the list
  167. * of selected items.
  168. *
  169. * @param context the node to use as context for evaluating
  170. * the XPath expression.
  171. *
  172. * @return the list of selected items, which may be of types: {@link Element},
  173. * {@link Attribute}, {@link Text}, {@link CDATA},
  174. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  175. * Double, or String.
  176. *
  177. * @throws JDOMException if the evaluation of the XPath
  178. * expression on the specified context
  179. * failed.
  180. */
  181. abstract public List selectNodes(Object context) throws JDOMException;
  182. /**
  183. * Evaluates the wrapped XPath expression and returns the first
  184. * entry in the list of selected nodes (or atomics).
  185. *
  186. * @param context the node to use as context for evaluating
  187. * the XPath expression.
  188. *
  189. * @return the first selected item, which may be of types: {@link Element},
  190. * {@link Attribute}, {@link Text}, {@link CDATA},
  191. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  192. * Double, String, or <code>null</code> if no item was selected.
  193. *
  194. * @throws JDOMException if the evaluation of the XPath
  195. * expression on the specified context
  196. * failed.
  197. */
  198. abstract public Object selectSingleNode(Object context) throws JDOMException;
  199. /**
  200. * Returns the string value of the first node selected by applying
  201. * the wrapped XPath expression to the given context.
  202. *
  203. * @param context the element to use as context for evaluating
  204. * the XPath expression.
  205. *
  206. * @return the string value of the first node selected by applying
  207. * the wrapped XPath expression to the given context.
  208. *
  209. * @throws JDOMException if the XPath expression is invalid or
  210. * its evaluation on the specified context
  211. * failed.
  212. */
  213. abstract public String valueOf(Object context) throws JDOMException;
  214. /**
  215. * Returns the number value of the first node selected by applying
  216. * the wrapped XPath expression to the given context.
  217. *
  218. * @param context the element to use as context for evaluating
  219. * the XPath expression.
  220. *
  221. * @return the number value of the first node selected by applying
  222. * the wrapped XPath expression to the given context,
  223. * <code>null</code> if no node was selected or the
  224. * special value {@link java.lang.Double#NaN}
  225. * (Not-a-Number) if the selected value can not be
  226. * converted into a number value.
  227. *
  228. * @throws JDOMException if the XPath expression is invalid or
  229. * its evaluation on the specified context
  230. * failed.
  231. */
  232. abstract public Number numberValueOf(Object context) throws JDOMException;
  233. /**
  234. * Defines an XPath variable and sets its value.
  235. *
  236. * @param name the variable name.
  237. * @param value the variable value.
  238. *
  239. * @throws IllegalArgumentException if <code>name</code> is not
  240. * a valid XPath variable name
  241. * or if the value type is not
  242. * supported by the underlying
  243. * implementation
  244. */
  245. abstract public void setVariable(String name, Object value);
  246. /**
  247. * Adds a namespace definition to the list of namespaces known of
  248. * this XPath expression.
  249. * <p>
  250. * <strong>Note</strong>: In XPath, there is no such thing as a
  251. * 'default namespace'. The empty prefix <b>always</b> resolves
  252. * to the empty namespace URI.</p>
  253. *
  254. * @param namespace the namespace.
  255. */
  256. abstract public void addNamespace(Namespace namespace);
  257. /**
  258. * Adds a namespace definition (prefix and URI) to the list of
  259. * namespaces known of this XPath expression.
  260. * <p>
  261. * <strong>Note</strong>: In XPath, there is no such thing as a
  262. * 'default namespace'. The empty prefix <b>always</b> resolves
  263. * to the empty namespace URI.</p>
  264. *
  265. * @param prefix the namespace prefix.
  266. * @param uri the namespace URI.
  267. *
  268. * @throws IllegalNameException if the prefix or uri are null or
  269. * empty strings or if they contain
  270. * illegal characters.
  271. */
  272. public void addNamespace(String prefix, String uri) {
  273. addNamespace(Namespace.getNamespace(prefix, uri));
  274. }
  275. /**
  276. * Returns the wrapped XPath expression as a string.
  277. *
  278. * @return the wrapped XPath expression as a string.
  279. */
  280. abstract public String getXPath();
  281. /**
  282. * Evaluates an XPath expression and returns the list of selected
  283. * items.
  284. * <p>
  285. * <strong>Note</strong>: This method should not be used when the
  286. * same XPath expression needs to be applied several times (on the
  287. * same or different contexts) as it requires the expression to be
  288. * compiled before being evaluated. In such cases,
  289. * {@link #newInstance allocating} an XPath wrapper instance and
  290. * {@link #selectNodes(java.lang.Object) evaluating} it several
  291. * times is way more efficient.
  292. * </p>
  293. *
  294. * @param context the node to use as context for evaluating
  295. * the XPath expression.
  296. * @param path the XPath expression to evaluate.
  297. *
  298. * @return the list of selected items, which may be of types: {@link Element},
  299. * {@link Attribute}, {@link Text}, {@link CDATA},
  300. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  301. * Double, or String.
  302. *
  303. * @throws JDOMException if the XPath expression is invalid or
  304. * its evaluation on the specified context
  305. * failed.
  306. */
  307. public static List selectNodes(Object context, String path)
  308. throws JDOMException {
  309. return newInstance(path).selectNodes(context);
  310. }
  311. /**
  312. * Evaluates the wrapped XPath expression and returns the first
  313. * entry in the list of selected nodes (or atomics).
  314. * <p>
  315. * <strong>Note</strong>: This method should not be used when the
  316. * same XPath expression needs to be applied several times (on the
  317. * same or different contexts) as it requires the expression to be
  318. * compiled before being evaluated. In such cases,
  319. * {@link #newInstance allocating} an XPath wrapper instance and
  320. * {@link #selectSingleNode(java.lang.Object) evaluating} it
  321. * several times is way more efficient.
  322. * </p>
  323. *
  324. * @param context the element to use as context for evaluating
  325. * the XPath expression.
  326. * @param path the XPath expression to evaluate.
  327. *
  328. * @return the first selected item, which may be of types: {@link Element},
  329. * {@link Attribute}, {@link Text}, {@link CDATA},
  330. * {@link Comment}, {@link ProcessingInstruction}, Boolean,
  331. * Double, String, or <code>null</code> if no item was selected.
  332. *
  333. * @throws JDOMException if the XPath expression is invalid or
  334. * its evaluation on the specified context
  335. * failed.
  336. */
  337. public static Object selectSingleNode(Object context, String path)
  338. throws JDOMException {
  339. return newInstance(path).selectSingleNode(context);
  340. }
  341. //-------------------------------------------------------------------------
  342. // Serialization support
  343. //-------------------------------------------------------------------------
  344. /**
  345. * <i>[Serialization support]</i> Returns the alternative object
  346. * to write to the stream when serializing this object. This
  347. * method returns an instance of a dedicated nested class to
  348. * serialize XPath expressions independently of the concrete
  349. * implementation being used.
  350. * <p>
  351. * <strong>Note</strong>: Subclasses are not allowed to override
  352. * this method to ensure valid serialization of all
  353. * implementations.</p>
  354. *
  355. * @return an XPathString instance configured with the wrapped
  356. * XPath expression.
  357. *
  358. * @throws ObjectStreamException never.
  359. */
  360. protected final Object writeReplace() throws ObjectStreamException {
  361. return new XPathString(this.getXPath());
  362. }
  363. /**
  364. * The XPathString is dedicated to serialize instances of
  365. * XPath subclasses in a implementation-independent manner.
  366. * <p>
  367. * XPathString ensures that only string data are serialized. Upon
  368. * deserialization, XPathString relies on XPath factory method to
  369. * to create instances of the concrete XPath wrapper currently
  370. * configured.</p>
  371. */
  372. private final static class XPathString implements Serializable {
  373. /**
  374. * The XPath expression as a string.
  375. */
  376. private String xPath = null;
  377. /**
  378. * Creates a new XPathString instance from the specified
  379. * XPath expression.
  380. *
  381. * @param xpath the XPath expression.
  382. */
  383. public XPathString(String xpath) {
  384. super();
  385. this.xPath = xpath;
  386. }
  387. /**
  388. * <i>[Serialization support]</i> Resolves the read XPathString
  389. * objects into XPath implementations.
  390. *
  391. * @return an instance of a concrete implementation of
  392. * XPath.
  393. *
  394. * @throws ObjectStreamException if no XPath could be built
  395. * from the read object.
  396. */
  397. private Object readResolve() throws ObjectStreamException {
  398. try {
  399. return XPath.newInstance(this.xPath);
  400. }
  401. catch (JDOMException ex1) {
  402. throw new InvalidObjectException(
  403. "Can't create XPath object for expression \"" +
  404. this.xPath + "\": " + ex1.toString());
  405. }
  406. }
  407. }
  408. }