Search in sources :

Example 16 with DocumentBuilderFactoryBuilder

use of com.adaptris.core.util.DocumentBuilderFactoryBuilder in project interlok by adaptris.

the class XmlRuleValidator method validate.

@Override
public void validate(AdaptrisMessage msg) throws CoreException {
    try {
        NamespaceContext namespaceCtx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
        DocumentBuilderFactoryBuilder builder = documentFactoryBuilder(namespaceCtx);
        Document doc = createDocument(msg, builder);
        XPath xp = XPath.newXPathInstance(builder, namespaceCtx);
        for (int stageIndex = 0; stageIndex < validationStages.size(); stageIndex++) {
            ValidationStage v = validationStages.get(stageIndex);
            NodeList n = xp.selectNodeList(doc, v.getIterationXpath());
            validate(n, v.getIterationXpath(), v.failOnIteratorFailure());
            for (int i = 0; i < n.getLength(); i++) {
                Node node = n.item(i);
                String contents = xp.selectSingleTextItem(node, v.getElementXpath());
                for (ContentValidation cv : v.getRules()) {
                    if (!cv.isValid(contents)) {
                        throw new ServiceException(ERR_MSG.replaceAll(I_COUNT, "" + i).replaceAll(I_XP, Matcher.quoteReplacement(v.getIterationXpath())).replaceAll(E_XP, Matcher.quoteReplacement(v.getElementXpath())).replaceAll(CONTENTS, Matcher.quoteReplacement(contents)).replaceAll(VALIDATION_MSG, Matcher.quoteReplacement(cv.getMessage())));
                    }
                }
            }
        }
    } catch (Exception e) {
        ExceptionHelper.rethrowCoreException(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) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) XmlHelper.createDocument(com.adaptris.core.util.XmlHelper.createDocument) Document(org.w3c.dom.Document) ContentValidation(com.adaptris.transform.validate.ContentValidation) ServiceException(com.adaptris.core.ServiceException) CoreException(com.adaptris.core.CoreException) ValidationStage(com.adaptris.transform.validate.ValidationStage)

Example 17 with DocumentBuilderFactoryBuilder

use of com.adaptris.core.util.DocumentBuilderFactoryBuilder in project interlok by adaptris.

the class XpathDocumentCopier method resolveXpath.

private String resolveXpath(AdaptrisMessage msg) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
    NamespaceContext ctx = SimpleNamespaceContext.create(getNamespaceContext(), msg);
    DocumentBuilderFactoryBuilder builder = documentFactoryBuilder();
    if (ctx != null) {
        builder.setNamespaceAware(true);
    }
    Document d = createDocument(msg, builder);
    XPath xp = XPath.newXPathInstance(builder, ctx);
    return xp.selectSingleTextItem(d, getXpath());
}
Also used : XPath(com.adaptris.util.text.xml.XPath) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) XmlHelper.createDocument(com.adaptris.core.util.XmlHelper.createDocument) Document(org.w3c.dom.Document)

Example 18 with DocumentBuilderFactoryBuilder

use of com.adaptris.core.util.DocumentBuilderFactoryBuilder in project interlok by adaptris.

the class XPathService method doService.

// @Override
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    NamespaceContext namespaceContext = SimpleNamespaceContext.create(getNamespaceContext(), msg);
    try {
        DocumentBuilderFactoryBuilder builder = documentFactoryBuilder(namespaceContext);
        Document document = buildDocument(builder, this.getXmlSource().extract(msg));
        XPath xPathHandler = XPath.newXPathInstance(builder, namespaceContext);
        for (Execution execution : this.getExecutions()) {
            String result = this.serializeNode(xPathHandler.selectNodeList(document, execution.getSource().extract(msg)));
            execution.getTarget().insert(result, msg);
        }
    } catch (Exception ex) {
        throw new ServiceException(ex);
    }
}
Also used : XPath(com.adaptris.util.text.xml.XPath) Execution(com.adaptris.core.common.Execution) ServiceException(com.adaptris.core.ServiceException) NamespaceContext(javax.xml.namespace.NamespaceContext) SimpleNamespaceContext(com.adaptris.util.text.xml.SimpleNamespaceContext) DocumentBuilderFactoryBuilder(com.adaptris.core.util.DocumentBuilderFactoryBuilder) Document(org.w3c.dom.Document) ServiceException(com.adaptris.core.ServiceException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) CoreException(com.adaptris.core.CoreException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 19 with DocumentBuilderFactoryBuilder

use of com.adaptris.core.util.DocumentBuilderFactoryBuilder 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 20 with DocumentBuilderFactoryBuilder

use of com.adaptris.core.util.DocumentBuilderFactoryBuilder 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)

Aggregations

DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)32 XPath (com.adaptris.util.text.xml.XPath)21 Document (org.w3c.dom.Document)21 Test (org.junit.Test)18 NamespaceContext (javax.xml.namespace.NamespaceContext)12 CoreException (com.adaptris.core.CoreException)10 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)10 ServiceException (com.adaptris.core.ServiceException)8 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)6 KeyValuePair (com.adaptris.util.KeyValuePair)4 NodeList (org.w3c.dom.NodeList)4 XpathQuery (com.adaptris.core.services.metadata.xpath.XpathQuery)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Node (org.w3c.dom.Node)3 XpathMetadataServiceTest (com.adaptris.core.services.metadata.XpathMetadataServiceTest)2 ConfiguredXpathQuery (com.adaptris.core.services.metadata.xpath.ConfiguredXpathQuery)2 MetadataXpathQuery (com.adaptris.core.services.metadata.xpath.MetadataXpathQuery)2 MultiItemConfiguredXpathQuery (com.adaptris.core.services.metadata.xpath.MultiItemConfiguredXpathQuery)2 MultiItemMetadataXpathQuery (com.adaptris.core.services.metadata.xpath.MultiItemMetadataXpathQuery)2 XmlHelper.createDocument (com.adaptris.core.util.XmlHelper.createDocument)2