Search in sources :

Example 1 with PSLet

use of com.helger.schematron.pure.model.PSLet in project ph-schematron by phax.

the class PSPreprocessor method _resolveRuleContent.

/**
 * Resolve all <extends> elements. This method calls itself recursively
 * until all extends elements are resolved.
 *
 * @param aRuleContent
 *        A list consisting of {@link PSAssertReport} and {@link PSExtends}
 *        objects. Never <code>null</code>.
 * @param aLookup
 *        The rule lookup object
 * @throws SchematronPreprocessException
 *         If the base rule of an extends object could not be resolved.
 */
private void _resolveRuleContent(@Nonnull final ICommonsList<IPSElement> aRuleContent, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool, @Nullable final ICommonsMap<String, String> aParamValueMap, @Nonnull final PSRule aTargetRule) throws SchematronPreprocessException {
    for (final IPSElement aElement : aRuleContent) {
        if (aElement instanceof PSAssertReport) {
            final PSAssertReport aAssertReport = (PSAssertReport) aElement;
            aTargetRule.addAssertReport(_getPreprocessedAssert(aAssertReport, aIDPool, aParamValueMap));
        } else {
            final PSExtends aExtends = (PSExtends) aElement;
            final String sRuleID = aExtends.getRule();
            final PSRule aBaseRule = aLookup.getAbstractRuleOfID(sRuleID);
            if (aBaseRule == null)
                throw new SchematronPreprocessException("Failed to resolve rule ID '" + sRuleID + "' in extends statement. Available rules are: " + aLookup.getAllAbstractRuleIDs());
            // Recursively resolve the extends of the base rule
            _resolveRuleContent(aBaseRule.getAllContentElements(), aLookup, aIDPool, aParamValueMap, aTargetRule);
            // Copy all lets
            for (final PSLet aBaseLet : aBaseRule.getAllLets()) aTargetRule.addLet(aBaseLet.getClone());
        }
    }
}
Also used : IPSElement(com.helger.schematron.pure.model.IPSElement) PSRule(com.helger.schematron.pure.model.PSRule) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) PSLet(com.helger.schematron.pure.model.PSLet) PSExtends(com.helger.schematron.pure.model.PSExtends)

Example 2 with PSLet

use of com.helger.schematron.pure.model.PSLet 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 3 with PSLet

use of com.helger.schematron.pure.model.PSLet in project ph-schematron by phax.

the class PSPreprocessor method _getPreprocessedPhase.

@Nonnull
private static PSPhase _getPreprocessedPhase(@Nonnull final PSPhase aPhase, @Nonnull final PreprocessorIDPool aIDPool) throws SchematronPreprocessException {
    final PSPhase ret = new PSPhase();
    ret.setID(aIDPool.getUniqueID(aPhase.getID()));
    ret.setRich(aPhase.getRichClone());
    if (aPhase.hasAnyInclude())
        throw new SchematronPreprocessException("Cannot preprocess <phase> with an <include>");
    for (final IPSElement aElement : aPhase.getAllContentElements()) {
        if (aElement instanceof PSActive)
            ret.addActive(((PSActive) aElement).getClone());
        else if (aElement instanceof PSLet)
            ret.addLet(((PSLet) aElement).getClone());
    // ps are ignored
    }
    ret.addForeignElements(aPhase.getAllForeignElements());
    ret.addForeignAttributes(aPhase.getAllForeignAttributes());
    return ret;
}
Also used : IPSElement(com.helger.schematron.pure.model.IPSElement) PSPhase(com.helger.schematron.pure.model.PSPhase) PSLet(com.helger.schematron.pure.model.PSLet) PSActive(com.helger.schematron.pure.model.PSActive) Nonnull(javax.annotation.Nonnull)

Example 4 with PSLet

use of com.helger.schematron.pure.model.PSLet in project ph-schematron by phax.

the class PSPreprocessor method _getPreprocessedPattern.

