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;
}
Aggregations