Class XPathService
- java.lang.Object
-
- com.adaptris.core.ServiceImp
-
- com.adaptris.core.services.path.XPathService
-
- All Implemented Interfaces:
AdaptrisComponent
,ComponentLifecycle
,ComponentLifecycleExtension
,MessageEventGenerator
,Service
,StateManagedComponent
@ComponentProfile(summary="Extract data via XPath and store it", tag="service,xml") public class XPathService extends ServiceImp
This service allows you to configure an xpath expression which will be executed on source xml, the result of which can be saved to multiple locations.
To specify where the source xml, source xpath expression and the result of the xpath execution should be saved, you shoud use
DataInputParameter
orDataOutputParameter
.
For example you can specify the source xml can be found in theAdaptrisMessage
payload, by usingStringPayloadDataInputParameter
like this :<xpath-service> <xml-source class="string-payload-data-input-parameter"/> ...
ConstantDataInputParameter
;<xpath-service> <xpath-execution> <source class="constant-data-input-parameter"> <value>//my/xpath/expression</value> </source> ...
AdaptrisMessage
metadata, usingMetadataDataOutputParameter
;<xpath-service> <xpath-execution> <target class="metadata-data-output-parameter"> <metadata-key>targetMetadataKey</metadata-key> </target> ...
While you may only specify a single source xml destination, you may if you wish apply multiple XPath expressions, each of which saves the result to a different location. To do this, simply configure multiple executions. Take the following example, where we specify the payload containing the source xml and 3 XPath expressions will be executed each of which will store the result in 3 different metadata items;
<xpath-service> <xml-source class="string-payload-data-input-parameter"/> <xpath-execution> <source class="constant-data-input-parameter"> <value>//my/first/xpath/expression</value> </source> <target class="metadata-data-output-parameter"> <metadata-key>targetMetadataKey1</metadata-key> </target> </xpath-execution> <xpath-execution> <source class="constant-data-input-parameter"> <value>//my/second/xpath/expression</value> </source> <target class="metadata-data-output-parameter"> <metadata-key>targetMetadataKey2</metadata-key> </target> </xpath-execution> <xpath-execution> <source class="constant-data-input-parameter"> <value>//my/third/xpath/expression</value> </source> <target class="metadata-data-output-parameter"> <metadata-key>targetMetadataKey3</metadata-key> </target> </xpath-execution> </xpath-service>
Should your source xml contain namespaces, you will need to configure the mappings in this service like this;
<xpath-service> ... <namespace-context> <key-value-pair> <key>n1</key> <value>http://adaptris.com/xml/namespace1</value> </key-value-pair> <key-value-pair> <key>n2</key> <value>http://adaptris.com/xml/namespace2</value> </key-value-pair> <key-value-pair> <key>n3</key> <value>http://adaptris.com/xml/namespace3</value> </key-value-pair> <key-value-pair> <key>n4</key> <value>http://adaptris.com/xml/namespace4</value> </key-value-pair> </namespace-context> ... </xpath-service>
If the
DocumentBuilderFactoryBuilder
has been explicitly set to be not namespace aware and the document does in fact contain namespaces, then Saxon can cause merry havoc in the sense that//NonNamespaceXpath
doesn't work if the document has namespaces in it. We have included a shim so that behaviour can be toggled based on what you have configured.- Since:
- 3.0.6
- See Also:
XPath.newXPathInstance(DocumentBuilderFactoryBuilder, NamespaceContext)
In the adapter configuration file this class is aliased as xpath-service which is the preferred alternative to the fully qualified classname when building your configuration.
-
-
Field Summary
-
Fields inherited from class com.adaptris.core.ServiceImp
log
-
-
Constructor Summary
Constructors Constructor Description XPathService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
closeService()
void
doService(AdaptrisMessage msg)
Apply the service to the message.java.util.List<Execution>
getExecutions()
KeyValuePairSet
getNamespaceContext()
DocumentBuilderFactoryBuilder
getXmlDocumentFactoryConfig()
DataInputParameter<java.lang.String>
getXmlSource()
protected void
initService()
void
prepare()
Prepare for initialisation.void
setExecutions(java.util.List<Execution> list)
void
setNamespaceContext(KeyValuePairSet namespaceContext)
void
setXmlDocumentFactoryConfig(DocumentBuilderFactoryBuilder xml)
void
setXmlSource(DataInputParameter<java.lang.String> sourceDestination)
-
Methods inherited from class com.adaptris.core.ServiceImp
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setUniqueId, start, stop
-
-
-
-
Method Detail
-
doService
public void doService(AdaptrisMessage msg) throws ServiceException
Description copied from interface:Service
Apply the service to the message.
- Parameters:
msg
- theAdaptrisMessage
to process- Throws:
ServiceException
- wrapping any underlyingException
s
-
prepare
public void prepare() throws CoreException
Description copied from interface:ComponentLifecycleExtension
Prepare for initialisation.- Throws:
CoreException
-
initService
protected void initService() throws CoreException
- Specified by:
initService
in classServiceImp
- Throws:
CoreException
-
closeService
protected void closeService()
- Specified by:
closeService
in classServiceImp
-
getXmlSource
public DataInputParameter<java.lang.String> getXmlSource()
-
setXmlSource
public void setXmlSource(DataInputParameter<java.lang.String> sourceDestination)
-
getExecutions
public java.util.List<Execution> getExecutions()
-
setExecutions
public void setExecutions(java.util.List<Execution> list)
-
getNamespaceContext
public KeyValuePairSet getNamespaceContext()
-
setNamespaceContext
public void setNamespaceContext(KeyValuePairSet namespaceContext)
-
getXmlDocumentFactoryConfig
public DocumentBuilderFactoryBuilder getXmlDocumentFactoryConfig()
-
setXmlDocumentFactoryConfig
public void setXmlDocumentFactoryConfig(DocumentBuilderFactoryBuilder xml)
-
-