Search in sources :

Example 1 with XpathObjectQuery

use of com.adaptris.core.services.metadata.xpath.XpathObjectQuery in project interlok by adaptris.

the class XpathObjectMetadataServiceTest method testDoService_UsingXpathQuery.

@Test
public void testDoService_UsingXpathQuery() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML);
    XpathObjectMetadataService service = new XpathObjectMetadataService();
    service.setXpathQueries(new ArrayList<XpathObjectQuery>(Arrays.asList(new ConfiguredXpathNodeQuery("source", "//source-id"), new ConfiguredXpathNodeQuery("destination", "//destination-id"))));
    execute(service, msg);
    assertFalse(msg.containsKey("source"));
    assertFalse(msg.containsKey("destination"));
    assertTrue(msg.getObjectHeaders().containsKey("source"));
    assertTrue(msg.getObjectHeaders().containsKey("destination"));
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) XpathObjectQuery(com.adaptris.core.services.metadata.xpath.XpathObjectQuery) ConfiguredXpathNodeQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathNodeQuery) Test(org.junit.Test)

Example 2 with XpathObjectQuery

use of com.adaptris.core.services.metadata.xpath.XpathObjectQuery in project interlok by adaptris.

the class XpathObjectMetadataServiceTest method retrieveObjectForSampleConfig.

@Override
protected Object retrieveObjectForSampleConfig() {
    XpathObjectMetadataService service = new XpathObjectMetadataService();
    service.setXpathQueries(new ArrayList<XpathObjectQuery>(Arrays.asList(new ConfiguredXpathNodeQuery("key1", "//source-id"), new ConfiguredXpathNodeListQuery("key3", "//xpath/that/resolves/to/multiple/items"), new MetadataXpathNodeQuery("key4", "metadata_containing_an_xpath"), new MetadataXpathNodeListQuery("key5", "metadata_containing_a_nodelist_xpath"))));
    service.setNamespaceContext(createContextEntries());
    return service;
}
Also used : XpathObjectQuery(com.adaptris.core.services.metadata.xpath.XpathObjectQuery) MetadataXpathNodeListQuery(com.adaptris.core.services.metadata.xpath.MetadataXpathNodeListQuery) ConfiguredXpathNodeListQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathNodeListQuery) MetadataXpathNodeQuery(com.adaptris.core.services.metadata.xpath.MetadataXpathNodeQuery) ConfiguredXpathNodeQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathNodeQuery)

Example 3 with XpathObjectQuery

use of com.adaptris.core.services.metadata.xpath.XpathObjectQuery in project interlok by adaptris.

the class XpathObjectMetadataServiceTest method testSetXpathQueryList.

@Test
public void testSetXpathQueryList() {
    XpathObjectMetadataService service = new XpathObjectMetadataService();
    assertEquals(0, service.getXpathQueries().size());
    XpathQuery query = new ConfiguredXpathQuery("metadataKey", "//root");
    List<XpathObjectQuery> list = new ArrayList<>();
    list.add(query);
    service.setXpathQueries(list);
    assertEquals(list, service.getXpathQueries());
    try {
        service.setXpathQueries(null);
        fail();
    } catch (IllegalArgumentException e) {
    }
    assertEquals(list, service.getXpathQueries());
}
Also used : XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) ConfiguredXpathQuery(com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery) XpathObjectQuery(com.adaptris.core.services.metadata.xpath.XpathObjectQuery) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with XpathObjectQuery

use of com.adaptris.core.services.metadata.xpath.XpathObjectQuery in project interlok by adaptris.

the class XpathObjectMetadataService method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    NamespaceContext namespaceCtx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
    try {
        DocumentBuilderFactoryBuilder builder = documentFactoryBuilder();
        if (namespaceCtx != null) {
            builder.setNamespaceAware(true);
        }
        XPath xpathToUse = XPath.newXPathInstance(builder, namespaceCtx);
        Document doc = XmlHelper.createDocument(msg, builder);
        for (XpathObjectQuery query : queriesToExecute) {
            msg.getObjectHeaders().put(query.getMetadataKey(), query.resolveXpath(doc, xpathToUse, query.createXpathQuery(msg)));
            log.trace("Added object against [{}]", query.getMetadataKey());
        }
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : XPath(com.adaptris.util.text.xml.XPath) ServiceException(com.adaptris.core.ServiceException) NamespaceContext(javax.xml.namespace.NamespaceContext) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) XpathObjectQuery(com.adaptris.core.services.metadata.xpath.XpathObjectQuery) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) Document(org.w3c.dom.Document) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException)

Example 5 with XpathObjectQuery

use of com.adaptris.core.services.metadata.xpath.XpathObjectQuery in project interlok by adaptris.

the class XpathObjectMetadataService method initService.

@Override
protected void initService() throws CoreException {
    for (XpathObjectQuery query : xpathQueries) {
        query.verify();
    }
    queriesToExecute = new ArrayList<XpathObjectQuery>();
    queriesToExecute.addAll(getXpathQueries());
}
Also used : XpathObjectQuery(com.adaptris.core.services.metadata.xpath.XpathObjectQuery)

Aggregations

XpathObjectQuery (com.adaptris.core.services.metadata.xpath.XpathObjectQuery)7 Test (org.junit.Test)4 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)3 ConfiguredXpathNodeQuery (com.adaptris.core.services.metadata.xpath.ConfiguredXpathNodeQuery)3 ServiceException (com.adaptris.core.ServiceException)2 ConfiguredXpathQuery (com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery)2 CoreException (com.adaptris.core.CoreException)1 ConfiguredXpathNodeListQuery (com.adaptris.core.services.metadata.xpath.ConfiguredXpathNodeListQuery)1 MetadataXpathNodeListQuery (com.adaptris.core.services.metadata.xpath.MetadataXpathNodeListQuery)1 MetadataXpathNodeQuery (com.adaptris.core.services.metadata.xpath.MetadataXpathNodeQuery)1 XpathQuery (com.adaptris.core.services.metadata.xpath.XpathQuery)1 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)1 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)1 XPath (com.adaptris.util.text.xml.XPath)1 ArrayList (java.util.ArrayList)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 Document (org.w3c.dom.Document)1