Search in sources :

Example 6 with PSSchema

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;
}
Also used : PSPhase(com.helger.schematron.pure.model.PSPhase) PSLet(com.helger.schematron.pure.model.PSLet) PSPattern(com.helger.schematron.pure.model.PSPattern) PSSchema(com.helger.schematron.pure.model.PSSchema) PSNS(com.helger.schematron.pure.model.PSNS) Nullable(javax.annotation.Nullable)

Example 7 with PSSchema

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;
}
Also used : PSDiagnostic(com.helger.schematron.pure.model.PSDiagnostic) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) PSSchema(com.helger.schematron.pure.model.PSSchema) Nullable(javax.annotation.Nullable)

Example 8 with PSSchema

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());
}
Also used : IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) PSSchema(com.helger.schematron.pure.model.PSSchema) Nonnull(javax.annotation.Nonnull)

Example 9 with PSSchema

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;
}
Also used : SchematronPreprocessException(com.helger.schematron.pure.preprocess.SchematronPreprocessException) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema) Nonnull(javax.annotation.Nonnull) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 10 with PSSchema

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);
        }
}
Also used : XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) PSWriter(com.helger.schematron.pure.exchange.PSWriter) PSWriterSettings(com.helger.schematron.pure.exchange.PSWriterSettings) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema)

Aggregations

PSSchema (com.helger.schematron.pure.model.PSSchema)18 PSReader (com.helger.schematron.pure.exchange.PSReader)9 IReadableResource (com.helger.commons.io.resource.IReadableResource)7 Test (org.junit.Test)7 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)6 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)5 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)4 Nonnull (javax.annotation.Nonnull)4 IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)3 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)3 PSPhase (com.helger.schematron.pure.model.PSPhase)3 IMicroDocument (com.helger.xml.microdom.IMicroDocument)3 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 ReadableResourceString (com.helger.commons.io.resource.inmemory.ReadableResourceString)2 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)2 SchematronBindException (com.helger.schematron.pure.binding.SchematronBindException)2 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)2 PSWriter (com.helger.schematron.pure.exchange.PSWriter)2 PSWriterSettings (com.helger.schematron.pure.exchange.PSWriterSettings)2