use of com.helger.schematron.svrl.DefaultSVRLErrorLevelDeterminator in project ph-schematron by phax.
the class Schematron method execute.
@Override
public void execute() throws BuildException {
boolean bCanRun = false;
if (m_aSchematronFile == null)
_errorOrFail("No Schematron file specified!");
else if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
_errorOrFail("The specified Schematron file " + m_aSchematronFile + " is not a file!");
else if (m_eSchematronProcessingEngine == null)
_errorOrFail("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())
_errorOrFail("No XML resources to be validated specified! Add e.g. a <fileset> element.");
else if (m_aSvrlDirectory != null && !m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
_errorOrFail("Failed to create the SVRL directory " + m_aSvrlDirectory);
else
bCanRun = true;
if (bCanRun) {
// Set error level
if (m_aErrorRoles.isNotEmpty()) {
// Set global default error level determinator
SVRLHelper.setErrorLevelDeterminator(new DefaultSVRLErrorLevelDeterminator() {
@Override
@Nonnull
public IErrorLevel getErrorLevelFromString(@Nullable final String sFlag) {
if (sFlag != null) {
// Check custom error roles; #66
for (final Schematron.ErrorRole aCustomRole : m_aErrorRoles) if (aCustomRole.equalsIgnoreCase(sFlag))
return EErrorLevel.ERROR;
}
// Fall back to default
return super.getErrorLevelFromString(sFlag);
}
});
}
// 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 IStringMap aParams = new StringMap();
m_aParameters.forEach(x -> x.addToMap(aParams));
if (aParams.isNotEmpty())
_info("Using the following custom parameters: " + aParams);
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setLanguageCode(m_sLanguageCode);
aRealSCH.setForceCacheResult(m_bForceCacheResult);
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.setURIResolver(getURIResolver());
aRealSCH.setEntityResolver(getEntityResolver());
aRealSCH.parameters().setAll(aParams);
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
case SCHXSLT_XSLT2:
{
// SchXslt
final IStringMap aParams = new StringMap();
m_aParameters.forEach(x -> x.addToMap(aParams));
if (aParams.isNotEmpty())
_info("Using the following custom parameters: " + aParams);
final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
final SchematronResourceSchXslt_XSLT2 aRealSCH = new SchematronResourceSchXslt_XSLT2(new FileSystemResource(m_aSchematronFile));
aRealSCH.setPhase(m_sPhaseName);
aRealSCH.setLanguageCode(m_sLanguageCode);
aRealSCH.setForceCacheResult(m_bForceCacheResult);
aRealSCH.setErrorListener(aErrorHdl);
aRealSCH.setURIResolver(getURIResolver());
aRealSCH.setEntityResolver(getEntityResolver());
aRealSCH.parameters().setAll(aParams);
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
case XSLT:
{
// XSLT
final IStringMap aParams = new StringMap();
m_aParameters.forEach(x -> x.addToMap(aParams));
if (aParams.isNotEmpty())
_info("Using the following custom parameters: " + aParams);
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.parameters().setAll(aParams);
aRealSCH.isValidSchematron();
aSch = aRealSCH;
aSCHErrors = aErrorHdl.getErrorList();
break;
}
default:
_errorOrFail("No handler for processing engine '" + m_eSchematronProcessingEngine + "'");
break;
}
boolean bAnyParsingError = false;
if (aSCHErrors != null)
// Error validating the Schematrons!!
for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
_error("Error in Schematron: " + aError.getAsString(aDisplayLocale));
bAnyParsingError = true;
} else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
_warn("Warning in Schematron: " + aError.getAsString(aDisplayLocale));
else
_info("Information in Schematron: " + aError.getAsString(aDisplayLocale));
if (bAnyParsingError)
_errorOrFail("The provided Schematron file contains errors. See log for details.");
else {
// Schematron is okay
_info("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'");
// Start validation
_performValidation(aSch, m_aResCollections, m_aSvrlDirectory, m_bExpectSuccess);
}
}
}
Aggregations