org.apache.camel.builder.xml
Class XPathBuilder

java.lang.Object
  extended by org.apache.camel.builder.xml.XPathBuilder
All Implemented Interfaces:
Expression, Predicate, Service, NamespaceAware

public class XPathBuilder
extends Object
implements Expression, Predicate, NamespaceAware, Service

Creates an XPath expression builder which creates a nodeset result by default. If you want to evaluate a String expression then call 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

See Also:
XPathConstants.NODESET

Constructor Summary
XPathBuilder(String text)
           
 
Method Summary
 XPathBuilder booleanResult()
          Sets the expression result type to boolean
protected  XPathFunctionResolver createDefaultFunctionResolver(XPathFunctionResolver parent)
           
protected  XPathExpression createTraceNamespaceExpression()
           
protected  XPathExpression createXPathExpression()
           
 XPathBuilder documentType(Class<?> documentType)
          Configures the document type to use.
protected  Object doGetDocument(Exchange exchange, Object body)
           
protected  Object doInEvaluateAs(XPathExpression xpathExpression, Exchange exchange, QName resultQName)
           
 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(CamelContext context, Object body)
          Evaluates the given xpath using the provided body as a String return type.
<T> T
evaluate(CamelContext context, Object body, Class<T> type)
          Evaluates the given xpath using the provided body.
protected  Object evaluate(Exchange exchange)
           
<T> T
evaluate(Exchange exchange, Class<T> type)
          Returns the value of the expression on the given exchange
protected  Object evaluateAs(Exchange exchange, QName resultQName)
          Evaluates the expression as the given result type
 XPathBuilder factory(XPathFactory xpathFactory)
          Configures to use the provided XPath factory.
 XPathBuilder functionResolver(XPathFunctionResolver functionResolver)
          Sets the XPathFunctionResolver instance to use on these XPath expressions
 XPathFunction getBodyFunction()
           
protected  Object getDocument(Exchange exchange, Object body)
          Strategy method to extract the document from the exchange.
 Class<?> getDocumentType()
           
 XPathFunctionResolver getFunctionResolver()
           
 XPathFunction getHeaderFunction()
           
 DefaultNamespaceContext getNamespaceContext()
           
 String getObjectModelUri()
           
 XPathFunction getOutBodyFunction()
           
 XPathFunction getOutHeaderFunction()
           
 XPathFunction getPropertiesFunction()
           
 QName getResultQName()
           
 Class<?> getResultType()
           
 XPathFunction getSimpleFunction()
           
 String getText()
           
 XPathFactory getXPathFactory()
           
protected  void initDefaultXPathFactory()
           
protected  boolean isInputStreamNeeded(Exchange exchange)
          Checks whether we need an InputStream to access the message body.
 boolean isLogNamespaces()
           
 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(CamelContext context, Object body)
          Matches the given xpath using the provided body.
 boolean matches(Exchange exchange)
          Evaluates the predicate on the message exchange and returns true if this exchange matches the predicate
 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(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 boolean
 XPathBuilder nodeSetResult()
          Sets the expression result type to boolean
 XPathBuilder numberResult()
          Sets the expression result type to boolean
 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 resultType(Class<?> resultType)
          Sets the expression result type to boolean
 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 setDocumentType(Class<?> documentType)
           
 void setFunctionResolver(XPathFunctionResolver functionResolver)
           
 void setHeaderFunction(XPathFunction headerFunction)
           
 void setLogNamespaces(boolean logNamespaces)
           
 void setNamespaceContext(DefaultNamespaceContext namespaceContext)
           
protected  void setNamespaceIfNotPresent(DefaultNamespaceContext context, String prefix, String uri)
           
 void setNamespaces(Map<String,String> namespaces)
          Injects the XML Namespaces of prefix -> uri mappings
 void setObjectModelUri(String objectModelUri)
           
 void setOutBodyFunction(XPathFunction outBodyFunction)
           
 void setOutHeaderFunction(XPathFunction outHeaderFunction)
           
 void setPropertiesFunction(XPathFunction propertiesFunction)
           
 void setResultQName(QName resultQName)
           
 void setResultType(Class<?> resultType)
           
 void setSimpleFunction(XPathFunction simpleFunction)
           
 void setXPathFactory(XPathFactory xpathFactory)
           
 void start()
          Starts the service
 void stop()
          Stops the service
 XPathBuilder stringResult()
          Sets the expression result type to boolean
 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)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XPathBuilder

