Search in sources :

Example 1 with PSPattern

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

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

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

the class PSXPathBoundSchema method _createBoundPatterns.

/**
 * Pre-compile all patterns incl. their content
 *
 * @param aXPathContext
 * @param aXPathContext
 *        Global XPath object to use. May not be <code>null</code>.
 * @param aBoundDiagnostics
 *        A map from DiagnosticID to its mapped counterpart. May not be
 *        <code>null</code>.
 * @param aGlobalVariables
 *        The global Schematron-let variables. May not be <code>null</code>.
 * @return <code>null</code> if an XPath error is contained
 */
@Nullable
private ICommonsList<PSXPathBoundPattern> _createBoundPatterns(@Nonnull final XPath aXPathContext, @Nonnull final ICommonsMap<String, PSXPathBoundDiagnostic> aBoundDiagnostics, @Nonnull final IPSXPathVariables aGlobalVariables) {
    final ICommonsList<PSXPathBoundPattern> ret = new CommonsArrayList<>();
    boolean bHasAnyError = false;
    // For all relevant patterns
    for (final PSPattern aPattern : getAllRelevantPatterns()) {
        // Handle pattern specific variables
        final PSXPathVariables aPatternVariables = aGlobalVariables.getClone();
        if (aPattern.hasAnyLet()) {
            // map
            for (final Map.Entry<String, String> aEntry : aPattern.getAllLetsAsMap().entrySet()) if (aPatternVariables.add(aEntry).isUnchanged())
                error(aPattern, "Duplicate <let> with name '" + aEntry.getKey() + "' in <pattern>");
        }
        // For all rules of the current pattern
        final ICommonsList<PSXPathBoundRule> aBoundRules = new CommonsArrayList<>();
        for (final PSRule aRule : aPattern.getAllRules()) {
            // Handle rule specific variables
            final PSXPathVariables aRuleVariables = aPatternVariables.getClone();
            if (aRule.hasAnyLet()) {
                // variable map
                for (final Map.Entry<String, String> aEntry : aRule.getAllLetsAsMap().entrySet()) if (aRuleVariables.add(aEntry).isUnchanged())
                    error(aRule, "Duplicate <let> with name '" + aEntry.getKey() + "' in <rule>");
            }
            // For all contained assert and reports within the current rule
            final ICommonsList<PSXPathBoundAssertReport> aBoundAssertReports = new CommonsArrayList<>();
            for (final PSAssertReport aAssertReport : aRule.getAllAssertReports()) {
                final String sTest = aRuleVariables.getAppliedReplacement(aAssertReport.getTest());
                try {
                    final XPathExpression aTestExpr = _compileXPath(aXPathContext, sTest);
                    final ICommonsList<PSXPathBoundElement> aBoundElements = _createBoundElements(aAssertReport, aXPathContext, aRuleVariables);
                    if (aBoundElements == null) {
                        // Error already emitted
                        bHasAnyError = true;
                    } else {
                        final PSXPathBoundAssertReport aBoundAssertReport = new PSXPathBoundAssertReport(aAssertReport, sTest, aTestExpr, aBoundElements, aBoundDiagnostics);
                        aBoundAssertReports.add(aBoundAssertReport);
                    }
                } catch (final Throwable t) {
                    error(aAssertReport, "Failed to compile XPath expression in <" + (aAssertReport.isAssert() ? "assert" : "report") + ">: '" + sTest + "' with the following variables: " + aRuleVariables.getAll(), t);
                    bHasAnyError = true;
                }
            }
            // Evaluate base node set for this rule
            final String sRuleContext = aGlobalVariables.getAppliedReplacement(getValidationContext(aRule.getContext()));
            PSXPathBoundRule aBoundRule = null;
            try {
                final XPathExpression aRuleContext = _compileXPath(aXPathContext, sRuleContext);
                aBoundRule = new PSXPathBoundRule(aRule, sRuleContext, aRuleContext, aBoundAssertReports);
                aBoundRules.add(aBoundRule);
            } catch (final XPathExpressionException ex) {
                error(aRule, "Failed to compile XPath expression in <rule>: '" + sRuleContext + "'", ex.getCause() != null ? ex.getCause() : ex);
                bHasAnyError = true;
            }
        }
        // Create the bound pattern
        final PSXPathBoundPattern aBoundPattern = new PSXPathBoundPattern(aPattern, aBoundRules);
        ret.add(aBoundPattern);
    }
    if (bHasAnyError)
        return null;
    return ret;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) PSRule(com.helger.schematron.pure.model.PSRule) XPathExpressionException(javax.xml.xpath.XPathExpressionException) PSXPathVariables(com.helger.schematron.pure.binding.xpath.PSXPathVariables) IPSXPathVariables(com.helger.schematron.pure.binding.xpath.IPSXPathVariables) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) PSPattern(com.helger.schematron.pure.model.PSPattern) Map(java.util.Map) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nullable(javax.annotation.Nullable)

Example 4 with PSPattern

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

the class PSXPathBoundSchema method validate.

