use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class Issue20150128Test method testEq.
@Test
public void testEq() throws Exception {
final String sTest2 = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<schema xmlns='http://purl.oclc.org/dsdl/schematron'>\n" + " <ns prefix='functx' uri='http://www.functx.com' />\n" + " <pattern >\n" + " <title>A very simple pattern with a title</title>\n" + " <rule context='chapter'>\n" + " <assert test='aaa eq 1'>aaa equals 1></assert>\n" + " </rule>\n" + " </pattern>\n" + "\n" + "</schema>";
final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler();
final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
final SchematronResourcePure resource = SchematronResourcePure.fromString(sTest2, StandardCharsets.ISO_8859_1);
resource.setErrorHandler(aErrorHandler);
resource.setFunctionResolver(aFunctionResolver);
assertTrue(resource.isValidSchematron());
assertEquals(1, aErrorHandler.getErrorList().size());
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler 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);
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class PSReaderTest method testReadAll.
@Test
public void testReadAll() throws Exception {
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
final PSReader aReader = new PSReader(aRes);
// Parse the schema
final PSSchema aSchema1 = aReader.readSchema();
assertNotNull(aSchema1);
final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler();
assertTrue(aRes.getPath(), aSchema1.isValid(aLogger));
assertTrue(aLogger.isEmpty());
// Convert back to XML
final IMicroElement e1 = aSchema1.getAsMicroElement();
final String sXML1 = MicroWriter.getNodeAsString(e1);
// Re-read the created XML and re-create it
final PSSchema aSchema2 = aReader.readSchemaFromXML(e1);
final IMicroElement e2 = aSchema2.getAsMicroElement();
final String sXML2 = MicroWriter.getNodeAsString(e2);
// Originally created XML and re-created-written XML must match
assertEquals(sXML1, sXML2);
}
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class PSPreprocessorTest method testBasic.
@Test
public void testBasic() throws Exception {
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
// Resolve all includes
final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
assertNotNull(aDoc);
// Read to domain object
final PSReader aReader = new PSReader(aRes);
final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
assertNotNull(aSchema);
// Ensure the schema is valid
final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler();
assertTrue(aRes.getPath(), aSchema.isValid(aErrHdl));
assertTrue(aErrHdl.isEmpty());
// Convert to minified schema if not-yet minimal
final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema(aSchema);
assertNotNull(aPreprocessedSchema);
if (false) {
final String sXML = MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement());
SimpleFileIO.writeFile(new File("test-minified", FilenameHelper.getWithoutPath(aRes.getPath()) + ".min-pure.sch"), sXML, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
}
// Ensure it is still valid and minimal
assertTrue(aRes.getPath(), aPreprocessedSchema.isValid(aErrHdl));
assertTrue(aRes.getPath(), aPreprocessedSchema.isMinimal());
}
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class PSXPathBoundSchemaTest method testBindAllValidSchematrons.
@Test
public void testBindAllValidSchematrons() throws SchematronException {
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
// Parse the schema
final PSSchema aSchema = new PSReader(aRes).readSchema();
assertNotNull(aSchema);
CommonsTestHelper.testToStringImplementation(aSchema);
final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler();
assertTrue(aRes.getPath(), aSchema.isValid(aLogger));
assertTrue(aLogger.isEmpty());
// Create a compiled schema
final String sPhaseID = null;
final IPSErrorHandler aErrorHandler = null;
final IPSBoundSchema aBoundSchema = PSXPathQueryBinding.getInstance().bind(aSchema, sPhaseID, aErrorHandler);
assertNotNull(aBoundSchema);
}
}
Aggregations