public XPathBuilder(String text)
Method Detail

xpath

public static XPathBuilder xpath(String text)

xpath

public static XPathBuilder xpath(String text,
                                 Class<?> resultType)

toString

public String toString()
Overrides:
toString in class Object

matches

public boolean matches(Exchange exchange)
Description copied from interface: Predicate
Evaluates the predicate on the message exchange and returns true if this exchange matches the predicate

Specified by:
matches in interface Predicate
Parameters:
exchange - the message exchange
Returns:
true if the predicate matches

evaluate

public <T> T evaluate(Exchange exchange,
                      Class<T> type)
Description copied from interface: Expression
Returns the value of the expression on the given exchange

Specified by:
evaluate in interface Expression
Parameters:
exchange - the message exchange on which to evaluate the expression
type - the expected type of the evaluation result
Returns:
the value of the expression

matches

public boolean matches(CamelContext context,
                       Object body)
Matches the given xpath using the provided body.

Parameters:
context - the camel context
body - the body
Returns:
true if matches, false otherwise

evaluate

public <T> T evaluate(CamelContext context,
                      Object body,
                      Class<T> type)
Evaluates the given xpath using the provided body.

Parameters:
context - the camel context
body - the body
type - the type to return
Returns:
result of the evaluation

evaluate

public String evaluate(CamelContext context,
                       Object body)
Evaluates the given xpath using the provided body as a String return type.

Parameters:
context - the camel context
body - the body
Returns:
result of the evaluation

booleanResult

public XPathBuilder booleanResult()
Sets the expression result type to boolean

Returns:
the current builder

nodeResult

public XPathBuilder nodeResult()
Sets the expression result type to boolean

Returns:
the current builder

nodeSetResult

public XPathBuilder nodeSetResult()
Sets the expression result type to boolean

Returns:
the current builder

numberResult

public XPathBuilder numberResult()
Sets the expression result type to boolean

Returns:
the current builder

stringResult

public XPathBuilder stringResult()
Sets the expression result type to boolean

Returns:
the current builder

resultType

public XPathBuilder resultType(Class<?> resultType)
Sets the expression result type to boolean

Returns:
the current builder

objectModel

public XPathBuilder objectModel(String uri)
Sets the object model URI to use

Returns:
the current builder

saxon

public 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.

Returns:
the current builder

functionResolver

public XPathBuilder functionResolver(XPathFunctionResolver functionResolver)
Sets the XPathFunctionResolver instance to use on these XPath expressions

Returns:
the current builder

namespace

public 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

Parameters:
prefix - is the namespace prefix that can be used in the XPath expressions
uri - is the namespace URI to which the prefix refers
Returns:
the current builder

namespaces

public XPathBuilder namespaces(Namespaces namespaces)
Registers namespaces with the builder so that the registered prefixes can be used in XPath expressions

Parameters:
namespaces - is namespaces object that should be used in the XPath expression
Returns:
the current builder

variable

public XPathBuilder variable(String name,
                             Object value)
Registers a variable (in the global namespace) which can be referred to from XPath expressions

Parameters:
name - name of variable
value - value of variable
Returns:
the current builder

documentType

public XPathBuilder documentType(Class<?> documentType)
Configures the document type to use.

The document type controls which kind of Class Camel should convert the payload to before doing the xpath evaluation.

For example you can set it to InputSource to use SAX streams. By default Camel uses Document as the type.

Parameters:
documentType - the document type
Returns:
the current builder

factory

public XPathBuilder factory(XPathFactory xpathFactory)
Configures to use the provided XPath factory.

Can be used to use Saxon instead of the build in factory from the JDK.

Parameters:
xpathFactory - the xpath factory to use
Returns:
the current builder.

logNamespaces

public 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]}.

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.

Returns:
the current builder.

getXPathFactory

public XPathFactory getXPathFactory()
                             throws XPathFactoryConfigurationException
Throws:
XPathFactoryConfigurationException

setXPathFactory

public void setXPathFactory(XPathFactory xpathFactory)

getDocumentType

public Class<?> getDocumentType()

setDocumentType

public void setDocumentType(Class<?> documentType)

getText

public String getText()

getResultQName

public QName getResultQName()

setResultQName

