use of com.helger.schematron.pure.model.PSSchema in project ph-schematron by phax.
the class PSPreprocessor method getForcedPreprocessedSchema.
/**
* Convert the passed schema to a pre-processed schema independent if it is
* already minimal or not.
*
* @param aSchema
* The schema to be made minimal. May not be <code>null</code>
* @return A minimal copy of the schema. May be <code>null</code> if the
* original schema is not yet minimal and {@link #isKeepEmptySchema()}
* is set to <code>false</code>.
* @throws SchematronPreprocessException
* In case a preprocessing error occurs
*/
@Nullable
public PSSchema getForcedPreprocessedSchema(@Nonnull final PSSchema aSchema) throws SchematronPreprocessException {
ValueEnforcer.notNull(aSchema, "Schema");
final PreprocessorLookup aLookup = new PreprocessorLookup(aSchema);
final PreprocessorIDPool aIDPool = new PreprocessorIDPool();
final PSSchema ret = new PSSchema(aSchema.getResource());
ret.setID(aIDPool.getUniqueID(aSchema.getID()));
ret.setRich(aSchema.getRichClone());
ret.setSchemaVersion(aSchema.getSchemaVersion());
ret.setDefaultPhase(aSchema.getDefaultPhase());
ret.setQueryBinding(aSchema.getQueryBinding());
if (m_bKeepTitles && aSchema.hasTitle())
ret.setTitle(aSchema.getTitle().getClone());
if (aSchema.hasAnyInclude())
throw new SchematronPreprocessException("Cannot preprocess <schema> with an <include>");
for (final PSNS aNS : aSchema.getAllNSs()) ret.addNS(aNS.getClone());
// start ps are skipped
for (final PSLet aLet : aSchema.getAllLets()) ret.addLet(aLet.getClone());
for (final PSPhase aPhase : aSchema.getAllPhases()) ret.addPhase(_getPreprocessedPhase(aPhase, aIDPool));
for (final PSPattern aPattern : aSchema.getAllPatterns()) {
final PSPattern aMinifiedPattern = _getPreprocessedPattern(aPattern, aLookup, aIDPool);
if (aMinifiedPattern != null) {
// Pattern without rules?
if (aMinifiedPattern.getRuleCount() > 0 || m_bKeepEmptyPatterns)
ret.addPattern(aMinifiedPattern);
}
}
// Schema without patterns?
if (aSchema.getPatternCount() == 0 && !m_bKeepEmptySchema)
return null;
// end ps are skipped
if (m_bKeepDiagnostics && aSchema.hasDiagnostics())
ret.setDiagnostics(_getPreprocessedDiagnostics(aSchema.getDiagnostics()));
ret.addForeignElements(aSchema.getAllForeignElements());
ret.addForeignAttributes(aSchema.getAllForeignAttributes());
return ret;
}
use of com.helger.schematron.pure.model.PSSchema in project ph-schematron by phax.
the class PSXPathBoundSchema method _createBoundDiagnostics.
@Nullable
private ICommonsMap<String, PSXPathBoundDiagnostic> _createBoundDiagnostics(@Nonnull final XPath aXPathContext, @Nonnull final IPSXPathVariables aGlobalVariables) {
final ICommonsMap<String, PSXPathBoundDiagnostic> ret = new CommonsHashMap<>();
boolean bHasAnyError = false;
final PSSchema aSchema = getOriginalSchema();
if (aSchema.hasDiagnostics()) {
// For all contained diagnostic elements
for (final PSDiagnostic aDiagnostic : aSchema.getDiagnostics().getAllDiagnostics()) {
final ICommonsList<PSXPathBoundElement> aBoundElements = _createBoundElements(aDiagnostic, aXPathContext, aGlobalVariables);
if (aBoundElements == null) {
// error already emitted
bHasAnyError = true;
} else {
final PSXPathBoundDiagnostic aBoundDiagnostic = new PSXPathBoundDiagnostic(aDiagnostic, aBoundElements);
if (ret.put(aDiagnostic.getID(), aBoundDiagnostic) != null) {
error(aDiagnostic, "A diagnostic element with ID '" + aDiagnostic.getID() + "' was overwritten!");
bHasAnyError = true;
}
}
}
}
if (bHasAnyError)
return null;
return ret;
}
use of com.helger.schematron.pure.model.PSSchema in project ph-schematron by phax.
the class PSBoundSchemaCacheKey method createBoundSchema.
/**
* The main routine to create a bound schema from the passed resource and
* phase. The usual routine is to
* <ol>
* <li>read the schema from the resource - see
* {@link #readSchema(IReadableResource, IPSErrorHandler, EntityResolver)}</li>
* <li>resolve the query binding - see {@link #getQueryBinding(PSSchema)}</li>
* <li>pre-process the schema -
* {@link #createPreprocessedSchema(PSSchema, IPSQueryBinding)}</li>
* <li>and finally bind it -
* {@link IPSQueryBinding#bind(PSSchema, String, IPSErrorHandler, javax.xml.xpath.XPathVariableResolver, javax.xml.xpath.XPathFunctionResolver)}
* </li>
* </ol>
*
* @return The bound schema. Never <code>null</code>.
* @throws SchematronException
* In case reading or binding fails.
*/
@Nonnull
public IPSBoundSchema createBoundSchema() throws SchematronException {
// Read schema from resource
final PSSchema aSchema = readSchema(getResource(), getErrorHandler(), getEntityResolver());
// Resolve the query binding to be used
final IPSQueryBinding aQueryBinding = getQueryBinding(aSchema);
// Pre-process schema
final PSSchema aPreprocessedSchema = createPreprocessedSchema(aSchema, aQueryBinding);
// And finally bind the pre-processed schema
return aQueryBinding.bind(aPreprocessedSchema, getPhase(), getErrorHandler(), getVariableResolver(), getFunctionResolver());
}
use of com.helger.schematron.pure.model.PSSchema in project ph-schematron by phax.
the class PSBoundSchemaCacheKey method createPreprocessedSchema.
/**
* Pre-process the read schema, using the determined query binding.
*
* @param aSchema
* The read schema. Never <code>null</code>.
* @param aQueryBinding
* The determined query binding. Never <code>null</code>.
* @return The pre-processed schema and never <code>null</code>.
* @throws SchematronException
* In case pre-processing fails
*/
@Nonnull
@OverrideOnDemand
public PSSchema createPreprocessedSchema(@Nonnull final PSSchema aSchema, @Nonnull final IPSQueryBinding aQueryBinding) throws SchematronException {
final PSPreprocessor aPreprocessor = createPreprocessor(aQueryBinding);
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
if (aPreprocessedSchema == null)
throw new SchematronPreprocessException("Failed to preprocess schema " + aSchema + " with query binding " + aQueryBinding);
if (SchematronDebug.isShowPreprocessedSchematron())
s_aLogger.info("Preprocessed Schematron:\n" + MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement()));
return aPreprocessedSchema;
}
use of com.helger.schematron.pure.model.PSSchema in project ph-schematron by phax.
the class SchematronPreprocess method execute.
@Override
public void execute() throws BuildException {
boolean bCanRun = false;
if (m_aSrcFile == null)
_error("No source Schematron file specified!");
else if (m_aSrcFile.exists() && !m_aSrcFile.isFile())
_error("The specified source Schematron file " + m_aSrcFile + " is not a file!");
else if (m_aDstFile == null)
_error("No destination Schematron file specified!");
else if (m_aDstFile.exists() && !m_aDstFile.isFile())
_error("The specified destination Schematron file " + m_aDstFile + " is not a file!");
else
bCanRun = true;
if (bCanRun)
try {
// Read source
final PSSchema aSchema = new PSReader(new FileSystemResource(m_aSrcFile)).readSchema();
// Setup preprocessor
final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
aPreprocessor.setKeepTitles(m_bKeepTitles);
aPreprocessor.setKeepDiagnostics(m_bKeepDiagnostics);
aPreprocessor.setKeepReports(m_bKeepReports);
aPreprocessor.setKeepEmptyPatterns(m_bKeepEmptyPatterns);
aPreprocessor.setKeepEmptySchema(true);
// Main pre-processing
final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
// Write the result file
new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).writeToFile(aPreprocessedSchema, m_aDstFile);
log("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile);
} catch (final SchematronReadException | SchematronPreprocessException ex) {
_error("Error processing Schemtron " + m_aSrcFile.getAbsolutePath(), ex);
}
}
Aggregations