@Nullable
private PSPattern _getPreprocessedPattern(@Nonnull final PSPattern aPattern, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool) throws SchematronPreprocessException {
    if (aPattern.isAbstract()) {
        // Will be inlined
        return null;
    }
    final PSPattern ret = new PSPattern();
    // abstract always false
    // is-a must be resolved
    ret.setID(aIDPool.getUniqueID(aPattern.getID()));
    ret.setRich(aPattern.getRichClone());
    if (aPattern.hasAnyInclude())
        throw new SchematronPreprocessException("Cannot preprocess <pattern> with an <include>");
    if (m_bKeepTitles && aPattern.hasTitle())
        ret.setTitle(aPattern.getTitle().getClone());
    final String sIsA = aPattern.getIsA();
    if (sIsA != null) {
        final PSPattern aBasePattern = aLookup.getAbstractPatternOfID(sIsA);
        if (aBasePattern == null)
            throw new SchematronPreprocessException("Failed to resolve the pattern denoted by is-a='" + sIsA + "'");
        if (!ret.hasID())
            ret.setID(aIDPool.getUniqueID(aBasePattern.getID()));
        if (!ret.hasRich())
            ret.setRich(aBasePattern.getRichClone());
        // get the string replacements
        final ICommonsNavigableMap<String, String> aParamValueMap = m_aQueryBinding.getStringReplacementMap(aPattern.getAllParams());
        for (final IPSElement aElement : aBasePattern.getAllContentElements()) {
            if (aElement instanceof PSLet)
                ret.addLet(((PSLet) aElement).getClone());
            else if (aElement instanceof PSRule) {
                final PSRule aMinifiedRule = _getPreprocessedRule((PSRule) aElement, aLookup, aIDPool, aParamValueMap);
                if (aMinifiedRule != null)
                    ret.addRule(aMinifiedRule);
            }
        // params must have be resolved
        // ps are ignored
        }
    } else {
        for (final IPSElement aElement : aPattern.getAllContentElements()) {
            if (aElement instanceof PSLet)
                ret.addLet(((PSLet) aElement).getClone());
            else if (aElement instanceof PSRule) {
                final PSRule aMinifiedRule = _getPreprocessedRule((PSRule) aElement, aLookup, aIDPool, null);
                if (aMinifiedRule != null)
                    ret.addRule(aMinifiedRule);
            }
        // params must be resolved
        // ps are ignored
        }
    }
    ret.addForeignElements(aPattern.getAllForeignElements());
    ret.addForeignAttributes(aPattern.getAllForeignAttributes());
    return ret;
}
Also used : IPSElement(com.helger.schematron.pure.model.IPSElement) PSRule(com.helger.schematron.pure.model.PSRule) PSLet(com.helger.schematron.pure.model.PSLet) PSPattern(com.helger.schematron.pure.model.PSPattern) Nullable(javax.annotation.Nullable)

Example 5 with PSLet

use of com.helger.schematron.pure.model.PSLet in project ph-schematron by phax.

the class PSPreprocessor method _getPreprocessedRule.

@Nullable
private PSRule _getPreprocessedRule(@Nonnull final PSRule aRule, @Nonnull final PreprocessorLookup aLookup, @Nonnull final PreprocessorIDPool aIDPool, @Nullable final ICommonsMap<String, String> aParamValueMap) throws SchematronPreprocessException {
    if (aRule.isAbstract()) {
        // Will be inlined
        return null;
    }
    final PSRule ret = new PSRule();
    ret.setFlag(aRule.getFlag());
    ret.setRich(aRule.getRichClone());
    ret.setLinkable(aRule.getLinkableClone());
    // abstract is always false
    ret.setContext(m_aQueryBinding.getWithParamTextsReplaced(aRule.getContext(), aParamValueMap));
    ret.setID(aIDPool.getUniqueID(aRule.getID()));
    if (aRule.hasAnyInclude())
        throw new SchematronPreprocessException("Cannot preprocess <rule> with an <include>");
    for (final PSLet aLet : aRule.getAllLets()) ret.addLet(aLet.getClone());
    _resolveRuleContent(aRule.getAllContentElements(), aLookup, aIDPool, aParamValueMap, ret);
    ret.addForeignElements(aRule.getAllForeignElements());
    ret.addForeignAttributes(aRule.getAllForeignAttributes());
    return ret;
}
Also used : PSRule(com.helger.schematron.pure.model.PSRule) PSLet(com.helger.schematron.pure.model.PSLet) Nullable(javax.annotation.Nullable)

Aggregations

PSLet (com.helger.schematron.pure.model.PSLet)5 IPSElement (com.helger.schematron.pure.model.IPSElement)3 PSRule (com.helger.schematron.pure.model.PSRule)3 Nullable (javax.annotation.Nullable)3 PSPattern (com.helger.schematron.pure.model.PSPattern)2 PSPhase (com.helger.schematron.pure.model.PSPhase)2 PSActive (com.helger.schematron.pure.model.PSActive)1 PSAssertReport (com.helger.schematron.pure.model.PSAssertReport)1 PSExtends (com.helger.schematron.pure.model.PSExtends)1 PSNS (com.helger.schematron.pure.model.PSNS)1 PSSchema (com.helger.schematron.pure.model.PSSchema)1 Nonnull (javax.annotation.Nonnull)1