Search in sources :

Example 1 with FailedAssert

use of com.helger.schematron.svrl.jaxb.FailedAssert in project ph-schematron by phax.

the class PSXPathValidationHandlerSVRL method onFailedAssert.

@Override
@Nonnull
public EContinue onFailedAssert(@Nonnull final PSAssertReport aAssertReport, @Nonnull final String sTestExpression, @Nonnull final Node aRuleMatchingNode, final int nNodeIndex, @Nullable final Object aContext) throws SchematronValidationException {
    if (!(aContext instanceof PSXPathBoundAssertReport))
        throw new SchematronValidationException("The passed context must be an XPath object but is a " + aContext);
    final PSXPathBoundAssertReport aBoundAssertReport = (PSXPathBoundAssertReport) aContext;
    final FailedAssert aFailedAssert = new FailedAssert();
    aFailedAssert.setFlag(aAssertReport.getFlag());
    aFailedAssert.setId(aAssertReport.getID());
    aFailedAssert.setLocation(_getPathToNode(aRuleMatchingNode));
    if (aAssertReport.hasLinkable())
        aFailedAssert.setRole(aAssertReport.getLinkable().getRole());
    aFailedAssert.setTest(sTestExpression);
    aFailedAssert.getDiagnosticReferenceOrPropertyReferenceOrText().add(_getErrorText(aBoundAssertReport.getAllBoundContentElements(), aRuleMatchingNode));
    _handleDiagnosticReferences(aAssertReport.getAllDiagnostics(), aFailedAssert.getDiagnosticReferenceOrPropertyReferenceOrText(), aBoundAssertReport, aRuleMatchingNode);
    m_aSchematronOutput.addActivePatternAndFiredRuleAndFailedAssert(aFailedAssert);
    return EContinue.CONTINUE;
}
Also used : PSXPathBoundAssertReport(com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) FailedAssert(com.helger.schematron.svrl.jaxb.FailedAssert) Nonnull(javax.annotation.Nonnull)

Example 2 with FailedAssert

use of com.helger.schematron.svrl.jaxb.FailedAssert in project eCRNow by drajer-health.

the class CdaValidatorUtil method validateEicrToSchematron.

/**
 * Method used for validating XML data against valid Schematron returns true if XML data matched
 * Schematton
 *
 * @param ecrData
 * @return boolean value
 */
public static boolean validateEicrToSchematron(String ecrData) {
    boolean validationResult = false;
    final ISchematronResource aResSCH = SchematronResourceSCH.fromFile(ActionRepo.getInstance().getSchematronFileLocation());
    if (!aResSCH.isValidSchematron()) {
        logger.warn("*** Cannot Validate EICR since Schematron is not valid ***");
    } else {
        SchematronOutputType output = null;
        try {
            logger.debug("Found Valid Schematron which can be applied to EICR");
            output = aResSCH.applySchematronValidationToSVRL(new StreamSource(new StringReader(ecrData)));
        } catch (Exception e) {
            logger.error("Unable to read/write execution state: ", e);
        }
        if (output != null) {
            List<Object> objs = output.getActivePatternAndFiredRuleAndFailedAssert();
            boolean foundFailures = false;
            logger.info("Number of Failed Assertions {}", objs.size());
            for (Object obj : objs) {
                if (obj instanceof FailedAssert) {
                    FailedAssert fa = (FailedAssert) obj;
                    if (fa.getFlag() != null && (fa.getFlag().contentEquals("error"))) {
                        foundFailures = true;
                        logger.error("Failed Assertion: \n" + "Id = {}\n" + "Location = {}\n" + "Text = {}\n" + "Flag = {}", fa.getId(), fa.getLocation(), fa.getText(), fa.getFlag());
                    } else {
                        // It is a warning, so need to print to log for analysis
                        logger.debug("Failed Assertion: \n" + "Id = {}\n" + "Location = {}\n" + "Text = {}\n" + "Flag = {}", fa.getId(), fa.getLocation(), fa.getText(), fa.getFlag());
                    }
                }
            }
            if (foundFailures)
                validationResult = false;
            else
                validationResult = true;
        } else {
            logger.warn("Schematron Validation Output is null, so validation was not performed");
            validationResult = false;
        }
    }
    logger.info("Schematron Validation Result: {}", validationResult);
    return validationResult;
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) StreamSource(javax.xml.transform.stream.StreamSource) FailedAssert(com.helger.schematron.svrl.jaxb.FailedAssert) StringReader(java.io.StringReader) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with FailedAssert

use of com.helger.schematron.svrl.jaxb.FailedAssert in project mustangproject by ZUGFeRD.

the class XMLValidator method validateSchematron.

/**
 * validate using a xslt file generated from a schematron in the build preparation of this software
 * @param xml the xml to be checked
 * @param xsltFilename the filename of the intermediate XSLT file
 * @param section the error type code, if one arises
 * @param severity how serious a error should be treated - may only be notice
 * @throws IrrecoverableValidationError if anything happened that prevents further checks
 */
public void validateSchematron(String xml, String xsltFilename, int section, ESeverity severity) throws IrrecoverableValidationError {
    ISchematronResource aResSCH = null;
    aResSCH = SchematronResourceXSLT.fromClassPath(xsltFilename);
    if (aResSCH != null) {
        if (!aResSCH.isValidSchematron()) {
            throw new IllegalArgumentException(xsltFilename + " is invalid Schematron!");
        }
        final SchematronOutputType sout;
        try {
            sout = aResSCH.applySchematronValidationToSVRL(new StreamSource(new StringReader(xml)));
        } catch (final Exception e) {
            throw new IrrecoverableValidationError(e.getMessage());
        }
        final List<Object> failedAsserts = sout.getActivePatternAndFiredRuleAndFailedAssert();
        if (failedAsserts.size() > 0) {
            for (final Object object : failedAsserts) {
                if (object instanceof FailedAssert) {
                    final FailedAssert failedAssert = (FailedAssert) object;
                    LOGGER.info("FailedAssert ", failedAssert);
                    context.addResultItem(new ValidationResultItem(severity, SVRLHelper.getAsString(failedAssert.getText()) + " (From " + xsltFilename + ")").setLocation(failedAssert.getLocation()).setCriterion(failedAssert.getTest()).setSection(section).setPart(EPart.fx));
                    failedRules++;
                } else if (object instanceof FiredRule) {
                    firedRules++;
                }
            }
        }
        if (firedRules == 0) {
            context.addResultItem(new ValidationResultItem(ESeverity.error, "No rules matched, XML to minimal?").setSection(26).setPart(EPart.fx));
        }
    // for (String currentString : sout.getText()) {
    // schematronValidationString += "<output>" + currentString + "</output>";
    // }
    // schematronValidationString += new SVRLMarshaller ().getAsString (sout);
    // returns the complete SVRL
    }
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) FiredRule(com.helger.schematron.svrl.jaxb.FiredRule) StreamSource(javax.xml.transform.stream.StreamSource) FailedAssert(com.helger.schematron.svrl.jaxb.FailedAssert) StringReader(java.io.StringReader) IOException(java.io.IOException)

Aggregations

FailedAssert (com.helger.schematron.svrl.jaxb.FailedAssert)3 ISchematronResource (com.helger.schematron.ISchematronResource)2 SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 StreamSource (javax.xml.transform.stream.StreamSource)2 PSXPathBoundAssertReport (com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport)1 SchematronValidationException (com.helger.schematron.pure.validation.SchematronValidationException)1 FiredRule (com.helger.schematron.svrl.jaxb.FiredRule)1 Nonnull (javax.annotation.Nonnull)1 SAXException (org.xml.sax.SAXException)1