Search in sources :

Example 1 with XmlValidatorException

use of nl.nn.adapterframework.validation.XmlValidatorException in project iaf by ibissource.

the class Json2XmlValidator method alignJson.

protected PipeRunResult alignJson(String messageToValidate, IPipeLineSession session, boolean responseMode) throws PipeRunException, XmlValidatorException {
    ValidationContext context;
    ValidatorHandler validatorHandler;
    try {
        context = validator.createValidationContext(session, getRootValidations(responseMode), getInvalidRootNamespaces());
        validatorHandler = validator.getValidatorHandler(session, context);
    } catch (ConfigurationException e) {
        throw new PipeRunException(this, "Cannot create ValidationContext", e);
    }
    String resultEvent;
    String out = null;
    try {
        Json2Xml aligner = new Json2Xml(validatorHandler, context.getXsModels(), isCompactJsonArrays(), getMessageRoot(responseMode), isStrictJsonArraySyntax());
        if (StringUtils.isNotEmpty(getTargetNamespace())) {
            aligner.setTargetNamespace(getTargetNamespace());
        }
        aligner.setDeepSearch(isDeepSearch());
        aligner.setErrorHandler(context.getErrorHandler());
        aligner.setFailOnWildcards(isFailOnWildcards());
        ParameterList parameterList = getParameterList();
        if (parameterList != null) {
            ParameterResolutionContext prc = new ParameterResolutionContext(messageToValidate, session, isNamespaceAware(), false);
            Map<String, Object> parametervalues = null;
            parametervalues = prc.getValueMap(parameterList);
            aligner.setOverrideValues(parametervalues);
        }
        JsonStructure jsonStructure = Json.createReader(new StringReader(messageToValidate)).read();
        if (getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
            Xml2Json xml2json = new Xml2Json(aligner, isCompactJsonArrays(), !isJsonWithRootElements());
            aligner.setContentHandler(xml2json);
            aligner.startParse(jsonStructure);
            out = xml2json.toString();
        } else {
            Source source = aligner.asSource(jsonStructure);
            out = XmlUtils.source2String(source, isProduceNamespaceLessXml());
        }
    } catch (Exception e) {
        resultEvent = validator.finalizeValidation(context, session, e);
    }
    resultEvent = validator.finalizeValidation(context, session, null);
    PipeForward forward = determineForward(resultEvent, session, responseMode);
    PipeRunResult result = new PipeRunResult(forward, out);
    return result;
}
Also used : ValidatorHandler(javax.xml.validation.ValidatorHandler) PipeForward(nl.nn.adapterframework.core.PipeForward) Source(javax.xml.transform.Source) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) ValidationContext(nl.nn.adapterframework.validation.ValidationContext) PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) StringReader(java.io.StringReader) ParameterList(nl.nn.adapterframework.parameters.ParameterList) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) Xml2Json(nl.nn.adapterframework.align.Xml2Json) Json2Xml(nl.nn.adapterframework.align.Json2Xml) JsonStructure(javax.json.JsonStructure)

Example 2 with XmlValidatorException

use of nl.nn.adapterframework.validation.XmlValidatorException in project iaf by ibissource.

the class Json2XmlValidator method doPipe.

/**
 * Validate the XML or JSON string. The format is automatically detected.
 * @param input a String
 * @param session a {@link nl.nn.adapterframework.core.IPipeLineSession Pipelinesession}
 *
 * @throws PipeRunException when <code>isThrowException</code> is true and a validationerror occurred.
 */
