Search in sources :

Example 16 with XmlUtils

use of com.adaptris.util.XmlUtils in project interlok by adaptris.

the class XmlPayloadTranslator method toDocument.

private DocumentWrapper toDocument(JdbcResult rs, AdaptrisMessage msg) throws Exception {
    XmlUtils xu = createXmlUtils(msg);
    DocumentWrapper wrapper = createWrapper(msg);
    Document doc = wrapper.document;
    ColumnStyle elementNameStyle = getColumnNameStyle();
    Element results = doc.createElement(elementNameStyle.format(ELEMENT_NAME_RESULTS));
    doc.appendChild(results);
    if (isPreserveOriginalMessage()) {
        Element originalMessage = doc.createElement(elementNameStyle.format(ORIGINAL_MESSAGE_ELEMENT));
        if (xu.isDocumentValid()) {
            Node n = xu.getSingleNode("/").getFirstChild();
            originalMessage.appendChild(doc.importNode(n, true));
        } else {
            // Not XML, so let's add it in as a CDATA node.
            originalMessage.appendChild(createTextNode(doc, msg.getContent(), true));
            wrapper.hasCDATA = true;
        }
        results.appendChild(originalMessage);
    }
    for (JdbcResultSet rSet : rs.getResultSets()) {
        List<Element> elements = createListFromResultSet(wrapper, rSet);
        for (Element element : elements) {
            results.appendChild(element);
        }
        wrapper.resultSetCount += elements.size();
    }
    return wrapper;
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 17 with XmlUtils

use of com.adaptris.util.XmlUtils in project interlok by adaptris.

the class XmlPayloadTranslatorImpl method createXmlUtils.

protected static XmlUtils createXmlUtils(AdaptrisMessage msg) {
    XmlUtils xu = null;
    try {
        NamespaceContext ctx = (NamespaceContext) msg.getObjectHeaders().get(JdbcDataQueryService.KEY_NAMESPACE_CTX);
        DocumentBuilderFactoryBuilder builder = (DocumentBuilderFactoryBuilder) msg.getObjectHeaders().get(JdbcDataQueryService.KEY_DOCBUILDER_FAC);
        xu = XmlHelper.createXmlUtils(msg, ctx, builder);
    } catch (CoreException e) {
        xu = new XmlUtils();
    }
    return xu;
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) CoreException(com.adaptris.core.CoreException) NamespaceContext(javax.xml.namespace.NamespaceContext) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder)

Example 18 with XmlUtils

use of com.adaptris.util.XmlUtils in project interlok by adaptris.

the class InsertNode method merge.

@Override
public Document merge(Document original, Document newDoc) throws Exception {
    if (StringUtils.isEmpty(getXpathToParentNode())) {
        throw new Exception("No parent node configured");
    }
    Document resultDoc = original;
    XmlUtils xml = create(resultDoc);
    Node parent = resolve(xml, getXpathToParentNode());
    if (parent.getOwnerDocument() == null) {
        throw new Exception("Invalid xpath-to-parent-node [" + getXpathToParentNode() + "]");
    }
    xml.appendNode(newDoc.getDocumentElement(), parent);
    return resultDoc;
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document)

Example 19 with XmlUtils

use of com.adaptris.util.XmlUtils in project interlok by adaptris.

the class XpathMergeImpl method create.

protected XmlUtils create(Document doc) throws Exception {
    DocumentBuilderFactoryBuilder builder = documentFactoryBuilder();
    XmlUtils xml = new XmlUtils(builder.getEntityResolver(), SimpleNamespaceContext.create(getNamespaceContext()), builder.build());
    xml.setSource(doc);
    return xml;
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder)

Example 20 with XmlUtils

use of com.adaptris.util.XmlUtils in project interlok by adaptris.

the class ExceptionReportServiceTest method testReplaceNode.

@Test
public void testReplaceNode() throws Exception {
    AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage(XML_PAYLOAD);
    msg.addObjectHeader(CoreConstants.OBJ_METADATA_EXCEPTION, new Exception("This is the exception"));
    ExceptionReportService service = new ExceptionReportService(new ExceptionAsXml().withExceptionGenerator(new SimpleExceptionReport()).withDocumentMerge(new ReplaceNode(XPATH_ORIGINAL_NODE)).withDocumentFactoryConfig(DocumentBuilderFactoryBuilder.newInstance()));
    execute(service, msg);
    assertNotSame(XML_PAYLOAD, msg.getContent());
    XmlUtils xml = XmlHelper.createXmlUtils(msg);
    assertNotSame(RAW_DATA, xml.getSingleNode(XPATH_ORIGINAL_NODE));
    assertEquals("UTF-8", msg.getContentEncoding());
}
Also used : XmlUtils(com.adaptris.util.XmlUtils) AdaptrisMessage(com.adaptris.core.AdaptrisMessage) ReplaceNode(com.adaptris.util.text.xml.ReplaceNode) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Aggregations

XmlUtils (com.adaptris.util.XmlUtils)41 Test (org.junit.Test)32 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)23 Document (org.w3c.dom.Document)13 InsertNode (com.adaptris.util.text.xml.InsertNode)8 CoreException (com.adaptris.core.CoreException)7 ServiceException (com.adaptris.core.ServiceException)4 Notification (javax.management.Notification)4 NamespaceContext (javax.xml.namespace.NamespaceContext)3 DefaultMessageFactory (com.adaptris.core.DefaultMessageFactory)2 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)2 ReplaceNode (com.adaptris.util.text.xml.ReplaceNode)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 AdaptrisMessageFactory (com.adaptris.core.AdaptrisMessageFactory)1 Service (com.adaptris.core.Service)1 ServiceImp (com.adaptris.core.ServiceImp)1 ServiceList (com.adaptris.core.ServiceList)1