use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.
the class Schematron2XSLTMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
StaticLoggerBinder.getSingleton().setMavenLog(getLog());
if (m_aSchematronDirectory == null)
throw new MojoExecutionException("No Schematron directory specified!");
if (m_aSchematronDirectory.exists() && !m_aSchematronDirectory.isDirectory())
throw new MojoExecutionException("The specified Schematron directory " + m_aSchematronDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sSchematronPattern))
throw new MojoExecutionException("No Schematron pattern specified!");
if (m_aXsltDirectory == null)
throw new MojoExecutionException("No XSLT directory specified!");
if (m_aXsltDirectory.exists() && !m_aXsltDirectory.isDirectory())
throw new MojoExecutionException("The specified XSLT directory " + m_aXsltDirectory + " is not a directory!");
if (StringHelper.hasNoText(m_sXsltExtension) || !m_sXsltExtension.startsWith("."))
throw new MojoExecutionException("The XSLT extension '" + m_sXsltExtension + "' is invalid!");
if (!m_aXsltDirectory.exists() && !m_aXsltDirectory.mkdirs())
throw new MojoExecutionException("Failed to create the XSLT directory " + m_aXsltDirectory);
// for all Schematron files that match the pattern
final DirectoryScanner aScanner = new DirectoryScanner();
aScanner.setBasedir(m_aSchematronDirectory);
aScanner.setIncludes(new String[] { m_sSchematronPattern });
aScanner.setCaseSensitive(true);
aScanner.scan();
final String[] aFilenames = aScanner.getIncludedFiles();
if (aFilenames != null) {
for (final String sFilename : aFilenames) {
final File aFile = new File(m_aSchematronDirectory, sFilename);
// 1. build XSLT file name (outputdir + localpath with new extension)
final File aXSLTFile = new File(m_aXsltDirectory, FilenameHelper.getWithoutExtension(sFilename) + m_sXsltExtension);
getLog().info("Converting Schematron file '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'");
// 2. The Schematron resource
final IReadableResource aSchematronResource = new FileSystemResource(aFile);
// 3. Check if the XSLT file already exists
if (aXSLTFile.exists() && !m_bOverwriteWithoutNotice) {
// 3.1 Not overwriting the existing file
getLog().debug("Skipping XSLT file '" + aXSLTFile.getPath() + "' because it already exists!");
} else {
// 3.2 Create the directory, if necessary
final File aXsltFileDirectory = aXSLTFile.getParentFile();
if (aXsltFileDirectory != null && !aXsltFileDirectory.exists()) {
getLog().debug("Creating directory '" + aXsltFileDirectory.getPath() + "'");
if (!aXsltFileDirectory.mkdirs()) {
final String message = "Failed to convert '" + aFile.getPath() + "' because directory '" + aXsltFileDirectory.getPath() + "' could not be created";
getLog().error(message);
throw new MojoFailureException(message);
}
}
// 3.3 Okay, write the XSLT file
try {
buildContext.removeMessages(aFile);
// Custom error listener to log to the Mojo logger
final ErrorListener aMojoErrorListener = new PluginErrorListener(aFile);
// Custom error listener
// No custom URI resolver
// Specified phase - default = null
// Specified language code - default = null
final SCHTransformerCustomizer aCustomizer = new SCHTransformerCustomizer().setErrorListener(aMojoErrorListener).setPhase(m_sPhaseName).setLanguageCode(m_sLanguageCode).setParameters(m_aCustomParameters);
final ISchematronXSLTBasedProvider aXsltProvider = SchematronResourceSCHCache.createSchematronXSLTProvider(aSchematronResource, aCustomizer);
if (aXsltProvider != null) {
// Write the resulting XSLT file to disk
final MapBasedNamespaceContext aNSContext = new MapBasedNamespaceContext().addMapping("svrl", CSVRL.SVRL_NAMESPACE_URI);
// Add all namespaces from XSLT document root
final String sNSPrefix = XMLConstants.XMLNS_ATTRIBUTE + ":";
XMLHelper.forAllAttributes(aXsltProvider.getXSLTDocument().getDocumentElement(), (sAttrName, sAttrValue) -> {
if (sAttrName.startsWith(sNSPrefix))
aNSContext.addMapping(sAttrName.substring(sNSPrefix.length()), sAttrValue);
});
final XMLWriterSettings aXWS = new XMLWriterSettings();
aXWS.setNamespaceContext(aNSContext).setPutNamespaceContextPrefixesInRoot(true);
final OutputStream aOS = FileHelper.getOutputStream(aXSLTFile);
if (aOS == null)
throw new IllegalStateException("Failed to open output stream for file " + aXSLTFile.getAbsolutePath());
XMLWriter.writeToStream(aXsltProvider.getXSLTDocument(), aOS, aXWS);
getLog().debug("Finished creating XSLT file '" + aXSLTFile.getPath() + "'");
buildContext.refresh(aXsltFileDirectory);
} else {
final String message = "Failed to convert '" + aFile.getPath() + "': the Schematron resource is invalid";
getLog().error(message);
throw new MojoFailureException(message);
}
} catch (final MojoFailureException up) {
throw up;
} catch (final Exception ex) {
final String sMessage = "Failed to convert '" + aFile.getPath() + "' to XSLT file '" + aXSLTFile.getPath() + "'";
getLog().error(sMessage, ex);
throw new MojoExecutionException(sMessage, ex);
}
}
}
}
}
use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.
the class SchematronResourcePureTest method testBasic.
@Test
public void testBasic() throws Exception {
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
// The validity is tested in another test case!
// Parse them
final SchematronResourcePure aResPure = new SchematronResourcePure(aRes);
assertTrue(aRes.getPath(), aResPure.isValidSchematron());
}
}
use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.
the class MainBenchmarkIsValidSchematron method main.
public static void main(final String[] args) {
logSystemInfo();
final ICommonsList<IReadableResource> aValidSchematrons = new CommonsArrayList<>();
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) if (!aRes.getPath().endsWith("/BIICORE-UBL-T01.sch") && !aRes.getPath().endsWith("/BIICORE-UBL-T10.sch") && !aRes.getPath().endsWith("/BIICORE-UBL-T14.sch") && !aRes.getPath().endsWith("/BIICORE-UBL-T15.sch") && !aRes.getPath().endsWith("/CellarBook.sch") && !aRes.getPath().endsWith("/pattern-example-with-includes.sch") && !aRes.getPath().endsWith("/pattern-example.sch") && !aRes.getPath().endsWith("/schematron-svrl.sch"))
aValidSchematrons.add(aRes);
s_aLogger.info("Starting");
final double dTime1 = benchmarkTask(new ValidSchematronPure(aValidSchematrons));
s_aLogger.info("Time pure: " + BigDecimal.valueOf(dTime1).toString() + " us");
final double dTime2 = benchmarkTask(new ValidSchematronSCH(aValidSchematrons));
s_aLogger.info("Time XSLT: " + BigDecimal.valueOf(dTime2).toString() + " us");
s_aLogger.info("Time1 is " + BigDecimal.valueOf(dTime1 / dTime2 * 100).toString() + "% of time2");
}
use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.
the class PSXPathBoundSchemaTest method testBindAllInvalidSchematrons.
@Test
public void testBindAllInvalidSchematrons() {
for (final IReadableResource aRes : SchematronTestHelper.getAllInvalidSchematronFiles()) {
System.out.println(aRes);
try {
// Parse the schema
final PSSchema aSchema = new PSReader(aRes).readSchema();
final CollectingPSErrorHandler aCEH = new CollectingPSErrorHandler();
PSXPathQueryBinding.getInstance().bind(aSchema, null, aCEH);
// Either an ERROR was collected or an exception was thrown
assertTrue(aCEH.getErrorList().getMostSevereErrorLevel().isGE(EErrorLevel.ERROR));
} catch (final SchematronException ex) {
System.out.println(" " + ex.getMessage());
}
}
}
use of com.helger.commons.io.resource.IReadableResource in project ph-schematron by phax.
the class PSXPathBoundSchemaTest method testSchematronValidation.
@Test
public void testSchematronValidation() throws SAXException, SchematronException {
for (int i = 0; i < SCH.length; ++i) {
final IReadableResource aSchRes = new ClassPathResource("test-sch/" + SCH[i]);
final IReadableResource aXmlRes = new ClassPathResource("test-xml/" + XML[i]);
// Resolve all includes
final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aSchRes);
assertNotNull(aDoc);
// Read to domain object
final PSReader aReader = new PSReader(aSchRes);
final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
assertNotNull(aSchema);
// Create a compiled schema
final String sPhaseID = null;
final IPSErrorHandler aErrorHandler = null;
final IPSBoundSchema aBoundSchema = PSXPathQueryBinding.getInstance().bind(aSchema, sPhaseID, aErrorHandler);
// Validate completely
final SchematronOutputType aSVRL = aBoundSchema.validateComplete(DOMReader.readXMLDOM(aXmlRes), aXmlRes.getAsURL().toExternalForm());
assertNotNull(aSVRL);
if (false)
System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
}
Aggregations