@Override
public PipeRunResult doPipe(Object input, IPipeLineSession session, boolean responseMode) throws PipeRunException {
    String messageToValidate = input == null ? "{}" : input.toString();
    int i = 0;
    while (i < messageToValidate.length() && Character.isWhitespace(messageToValidate.charAt(i))) i++;
    if (i >= messageToValidate.length()) {
        messageToValidate = "{}";
        storeInputFormat(FORMAT_JSON, session, responseMode);
    } else {
        char firstChar = messageToValidate.charAt(i);
        if (firstChar == '<') {
            // message is XML
            if (isAcceptNamespaceLessXml()) {
                messageToValidate = addNamespace(messageToValidate);
            // if (log.isDebugEnabled()) log.debug("added namespace to message ["+messageToValidate+"]");
            }
            storeInputFormat(FORMAT_XML, session, responseMode);
            if (!getOutputFormat(session, responseMode).equalsIgnoreCase(FORMAT_JSON)) {
                PipeRunResult result = super.doPipe(messageToValidate, session, responseMode);
                if (isProduceNamespaceLessXml()) {
                    String msg = (String) result.getResult();
                    msg = XmlUtils.removeNamespaces(msg);
                    result.setResult(msg);
                }
                return result;
            }
            try {
                return alignXml2Json(messageToValidate, session, responseMode);
            } catch (Exception e) {
                throw new PipeRunException(this, "Alignment of XML to JSON failed", e);
            }
        }
        if (firstChar != '{' && firstChar != '[') {
            throw new PipeRunException(this, "message is not XML or JSON, because it starts with [" + firstChar + "] and not with '<', '{' or '['");
        }
        storeInputFormat(FORMAT_JSON, session, responseMode);
    }
    try {
        return alignJson(messageToValidate, session, responseMode);
    } catch (XmlValidatorException e) {
        throw new PipeRunException(this, "Cannot align JSON", e);
    }
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException)

Example 3 with XmlValidatorException

use of nl.nn.adapterframework.validation.XmlValidatorException in project iaf by ibissource.

the class XmlValidatorTest2 method validate.

@Override
public String validate(String rootNamespace, String schemaLocation, boolean addNamespaceToSchema, boolean ignoreUnknownNamespaces, String inputfile, String[] expectedFailureReasons) throws ConfigurationException, InstantiationException, IllegalAccessException, XmlValidatorException, PipeRunException, IOException {
    String testXml = inputfile != null ? getTestXml(inputfile + ".xml") : null;
    IPipeLineSession session = new PipeLineSessionBase();
    try {
        XmlValidator validator = getValidator(schemaLocation, addNamespaceToSchema, implementation);
        validator.setIgnoreUnknownNamespaces(ignoreUnknownNamespaces);
        PipeForward forward = validator.validate(testXml, session);
        evaluateResult(forward.getName(), session, null, expectedFailureReasons);
    } catch (Exception e) {
        evaluateResult(null, session, e, expectedFailureReasons);
        return "Invalid XML";
    }
    return null;
}
Also used : AbstractXmlValidator(nl.nn.adapterframework.validation.AbstractXmlValidator) XercesXmlValidator(nl.nn.adapterframework.validation.XercesXmlValidator) JavaxXmlValidator(nl.nn.adapterframework.validation.JavaxXmlValidator) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) PipeForward(nl.nn.adapterframework.core.PipeForward) PipeRunException(nl.nn.adapterframework.core.PipeRunException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) XmlValidatorException(nl.nn.adapterframework.validation.XmlValidatorException) PipeLineSessionBase(nl.nn.adapterframework.core.PipeLineSessionBase)

Aggregations

ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)3 PipeRunException (nl.nn.adapterframework.core.PipeRunException)3 XmlValidatorException (nl.nn.adapterframework.validation.XmlValidatorException)3 PipeForward (nl.nn.adapterframework.core.PipeForward)2 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 JsonStructure (javax.json.JsonStructure)1 Source (javax.xml.transform.Source)1 ValidatorHandler (javax.xml.validation.ValidatorHandler)1 Json2Xml (nl.nn.adapterframework.align.Json2Xml)1 Xml2Json (nl.nn.adapterframework.align.Xml2Json)1 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)1 PipeLineSessionBase (nl.nn.adapterframework.core.PipeLineSessionBase)1 ParameterList (nl.nn.adapterframework.parameters.ParameterList)1 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)1 AbstractXmlValidator (nl.nn.adapterframework.validation.AbstractXmlValidator)1 JavaxXmlValidator (nl.nn.adapterframework.validation.JavaxXmlValidator)1 ValidationContext (nl.nn.adapterframework.validation.ValidationContext)1 XercesXmlValidator (nl.nn.adapterframework.validation.XercesXmlValidator)1