use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class Schematron method execute.
@Override
public void execute() throws BuildException {
boolean bCanRun = false;
if (m_aSchematronFile == null)
_error("No Schematron file specified!");
else if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
_error("The specified Schematron file " + m_aSchematronFile + " is not a file!");
else if (m_eSchematronProcessingEngine == null)
_error("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " + StringHelper.getImplodedMapped(", ", ESchematronMode.values(), x -> "'" + x.getID() + "'"));
else if (m_aResCollections.isEmpty())
_error("No XML resources to be validated specified! Add e.g. a <fileset> element.");
else if (m_aSvrlDirectory != null && !m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
_error("Failed to create the SVRL directory " + m_aSvrlDirectory);
else
bCanRun = true;
if (bCanRun) {
// 1. Parse Schematron file
final Locale aDisplayLocale = Locale.US;
ISchematronResource aSch = null;
IErrorList aSCHErrors = null;
switch(m_eSchematronProcessingEngine) {
case PURE:
{
// pure
final CollectingPSErrorHandler aErrorHdl = new CollectingPSErrorHandler();
final SchematronResourcePure aRealSCH = new SchematronResourcePure(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setErrorHandler(aErrorHdl);
aRealSCH.setEntityResolver(getEntityResolver());
aRealSCH.validateCompletely();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getAllErrors();
break;
}
case SCHEMATRON:
{
// SCH
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setLanguageCode(m_sLanguageCode);
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.setURIResolver(getURIResolver());
aRealSCH.setEntityResolver(getEntityResolver());
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
case XSLT:
{
// SCH
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceXSLT aRealSCH = new SchematronResourceXSLT(new FileSystemResource(m_aSchematronFile));
// phase and language are ignored because this was decided when the
// XSLT
// was created
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.setURIResolver(getURIResolver());
aRealSCH.setEntityResolver(getEntityResolver());
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
default:
_error("No handler for processing engine '" + m_eSchematronProcessingEngine + "'");
break;
}
if (aSCHErrors != null) {
// Error validating the Schematrons!!
boolean bAnyParsingError = false;
for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
log("Error in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_ERR);
bAnyParsingError = true;
} else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
log("Warning in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_WARN);
if (bAnyParsingError)
_error("The provided Schematron file contains errors. See log for details.");
else {
log("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'", Project.MSG_INFO);
// 2. for all XML files that match the pattern
_performValidation(aSch, m_aResCollections, m_aSvrlDirectory, m_bExpectSuccess);
}
}
}
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class Issue44Test method validateAndProduceSVRL.
public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
// SchematronResourcePure fails!
ISchematronResource aSCH = SchematronResourcePure.fromFile(aSchematron);
// Parsing Schematron works!
aSCH = SchematronResourceSCH.fromFile(aSchematron);
// Perform validation
final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
assertEquals(3, SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSVRL).size());
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class SchematronResourceSCHCacheTest method testValidAsynchronous.
@Test
public void testValidAsynchronous() throws Exception {
// Ensure that the Schematron is cached
SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON);
// Create Thread pool with fixed number of threads
final ExecutorService aSenderThreadPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
final long nStart = System.nanoTime();
for (int i = 0; i < RUNS; ++i) {
aSenderThreadPool.submit(() -> {
try {
final ISchematronResource aSV = SchematronResourceSCH.fromClassPath(VALID_SCHEMATRON);
final Document aDoc = aSV.applySchematronValidation(new ClassPathResource(VALID_XMLINSTANCE));
assertNotNull(aDoc);
} catch (final Exception ex) {
throw new IllegalStateException(ex);
}
});
}
ExecutorServiceHelper.shutdownAndWaitUntilAllTasksAreFinished(aSenderThreadPool);
final long nEnd = System.nanoTime();
s_aLogger.info("Async 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 IssueSO32784781Test method testBasic.
@Test
public void testBasic() {
final String sSCH = "<schema xmlns=\"http://purl.oclc.org/dsdl/schematron\">\r\n" + " <ns prefix=\"m\" uri=\"http://www.ociweb.com/movies\"/>\r\n" + " <pattern name=\"all\">\r\n" + " <rule context=\"m:actor\">\r\n" + " <report test=\"@role=preceding-sibling::m:actor/@role\"\r\n" + " diagnostics=\"duplicateActorRole\">\r\n" + " Duplicate role!\r\n" + " </report>\r\n" + " </rule>\r\n" + " </pattern>\r\n" + " <diagnostics>\r\n" + " <diagnostic id=\"duplicateActorRole\">\r\n" + " More than one actor plays the role<value-of select=\"@role\"/>.\r\n" + " A duplicate is named<value-of select=\"@name\"/>.\r\n" + " </diagnostic>\r\n" + " </diagnostics>\r\n" + "</schema>";
final ISchematronResource isr = new SchematronResourceSCH(new ReadableResourceString(sSCH, StandardCharsets.UTF_8));
assertTrue(isr.isValidSchematron());
}
use of com.helger.schematron.ISchematronResource in project ph-schematron by phax.
the class SchematronValidationMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
StaticLoggerBinder.getSingleton().setMavenLog(getLog());
if (m_aSchematronFile == null)
throw new MojoExecutionException("No Schematron file specified!");
if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
throw new MojoExecutionException("The specified Schematron file " + m_aSchematronFile + " is not a file!");
if (m_sSchematronProcessingEngine == null)
throw new MojoExecutionException("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " + StringHelper.getImplodedMapped(", ", ESchematronMode.values(), x -> "'" + x.getID() + "'"));
if (m_aXmlDirectory == null && m_aXmlErrorDirectory == null)
throw new MojoExecutionException("No XML directory specified - positive or negative directory must be present!");
if (m_aXmlDirectory != null) {
if (m_aXmlDirectory.exists() && !m_aXmlDirectory.isDirectory())
throw new MojoExecutionException("The specified XML directory " + m_aXmlDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sXmlIncludes))
throw new MojoExecutionException("No XML include pattern specified!");
if (m_aSvrlDirectory != null) {
if (!m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
throw new MojoExecutionException("Failed to create the SVRL directory " + m_aSvrlDirectory);
}
}
if (m_aXmlErrorDirectory != null) {
if (m_aXmlErrorDirectory.exists() && !m_aXmlErrorDirectory.isDirectory())
throw new MojoExecutionException("The specified erroneous XML directory " + m_aXmlErrorDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sXmlErrorIncludes))
throw new MojoExecutionException("No erroneous XML include pattern specified!");
if (m_aSvrlErrorDirectory != null) {
if (!m_aSvrlErrorDirectory.exists() && !m_aSvrlErrorDirectory.mkdirs())
throw new MojoExecutionException("Failed to create the erroneous SVRL directory " + m_aSvrlErrorDirectory);
}
}
// 1. Parse Schematron file
final Locale aDisplayLocale = Locale.US;
ISchematronResource aSch;
IErrorList aSCHErrors;
switch(ESchematronMode.getFromIDOrNull(m_sSchematronProcessingEngine)) {
case PURE:
{
// pure
final CollectingPSErrorHandler aErrorHdl = new CollectingPSErrorHandler();
final SchematronResourcePure aRealSCH = new SchematronResourcePure(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setErrorHandler(aErrorHdl);
aRealSCH.validateCompletely();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getAllErrors();
break;
}
case SCHEMATRON:
{
// SCH
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setLanguageCode(languageCode);
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
case XSLT:
{
// SCH
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceXSLT aRealSCH = new SchematronResourceXSLT(new FileSystemResource(m_aSchematronFile));
// phase is ignored
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
default:
throw new MojoExecutionException("No handler for processing engine '" + m_sSchematronProcessingEngine + "'");
}
if (aSCHErrors != null) {
// Error validating the Schematrons!!
boolean bAnyError = false;
for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
getLog().error("Error in Schematron: " + aError.getAsString(aDisplayLocale));
bAnyError = true;
} else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
getLog().warn("Warning in Schematron: " + aError.getAsString(aDisplayLocale));
if (bAnyError)
throw new MojoExecutionException("The provided Schematron file contains errors. See log for details.");
}
getLog().info("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'");
// 2. for all XML files that match the pattern
if (m_aXmlDirectory != null)
_performValidation(aSch, m_aXmlDirectory, m_sXmlIncludes, m_sXmlExcludes, m_aSvrlDirectory, true);
if (m_aXmlErrorDirectory != null)
_performValidation(aSch, m_aXmlErrorDirectory, m_sXmlErrorIncludes, m_sXmlErrorExcludes, m_aSvrlErrorDirectory, false);
}
Aggregations