📄 xpath.java
字号:
/*--
$Id: XPath.java,v 1.15 2004/02/06 09:28:32 jhunter Exp $
Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the disclaimer that follows
these conditions in the documentation and/or other materials
provided with the distribution.
3. The name "JDOM" must not be used to endorse or promote products
derived from this software without prior written permission. For
written permission, please contact <request_AT_jdom_DOT_org>.
4. Products derived from this software may not be called "JDOM", nor
may "JDOM" appear in their name, without prior written permission
from the JDOM Project Management <request_AT_jdom_DOT_org>.
In addition, we request (but do not require) that you include in the
end-user documentation provided with the redistribution and/or in the
software itself an acknowledgement equivalent to the following:
"This product includes software developed by the
JDOM Project (http://www.jdom.org/)."
Alternatively, the acknowledgment may be graphical using the logos
available at http://www.jdom.org/images/logos.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
This software consists of voluntary contributions made by many
individuals on behalf of the JDOM Project and was originally
created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
on the JDOM Project, please see <http://www.jdom.org/>.
*/
package org.jdom.xpath;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.jdom.*;
/**
* A utility class for performing XPath calls on JDOM nodes, with a factory
* interface for obtaining a first XPath instance. Users operate against this
* class while XPath vendors can plug-in implementations underneath. Users
* can choose an implementation using either {@link #setXPathClass} or
* the system property "org.jdom.xpath.class".
*
* @version $Revision: 1.15 $, $Date: 2004/02/06 09:28:32 $
* @author Laurent Bihanic
*/
public abstract class XPath implements Serializable {
private static final String CVS_ID =
"@(#) $RCSfile: XPath.java,v $ $Revision: 1.15 $ $Date: 2004/02/06 09:28:32 $ $Name: jdom_1_0 $";
/**
* The name of the system property from which to retrieve the
* name of the implementation class to use.
* <p>
* The property name is:
* "<code>org.jdom.xpath.class</code>".</p>
*/
private final static String XPATH_CLASS_PROPERTY = "org.jdom.xpath.class";
/**
* The default implementation class to use if none was configured.
*/
private final static String DEFAULT_XPATH_CLASS =
"org.jdom.xpath.JaxenXPath";
/**
* The constructor to instanciate a new XPath concrete
* implementation.
*
* @see #newInstance
*/
private static Constructor constructor = null;
/**
* Creates a new XPath wrapper object, compiling the specified
* XPath expression.
*
* @param path the XPath expression to wrap.
*
* @throws JDOMException if the XPath expression is invalid.
*/
public static XPath newInstance(String path) throws JDOMException {
try {
if (constructor == null) {
// First call => Determine implementation.
String className;
try {
className = System.getProperty(XPATH_CLASS_PROPERTY,
DEFAULT_XPATH_CLASS);
}
catch (SecurityException ex1) {
// Access to system property denied. => Use default impl.
className = DEFAULT_XPATH_CLASS;
}
setXPathClass(Class.forName(className));
}
// Allocate and return new implementation instance.
return (XPath)constructor.newInstance(new Object[] { path });
}
catch (JDOMException ex1) {
throw ex1;
}
catch (InvocationTargetException ex2) {
// Constructor threw an error on invocation.
Throwable t = ex2.getTargetException();
throw (t instanceof JDOMException)? (JDOMException)t:
new JDOMException(t.toString(), t);
}
catch (Exception ex3) {
// Any reflection error (probably due to a configuration mistake).
throw new JDOMException(ex3.toString(), ex3);
}
}
/**
* Sets the concrete XPath subclass to use when allocating XPath
* instances.
*
* @param aClass the concrete subclass of XPath.
*
* @throws IllegalArgumentException if <code>aClass</code> is
* <code>null</code>.
* @throws JDOMException if <code>aClass</code> is
* not a concrete subclass
* of XPath.
*/
public static void setXPathClass(Class aClass) throws JDOMException {
if (aClass == null) {
throw new IllegalArgumentException("aClass");
}
try {
if ((XPath.class.isAssignableFrom(aClass)) &&
(Modifier.isAbstract(aClass.getModifiers()) == false)) {
// Concrete subclass of XPath => Get constructor
constructor = aClass.getConstructor(new Class[] { String.class });
}
else {
throw new JDOMException(aClass.getName() +
" is not a concrete JDOM XPath implementation");
}
}
catch (JDOMException ex1) {
throw ex1;
}
catch (Exception ex2) {
// Any reflection error (probably due to a configuration mistake).
throw new JDOMException(ex2.toString(), ex2);
}
}
/**
* Evaluates the wrapped XPath expression and returns the list
* of selected items.
*
* @param context the node to use as context for evaluating
* the XPath expression.
*
* @return the list of selected items, which may be of types: {@link Element},
* {@link Attribute}, {@link Text}, {@link CDATA},
* {@link Comment}, {@link ProcessingInstruction}, Boolean,
* Double, or String.
*
* @throws JDOMException if the evaluation of the XPath
* expression on the specified context
* failed.
*/
abstract public List selectNodes(Object context) throws JDOMException;
/**
* Evaluates the wrapped XPath expression and returns the first
* entry in the list of selected nodes (or atomics).
*
* @param context the node to use as context for evaluating
* the XPath expression.
*
* @return the first selected item, which may be of types: {@link Element},
* {@link Attribute}, {@link Text}, {@link CDATA},
* {@link Comment}, {@link ProcessingInstruction}, Boolean,
* Double, String, or <code>null</code> if no item was selected.
*
* @throws JDOMException if the evaluation of the XPath
* expression on the specified context
* failed.
*/
abstract public Object selectSingleNode(Object context) throws JDOMException;
/**
* Returns the string value of the first node selected by applying
* the wrapped XPath expression to the given context.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -