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());
}
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());
}
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);
}
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);
}
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);
}
}
}
}
Aggregations