Package org.apache.commons.jxpath.ri
Class JXPathContextReferenceImpl
- java.lang.Object
-
- org.apache.commons.jxpath.JXPathContext
-
- org.apache.commons.jxpath.ri.JXPathContextReferenceImpl
-
public class JXPathContextReferenceImpl extends JXPathContext
The reference implementation of JXPathContext.- Version:
- $Revision: 670727 $ $Date: 2008-06-23 15:10:38 -0500 (Mon, 23 Jun 2008) $
-
-
Field Summary
Fields Modifier and Type Field Description private static intCLEANUP_THRESHOLDprivate static intcleanupCountprivate static java.util.Mapcompiledprivate static CompilerCOMPILERprivate PointercontextPointerprotected NamespaceResolvernamespaceResolverNamespace resolverprivate static java.util.VectornodeFactoriesprivate static NodePointerFactory[]nodeFactoryArrayprivate PointerrootPointerstatic booleanUSE_SOFT_CACHEChange this tofalseto disable soft caching of CompiledExpressions.-
Fields inherited from class org.apache.commons.jxpath.JXPathContext
contextBean, decimalFormats, factory, functions, idManager, keyManager, parentContext, vars
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedJXPathContextReferenceImpl(JXPathContext parentContext, java.lang.Object contextBean)Create a new JXPathContextReferenceImpl.JXPathContextReferenceImpl(JXPathContext parentContext, java.lang.Object contextBean, Pointer contextPointer)Create a new JXPathContextReferenceImpl.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidaddNodePointerFactory(NodePointerFactory factory)Call this with a custom NodePointerFactory to add support for additional types of objects.static java.lang.ObjectallocateConditionally(java.lang.String className, java.lang.String existenceCheckClassName)Checks if existenceCheckClass exists on the class path.private voidcheckSimplePath(Expression expr)Checks if the path follows the JXPath restrictions on the type of path that can be passed to create...private ExpressioncompileExpression(java.lang.String xpath)Compile the given expression.protected CompiledExpressioncompilePath(java.lang.String xpath)Overridden by each concrete implementation of JXPathContext to perform compilation.private static voidcreateNodeFactoryArray()Create the default node factory array.PointercreatePath(java.lang.String xpath)Creates missing elements of the path by invoking anAbstractFactory, which should first be installed on the context by callingJXPathContext.setFactory(org.apache.commons.jxpath.AbstractFactory).PointercreatePath(java.lang.String xpath, Expression expr)Create the given path.PointercreatePathAndSetValue(java.lang.String xpath, java.lang.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 callingJXPathContext.setFactory(org.apache.commons.jxpath.AbstractFactory).PointercreatePathAndSetValue(java.lang.String xpath, Expression expr, java.lang.Object value)Create the given path setting its value to value.EvalContextgetAbsoluteRootContext()Get the absolute root context.private NodePointergetAbsoluteRootPointer()Get absolute root pointer.protected CompilergetCompiler()Returns a static instance of TreeCompiler.PointergetContextPointer()Returns a Pointer for the context bean.private EvalContextgetEvalContext()Get the evaluation context.FunctiongetFunction(QName functionName, java.lang.Object[] parameters)Get the named Function.PointergetNamespaceContextPointer()Returns the namespace context pointer set withsetNamespaceContextPointer()or, if none has been specified, the context pointer otherwise.NamespaceResolvergetNamespaceResolver()Get the namespace resolver.java.lang.StringgetNamespaceURI(java.lang.String prefix)Given a prefix, returns a registered namespace URI.static NodePointerFactory[]getNodePointerFactories()Get the registered NodePointerFactories.PointergetPointer(java.lang.String xpath)Traverses the xpath and returns a Pointer.PointergetPointer(java.lang.String xpath, Expression expr)Get a pointer to the specified path/expression.java.lang.StringgetPrefix(java.lang.String namespaceURI)Get the prefix associated with the specifed namespace URI.JXPathContextgetRelativeContext(Pointer pointer)Returns a JXPathContext that is relative to the current JXPathContext.java.lang.ObjectgetValue(java.lang.String xpath)Traverses the xpath and returns the resulting object.java.lang.ObjectgetValue(java.lang.String xpath, java.lang.Class requiredType)Calls getValue(xpath), converts the result to the required type and returns the result of the conversion.java.lang.ObjectgetValue(java.lang.String xpath, Expression expr)Get the value indicated.java.lang.ObjectgetValue(java.lang.String xpath, Expression expr, java.lang.Class requiredType)Get the value indicated.NodePointergetVariablePointer(QName name)Get a VariablePointer for the given variable name.java.util.Iteratoriterate(java.lang.String xpath)Traverses the xpath and returns a Iterator of all results found for the path.java.util.Iteratoriterate(java.lang.String xpath, Expression expr)Traverses the xpath and returns a Iterator of all results found for the path.java.util.IteratoriteratePointers(java.lang.String xpath)Traverses the xpath and returns an Iterator of Pointers.java.util.IteratoriteratePointers(java.lang.String xpath, Expression expr)Traverses the xpath and returns an Iterator of Pointers.voidregisterNamespace(java.lang.String prefix, java.lang.String namespaceURI)Registers a namespace prefix.voidremoveAll(java.lang.String xpath)Removes all elements of the object graph described by the xpath.voidremoveAll(java.lang.String xpath, Expression expr)Remove all matching nodes.voidremovePath(java.lang.String xpath)Removes the element of the object graph described by the xpath.voidremovePath(java.lang.String xpath, Expression expr)Remove the specified path.voidsetNamespaceContextPointer(Pointer pointer)Namespace prefixes can be defined implicitly by specifying a pointer to a context where the namespaces are defined.voidsetValue(java.lang.String xpath, java.lang.Object value)Modifies the value of the property described by the supplied xpath.voidsetValue(java.lang.String xpath, Expression expr, java.lang.Object value)Set the value of xpath to value.private PointersetValue(java.lang.String xpath, Expression expr, java.lang.Object value, boolean create)Set the specified value.-
Methods inherited from class org.apache.commons.jxpath.JXPathContext
compile, getContextBean, getDecimalFormatSymbols, getFactory, getFunctions, getIdentityManager, getKeyManager, getLocale, getNodeSetByKey, getParentContext, getPointerByID, getPointerByKey, getVariables, isLenient, newContext, newContext, selectNodes, selectSingleNode, setDecimalFormatSymbols, setFactory, setFunctions, setIdentityManager, setKeyManager, setLenient, setLocale, setVariables
-
-
-
-
Field Detail
-
USE_SOFT_CACHE
public static final boolean USE_SOFT_CACHE
Change this tofalseto disable soft caching of CompiledExpressions.- See Also:
- Constant Field Values
-
COMPILER
private static final Compiler COMPILER
-
compiled
private static java.util.Map compiled
-
cleanupCount
private static int cleanupCount
-
nodeFactoryArray
private static NodePointerFactory[] nodeFactoryArray
-
CLEANUP_THRESHOLD
private static final int CLEANUP_THRESHOLD
- See Also:
- Constant Field Values
-
nodeFactories
private static final java.util.Vector nodeFactories
-
namespaceResolver
protected NamespaceResolver namespaceResolver
Namespace resolver
-
rootPointer
private Pointer rootPointer
-
contextPointer
private Pointer contextPointer
-
-
Constructor Detail
-
JXPathContextReferenceImpl
protected JXPathContextReferenceImpl(JXPathContext parentContext, java.lang.Object contextBean)
Create a new JXPathContextReferenceImpl.- Parameters:
parentContext- parent contextcontextBean- Object
-
JXPathContextReferenceImpl
public JXPathContextReferenceImpl(JXPathContext parentContext, java.lang.Object contextBean, Pointer contextPointer)
Create a new JXPathContextReferenceImpl.- Parameters:
parentContext- parent contextcontextBean- ObjectcontextPointer- context pointer
-
-
Method Detail
-
createNodeFactoryArray
private static void createNodeFactoryArray()
Create the default node factory array.
-
addNodePointerFactory
public static void addNodePointerFactory(NodePointerFactory factory)
Call this with a custom NodePointerFactory to add support for additional types of objects. Make sure the factory returns a name that puts it in the right position on the list of factories.- Parameters:
factory- NodePointerFactory to add
-
getNodePointerFactories
public static NodePointerFactory[] getNodePointerFactories()
Get the registered NodePointerFactories.- Returns:
- NodePointerFactory[]
-
getCompiler
protected Compiler getCompiler()
Returns a static instance of TreeCompiler. Override this to return an alternate compiler.- Returns:
- Compiler
-
compilePath
protected CompiledExpression compilePath(java.lang.String xpath)
Description copied from class:JXPathContextOverridden by each concrete implementation of JXPathContext to perform compilation. Is called bycompile().- Specified by:
compilePathin classJXPathContext- Parameters:
xpath- to compile- Returns:
- CompiledExpression
-
compileExpression
private Expression compileExpression(java.lang.String xpath)
Compile the given expression.- Parameters:
xpath- to compile- Returns:
- Expression
-
getValue
public java.lang.Object getValue(java.lang.String xpath)
Traverses the xpath and returns the resulting object. Primitive types are wrapped into objects.- Specified by:
getValuein classJXPathContext- Parameters:
xpath- expression- Returns:
- Object found
-
getValue
public java.lang.Object getValue(java.lang.String xpath, Expression expr)Get the value indicated.- Parameters:
xpath- Stringexpr- Expression- Returns:
- Object
-
getValue
public java.lang.Object getValue(java.lang.String xpath, java.lang.Class requiredType)Calls getValue(xpath), converts the result to the required type and returns the result of the conversion.- Specified by:
getValuein classJXPathContext- Parameters:
xpath- expressionrequiredType- Class- Returns:
- Object
-
getValue
public java.lang.Object getValue(java.lang.String xpath, Expression expr, java.lang.Class requiredType)Get the value indicated.- Parameters:
xpath- expressionexpr- compiled ExpressionrequiredType- Class- Returns:
- Object
-
iterate
public java.util.Iterator iterate(java.lang.String xpath)
Traverses the xpath and returns a Iterator of all results found for the path. If the xpath matches no properties in the graph, the Iterator will not be null.- Specified by:
iteratein classJXPathContext- Parameters:
xpath- expression- Returns:
- Iterator
-
iterate
public java.util.Iterator iterate(java.lang.String xpath, Expression expr)Traverses the xpath and returns a Iterator of all results found for the path. If the xpath matches no properties in the graph, the Iterator will not be null.- Parameters:
xpath- expressionexpr- compiled Expression- Returns:
- Iterator
-
getPointer
public Pointer getPointer(java.lang.String xpath)
Description copied from class:JXPathContextTraverses 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.- Specified by:
getPointerin classJXPathContext- Parameters:
xpath- desired- Returns:
- Pointer
-
getPointer
public Pointer getPointer(java.lang.String xpath, Expression expr)
Get a pointer to the specified path/expression.- Parameters:
xpath- Stringexpr- compiled Expression- Returns:
- Pointer
-
setValue
public void setValue(java.lang.String xpath, java.lang.Object value)Description copied from class:JXPathContextModifies 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)
- Specified by:
setValuein classJXPathContext- Parameters:
xpath- indicating positionvalue- to set
-
setValue
public void setValue(java.lang.String xpath, Expression expr, java.lang.Object value)Set the value of xpath to value.- Parameters:
xpath- pathexpr- compiled Expressionvalue- Object
-
createPath
public Pointer createPath(java.lang.String xpath)
Description copied from class:JXPathContextCreates missing elements of the path by invoking anAbstractFactory, which should first be installed on the context by callingJXPathContext.setFactory(org.apache.commons.jxpath.AbstractFactory).Will throw an exception if the AbstractFactory fails to create an instance for a path element.
- Specified by:
createPathin classJXPathContext- Parameters:
xpath- indicating destination to create- Returns:
- pointer to new location
-
createPath
public Pointer createPath(java.lang.String xpath, Expression expr)
Create the given path.- Parameters:
xpath- Stringexpr- compiled Expression- Returns:
- resulting Pointer
-
createPathAndSetValue
public Pointer createPathAndSetValue(java.lang.String xpath, java.lang.Object value)
Description copied from class:JXPathContextThe same as setValue, except it creates intermediate elements of the path by invoking anAbstractFactory, which should first be installed on the context by callingJXPathContext.setFactory(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)
- Specified by:
createPathAndSetValuein classJXPathContext- Parameters:
xpath- indicating position to createvalue- to set- Returns:
- pointer to new location
-
createPathAndSetValue
public Pointer createPathAndSetValue(java.lang.String xpath, Expression expr, java.lang.Object value)
Create the given path setting its value to value.- Parameters:
xpath- Stringexpr- compiled Expressionvalue- Object- Returns:
- resulting Pointer
-
setValue
private Pointer setValue(java.lang.String xpath, Expression expr, java.lang.Object value, boolean create)
Set the specified value.- Parameters:
xpath- pathexpr- compiled Expressionvalue- destination valuecreate- whether to create missing node(s)- Returns:
- Pointer created
-
checkSimplePath
private void checkSimplePath(Expression expr)
Checks if the path follows the JXPath restrictions on the type of path that can be passed to create... methods.- Parameters:
expr- Expression to check
-
iteratePointers
public 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.- Specified by:
iteratePointersin classJXPathContext- Parameters:
xpath- expression- Returns:
- Iterator
-
iteratePointers
public java.util.Iterator iteratePointers(java.lang.String xpath, Expression expr)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- expressionexpr- compiled Expression- Returns:
- Iterator
-
removePath
public void removePath(java.lang.String xpath)
Description copied from class:JXPathContextRemoves the element of the object graph described by the xpath.- Specified by:
removePathin classJXPathContext- Parameters:
xpath- indicating position to remove
-
removePath
public void removePath(java.lang.String xpath, Expression expr)Remove the specified path.- Parameters:
xpath- expressionexpr- compiled Expression
-
removeAll
public void removeAll(java.lang.String xpath)
Description copied from class:JXPathContextRemoves all elements of the object graph described by the xpath.- Specified by:
removeAllin classJXPathContext- Parameters:
xpath- indicating positions to remove
-
removeAll
public void removeAll(java.lang.String xpath, Expression expr)Remove all matching nodes.- Parameters:
xpath- expressionexpr- compiled Expression
-
getRelativeContext
public JXPathContext getRelativeContext(Pointer pointer)
Description copied from class:JXPathContextReturns 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.- Specified by:
getRelativeContextin classJXPathContext- Parameters:
pointer- Pointer- Returns:
- JXPathContext
-
getContextPointer
public Pointer getContextPointer()
Description copied from class:JXPathContextReturns a Pointer for the context bean.- Specified by:
getContextPointerin classJXPathContext- Returns:
- Pointer
-
getAbsoluteRootPointer
private NodePointer getAbsoluteRootPointer()
Get absolute root pointer.- Returns:
- NodePointer
-
getEvalContext
private EvalContext getEvalContext()
Get the evaluation context.- Returns:
- EvalContext
-
getAbsoluteRootContext
public EvalContext getAbsoluteRootContext()
Get the absolute root context.- Returns:
- EvalContext
-
getVariablePointer
public NodePointer getVariablePointer(QName name)
Get a VariablePointer for the given variable name.- Parameters:
name- variable name- Returns:
- NodePointer
-
getFunction
public Function getFunction(QName functionName, java.lang.Object[] parameters)
Get the named Function.- Parameters:
functionName- nameparameters- function args- Returns:
- Function
-
registerNamespace
public void registerNamespace(java.lang.String prefix, java.lang.String namespaceURI)Description copied from class:JXPathContextRegisters a namespace prefix.- Overrides:
registerNamespacein classJXPathContext- Parameters:
prefix- A namespace prefixnamespaceURI- A URI for that prefix
-
getNamespaceURI
public java.lang.String getNamespaceURI(java.lang.String prefix)
Description copied from class:JXPathContextGiven 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.- Overrides:
getNamespaceURIin classJXPathContext- Parameters:
prefix- The namespace prefix to look up- Returns:
- namespace URI or null if the prefix is undefined.
-
getPrefix
public java.lang.String getPrefix(java.lang.String namespaceURI)
Get the prefix associated with the specifed namespace URI.- Overrides:
getPrefixin classJXPathContext- Parameters:
namespaceURI- the ns URI to check.- Returns:
- String prefix
- See Also:
JXPathContext.getPrefix(java.lang.String)
-
setNamespaceContextPointer
public void setNamespaceContextPointer(Pointer pointer)
Description copied from class:JXPathContextNamespace 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()- Overrides:
setNamespaceContextPointerin classJXPathContext- Parameters:
pointer- The pointer to the context where prefixes used in XPath expressions should be resolved.
-
getNamespaceContextPointer
public Pointer getNamespaceContextPointer()
Description copied from class:JXPathContextReturns the namespace context pointer set withsetNamespaceContextPointer()or, if none has been specified, the context pointer otherwise.- Overrides:
getNamespaceContextPointerin classJXPathContext- Returns:
- The namespace context pointer.
-
getNamespaceResolver
public NamespaceResolver getNamespaceResolver()
Get the namespace resolver.- Returns:
- NamespaceResolver
-
allocateConditionally
public static java.lang.Object allocateConditionally(java.lang.String className, java.lang.String existenceCheckClassName)Checks if existenceCheckClass exists on the class path. If so, allocates an instance of the specified class, otherwise returns null.- Parameters:
className- to instantiateexistenceCheckClassName- guard class- Returns:
- className instance
-
-