Search in sources :

Example 71 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class IgnoreOriginalXmlAggregatorTest method testSplitJoinService_WithImplicitDocumentEnoding.

@Test
public void testSplitJoinService_WithImplicitDocumentEnoding() throws Exception {
    // This is a XML doc with 3 iterable elements...
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(SplitterCase.XML_MESSAGE, "ISO-8859-1");
    PooledSplitJoinService service = new PooledSplitJoinService();
    // The service doesn't actually matter right now.
    service.setService(asCollection(new NullService()));
    service.setTimeout(new TimeInterval(10L, TimeUnit.SECONDS));
    service.setSplitter(new XpathMessageSplitter(ENVELOPE_DOCUMENT, ENCODING_UTF8));
    service.setAggregator(new IgnoreOriginalXmlDocumentAggregator("<new/>", new InsertNode("/new")));
    execute(service, msg);
    // Should now be 6 document nodes
    XPath xpath = new XPath();
    assertEquals(3, xpath.selectNodeList(XmlHelper.createDocument(msg, true), "/new/document").getLength());
    assertEquals("ISO-8859-1", msg.getContentEncoding());
}
Also used : PooledSplitJoinService(com.adaptris.core.services.splitter.PooledSplitJoinService) XPath(com.adaptris.util.text.xml.XPath) TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) NullService(com.adaptris.core.NullService) XpathMessageSplitter(com.adaptris.core.services.splitter.XpathMessageSplitter) InsertNode(com.adaptris.util.text.xml.InsertNode) Test(org.junit.Test)

Example 72 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class IgnoreOriginalXmlAggregatorTest method testSplitJoinService_withFilter.

@Test
public void testSplitJoinService_withFilter() throws Exception {
    // This is a XML doc with 3 iterable elements...
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(SplitterCase.XML_MESSAGE);
    PooledSplitJoinService service = new PooledSplitJoinService();
    // The service doesn't actually matter right now.
    service.setService(asCollection(new NullService()));
    service.setTimeout(new TimeInterval(10L, TimeUnit.SECONDS));
    service.setSplitter(new XpathMessageSplitter(ENVELOPE_DOCUMENT, ENCODING_UTF8));
    IgnoreOriginalXmlDocumentAggregator aggr = new IgnoreOriginalXmlDocumentAggregator("<new/>", new InsertNode("/new"));
    aggr.setDocumentEncoding("UTF-8");
    aggr.setFilterCondition(new EvenOddCondition());
    service.setAggregator(aggr);
    execute(service, msg);
    // Should now be 3 document nodes
    // because we ignore the original
    XPath xpath = new XPath();
    assertEquals(1, xpath.selectNodeList(XmlHelper.createDocument(msg, true), "/new/document").getLength());
    assertEquals("UTF-8", msg.getContentEncoding());
}
Also used : PooledSplitJoinService(com.adaptris.core.services.splitter.PooledSplitJoinService) XPath(com.adaptris.util.text.xml.XPath) EvenOddCondition(com.adaptris.core.services.aggregator.MessageAggregatorTest.EvenOddCondition) TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) NullService(com.adaptris.core.NullService) XpathMessageSplitter(com.adaptris.core.services.splitter.XpathMessageSplitter) InsertNode(com.adaptris.util.text.xml.InsertNode) Test(org.junit.Test)

Example 73 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class XmlAggregatorTest method testSplitJoinService_WithImplicitDocumentEnoding.

