public class XPathBuilder
extends org.apache.camel.support.service.ServiceSupport
implements org.apache.camel.CamelContextAware, org.apache.camel.Expression, org.apache.camel.Predicate, org.apache.camel.spi.NamespaceAware, org.apache.camel.spi.ExpressionResultTypeAware
stringResult()
An XPath object is not thread-safe and not reentrant. In other words, it is the application's responsibility to make
sure that one XPath object is not used from more than one thread at any given time, and while the evaluate method is
invoked, applications may not recursively call the evaluate method.
This implementation is thread safe by using thread locals and pooling to allow concurrency.
Important: After configuring the XPathBuilder its advised to invoke BaseService.start() to prepare the
builder before using; though the builder will auto-start on first use.XPathConstants.NODESET| Constructor and Description |
|---|
XPathBuilder(String text) |
| Modifier and Type | Method and Description |
|---|---|
XPathBuilder |
booleanResult()
Sets the expression result type to
XPathConstants.BOOLEAN |
protected XPathFunctionResolver |
createDefaultFunctionResolver(XPathFunctionResolver parent) |
protected static XPathFactory |
createDefaultXPathFactory() |
protected DefaultNamespaceContext |
createNamespaceContext(XPathFactory factory) |
protected XPathExpression |
createTraceNamespaceExpression() |
protected XPathExpression |
createXPathExpression()
Creates a new xpath expression as there we no available in the pool.
|
protected XPathFactory |
createXPathFactory() |
XPathBuilder |
documentType(Class<?> documentType)
Configures the document type to use.
|
protected Object |
doGetDocument(org.apache.camel.Exchange exchange,
Object body) |
protected Object |
doInEvaluateAs(XPathExpression xpathExpression,
org.apache.camel.Exchange exchange,
QName resultQName) |
void |
doStart() |
void |
doStop() |
void |
enableSaxon()
Enables Saxon on this particular XPath expression, as
saxon() sets the default static XPathFactory which
may have already been initialised by previous XPath expressions |
String |
evaluate(org.apache.camel.CamelContext context,
Object body)
Evaluates the given xpath using the provided body as a String return type.
|
<T> T |
evaluate(org.apache.camel.CamelContext context,
Object body,
Class<T> type)
Evaluates the given xpath using the provided body.
|
Object |
evaluate(org.apache.camel.Exchange exchange) |
<T> T |
evaluate(org.apache.camel.Exchange exchange,
Class<T> type) |
protected Object |
evaluateAs(org.apache.camel.Exchange exchange,
QName resultQName)
Evaluates the expression as the given result type
|
XPathBuilder |
factory(XPathFactory xpathFactory)
Configures to use the provided XPath factory.
|
XPathBuilder |
factoryClassName(String factoryClassName)
Sets the factory class name to use
|
XPathBuilder |
functionResolver(XPathFunctionResolver functionResolver)
Sets the
XPathFunctionResolver instance to use on these XPath expressions |
XPathFunction |
getBodyFunction()
Gets the
XPathFunction for getting the input message body. |
org.apache.camel.CamelContext |
getCamelContext() |
protected Object |
getDocument(org.apache.camel.Exchange exchange,
Object body)
Strategy method to extract the document from the exchange.
|
Class<?> |
getDocumentType() |
String |
getExpressionText() |
String |
getFactoryClassName() |
XPathFunctionResolver |
getFunctionResolver() |
XPathFunction |
getHeaderFunction()
Gets the
XPathFunction for getting the input message header. |
String |
getHeaderName() |
DefaultNamespaceContext |
getNamespaceContext()
Gets the namespace context, can be null if no custom context has been assigned.
|
Map<String,String> |
getNamespaces() |
String |
getObjectModelUri() |
XPathFunction |
getOutBodyFunction()
Gets the
XPathFunction for getting the output message body. |
XPathFunction |
getOutHeaderFunction()
Gets the
XPathFunction for getting the output message header. |
XPathFunction |
getPropertiesFunction()
Gets the
XPathFunction for getting the exchange properties. |
QName |
getResultQName() |
Class<?> |
getResultType() |
XPathFunction |
getSimpleFunction()
Gets the
XPathFunction for executing simple language as
xpath function. |
String |
getText() |
XPathFactory |
getXPathFactory()
Gets the xpath factory, can be null if no custom factory has been assigned.
|
void |
init(org.apache.camel.CamelContext context) |
protected boolean |
isInputStreamNeeded(org.apache.camel.Exchange exchange)
Checks whether we need an
InputStream to access the message body. |
protected boolean |
isInputStreamNeeded(org.apache.camel.Exchange exchange,
String headerName)
Checks whether we need an
InputStream to access the message header. |
protected boolean |
isInputStreamNeededForObject(org.apache.camel.Exchange exchange,
Object obj)
Checks whether we need an
InputStream to access this object
Depending on the content in the object, we may not need to convert to InputStream. |
boolean |
isLogNamespaces() |
boolean |
isPreCompile() |
boolean |
isThreadSafety() |
boolean |
isUseSaxon() |
XPathBuilder |
logNamespaces()
Activates trace logging of all discovered namespaces in the message - to simplify debugging namespace-related
issues
Namespaces are printed in Hashmap style
{xmlns:prefix=[namespaceURI], xmlns:prefix=[namespaceURI]}. |
boolean |
matches(org.apache.camel.CamelContext context,
Object body)
Matches the given xpath using the provided body.
|
boolean |
matches(org.apache.camel.Exchange exchange) |
XPathBuilder |
namespace(String prefix,
String uri)
Registers the namespace prefix and URI with the builder so that the prefix can be used in XPath expressions
|
XPathBuilder |
namespaces(org.apache.camel.support.builder.Namespaces namespaces)
Registers namespaces with the builder so that the registered prefixes can be used in XPath expressions
|
XPathBuilder |
nodeResult()
Sets the expression result type to
XPathConstants.NODE |
XPathBuilder |
nodeSetResult()
Sets the expression result type to
XPathConstants.NODESET |
XPathBuilder |
numberResult()
Sets the expression result type to
XPathConstants.NUMBER |
XPathBuilder |
objectModel(String uri)
Sets the object model URI to use
|
protected void |
populateDefaultNamespaces(DefaultNamespaceContext context)
Populate a number of standard prefixes if they are not already there
|
XPathBuilder |
preCompile(boolean preCompile)
Whether to enable pre-compiling the xpath expression during initialization phase.
|
XPathBuilder |
resultType(Class<?> resultType)
Sets the expression result type to the given
resultType |
XPathBuilder |
saxon()
Configures to use Saxon as the XPathFactory which allows you to use XPath 2.0 functions which may not be part of
the build in JDK XPath parser.
|
void |
setBodyFunction(XPathFunction bodyFunction) |
void |
setCamelContext(org.apache.camel.CamelContext camelContext) |
void |
setDocumentType(Class<?> documentType) |
void |
setFactoryClassName(String factoryClassName) |
void |
setFunctionResolver(XPathFunctionResolver functionResolver) |
void |
setHeaderFunction(XPathFunction headerFunction) |
void |
setHeaderName(String headerName) |
void |
setLogNamespaces(boolean logNamespaces) |
void |
setNamespaceContext(DefaultNamespaceContext namespaceContext) |
protected void |
setNamespaceIfNotPresent(DefaultNamespaceContext context,
String prefix,
String uri) |
void |
setNamespaces(Map<String,String> namespaces) |
void |
setObjectModelUri(String objectModelUri) |
void |
setOutBodyFunction(XPathFunction outBodyFunction) |
void |
setOutHeaderFunction(XPathFunction outHeaderFunction) |
void |
setPreCompile(boolean preCompile) |
void |
setPropertiesFunction(XPathFunction propertiesFunction) |
void |
setResultQName(QName resultQName) |
void |
setResultType(Class<?> resultType) |
void |
setSimpleFunction(XPathFunction simpleFunction) |
void |
setThreadSafety(boolean threadSafety) |
void |
setUseSaxon(boolean useSaxon)
Whether to enable Saxon on this particular XPath expression.
|
void |
setXPathFactory(XPathFactory xpathFactory) |
XPathBuilder |
stringResult()
Sets the expression result type to
XPathConstants.STRING |
XPathBuilder |
threadSafety(boolean threadSafety)
Whether to enable thread-safety for the returned result of the xpath expression.
|
String |
toString() |
XPathBuilder |
variable(String name,
Object value)
Registers a variable (in the global namespace) which can be referred to from XPath expressions
|
static XPathBuilder |
xpath(String text) |
static XPathBuilder |
xpath(String text,
Class<?> resultType) |
build, doBuild, doFail, doInit, doLifecycleChange, doResume, doShutdown, doSuspend, fail, getStatus, init, isBuild, isInit, isNew, isRunAllowed, isShutdown, isStarted, isStarting, isStartingOrStarted, isStopped, isStopping, isStoppingOrStopped, isSuspended, isSuspending, isSuspendingOrSuspended, resume, shutdown, start, stop, suspendclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitpublic XPathBuilder(String text)
text - The XPath expressionpublic static XPathBuilder xpath(String text)
text - The XPath expressionpublic static XPathBuilder xpath(String text, Class<?> resultType)
text - The XPath expressionresultType - The result type that the XPath expression will return.public void init(org.apache.camel.CamelContext context)
init in interface org.apache.camel.Expressioninit in interface org.apache.camel.Predicatepublic org.apache.camel.CamelContext getCamelContext()
getCamelContext in interface org.apache.camel.CamelContextAwarepublic void setCamelContext(org.apache.camel.CamelContext camelContext)
setCamelContext in interface org.apache.camel.CamelContextAwarepublic boolean matches(org.apache.camel.Exchange exchange)
matches in interface org.apache.camel.Predicatepublic <T> T evaluate(org.apache.camel.Exchange exchange,
Class<T> type)
evaluate in interface org.apache.camel.Expressionpublic boolean matches(org.apache.camel.CamelContext context,
Object body)
context - the camel contextbody - the bodypublic <T> T evaluate(org.apache.camel.CamelContext context,
Object body,
Class<T> type)
XPathConstants.NODESET as the type used during xpath
evaluation. The output from xpath is then afterwards type converted using Camel's type converter to the given
type.
If you want to evaluate xpath using a different type, then call setResultType(Class) prior to calling
this evaluate method.context - the camel contextbody - the bodytype - the type to returnpublic String evaluate(org.apache.camel.CamelContext context, Object body)
context - the camel contextbody - the bodypublic XPathBuilder booleanResult()
XPathConstants.BOOLEANpublic XPathBuilder nodeResult()
XPathConstants.NODEpublic XPathBuilder nodeSetResult()
XPathConstants.NODESETpublic XPathBuilder numberResult()
XPathConstants.NUMBERpublic XPathBuilder stringResult()
XPathConstants.STRINGpublic XPathBuilder resultType(Class<?> resultType)
resultTypepublic XPathBuilder objectModel(String uri)
public XPathBuilder factoryClassName(String factoryClassName)
public XPathBuilder saxon()
public XPathBuilder functionResolver(XPathFunctionResolver functionResolver)
XPathFunctionResolver instance to use on these XPath expressionspublic XPathBuilder namespace(String prefix, String uri)
prefix - is the namespace prefix that can be used in the XPath expressionsuri - is the namespace URI to which the prefix referspublic XPathBuilder namespaces(org.apache.camel.support.builder.Namespaces namespaces)
namespaces - is namespaces object that should be used in the XPath expressionpublic XPathBuilder variable(String name, Object value)
name - name of variablevalue - value of variablepublic XPathBuilder documentType(Class<?> documentType)
InputSource to use SAX streams. By default Camel uses Document as
the type.documentType - the document typepublic XPathBuilder factory(XPathFactory xpathFactory)
xpathFactory - the xpath factory to usepublic XPathBuilder logNamespaces()
{xmlns:prefix=[namespaceURI], xmlns:prefix=[namespaceURI]}.
The implicit XML namespace is omitted (http://www.w3.org/XML/1998/namespace). XML allows for namespace prefixes
to be redefined/overridden due to hierarchical scoping, i.e. prefix abc can be mapped to http://abc.com, and
deeper in the document it can be mapped to http://def.com. When two prefixes are detected which are equal but are
mapped to different namespace URIs, Camel will show all namespaces URIs it is mapped to in an array-style.
This feature is disabled by default.public XPathBuilder threadSafety(boolean threadSafety)
public XPathBuilder preCompile(boolean preCompile)
public XPathFactory getXPathFactory()
public void setXPathFactory(XPathFactory xpathFactory)
public Class<?> getDocumentType()
public void setDocumentType(Class<?> documentType)
public String getText()
public QName getResultQName()
public void setResultQName(QName resultQName)
public String getHeaderName()
public void setHeaderName(String headerName)
public boolean isThreadSafety()
public void setThreadSafety(boolean threadSafety)
public boolean isPreCompile()
public void setPreCompile(boolean preCompile)
public DefaultNamespaceContext getNamespaceContext()
public void setNamespaceContext(DefaultNamespaceContext namespaceContext)
public XPathFunctionResolver getFunctionResolver()
public void setFunctionResolver(XPathFunctionResolver functionResolver)
public void setNamespaces(Map<String,String> namespaces)
setNamespaces in interface org.apache.camel.spi.NamespaceAwarepublic Map<String,String> getNamespaces()
getNamespaces in interface org.apache.camel.spi.NamespaceAwarepublic XPathFunction getBodyFunction()
XPathFunction for getting the input message body.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setBodyFunction(XPathFunction bodyFunction)
public XPathFunction getHeaderFunction()
XPathFunction for getting the input message header.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setHeaderFunction(XPathFunction headerFunction)
public XPathFunction getOutBodyFunction()
XPathFunction for getting the output message body.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setOutBodyFunction(XPathFunction outBodyFunction)
public XPathFunction getOutHeaderFunction()
XPathFunction for getting the output message header.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setOutHeaderFunction(XPathFunction outHeaderFunction)
public XPathFunction getPropertiesFunction()
XPathFunction for getting the exchange properties.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setPropertiesFunction(XPathFunction propertiesFunction)
public XPathFunction getSimpleFunction()
XPathFunction for executing simple language as
xpath function.
A default function will be assigned (if no custom assigned) when either starting this builder or on first
evaluation.public void setSimpleFunction(XPathFunction simpleFunction)
public String getExpressionText()
getExpressionText in interface org.apache.camel.spi.ExpressionResultTypeAwarepublic Class<?> getResultType()
getResultType in interface org.apache.camel.spi.ExpressionResultTypeAwarepublic void setResultType(Class<?> resultType)
public void setLogNamespaces(boolean logNamespaces)
public boolean isLogNamespaces()
public void enableSaxon()
saxon() sets the default static XPathFactory which
may have already been initialised by previous XPath expressionspublic void setUseSaxon(boolean useSaxon)
public boolean isUseSaxon()
public String getObjectModelUri()
public void setObjectModelUri(String objectModelUri)
public String getFactoryClassName()
public void setFactoryClassName(String factoryClassName)
public Object evaluate(org.apache.camel.Exchange exchange)
protected Object evaluateAs(org.apache.camel.Exchange exchange, QName resultQName)
protected Object doInEvaluateAs(XPathExpression xpathExpression, org.apache.camel.Exchange exchange, QName resultQName)
protected XPathExpression createXPathExpression() throws XPathExpressionException
XPathExpressionExceptionprotected XPathExpression createTraceNamespaceExpression() throws XPathExpressionException
XPathExpressionExceptionprotected DefaultNamespaceContext createNamespaceContext(XPathFactory factory)
protected void populateDefaultNamespaces(DefaultNamespaceContext context)
protected void setNamespaceIfNotPresent(DefaultNamespaceContext context, String prefix, String uri)
protected XPathFunctionResolver createDefaultFunctionResolver(XPathFunctionResolver parent)
protected boolean isInputStreamNeeded(org.apache.camel.Exchange exchange)
InputStream to access the message body.
Depending on the content in the message body, we may not need to convert to InputStream.exchange - the current exchangeInputStream beforehand converting afterwards.protected boolean isInputStreamNeeded(org.apache.camel.Exchange exchange,
String headerName)
InputStream to access the message header.
Depending on the content in the message header, we may not need to convert to InputStream.exchange - the current exchangeInputStream beforehand converting afterwards.protected boolean isInputStreamNeededForObject(org.apache.camel.Exchange exchange,
Object obj)
InputStream to access this object
Depending on the content in the object, we may not need to convert to InputStream.exchange - the current exchangeInputStream beforehand converting afterwards.protected Object getDocument(org.apache.camel.Exchange exchange, Object body)
protected Object doGetDocument(org.apache.camel.Exchange exchange, Object body) throws Exception
Exceptionpublic void doStart()
throws Exception
doStart in class org.apache.camel.support.service.BaseServiceExceptionpublic void doStop()
throws Exception
doStop in class org.apache.camel.support.service.BaseServiceExceptionprotected XPathFactory createXPathFactory() throws XPathFactoryConfigurationException
protected static XPathFactory createDefaultXPathFactory() throws XPathFactoryConfigurationException
Apache Camel