Search in sources :

Example 1 with ContentValidation

use of com.adaptris.transform.validate.ContentValidation 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)

Aggregations

CoreException (com.adaptris.core.CoreException)1 ServiceException (com.adaptris.core.ServiceException)1 DocumentBuilderFactoryBuilder (com.adaptris.core.util.DocumentBuilderFactoryBuilder)1 XmlHelper.createDocument (com.adaptris.core.util.XmlHelper.createDocument)1 ContentValidation (com.adaptris.transform.validate.ContentValidation)1 ValidationStage (com.adaptris.transform.validate.ValidationStage)1 SimpleNamespaceContext (com.adaptris.util.text.xml.SimpleNamespaceContext)1 XPath (com.adaptris.util.text.xml.XPath)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 Document (org.w3c.dom.Document)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1