@Test
public void testSplitJoinService_WithImplicitDocumentEnoding() throws Exception {
    // This is a XML doc with 3 iterable elements...
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(SplitterCase.XML_MESSAGE, "ISO-8859-1");
    PooledSplitJoinService service = new PooledSplitJoinService();
    // The service doesn't actually matter right now.
    service.setService(asCollection(new NullService()));
    service.setTimeout(new TimeInterval(10L, TimeUnit.SECONDS));
    service.setSplitter(new XpathMessageSplitter(ENVELOPE_DOCUMENT, ENCODING_UTF8));
    service.setAggregator(new XmlDocumentAggregator(new InsertNode(XPATH_ENVELOPE)));
    execute(service, msg);
    // Should now be 6 document nodes
    XPath xpath = new XPath();
    assertEquals(6, xpath.selectNodeList(XmlHelper.createDocument(msg, true), ENVELOPE_DOCUMENT).getLength());
    assertEquals("ISO-8859-1", msg.getContentEncoding());
}
Also used : PooledSplitJoinService(com.adaptris.core.services.splitter.PooledSplitJoinService) XPath(com.adaptris.util.text.xml.XPath) TimeInterval(com.adaptris.util.TimeInterval) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) NullService(com.adaptris.core.NullService) XpathMessageSplitter(com.adaptris.core.services.splitter.XpathMessageSplitter) InsertNode(com.adaptris.util.text.xml.InsertNode) Test(org.junit.Test)

Example 74 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class XmlUtilsTest method testWriteDocumentToWriter.

@Test
public void testWriteDocumentToWriter() throws Exception {
    XmlUtils xu = new XmlUtils(null, null, DocumentBuilderFactory.newInstance());
    xu.setSource(new java.io.StringReader(unpadded));
    Document d = (Document) xu.getSingleNode("/");
    StringWriter sw = new StringWriter();
    xu.writeDocument(d, sw);
    // Let's check that XPAth resolves.
    XPath xpath = new XPath();
    XmlHelper.createDocument(sw.toString());
    String text = xpath.selectSingleTextItem(XmlHelper.createDocument(sw.toString()), "/root/test");
    assertEquals(CHAR_80 + CHAR_80, text);
}
Also used : XPath(com.adaptris.util.text.xml.XPath) StringReader(java.io.StringReader) StringWriter(java.io.StringWriter) Document(org.w3c.dom.Document) XmlValidationServiceTest(com.adaptris.core.transform.XmlValidationServiceTest) Test(org.junit.Test)

Example 75 with XPath

use of com.adaptris.util.text.xml.XPath in project interlok by adaptris.

the class XpathMetadataService method doService.

@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    Set<MetadataElement> metadataElements = new HashSet<MetadataElement>();
    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 (XpathQuery query : queriesToExecute) {
            metadataElements.add(query.resolveXpath(doc, xpathToUse, query.createXpathQuery(msg)));
        }
        logMetadata("Xpath Metadata resolved {}", metadataElements);
        msg.setMetadata(metadataElements);
    } catch (Exception e) {
        throw new ServiceException(e);
    }
}
Also used : XPath(com.adaptris.util.text.xml.XPath) XpathQuery(com.adaptris.core.services.metadata.xpath.XpathQuery) ServiceException(com.adaptris.core.ServiceException) NamespaceContext(javax.xml.namespace.NamespaceContext) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) MetadataElement(com.adaptris.core.MetadataElement) Document(org.w3c.dom.Document) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException) HashSet(java.util.HashSet)

Aggregations

XPath (com.adaptris.util.text.xml.XPath)75 Document (org.w3c.dom.Document)64 Test (org.junit.Test)63 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)45 MetadataElement (com.adaptris.core.MetadataElement)23 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)21 CoreException (com.adaptris.core.CoreException)12 Node (org.w3c.dom.Node)11 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)9 NamespaceContext (javax.xml.namespace.NamespaceContext)9 ServiceException (com.adaptris.core.ServiceException)7 InsertNode (com.adaptris.util.text.xml.InsertNode)6 NodeList (org.w3c.dom.NodeList)6 NullService (com.adaptris.core.NullService)5 PooledSplitJoinService (com.adaptris.core.services.splitter.PooledSplitJoinService)5 XpathMessageSplitter (com.adaptris.core.services.splitter.XpathMessageSplitter)5 TimeInterval (com.adaptris.util.TimeInterval)5 XmlHelper.createDocument (com.adaptris.core.util.XmlHelper.createDocument)3 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)2 MultiPayloadAdaptrisMessage (com.adaptris.core.MultiPayloadAdaptrisMessage)2