use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class Issue29Test method validateXmlUsingSchematron.
@Nullable
static SchematronOutputType validateXmlUsingSchematron(@Nonnull final IReadableResource aRes) {
SchematronOutputType ob = null;
// Must use the XSLT based version, because of "key" usage
final ISchematronResource aResSCH = new SchematronResourcePure(new ClassPathResource("issues/github29/pbs.sch"));
if (!aResSCH.isValidSchematron())
throw new IllegalArgumentException("Invalid Schematron!");
try {
final Document aDoc = aResSCH.applySchematronValidation(new ResourceStreamSource(aRes));
if (aDoc != null) {
final SVRLMarshaller marshaller = new SVRLMarshaller();
ob = marshaller.read(aDoc);
}
} catch (final Exception pE) {
pE.printStackTrace();
}
return ob;
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class Issue54Test method validateAndProduceSVRL.
public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
final ISchematronResource aSCH = SchematronResourcePure.fromFile(aSchematron);
// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class IssueGC4Test method validateXMLViaPureSchematron.
public static boolean validateXMLViaPureSchematron(final File aSchematronFile, final File aXMLFile) throws Exception {
final ISchematronResource aResPure = SchematronResourcePure.fromFile(aSchematronFile);
System.out.println("Valid Şematron mu? " + aResPure.isValidSchematron());
return aResPure.getSchematronValidity(new StreamSource(aXMLFile)).isValid();
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class SchematronResourceSCHCacheTest method testValidSynchronous.
@Test
public void testValidSynchronous() throws Exception {
// Ensure that the Schematron is cached
SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON);
final long nStart = System.nanoTime();
for (int i = 0; i < RUNS; ++i) {
final ISchematronResource aSV = SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON);
final Document aDoc = aSV.applySchematronValidation(new ClassPathResource(VALID_XMLINSTANCE));
assertNotNull(aDoc);
if (false)
s_aLogger.info(XMLWriter.getNodeAsString(aDoc));
}
final long nEnd = System.nanoTime();
s_aLogger.info("Sync Total: " + ((nEnd - nStart) / 1000) + " microsecs btw. " + ((nEnd - nStart) / 1000 / RUNS) + " microsecs/run");
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class Schematron method _performValidation.
private void _performValidation(@Nonnull final ISchematronResource aSch, @Nonnull final ICommonsList<ResourceCollection> aResCollections, @Nullable final File aSVRLDirectory, final boolean bExpectSuccess) throws BuildException {
// Resolve resourceCollections - pain in the ass
final ICommonsMap<File, DirectoryData> aFiles = new CommonsHashMap<>();
for (final ResourceCollection aResCollection : aResCollections) {
if (!aResCollection.isFilesystemOnly())
_error("Only FileSystem resources are supported.");
else
for (final Resource aRes : aResCollection) {
if (!aRes.isExists()) {
_error("Could not find resource " + aRes.toLongString() + " to copy.");
continue;
}
File baseDir = NULL_FILE_PLACEHOLDER;
String name = aRes.getName();
final FileProvider fp = aRes.as(FileProvider.class);
if (fp != null) {
final FileResource fr = ResourceUtils.asFileResource(fp);
baseDir = _getKeyFile(fr.getBaseDir());
if (baseDir == NULL_FILE_PLACEHOLDER)
name = fr.getFile().getAbsolutePath();
}
if ((aRes.isDirectory() || fp != null) && name != null) {
final DirectoryData aBaseDir = aFiles.computeIfAbsent(_getKeyFile(baseDir), k -> new DirectoryData(k));
if (aRes.isDirectory())
aBaseDir.addDir(name);
else
aBaseDir.addFile(name);
} else
_error("Could not resolve resource " + aRes.toLongString() + " to a file.");
}
}
for (final DirectoryData aBaseDir : aFiles.values()) {
log("Scanning directory " + aBaseDir.getBaseDir() + " for XMLs to be Schematron validated", Project.MSG_DEBUG);
final ICommonsList<String> aIncludes = new CommonsArrayList<>();
aIncludes.addAll(aBaseDir.getFiles());
for (final String sFile : aBaseDir.getDirs()) aIncludes.add(sFile + "/**");
final DirectoryScanner aScanner = new DirectoryScanner();
aScanner.setBasedir(aBaseDir.getBaseDir());
if (aIncludes.isNotEmpty())
aScanner.setIncludes(aIncludes.toArray(new String[0]));
aScanner.setCaseSensitive(true);
aScanner.scan();
final String[] aXMLFilenames = aScanner.getIncludedFiles();
if (aXMLFilenames != null) {
for (final String sXMLFilename : aXMLFilenames) {
final File aXMLFile = new File(aBaseDir.getBaseDir(), sXMLFilename);
// Validate XML file
log("Validating XML file '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "' expecting " + (bExpectSuccess ? "success" : "failure"), Project.MSG_INFO);
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())
log("Failed to create parent directory of '" + aSVRLFile.getAbsolutePath() + "'!", Project.MSG_ERR);
if (new SVRLMarshaller().write(aSOT, aSVRLFile).isSuccess())
log("Successfully saved SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_INFO);
else
log("Error saving SVRL file '" + aSVRLFile.getPath() + "'", Project.MSG_ERR);
}
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSOT));
final ICommonsList<AbstractSVRLMessage> aMessages = SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSOT);
final int nErrorMessages = aMessages.getCount(x -> x.getFlag().isError());
final int nWarningMessages = aMessages.size() - nErrorMessages;
final String sErrors = nErrorMessages + " Schematron error" + (nErrorMessages == 1 ? "" : "s");
final String sWarnings = nWarningMessages + " Schematron warning" + (nWarningMessages == 1 ? "" : "s");
if (bExpectSuccess) {
// No failed assertions expected
if (nErrorMessages > 0) {
final StringBuilder aMessage = new StringBuilder();
aMessage.append(sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " for XML file '" + aXMLFile.getPath() + "'");
for (final AbstractSVRLMessage aMsg : aMessages) {
aMessage.append("\n ").append(ErrorTextProvider.DEFAULT.getErrorText(aMsg.getAsResourceError(aXMLFile.getPath()), Locale.US));
}
// As at least one error is contained, it's okay to throw an
// exception in case
_error(aMessage.toString());
continue;
}
// Success as expected
log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' and matches the rules" + (nWarningMessages > 0 ? " - only " + sWarnings + " are contained" : ""), Project.MSG_INFO);
} else {
// At least one failed assertions expected
if (nErrorMessages == 0) {
String sMessage = "No Schematron errors for erroneous XML file '" + aXMLFile.getPath() + "'";
if (nWarningMessages > 0)
sMessage += " - only " + sWarnings + " are contained";
_error(sMessage);
continue;
}
// Success as expected
log("XML file '" + aXMLFile.getPath() + "' was validated against Schematron '" + aSch.getResource().getPath() + "' " + sErrors + (nWarningMessages > 0 ? " and " + sWarnings : "") + " were found (as expected)", Project.MSG_INFO);
}
} catch (final BuildException up) {
throw up;
} catch (final Exception ex) {
final String sMessage = "Exception validating XML '" + aXMLFile.getPath() + "' against Schematron rules from '" + m_aSchematronFile.getName() + "'. Technical details: " + ex.getClass().getSimpleName() + " - " + ex.getMessage();
_error(sMessage, ex);
continue;
}
}
}
}
}
Aggregations