public void setResultQName(QName resultQName)

getNamespaceContext

public DefaultNamespaceContext getNamespaceContext()

setNamespaceContext

public void setNamespaceContext(DefaultNamespaceContext namespaceContext)

getFunctionResolver

public XPathFunctionResolver getFunctionResolver()

setFunctionResolver

public void setFunctionResolver(XPathFunctionResolver functionResolver)

setNamespaces

public void setNamespaces(Map<String,String> namespaces)
Description copied from interface: NamespaceAware
Injects the XML Namespaces of prefix -> uri mappings

Specified by:
setNamespaces in interface NamespaceAware
Parameters:
namespaces - the XML namespaces with the key of prefixes and the value the URIs

getBodyFunction

public XPathFunction getBodyFunction()

setBodyFunction

public void setBodyFunction(XPathFunction bodyFunction)

getHeaderFunction

public XPathFunction getHeaderFunction()

setHeaderFunction

public void setHeaderFunction(XPathFunction headerFunction)

getOutBodyFunction

public XPathFunction getOutBodyFunction()

setOutBodyFunction

public void setOutBodyFunction(XPathFunction outBodyFunction)

getOutHeaderFunction

public XPathFunction getOutHeaderFunction()

setOutHeaderFunction

public void setOutHeaderFunction(XPathFunction outHeaderFunction)

getPropertiesFunction

public XPathFunction getPropertiesFunction()

setPropertiesFunction

public void setPropertiesFunction(XPathFunction propertiesFunction)

getSimpleFunction

public XPathFunction getSimpleFunction()

setSimpleFunction

public void setSimpleFunction(XPathFunction simpleFunction)

getResultType

public Class<?> getResultType()

setResultType

public void setResultType(Class<?> resultType)

setLogNamespaces

public void setLogNamespaces(boolean logNamespaces)

isLogNamespaces

public boolean isLogNamespaces()

getObjectModelUri

public String getObjectModelUri()

enableSaxon

public 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


setObjectModelUri

public void setObjectModelUri(String objectModelUri)

evaluate

protected Object evaluate(Exchange exchange)

evaluateAs

protected Object evaluateAs(Exchange exchange,
                            QName resultQName)
Evaluates the expression as the given result type


doInEvaluateAs

protected Object doInEvaluateAs(XPathExpression xpathExpression,
                                Exchange exchange,
                                QName resultQName)

createXPathExpression

protected XPathExpression createXPathExpression()
                                         throws XPathExpressionException,
                                                XPathFactoryConfigurationException
Throws:
XPathExpressionException
XPathFactoryConfigurationException

createTraceNamespaceExpression

protected XPathExpression createTraceNamespaceExpression()
                                                  throws XPathFactoryConfigurationException,
                                                         XPathExpressionException
Throws:
XPathFactoryConfigurationException
XPathExpressionException

populateDefaultNamespaces

protected void populateDefaultNamespaces(DefaultNamespaceContext context)
Populate a number of standard prefixes if they are not already there


setNamespaceIfNotPresent

protected void setNamespaceIfNotPresent(DefaultNamespaceContext context,
                                        String prefix,
                                        String uri)

createDefaultFunctionResolver

protected XPathFunctionResolver createDefaultFunctionResolver(XPathFunctionResolver parent)

isInputStreamNeeded

protected boolean isInputStreamNeeded(Exchange exchange)
Checks whether we need an InputStream to access the message body.

Depending on the content in the message body, we may not need to convert to InputStream.

Parameters:
exchange - the current exchange
Returns:
true to convert to InputStream beforehand converting afterwards.

getDocument

protected Object getDocument(Exchange exchange,
                             Object body)
Strategy method to extract the document from the exchange.


doGetDocument

protected Object doGetDocument(Exchange exchange,
                               Object body)
                        throws Exception
Throws:
Exception

start

public void start()
           throws Exception
Description copied from interface: Service
Starts the service

Specified by:
start in interface Service
Throws:
Exception - is thrown if starting failed

stop

public void stop()
          throws Exception
Description copied from interface: Service
Stops the service

Specified by:
stop in interface Service
Throws:
Exception - is thrown if stopping failed

initDefaultXPathFactory

protected void initDefaultXPathFactory()
                                throws XPathFactoryConfigurationException
Throws:
XPathFactoryConfigurationException


Apache CAMEL