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