use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler 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.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class SchematronResourcePureTest method testParseWithXPathError.
@Test
public void testParseWithXPathError() {
final String sTest = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" + "<iso:schema xmlns=\"http://purl.oclc.org/dsdl/schematron\" \n" + " xmlns:iso=\"http://purl.oclc.org/dsdl/schematron\" \n" + " xmlns:sch=\"http://www.ascc.net/xml/schematron\"\n" + " queryBinding='xslt2'\n" + " schemaVersion=\"ISO19757-3\">\n" + " <iso:title>Test ISO schematron file. Introduction mode</iso:title>\n" + " <iso:ns prefix=\"dp\" uri=\"http://www.dpawson.co.uk/ns#\" />\n" + " <iso:pattern>\n" + " <iso:rule context=\"chapter\">\n" + " <iso:assert test=\"title xor 55\">Chapter should have a title</iso:assert>\n" + " <iso:assert test=\"title xor 33\">Chapter should have a title</iso:assert>\n" + " </iso:rule>\n" + " </iso:pattern>\n" + "</iso:schema>";
final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler();
final SchematronResourcePure aSch = SchematronResourcePure.fromByteArray(sTest.getBytes(StandardCharsets.UTF_8)).setErrorHandler(aErrorHandler);
// Perform quick validation
assertFalse(aSch.isValidSchematron());
assertEquals("Expected three errors: " + aErrorHandler.getErrorList().toString(), 3, aErrorHandler.getErrorList().size());
if (false)
System.out.println(aErrorHandler.getErrorList().toString());
// Perform complete validation
aErrorHandler.clearResourceErrors();
aSch.validateCompletely();
assertEquals("Expected three errors: " + aErrorHandler.getErrorList().toString(), 3, aErrorHandler.getErrorList().size());
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class SchematronResourcePureTest method testResolveFunctXAreDistinctValuesQueryFunctions.
@Test
public void testResolveFunctXAreDistinctValuesQueryFunctions() throws Exception {
final String sTest = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<iso:schema xmlns='http://purl.oclc.org/dsdl/schematron' \n" + " xmlns:iso='http://purl.oclc.org/dsdl/schematron' \n" + " xmlns:sch='http://www.ascc.net/xml/schematron'\n" + " queryBinding='xslt2'\n" + " schemaVersion='ISO19757-3'>\n" + " <iso:title>Test ISO schematron file. Introduction mode</iso:title>\n" + " <iso:ns prefix='dp' uri='http://www.dpawson.co.uk/ns#' />\n" + " <iso:ns prefix='fn' uri='http://www.w3.org/2005/xpath-functions' />\n" + " <iso:ns prefix='functx' uri='http://www.functx.com' />\n" + " <iso:pattern >\n" + " <iso:title>A very simple pattern with a title</iso:title>\n" + " <iso:rule context='chapter'>\n" + " <iso:assert test='functx:are-distinct-values(para)'>Should have distinct values</iso:assert>\n" + " </iso:rule>\n" + " </iso:pattern>\n" + "</iso:schema>";
final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
// Test with variable and function resolver
final Document aTestDoc = DOMReader.readXMLDOM("<?xml version='1.0'?>" + "<chapter>" + "<title />" + "<para>100</para>" + "<para>200</para>" + "</chapter>");
final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler(new LoggingPSErrorHandler());
final SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).setErrorHandler(aErrorHandler).applySchematronValidationToSVRL(aTestDoc, null);
assertNotNull(aOT);
// XXX fails :(
if (false)
assertTrue(aErrorHandler.getAllErrors().toString(), aErrorHandler.isEmpty());
assertEquals(0, SVRLHelper.getAllFailedAssertions(aOT).size());
}
use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler 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.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.
the class PSWriterTest method testReadAll.
@Test
public void testReadAll() throws Exception {
final PSWriter aWriter = new PSWriter();
for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
// Parse the schema
final PSSchema aSchema1 = new PSReader(aRes).readSchema();
assertNotNull(aSchema1);
final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler();
assertTrue(aRes.getPath(), aSchema1.isValid(aLogger));
assertTrue(aLogger.isEmpty());
// Convert back to XML
final String sXML1 = aWriter.getXMLStringNotNull(aSchema1);
// Re-read the created XML and re-create it
final PSSchema aSchema2 = new PSReader(new ReadableResourceString(sXML1, StandardCharsets.UTF_8)).readSchema();
final String sXML2 = aWriter.getXMLStringNotNull(aSchema2);
// Originally created XML and re-created-written XML must match
assertEquals(sXML1, sXML2);
}
}
Aggregations