|
|||||||||
| Home >> All >> org >> apache >> commons >> [ jxpath overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
org.apache.commons.jxpath
Class JXPathContext

java.lang.Objectorg.apache.commons.jxpath.JXPathContext
- public abstract class JXPathContext
- extends java.lang.Object
JXPathContext provides APIs for the traversal of graphs of JavaBeans using the XPath syntax. Using JXPathContext, you can read and write properties of JavaBeans, arrays, collections and maps. JXPathContext uses JavaBeans introspection to enumerate and access JavaBeans properties.
JXPathContext allows alternative implementations. This is why instead of
allocating JXPathContext directly, you should call a static
newContext method. This method will utilize the
JXPathContextFactory API to locate a suitable implementation of JXPath.
Bundled with JXPath comes a default implementation called Reference
Implementation.
JXPath Interprets XPath Syntax on Java Object Graphs
JXPath uses an intuitive interpretation of the xpath syntax in the context of Java object graphs. Here are some examples:Example 1: JavaBean Property Access
JXPath can be used to access properties of a JavaBean.In this example, we are using JXPath to access a property of thepublic class Employee { public String getFirstName(){ ... } } Employee emp = new Employee(); ... JXPathContext context = JXPathContext.newContext(emp); String fName = (String)context.getValue("firstName");
emp bean. In this simple case the invocation of JXPath is
equivalent to invocation of getFirstName() on the bean.
Example 2: Nested Bean Property Access
JXPath can traverse object graphs:In this case XPath is used to access a property of a nested bean.public class Employee { public Address getHomeAddress(){ ... } } public class Address { public String getStreetNumber(){ ... } } Employee emp = new Employee(); ... JXPathContext context = JXPathContext.newContext(emp); String sNumber = (String)context.getValue("homeAddress/streetNumber");
A property identified by the xpath does not have to be a "leaf" property. For instance, we can extract the whole Address object in above example:
Address addr = (Address)context.getValue("homeAddress");
Example 3: Collection Subscripts
JXPath can extract elements from arrays and collections.A collection can be an arbitrary array or an instance of java.util. Collection.public class Integers { public int[] getNumbers(){ ... } } Integers ints = new Integers(); ... JXPathContext context = JXPathContext.newContext(ints); Integer thirdInt = (Integer)context.getValue("numbers[3]");
Note: in XPath the first element of a collection has index 1, not 0.
Example 4: Map Element Access
JXPath supports maps. To get a value use its key.Often you will need to use the alternative syntax for accessing Map elements:public class Employee { public Map getAddresses(){ return addressMap; } public void addAddress(String key, Address address){ addressMap.put(key, address); } ... } Employee emp = new Employee(); emp.addAddress("home", new Address(...)); emp.addAddress("office", new Address(...)); ... JXPathContext context = JXPathContext.newContext(emp); String homeZipCode = (String)context.getValue("addresses/home/zipCode");
In this case, the key can be an expression, e.g. a variable.String homeZipCode = (String) context.getValue("addresses[@name='home']/zipCode");
Note: At this point JXPath only supports Maps that use strings for keys.
Note: JXPath supports the extended notion of Map: any object with dynamic properties can be handled by JXPath provided that its class is registered with the JXPathIntrospector.
Example 5: Retrieving Multiple Results
JXPath can retrieve multiple objects from a graph. Note that the method called in this case is notgetValue, but iterate.
This returns a list of at most three books from the array of all books written by the author.public class Author { public Book[] getBooks(){ ... } } Author auth = new Author(); ... JXPathContext context = JXPathContext.newContext(auth); Iterator threeBooks = context.iterate("books[position() < 4]");
Example 6: Setting Properties
JXPath can be used to modify property values.public class Employee { public Address getAddress() { ... } public void setAddress(Address address) { ... } } Employee emp = new Employee(); Address addr = new Address(); ... JXPathContext context = JXPathContext.newContext(emp); context.setValue("address", addr); context.setValue("address/zipCode", "90190");
Example 7: Creating objects
JXPath can be used to create new objects. First, create a subclass of AbstractFactory and install it on the JXPathContext. Then call createPathAndSetValue() 55 instead of "setValue". JXPathContext will invoke your AbstractFactory when it discovers that an intermediate node of the path is null. It will not override existing nodes.public class AddressFactory extends AbstractFactory { public boolean createObject(JXPathContext context, Pointer pointer, Object parent, String name, int index){ if ((parent instanceof Employee) && name.equals("address"){ ((Employee)parent).setAddress(new Address()); return true; } return false; } } JXPathContext context = JXPathContext.newContext(emp); context.setFactory(new AddressFactory()); context.createPathAndSetValue("address/zipCode", "90190");
Example 8: Using Variables
JXPath supports the notion of variables. The XPath syntax for accessing variables is "$varName".You can also set variables using JXPath:public class Author { public Book[] getBooks(){ ... } } Author auth = new Author(); ... JXPathContext context = JXPathContext.newContext(auth); context.getVariables().declareVariable("index", new Integer(2)); Book secondBook = (Book)context.getValue("books[$index]");
Note: you can only change the value of an existing variable this way, you cannot define a new variable.context.setValue("$index", new Integer(3));
When a variable contains a JavaBean or a collection, you can traverse the bean or collection as well:
... context.getVariables().declareVariable("book", myBook); String title = (String)context.getValue("$book/title); Book array[] = new Book[]{...}; context.getVariables().declareVariable("books", array); String title = (String)context.getValue("$books[2]/title);
Example 9: Using Nested Contexts
If you need to use the same set of variable while interpreting XPaths with different beans, it makes sense to put the variables in a separate context and specify that context as a parent context every time you allocate a new JXPathContext for a JavaBean.JXPathContext varContext = JXPathContext.newContext(null); varContext.getVariables().declareVariable("title", "Java"); JXPathContext context = JXPathContext.newContext(varContext, auth); Iterator javaBooks = context.iterate("books[title = $title]");
Using Custom Variable Pools
By default, JXPathContext creates a HashMap of variables. However, you can substitute a custom implementation of the Variables interface to make JXPath work with an alternative source of variables. For example, you can define implementations of Variables that cover a servlet context, HTTP request or any similar structure.Example 10: Using Standard Extension Functions
Using the standard extension functions, you can call methods on objects, static methods on classes and create objects using any constructor. The class names should be fully qualified.Here's how you can create new objects:
Here's how you can call static methods:Book book = (Book) context.getValue( "org.apache.commons.jxpath.example.Book.new ('John Updike')");
Here's how you can call regular methods:Book book = (Book) context.getValue( "org. apache.commons.jxpath.example.Book.getBestBook('John Updike')");
As you can see, the target of the method is specified as the first parameter of the function.String firstName = (String)context.getValue("getAuthorsFirstName($book)");
Example 11: Using Custom Extension Functions
Collections of custom extension functions can be implemented as Functions objects or as Java classes, whose methods become extenstion functions.Let's say the following class implements various formatting operations:
We can register this class with a JXPathContext:public class Formats { public static String date(Date d, String pattern){ return new SimpleDateFormat(pattern).format(d); } ... }
You can also register whole packages of Java classes using PackageFunctions.context.setFunctions(new ClassFunctions(Formats.class, "format")); ... context.getVariables().declareVariable("today", new Date()); String today = (String)context.getValue("format:date($today, 'MM/dd/yyyy')");
Also, see FunctionLibrary, which is a class that allows you to register multiple sets of extension functions with the same JXPathContext.
Configuring JXPath
JXPath uses JavaBeans introspection to discover properties of JavaBeans. You can provide alternative property lists by supplying custom JXPathBeanInfo classes (see JXPathBeanInfo).Notes
- JXPath does not support DOM attributes for non-DOM objects. Even though XPaths like "para[@type='warning']" are legitimate, they will always produce empty results. The only attribute supported for JavaBeans is "name". The XPath "foo/bar" is equivalent to "foo[@name='bar']".
. Also see XML Path Language (XPath) Version 1.0
You will also find more information and examples in JXPath User's Guide
- Version:
- $Revision: 1.25 $ $Date: 2004/06/29 21:15:46 $
| Field Summary | |
private static JXPathContext |
compilationContext
|
protected java.lang.Object |
contextBean
|
private static JXPathContextFactory |
contextFactory
|
protected java.util.HashMap |
decimalFormats
|
protected AbstractFactory |
factory
|
protected Functions |
functions
|
private static PackageFunctions |
GENERIC_FUNCTIONS
|
protected IdentityManager |
idManager
|
protected KeyManager |
keyManager
|
private boolean |
lenient
|
private boolean |
lenientSet
|
private java.util.Locale |
locale
|
protected JXPathContext |
parentContext
|
protected Variables |
vars
|
| Constructor Summary | |
protected |
JXPathContext(JXPathContext parentContext,
java.lang.Object contextBean)
This constructor should remain protected - it is to be overridden by subclasses, but never explicitly invoked by clients. |
| Method Summary | |
static CompiledExpression |
compile(java.lang.String xpath)
Compiles the supplied XPath and returns an internal representation of the path that can then be evaluated. |
protected abstract CompiledExpression |
compilePath(java.lang.String xpath)
Overridden by each concrete implementation of JXPathContext to perform compilation. |
abstract Pointer |
createPath(java.lang.String xpath)
Creates missing elements of the path by invoking an AbstractFactory, which should first be installed on the context by calling "setFactory". |
abstract Pointer |
createPathAndSetValue(java.lang.String xpath,
java.lang.Object value)
The same as setValue, except it creates intermediate elements of the path by invoking an AbstractFactory, which should first be installed on the context by calling "setFactory". |
java.lang.Object |
getContextBean()
Returns the JavaBean associated with this context. |
private static JXPathContextFactory |
getContextFactory()
Acquires a context factory and caches it. |
abstract Pointer |
getContextPointer()
Returns a Pointer for the context bean. |
java.text.DecimalFormatSymbols |
getDecimalFormatSymbols(java.lang.String name)
|
AbstractFactory |
getFactory()
Returns the AbstractFactory installed on this context. |
Functions |
getFunctions()
Returns the set of functions installed on the context. |
IdentityManager |
getIdentityManager()
Returns this context's identity manager. |
KeyManager |
getKeyManager()
Returns this context's key manager. |
java.util.Locale |
getLocale()
Returns the locale set with setLocale. |
Pointer |
getNamespaceContextPointer()
Returns the namespace context pointer set with setNamespaceContextPointer() 55 or, if none has been specified, the context pointer otherwise. |
java.lang.String |
getNamespaceURI(java.lang.String prefix)
Given a prefix, returns a registered namespace URI. |
JXPathContext |
getParentContext()
Returns the parent context of this context or null. |
abstract Pointer |
getPointer(java.lang.String xpath)
Traverses the xpath and returns a Pointer. |
Pointer |
getPointerByID(java.lang.String id)
Locates a Node by its ID. |
Pointer |
getPointerByKey(java.lang.String key,
java.lang.String value)
Locates a Node by a key value. |
abstract JXPathContext |
getRelativeContext(Pointer pointer)
Returns a JXPathContext that is relative to the current JXPathContext. |
abstract java.lang.Object |
getValue(java.lang.String xpath)
Evaluates the xpath and returns the resulting object. |
abstract java.lang.Object |
getValue(java.lang.String xpath,
java.lang.Class requiredType)
Evaluates the xpath, converts the result to the specified class and returns the resulting object. |
Variables |
getVariables()
Returns the variable pool associated with the context. |
boolean |
isLenient()
|
abstract java.util.Iterator |
iterate(java.lang.String xpath)
Traverses the xpath and returns an Iterator of all results found for the path. |
abstract java.util.Iterator |
iteratePointers(java.lang.String xpath)
Traverses the xpath and returns an Iterator of Pointers. |
static JXPathContext |
newContext(JXPathContext parentContext,
java.lang.Object contextBean)
Creates a new JXPathContext with the specified bean as the root node and the specified parent context. |
static JXPathContext |
newContext(java.lang.Object contextBean)
Creates a new JXPathContext with the specified object as the root node. |
void |
registerNamespace(java.lang.String prefix,
java.lang.String namespaceURI)
Registers a namespace prefix. |
abstract void |
removeAll(java.lang.String xpath)
Removes all elements of the object graph described by the xpath. |
abstract void |
removePath(java.lang.String xpath)
Removes the element of the object graph described by the xpath. |
java.util.List |
selectNodes(java.lang.String xpath)
Finds all nodes that match the specified XPath. |
java.lang.Object |
selectSingleNode(java.lang.String xpath)
Finds the first object that matches the specified XPath. |
void |
setDecimalFormatSymbols(java.lang.String name,
java.text.DecimalFormatSymbols symbols)
Sets DecimalFormatSymbols for a given name. |
void |
setFactory(AbstractFactory factory)
Install an abstract factory that should be used by the createPath() and createPathAndSetValue()
methods. |
void |
setFunctions(Functions functions)
Install a library of extension functions. |
void |
setIdentityManager(IdentityManager idManager)
Install an identity manager that will be used by the context to look up a node by its ID. |
void |
setKeyManager(KeyManager keyManager)
Install a key manager that will be used by the context to look up a node by a key value. |
void |
setLenient(boolean lenient)
If the context is in the lenient mode, then getValue() returns null for inexistent paths. |
void |
setLocale(java.util.Locale locale)
Set the locale for this context. |
void |
setNamespaceContextPointer(Pointer namespaceContextPointer)
Namespace prefixes can be defined implicitly by specifying a pointer to a context where the namespaces are defined. |
abstract void |
setValue(java.lang.String xpath,
java.lang.Object value)
Modifies the value of the property described by the supplied xpath. |
void |
setVariables(Variables vars)
Installs a custom implementation of the Variables interface. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
parentContext
protected JXPathContext parentContext
contextBean
protected java.lang.Object contextBean
vars
protected Variables vars
functions
protected Functions functions
factory
protected AbstractFactory factory
locale
private java.util.Locale locale
lenientSet
private boolean lenientSet
lenient
private boolean lenient
idManager
protected IdentityManager idManager
keyManager
protected KeyManager keyManager
decimalFormats
protected java.util.HashMap decimalFormats
contextFactory
private static JXPathContextFactory contextFactory
compilationContext
private static JXPathContext compilationContext
GENERIC_FUNCTIONS
private static final PackageFunctions GENERIC_FUNCTIONS
| Constructor Detail |
JXPathContext
protected JXPathContext(JXPathContext parentContext, java.lang.Object contextBean)
- This constructor should remain protected - it is to be overridden by
subclasses, but never explicitly invoked by clients.
| Method Detail |
newContext
public static JXPathContext newContext(java.lang.Object contextBean)
- Creates a new JXPathContext with the specified object as the root node.
newContext
public static JXPathContext newContext(JXPathContext parentContext, java.lang.Object contextBean)
- Creates a new JXPathContext with the specified bean as the root node and
the specified parent context. Variables defined in a parent context can
be referenced in XPaths passed to the child context.
getContextFactory
private static JXPathContextFactory getContextFactory()
- Acquires a context factory and caches it.
getParentContext
public JXPathContext getParentContext()
- Returns the parent context of this context or null.
getContextBean
public java.lang.Object getContextBean()
- Returns the JavaBean associated with this context.
getContextPointer
public abstract Pointer getContextPointer()
- Returns a Pointer for the context bean.
getRelativeContext
public abstract JXPathContext getRelativeContext(Pointer pointer)
- Returns a JXPathContext that is relative to the current JXPathContext.
The supplied pointer becomes the context pointer of the new context.
The relative context inherits variables, extension functions, locale etc
from the parent context.
setVariables
public void setVariables(Variables vars)
- Installs a custom implementation of the Variables interface.
getVariables
public Variables getVariables()
- Returns the variable pool associated with the context. If no such
pool was specified with the
setVariables()method, returns the default implementation of Variables, BasicVariables.
setFunctions
public void setFunctions(Functions functions)
- Install a library of extension functions.
getFunctions
public Functions getFunctions()
- Returns the set of functions installed on the context.
setFactory
public void setFactory(AbstractFactory factory)
- Install an abstract factory that should be used by the
createPath()andcreatePathAndSetValue()methods.
getFactory
public AbstractFactory getFactory()
- Returns the AbstractFactory installed on this context.
If none has been installed and this context has a parent context,
returns the parent's factory. Otherwise returns null.
setLocale
public void setLocale(java.util.Locale locale)
- Set the locale for this context. The value of the "lang"
attribute as well as the the lang() function will be
affected by the locale. By default, JXPath uses
Locale.getDefault()
getLocale
public java.util.Locale getLocale()
- Returns the locale set with setLocale. If none was set and
the context has a parent, returns the parent's locale.
Otherwise, returns Locale.getDefault().
setDecimalFormatSymbols
public void setDecimalFormatSymbols(java.lang.String name, java.text.DecimalFormatSymbols symbols)
- Sets DecimalFormatSymbols for a given name. The DecimalFormatSymbols can
be referenced as the third, optional argument in the invocation of
format-number (number,format,decimal-format-name)function. By default, JXPath uses the symbols for the current locale.
getDecimalFormatSymbols
public java.text.DecimalFormatSymbols getDecimalFormatSymbols(java.lang.String name)
setLenient
public void setLenient(boolean lenient)
- If the context is in the lenient mode, then getValue() returns null
for inexistent paths. Otherwise, a path that does not map to
an existing property will throw an exception. Note that if the
property exists, but its value is null, the exception is not
thrown.
By default, lenient = false
isLenient
public boolean isLenient()
compile
public static CompiledExpression compile(java.lang.String xpath)
- Compiles the supplied XPath and returns an internal representation
of the path that can then be evaluated. Use CompiledExpressions
when you need to evaluate the same expression multiple times
and there is a convenient place to cache CompiledExpression
between invocations.
compilePath
protected abstract CompiledExpression compilePath(java.lang.String xpath)
- Overridden by each concrete implementation of JXPathContext
to perform compilation. Is called by
compile().
selectSingleNode
public java.lang.Object selectSingleNode(java.lang.String xpath)
- Finds the first object that matches the specified XPath. It is equivalent
to
getPointer(xpath).getNode(). Note, that this method produces the same result asgetValue()on object models like JavaBeans, but a different result for DOM/JDOM etc., because it returns the Node itself, rather than its textual contents.
selectNodes
public java.util.List selectNodes(java.lang.String xpath)
- Finds all nodes that match the specified XPath.
getValue
public abstract java.lang.Object getValue(java.lang.String xpath)
- Evaluates the xpath and returns the resulting object. Primitive
types are wrapped into objects.
getValue
public abstract java.lang.Object getValue(java.lang.String xpath, java.lang.Class requiredType)
- Evaluates the xpath, converts the result to the specified class and
returns the resulting object.
setValue
public abstract void setValue(java.lang.String xpath, java.lang.Object value)
- Modifies the value of the property described by the supplied xpath.
Will throw an exception if one of the following conditions occurs:
- The xpath does not in fact describe an existing property
- The property is not writable (no public, non-static set method)
createPath
public abstract Pointer createPath(java.lang.String xpath)
- Creates missing elements of the path by invoking an AbstractFactory,
which should first be installed on the context by calling "setFactory".
Will throw an exception if the AbstractFactory fails to create an instance for a path element.
createPathAndSetValue
public abstract Pointer createPathAndSetValue(java.lang.String xpath, java.lang.Object value)
- The same as setValue, except it creates intermediate elements of
the path by invoking an AbstractFactory, which should first be
installed on the context by calling "setFactory".
Will throw an exception if one of the following conditions occurs:
- Elements of the xpath aleady exist, but the path does not in fact describe an existing property
- The AbstractFactory fails to create an instance for an intermediate element.
- The property is not writable (no public, non-static set method)
removePath
public abstract void removePath(java.lang.String xpath)
- Removes the element of the object graph described by the xpath.
removeAll
public abstract void removeAll(java.lang.String xpath)
- Removes all elements of the object graph described by the xpath.
iterate
public abstract java.util.Iterator iterate(java.lang.String xpath)
- Traverses the xpath and returns an Iterator of all results found
for the path. If the xpath matches no properties
in the graph, the Iterator will be empty, but not null.
getPointer
public abstract Pointer getPointer(java.lang.String xpath)
- Traverses the xpath and returns a Pointer.
A Pointer provides easy access to a property.
If the xpath matches no properties
in the graph, the pointer will be null.
iteratePointers
public abstract java.util.Iterator iteratePointers(java.lang.String xpath)
- Traverses the xpath and returns an Iterator of Pointers.
A Pointer provides easy access to a property.
If the xpath matches no properties
in the graph, the Iterator be empty, but not null.
setIdentityManager
public void setIdentityManager(IdentityManager idManager)
- Install an identity manager that will be used by the context
to look up a node by its ID.
getIdentityManager
public IdentityManager getIdentityManager()
- Returns this context's identity manager. If none has been installed,
returns the identity manager of the parent context.
getPointerByID
public Pointer getPointerByID(java.lang.String id)
- Locates a Node by its ID.
setKeyManager
public void setKeyManager(KeyManager keyManager)
- Install a key manager that will be used by the context
to look up a node by a key value.
getKeyManager
public KeyManager getKeyManager()
- Returns this context's key manager. If none has been installed,
returns the key manager of the parent context.
getPointerByKey
public Pointer getPointerByKey(java.lang.String key, java.lang.String value)
- Locates a Node by a key value.
registerNamespace
public void registerNamespace(java.lang.String prefix, java.lang.String namespaceURI)
- Registers a namespace prefix.
getNamespaceURI
public java.lang.String getNamespaceURI(java.lang.String prefix)
- Given a prefix, returns a registered namespace URI. If the requested
prefix was not defined explicitly using the registerNamespace method,
JXPathContext will then check the context node to see if the prefix is
defined there. See
setNamespaceContextPointer 55 .
setNamespaceContextPointer
public void setNamespaceContextPointer(Pointer namespaceContextPointer)
- Namespace prefixes can be defined implicitly by specifying a pointer to a
context where the namespaces are defined. By default,
NamespaceContextPointer is the same as the Context Pointer, see
getContextPointer() 55
getNamespaceContextPointer
public Pointer getNamespaceContextPointer()
- Returns the namespace context pointer set with
setNamespaceContextPointer() 55
or, if none has been specified, the context pointer otherwise.
|
|||||||||
| Home >> All >> org >> apache >> commons >> [ jxpath overview ] | PREV CLASS NEXT CLASS | ||||||||
SUMMARY: JAVADOC | SOURCE | DOWNLOAD | NESTED | FIELD | CONSTR | METHOD |
DETAIL: FIELD | CONSTR | METHOD | ||||||||
JAVADOC
org.apache.commons.jxpath.JXPathContext