Search in sources :

Example 51 with SchematronOutputType

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

the class Issue083Test method _validateAndProduceSVRL.

private static void _validateAndProduceSVRL(@Nonnull final File aSchematron, @Nonnull final File aXML) throws Exception {
    final SchematronResourceSCH aSCH = SchematronResourceSCH.fromFile(aSchematron);
    SchematronDebug.setSaveIntermediateXSLTFiles(true);
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    final String sSVRL = new SVRLMarshaller().getAsString(aSVRL);
    assertNotNull(sSVRL);
    if (true)
        LOGGER.info("SVRL:\n" + sSVRL);
    // XXX should be 1 according to #83
    assertEquals(0, SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSVRL).size());
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) FileSystemResource(com.helger.commons.io.resource.FileSystemResource)

Example 52 with SchematronOutputType

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

the class Issue123Test method _validateAndProduceSVRL.

private static void _validateAndProduceSVRL(@Nonnull final File aSchematron, @Nonnull final File aXML) throws Exception {
    SchematronDebug.setSaveIntermediateXSLTFiles(true);
    final ISchematronResource aSCH = SchematronResourceSCH.fromFile(aSchematron);
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    LOGGER.info("SVRL:\n" + new SVRLMarshaller().getAsString(aSVRL));
    if (false)
        assertTrue(SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSVRL).isEmpty());
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) FileSystemResource(com.helger.commons.io.resource.FileSystemResource)

Example 53 with SchematronOutputType

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

the class SchematronHelperFuncTest method testReadInvalidSchematronInvalidXML.

@Test
public void testReadInvalidSchematronInvalidXML() throws Exception {
    final SchematronOutputType aSO = SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON + ".does.not.exist").applySchematronValidationToSVRL(new ClassPathResource(VALID_XMLINSTANCE + ".does.not.exist"));
    assertNull("Invalid Schematron and XML", aSO);
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 54 with SchematronOutputType

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

the class SchematronHelperFuncTest method testReadValidSchematronValidXML.

@Test
public void testReadValidSchematronValidXML() throws Exception {
    final ISchematronResource aSchematron = SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON);
    final IReadableResource aXML = new ClassPathResource(VALID_XMLINSTANCE);
    final SchematronOutputType aSO = aSchematron.applySchematronValidationToSVRL(aXML);
    assertNotNull("Failed to parse Schematron output", aSO);
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) IReadableResource(com.helger.commons.io.resource.IReadableResource) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 55 with SchematronOutputType

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

the class SchematronValidationMojo method _performValidation.

/**
 * @param aSch
 *        Schematron resource to apply on validation artefacts
 * @param aXMLDirectory
 *        XML directory to be scanned
 * @param aXMLIncludes
 *        XML include mask - may be <code>null</code> or empty
 * @param aXMLExcludes
 *        XML exclude mask - may be <code>null</code> or empty
 * @param aSVRLDirectory
 *        SVRL directory to write to (maybe <code>null</code> in which case
 *        the SVRL is not written)
 * @param bExpectSuccess
 *        <code>true</code> if this is a positive validation,
 *        <code>false</code> if error is expected
 * @param aErrorMessages
 *        The list of collected error messages (only used if fail-fast is
 *        disabled)
 * @throws MojoExecutionException
 *         Internal error
 * @throws MojoFailureException
 *         Validation error
 */
private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final File aXMLDirectory, @Nullable final String[] aXMLIncludes, @Nullable final String[] aXMLExcludes, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess, @Nonnull final ICommonsList<String> aErrorMessages) throws MojoExecutionException, MojoFailureException {
    final DirectoryScanner aScanner = new DirectoryScanner();
    aScanner.setBasedir(aXMLDirectory);
    if (ArrayHelper.isNotEmpty(aXMLIncludes))
        aScanner.setIncludes(aXMLIncludes);
    if (ArrayHelper.isNotEmpty(aXMLExcludes))
        aScanner.setExcludes(aXMLExcludes);
    aScanner.setCaseSensitive(true);
    aScanner.scan();
    final String[] aXMLFilenames = aScanner.getIncludedFiles();
    if (aXMLFilenames != null) {
        for (final String sXMLFilename : aXMLFilenames) {
            final File aXMLFile = new File(aXMLDirectory, sXMLFilename);
            // Validate XML file
            getLog().info("Validating XML file '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile + "' expecting " + (bExpectSuccess ? "success" : "failure"));
            try {
                final SchematronOutputType aSOT = aSch.applySchematronValidationToSVRL(TransformSourceFactory.create(aXMLFile));
                if (aSVRLDirectory != null) {
                    // Save SVRL
                    final File aSVRLFile = new File(aSVRLDirectory, sXMLFilename + ".svrl");
                    final FileIOError aIOErr = FileOperationManager.INSTANCE.createDirRecursiveIfNotExisting(aSVRLFile.getParentFile());
                    if (aIOErr.isFailure())
                        getLog().error("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "': " + aIOErr.toString());
                    if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
                        getLog().info("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'");
                    else
                        getLog().error("Error saving SVRL file '" + aSVRLFile.getPath() + "'");
                }
                // Failed asserts and Successful reports
                final ICommonsList<AbstractSVRLMessage> aSVRLErrors = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSOT);
                if (bExpectSuccess) {
                    // No failed assertions expected
                    if (aSVRLErrors.isNotEmpty()) {
                        final String sMessage = aSVRLErrors.size() + " failed Schematron assertions for XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        aSVRLErrors.forEach(x -> getLog().error(x.getAsResourceError(aXMLFile.getPath()).getAsString(Locale.US)));
                        if (m_bFailFast)
                            throw new MojoFailureException(sMessage);
                        aErrorMessages.add(sMessage);
                    }
                } else {
                    // At least one failed assertions expected
                    if (aSVRLErrors.isEmpty()) {
                        final String sMessage = "No failed Schematron assertions for erroneous XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        if (m_bFailFast)
                            throw new MojoFailureException(sMessage);
                        aErrorMessages.add(sMessage);
                    }
                }
            } catch (final MojoExecutionException | MojoFailureException up) {
                throw up;
            } catch (final Exception ex) {
                final String sMessage = "Exception validating XML '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile + "'";
                getLog().error(sMessage, ex);
                throw new MojoExecutionException(sMessage, ex);
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) FileIOError(com.helger.commons.io.file.FileIOError) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) File(java.io.File)

Aggregations

SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)59 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)33 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)32 Test (org.junit.Test)21 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)18 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)15 IReadableResource (com.helger.commons.io.resource.IReadableResource)13 SchematronResourceSCH (com.helger.schematron.sch.SchematronResourceSCH)12 ISchematronResource (com.helger.schematron.ISchematronResource)11 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)9 Document (org.w3c.dom.Document)9 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)7 IXPathConfig (com.helger.schematron.pure.xpath.IXPathConfig)6 XPathConfigBuilder (com.helger.schematron.pure.xpath.XPathConfigBuilder)6 Nonnull (javax.annotation.Nonnull)5 SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)4 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)4 File (java.io.File)4 LoggingPSValidationHandler (com.helger.schematron.pure.validation.LoggingPSValidationHandler)3 XQueryAsXPathFunctionConverter (com.helger.schematron.pure.xpath.XQueryAsXPathFunctionConverter)3