Search in sources :

Example 1 with SVRLFailedAssert

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

the class Issue16Test method testIssue16.

@Test
@Ignore
public void testIssue16() throws Exception {
    final File schematronFile = new ClassPathResource("issues/github16/sample_schematron.sch").getAsFile();
    final File xmlFile = new ClassPathResource("issues/github16/test.xml").getAsFile();
    final SchematronOutputType outputType = SchematronUtil.validateXMLViaXSLTSchematronFull(schematronFile, xmlFile);
    if (outputType == null)
        throw new Exception("SchematronOutputType null");
    final List<SVRLSuccessfulReport> succeededList = SVRLHelper.getAllSuccessfulReports(outputType);
    for (final SVRLSuccessfulReport succeededReport : succeededList) {
        System.out.println(succeededReport.getTest());
    }
    int i = 1;
    final List<SVRLFailedAssert> failedList = SVRLHelper.getAllFailedAssertions(outputType);
    for (final SVRLFailedAssert failedAssert : failedList) {
        System.out.println(i++ + ". Location:" + failedAssert.getLocation());
        System.out.println("Test: " + failedAssert.getTest());
        System.out.println("Text: " + failedAssert.getText());
        final List<DiagnosticReference> diagnisticReferences = failedAssert.getDiagnisticReferences();
        for (final DiagnosticReference diagnisticRef : diagnisticReferences) {
            System.out.println("Diag ref: " + diagnisticRef.getDiagnostic());
            System.out.println("Diag text: " + diagnisticRef.getText());
        }
    }
    if (failedList.isEmpty()) {
        System.out.println("PASS");
    } else {
        System.out.println("FAIL");
    }
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) DiagnosticReference(org.oclc.purl.dsdl.svrl.DiagnosticReference) SVRLSuccessfulReport(com.helger.schematron.svrl.SVRLSuccessfulReport) File(java.io.File) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with SVRLFailedAssert

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

the class Issue29Test method testGood.

@Test
@Ignore("Takes too long - more than 1 min")
public void testGood() throws Exception {
    final SchematronOutputType aSOT = validateXmlUsingSchematron(new GZIPReadableResource(new ClassPathResource("issues/github29/sample.xml.gz")));
    assertNotNull(aSOT);
    final ICommonsList<SVRLFailedAssert> aErrors = SVRLHelper.getAllFailedAssertions(aSOT);
    assertNotNull(aErrors);
    s_aLogger.info("Errors found: " + aErrors);
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) GZIPReadableResource(com.helger.commons.io.resource.wrapped.GZIPReadableResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SVRLFailedAssert

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

the class IssueGC5Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(final File schematron, final File xml) throws Exception {
    final IReadableResource aSchematron = new FileSystemResource(schematron.getAbsoluteFile());
    final IReadableResource anXMLSource = new FileSystemResource(xml.getAbsoluteFile());
    final SchematronResourcePure pure = new SchematronResourcePure(aSchematron);
    // final FileOutputStream fos = new FileOutputStream (result);
    // final Result res = new StreamResult (fos);
    // res.setSystemId(result.getAbsolutePath());
    // final SchematronOutputType svrl = pure.applySchematronValidationToSVRL
    // (anXMLSource);
    final SchematronOutputType aSO = SchematronHelper.applySchematron(pure, anXMLSource);
    final List<SVRLFailedAssert> aFailedAsserts = SVRLHelper.getAllFailedAssertions(aSO);
    System.out.println(aFailedAsserts);
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) IReadableResource(com.helger.commons.io.resource.IReadableResource) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure)

Example 4 with SVRLFailedAssert

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

the class SchematronValidationMojo method _performValidation.

private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final File aXMLDirectory, @Nullable final String sXMLIncludes, @Nullable final String sXMLExcludes, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess) throws MojoExecutionException, MojoFailureException {
    final DirectoryScanner aScanner = new DirectoryScanner();
    aScanner.setBasedir(aXMLDirectory);
    if (StringHelper.hasText(sXMLIncludes))
        aScanner.setIncludes(new String[] { sXMLIncludes });
    if (StringHelper.hasText(sXMLExcludes))
        aScanner.setExcludes(new String[] { sXMLExcludes });
    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");
                    if (!aSVRLFile.getParentFile().mkdirs())
                        getLog().error("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "'!");
                    if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
                        getLog().info("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'");
                    else
                        getLog().error("Error saving SVRL file '" + aSVRLFile.getPath() + "'");
                }
                final ICommonsList<SVRLFailedAssert> aFailedAsserts = SVRLHelper.getAllFailedAssertions(aSOT);
                if (bExpectSuccess) {
                    // No failed assertions expected
                    if (aFailedAsserts.isNotEmpty()) {
                        final String sMessage = aFailedAsserts.size() + " failed Schematron assertions for XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        aFailedAsserts.forEach(x -> getLog().error(x.getAsResourceError(aXMLFile.getPath()).getAsString(Locale.US)));
                        throw new MojoFailureException(sMessage);
                    }
                } else {
                    // At least one failed assertions expected
                    if (aFailedAsserts.isEmpty()) {
                        final String sMessage = "No failed Schematron assertions for erroneous XML file '" + aXMLFile.getPath() + "'";
                        getLog().error(sMessage);
                        throw new MojoFailureException(sMessage);
                    }
                }
            } catch (final MojoFailureException | MojoExecutionException 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 : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) DirectoryScanner(org.codehaus.plexus.util.DirectoryScanner) MojoFailureException(org.apache.maven.plugin.MojoFailureException) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 5 with SVRLFailedAssert

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

the class SchematronHelper method convertToErrorList.

/**
 * Convert a {@link SchematronOutputType} to an {@link IErrorList}.
 *
 * @param aSchematronOutput
 *        The result of Schematron validation
 * @param sResourceName
 *        The name of the resource that was validated (may be a file path
 *        etc.)
 * @return List non-<code>null</code> error list of {@link SVRLResourceError}
 *         objects.
 */
@Nonnull
public static IErrorList convertToErrorList(@Nonnull final SchematronOutputType aSchematronOutput, @Nullable final String sResourceName) {
    ValueEnforcer.notNull(aSchematronOutput, "SchematronOutput");
    final ErrorList ret = new ErrorList();
    for (final SVRLFailedAssert aFailedAssert : SVRLHelper.getAllFailedAssertions(aSchematronOutput)) ret.add(aFailedAssert.getAsResourceError(sResourceName));
    return ret;
}
Also used : ErrorList(com.helger.commons.error.list.ErrorList) IErrorList(com.helger.commons.error.list.IErrorList) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) Nonnull(javax.annotation.Nonnull)

Aggregations

SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)5 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)4 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 File (java.io.File)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 ErrorList (com.helger.commons.error.list.ErrorList)1 IErrorList (com.helger.commons.error.list.IErrorList)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 GZIPReadableResource (com.helger.commons.io.resource.wrapped.GZIPReadableResource)1 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)1 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 SVRLSuccessfulReport (com.helger.schematron.svrl.SVRLSuccessfulReport)1 Nonnull (javax.annotation.Nonnull)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 DirectoryScanner (org.codehaus.plexus.util.DirectoryScanner)1 DiagnosticReference (org.oclc.purl.dsdl.svrl.DiagnosticReference)1