public void validate(@Nonnull final Node aNode, @Nullable final String sBaseURI, @Nonnull final IPSValidationHandler aValidationHandler) throws SchematronValidationException {
    ValueEnforcer.notNull(aNode, "Node");
    ValueEnforcer.notNull(aValidationHandler, "ValidationHandler");
    if (m_aBoundPatterns == null)
        throw new IllegalStateException("bind was never called!");
    final PSSchema aSchema = getOriginalSchema();
    final PSPhase aPhase = getPhase();
    // Call the "start" callback method
    aValidationHandler.onStart(aSchema, aPhase, sBaseURI);
    // For all bound patterns
    for (final PSXPathBoundPattern aBoundPattern : m_aBoundPatterns) {
        final PSPattern aPattern = aBoundPattern.getPattern();
        aValidationHandler.onPattern(aPattern);
        // For all bound rules
        rules: for (final PSXPathBoundRule aBoundRule : aBoundPattern.getAllBoundRules()) {
            final PSRule aRule = aBoundRule.getRule();
            // Find all nodes matching the rules
            NodeList aRuleMatchingNodes = null;
            try {
                aRuleMatchingNodes = XPathEvaluationHelper.evaluate(aBoundRule.getBoundRuleExpression(), aNode, XPathConstants.NODESET, sBaseURI);
            } catch (final XPathExpressionException ex) {
                // Handle the cause, because it is usually a wrapper only
                error(aRule, "Failed to evaluate XPath expression to a nodeset: '" + aBoundRule.getRuleExpression() + "'", ex.getCause() != null ? ex.getCause() : ex);
                continue rules;
            }
            final int nRuleMatchingNodes = aRuleMatchingNodes.getLength();
            if (nRuleMatchingNodes > 0) {
                // For all contained assert and report elements
                for (final PSXPathBoundAssertReport aBoundAssertReport : aBoundRule.getAllBoundAssertReports()) {
                    // XSLT does "fired-rule" for each node
                    aValidationHandler.onRule(aRule, aBoundRule.getRuleExpression());
                    final PSAssertReport aAssertReport = aBoundAssertReport.getAssertReport();
                    final boolean bIsAssert = aAssertReport.isAssert();
                    final XPathExpression aTestExpression = aBoundAssertReport.getBoundTestExpression();
                    // Check each node, if it matches the assert/report
                    for (int i = 0; i < nRuleMatchingNodes; ++i) {
                        final Node aRuleMatchingNode = aRuleMatchingNodes.item(i);
                        try {
                            final boolean bTestResult = ((Boolean) XPathEvaluationHelper.evaluate(aTestExpression, aRuleMatchingNode, XPathConstants.BOOLEAN, sBaseURI)).booleanValue();
                            if (bIsAssert) {
                                // It's an assert
                                if (!bTestResult) {
                                    // Assert failed
                                    if (aValidationHandler.onFailedAssert(aAssertReport, aBoundAssertReport.getTestExpression(), aRuleMatchingNode, i, aBoundAssertReport).isBreak()) {
                                        return;
                                    }
                                }
                            } else {
                                // It's a report
                                if (bTestResult) {
                                    // Successful report
                                    if (aValidationHandler.onSuccessfulReport(aAssertReport, aBoundAssertReport.getTestExpression(), aRuleMatchingNode, i, aBoundAssertReport).isBreak()) {
                                        return;
                                    }
                                }
                            }
                        } catch (final XPathExpressionException ex) {
                            error(aRule, "Failed to evaluate XPath expression to a boolean: '" + aBoundAssertReport.getTestExpression() + "'", ex.getCause() != null ? ex.getCause() : ex);
                        }
                    }
                }
                if (false) {
                    // the next pattern
                    break rules;
                }
            }
        }
    }
    // Call the "end" callback method
    aValidationHandler.onEnd(aSchema, aPhase);
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) PSRule(com.helger.schematron.pure.model.PSRule) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) PSSchema(com.helger.schematron.pure.model.PSSchema) PSPhase(com.helger.schematron.pure.model.PSPhase) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) PSPattern(com.helger.schematron.pure.model.PSPattern)

Aggregations

PSPattern (com.helger.schematron.pure.model.PSPattern)4 PSRule (com.helger.schematron.pure.model.PSRule)3 Nullable (javax.annotation.Nullable)3 PSAssertReport (com.helger.schematron.pure.model.PSAssertReport)2 PSLet (com.helger.schematron.pure.model.PSLet)2 PSPhase (com.helger.schematron.pure.model.PSPhase)2 PSSchema (com.helger.schematron.pure.model.PSSchema)2 XPathExpression (javax.xml.xpath.XPathExpression)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)1 ICommonsMap (com.helger.commons.collection.impl.ICommonsMap)1 IPSXPathVariables (com.helger.schematron.pure.binding.xpath.IPSXPathVariables)1 PSXPathVariables (com.helger.schematron.pure.binding.xpath.PSXPathVariables)1 IPSElement (com.helger.schematron.pure.model.IPSElement)1 PSNS (com.helger.schematron.pure.model.PSNS)1 Map (java.util.Map)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1