Search in sources :

Example 41 with XmlUtils

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

the class XmlHelper method createXmlUtils.

/**
 * Create an XMLUtils class from an AdaptrisMessage.
 *
 * @param msg the Adaptris message
 * @param ctx the NamespaceContext.
 * @param builder configuration for the underlying {@link DocumentBuilderFactory} instance..
 * @return an XmlUtils instance
 * @throws CoreException if the msg does not contain valid XML.
 */
public static XmlUtils createXmlUtils(AdaptrisMessage msg, NamespaceContext ctx, DocumentBuilderFactoryBuilder builder) throws CoreException {
    XmlUtils result = null;
    DivertConsoleOutput dc = new DivertConsoleOutput();
    DocumentBuilderFactoryBuilder builderToUse = copy(DocumentBuilderFactoryBuilder.newInstanceIfNull(builder));
    if (ctx != null) {
        builderToUse.setNamespaceAware(true);
    }
    try (InputStream input = msg.getInputStream()) {
        DocumentBuilderFactory dbf = builderToUse.build();
        result = new XmlUtils(builderToUse.getEntityResolver(), ctx, dbf);
        InputSource in = new InputSource(input);
        // Well what we're going to do here is annoyingly bad, but I want to eat
        // those stupid System.err messages that contain shit like
        // "[Fatal Error] :1:1: Content is not allowed in prolog"
        dc.divert();
        result.setSource(in);
    } catch (Exception e) {
        result = null;
    } finally {
        dc.resume();
    }
    if (dc.consoleOutput().contains("[Fatal Error]") || result == null) {
        throw new CoreException("Document " + msg.getUniqueId() + " does not appear to be an XML Document, error was [" + dc.consoleOutput() + "]");
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XmlUtils(com.adaptris.util.XmlUtils) CoreException(com.adaptris.core.CoreException) InputStream(java.io.InputStream) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) CoreException(com.adaptris.core.CoreException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

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