Class JXPathContext
- Direct Known Subclasses:
JXPathContextReferenceImpl
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.public class Employee { public String getFirstName(){ ... } } Employee emp = new Employee(); ... JXPathContext context = JXPathContext.newContext(emp); String fName = (String)context.getValue("firstName");In this example, we are using JXPath to access a property of the
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: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");In this case XPath is used to access a property of a nested bean.
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.
public class Integers { public int[] getNumbers(){ ... } } Integers ints = new Integers(); ... JXPathContext context = JXPathContext.newContext(ints); Integer thirdInt = (Integer)context.getValue("numbers[3]");A collection can be an arbitrary array or an instance of java.util. Collection.
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.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");
Often you will need to use the alternative syntax for accessing Map elements:
String homeZipCode = (String) context.getValue("addresses[@name='home']/zipCode");In this case, the key can be an expression, e.g. a variable.
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
.
public class Author {
public Book[] getBooks(){
...
}
}
Author auth = new Author();
...
JXPathContext context = JXPathContext.newContext(auth);
Iterator threeBooks = context.iterate("books[position() < 4]");
This returns a list of at most three books from the array of all books written by the author.
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()
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".
public class Author { public Book[] getBooks(){ ... } } Author auth = new Author(); ... JXPathContext context = JXPathContext.newContext(auth); context.getVariables().declareVariable("index", Integer.valueOf(2)); Book secondBook = (Book)context.getValue("books[$index]");
You can also set variables using JXPath:
context.setValue("$index", Integer.valueOf(3));
Note: you can only change the value of an existing variable this way, you cannot define a new variable.
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:
Book book = (Book) context.getValue("org.apache.commons.jxpath.example.Book.new ('John Updike')");
Here's how you can call static methods:
Book book = (Book) context.getValue("org. apache.commons.jxpath.example.Book.getBestBook('John Updike')");
Here's how you can call regular methods:
String firstName = (String) context.getValue("getAuthorsFirstName($book)");
As you can see, the target of the method is specified as the first parameter of the function.
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:
public class Formats { public static String date(Date d, String pattern){ return new SimpleDateFormat(pattern).format(d); } ... }
We can register this class with a JXPathContext:
context.setFunctions(new ClassFunctions(Formats.class, "format")); ... context.getVariables().declareVariable("today", new Date()); String today = (String)context.getValue("format:date($today, 'MM/dd/yyyy')");
You can also register whole packages of Java classes using PackageFunctions.
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']".
- The term matches no property in the graph is used throughout the documentation. It describes a property or
path that can be determined as not belonging to the graph. Determining whether a property or path belongs to the graph depends on the type of object being
used as
cotextBean
(seenewContext(Object)
). It is only possible strongly typed models where a specific Java model is used as context. It is not possible with dynamic models such Maps or DOM implementations.When a XPath does not match a property in the graph, the methods of this class that retrieve a pointer will generally behave in the following way, depending on the last value configured with
setLenient(boolean)
:- If
lenient
isfalse
(default) - methods will throwJXPathNotFoundException
. - If
lenient
istrue
- methods will throw no exception and return a value appropriate for that method to express the absence: might be a Javanull
or aPointer
whosePointer.getValue()
returnsnull
, depends on the method.
- If
See also:
You will also find more information and examples in the JXPath User's Guide
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Object
Context beanprotected HashMap
<String, DecimalFormatSymbols> Decimal format mapprotected AbstractFactory
AbstractFactoryprotected Functions
Functionsprotected IdentityManager
IdentityManagerprotected KeyManager
KeyManagerprotected JXPathContext
Parent contextprotected Variables
Variables -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
JXPathContext
(JXPathContext parentContext, Object contextBean) This constructor should remain protected - it is to be overridden by subclasses, but never explicitly invoked by clients. -
Method Summary
Modifier and TypeMethodDescriptionstatic CompiledExpression
Compiles the supplied XPath and returns an internal representation of the path that can then be evaluated.protected abstract CompiledExpression
compilePath
(String xpath) Overridden by each concrete implementation of JXPathContext to perform compilation.abstract Pointer
createPath
(String xpath) Creates missing elements of the path by invoking anAbstractFactory
, which should first be installed on the context by callingsetFactory(org.apache.commons.jxpath.AbstractFactory)
.abstract Pointer
createPathAndSetValue
(String xpath, Object value) The same as setValue, except it creates intermediate elements of the path by invoking anAbstractFactory
, which should first be installed on the context by callingsetFactory(org.apache.commons.jxpath.AbstractFactory)
.Returns the JavaBean associated with this context.abstract Pointer
Returns a Pointer for the context bean.Gets the named DecimalFormatSymbols.Returns the AbstractFactory installed on this context.Returns the set of functions installed on the context.Returns this context's identity manager.Returns this context's key manager.Returns the locale set with setLocale.Returns the namespace context pointer set withsetNamespaceContextPointer()
or, if none has been specified, the context pointer otherwise.getNamespaceURI
(String prefix) Given a prefix, returns a registered namespace URI.getNodeSetByKey
(String key, Object value) Locates a NodeSet by key/value.Returns the parent context of this context or null.abstract Pointer
getPointer
(String xpath) Traverses the XPath and returns a Pointer.getPointerByID
(String id) Locates a Node by its ID.getPointerByKey
(String key, String value) Locates a Node by a key value.Gets the prefix associated with the specifed namespace URI.abstract JXPathContext
getRelativeContext
(Pointer pointer) Returns a JXPathContext that is relative to the current JXPathContext.abstract Object
Evaluates the XPath and returns the resulting object.abstract Object
Evaluates the xpath, converts the result to the specified class and returns the resulting object.Returns the variable pool associated with the context.boolean
Tests whether this JXPathContext is lenient.abstract <E> Iterator
<E> Traverses the XPath and returns an Iterator of all results found for the path.iteratePointers
(String xpath) Traverses the XPath and returns an Iterator of Pointers.static JXPathContext
newContext
(Object contextBean) Creates a new JXPathContext with the specified object as the root node.static JXPathContext
newContext
(JXPathContext parentContext, Object contextBean) Creates a new JXPathContext with the specified bean as the root node and the specified parent context.void
registerNamespace
(String prefix, String namespaceURI) Registers a namespace prefix.abstract void
Removes all elements of the object graph described by the xpath.abstract void
removePath
(String xpath) Removes the element of the object graph described by the xpath.selectNodes
(String xpath) Finds all nodes that match the specified XPath.selectSingleNode
(String xpath) Finds the first object that matches the specified XPath.void
setDecimalFormatSymbols
(String name, DecimalFormatSymbols symbols) SetsDecimalFormatSymbols
for a given name.void
setExceptionHandler
(ExceptionHandler exceptionHandler) Sets the ExceptionHandler used by this context, if any.void
setFactory
(AbstractFactory factory) Install an abstract factory that should be used by thecreatePath()
andcreatePathAndSetValue()
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
Sets 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
Modifies the value of the property described by the supplied xpath.void
setVariables
(Variables vars) Installs a custom implementation of the Variables interface.
-
Field Details
-
parentContext
Parent context -
contextBean
Context bean -
vars
Variables -
functions
Functions -
factory
AbstractFactory -
idManager
IdentityManager -
keyManager
KeyManager -
decimalFormats
Decimal format map
-
-
Constructor Details
-
JXPathContext
This constructor should remain protected - it is to be overridden by subclasses, but never explicitly invoked by clients.- Parameters:
parentContext
- parent contextcontextBean
- Object
-
-
Method Details
-
compile
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.- Parameters:
xpath
- to compile- Returns:
- CompiledExpression
-
newContext
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.- Parameters:
parentContext
- parent contextcontextBean
- Object- Returns:
- JXPathContext
-
newContext
Creates a new JXPathContext with the specified object as the root node.- Parameters:
contextBean
- Object- Returns:
- JXPathContext
-
compilePath
Overridden by each concrete implementation of JXPathContext to perform compilation. Is called bycompile()
.- Parameters:
xpath
- to compile- Returns:
- CompiledExpression
-
createPath
Creates missing elements of the path by invoking anAbstractFactory
, which should first be installed on the context by callingsetFactory(org.apache.commons.jxpath.AbstractFactory)
.Will throw an exception if the AbstractFactory fails to create an instance for a path element.
- Parameters:
xpath
- indicating destination to create- Returns:
- pointer to new location
-
createPathAndSetValue
The same as setValue, except it creates intermediate elements of the path by invoking anAbstractFactory
, which should first be installed on the context by callingsetFactory(org.apache.commons.jxpath.AbstractFactory)
.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)
- Parameters:
xpath
- indicating position to createvalue
- to set- Returns:
- pointer to new location
-
getContextBean
Returns the JavaBean associated with this context.- Returns:
- Object
-
getContextPointer
Returns a Pointer for the context bean.- Returns:
- Pointer
-
getDecimalFormatSymbols
Gets the named DecimalFormatSymbols.- Parameters:
name
- key- Returns:
- DecimalFormatSymbols
- See Also:
-
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.- Returns:
- AbstractFactory
-
getFunctions
Returns the set of functions installed on the context.- Returns:
- Functions
-
getIdentityManager
Returns this context's identity manager. If none has been installed, returns the identity manager of the parent context.- Returns:
- IdentityManager
-
getKeyManager
Returns this context's key manager. If none has been installed, returns the key manager of the parent context.- Returns:
- KeyManager
-
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().- Returns:
- Locale
-
getNamespaceContextPointer
Returns the namespace context pointer set withsetNamespaceContextPointer()
or, if none has been specified, the context pointer otherwise.- Returns:
- The namespace context pointer.
-
getNamespaceURI
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. SeesetNamespaceContextPointer
.- Parameters:
prefix
- The namespace prefix to look up- Returns:
- namespace URI or null if the prefix is undefined.
-
getNodeSetByKey
Locates a NodeSet by key/value.- Parameters:
key
- stringvalue
- object- Returns:
- NodeSet found
-
getParentContext
Returns the parent context of this context or null.- Returns:
- JXPathContext
-
getPointer
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 behavior depends on the value that has been configured with
setLenient(boolean)
:false
(default) the method will throw aJXPathNotFoundException
.true
the method returns a pointer whosePointer.getValue()
method will always return null.
- Parameters:
xpath
- desired- Returns:
- Pointer A
Pointer
, nevernull
. - Throws:
JXPathNotFoundException
- see method description.
-
getPointerByID
Locates a Node by its ID.- Parameters:
id
- is the ID of the sought node.- Returns:
- Pointer
-
getPointerByKey
Locates a Node by a key value.- Parameters:
key
- stringvalue
- string- Returns:
- Pointer found
-
getPrefix
Gets the prefix associated with the specifed namespace URI.- Parameters:
namespaceURI
- the ns URI to check.- Returns:
- String prefix
- Since:
- JXPath 1.3
-
getRelativeContext
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.- Parameters:
pointer
- Pointer- Returns:
- JXPathContext
-
getValue
Evaluates the XPath and returns the resulting object. Primitive types are wrapped into objects.- Parameters:
xpath
- to evaluate- Returns:
- Object found
-
getValue
Evaluates the xpath, converts the result to the specified class and returns the resulting object.- Parameters:
xpath
- to evaluaterequiredType
- required type- Returns:
- Object found
-
getVariables
Returns the variable pool associated with the context. If no such pool was specified with thesetVariables(org.apache.commons.jxpath.Variables)
method, returns the default implementation of Variables,BasicVariables
.- Returns:
- Variables
-
isLenient
Tests whether this JXPathContext is lenient.- Returns:
- boolean
- See Also:
-
iterate
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.- Type Parameters:
E
- the type of elements returned by the iterator.- Parameters:
xpath
- to iterate- Returns:
- Iterator
-
iteratePointers
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.- Parameters:
xpath
- to iterate- Returns:
- Iterator
-
registerNamespace
Registers a namespace prefix.- Parameters:
prefix
- A namespace prefixnamespaceURI
- A URI for that prefix
-
removeAll
Removes all elements of the object graph described by the xpath.- Parameters:
xpath
- indicating positions to remove
-
removePath
Removes the element of the object graph described by the xpath.- Parameters:
xpath
- indicating position to remove
-
selectNodes
Finds all nodes that match the specified XPath.- Parameters:
xpath
- the xpath to be evaluated- Returns:
- a list of found objects
-
selectSingleNode
Finds the first object that matches the specified XPath. It is equivalent togetPointer(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.- Parameters:
xpath
- the xpath to be evaluated- Returns:
- the found object
-
setDecimalFormatSymbols
SetsDecimalFormatSymbols
for a given name. The DecimalFormatSymbols can be referenced as the third, optional argument in the invocation offormat-number (number,format,decimal-format-name)
function. By default, JXPath uses the symbols for the current locale.- Parameters:
name
- the format name or null for default format.symbols
- DecimalFormatSymbols
-
setExceptionHandler
Sets the ExceptionHandler used by this context, if any.- Parameters:
exceptionHandler
- to set- Since:
- 1.4.0
-
setFactory
Install an abstract factory that should be used by thecreatePath()
andcreatePathAndSetValue()
methods.- Parameters:
factory
- AbstractFactory
-
setFunctions
Install a library of extension functions.- Parameters:
functions
- Functions- See Also:
-
setIdentityManager
Install an identity manager that will be used by the context to look up a node by its ID.- Parameters:
idManager
- IdentityManager to set
-
setKeyManager
Install a key manager that will be used by the context to look up a node by a key value.- Parameters:
keyManager
- KeyManager
-
setLenient
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
- Parameters:
lenient
- flag
-
setLocale
Sets the locale for this context. The value of the "lang" attribute as well as the lang() function will be affected by the locale. By default, JXPath usesLocale.getDefault()
- Parameters:
locale
- Locale
-
setNamespaceContextPointer
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, seegetContextPointer()
- Parameters:
namespaceContextPointer
- The pointer to the context where prefixes used in XPath expressions should be resolved.
-
setValue
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)
- Parameters:
xpath
- indicating positionvalue
- to set
-
setVariables
Installs a custom implementation of the Variables interface.- Parameters:
vars
- Variables
-