Search in sources :

Example 1 with IPSElement

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

the class Issue30Test method testOfSchematronPH.

@Test
public void testOfSchematronPH() throws Exception {
    final SchematronResourcePure aResPure = SchematronResourcePure.fromFile("src/test/resources/issues/github30/ph-test.sch");
    aResPure.setEntityResolver(DefaultEntityResolver.createOnDemand(aResPure.getResource()));
    final IPSErrorHandler aErrorHandler = new IPSErrorHandler() {

        @Override
        public void warn(final IReadableResource aRes, final IPSElement aSourceElement, final String sMessage) {
            s_aLogger.info(sMessage);
        }

        @Override
        public void error(final IReadableResource aRes, final IPSElement aSourceElement, final String sMessage, final Throwable t) {
            s_aLogger.info(sMessage);
        }

        @Override
        public void error(final IPSElement aSourceElement, final String sMessage) {
            s_aLogger.info(sMessage);
        }
    };
    aResPure.setErrorHandler(aErrorHandler);
    aResPure.validateCompletely();
    assertFalse(aResPure.isValidSchematron());
}
Also used : IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) IPSElement(com.helger.schematron.pure.model.IPSElement) IReadableResource(com.helger.commons.io.resource.IReadableResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Test(org.junit.Test)

Example 2 with IPSElement

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

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

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

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

the class PSXPathBoundSchema method _createBoundElements.

@Nullable
private ICommonsList<PSXPathBoundElement> _createBoundElements(@Nonnull final IPSHasMixedContent aMixedContent, @Nonnull final XPath aXPathContext, @Nonnull final IPSXPathVariables aVariables) {
    final ICommonsList<PSXPathBoundElement> ret = new CommonsArrayList<>();
    boolean bHasAnyError = false;
    for (final Object aContentElement : aMixedContent.getAllContentElements()) {
        if (aContentElement instanceof PSName) {
            final PSName aName = (PSName) aContentElement;
            if (aName.hasPath()) {
                // Replace all variables
                final String sPath = aVariables.getAppliedReplacement(aName.getPath());
                try {
                    final XPathExpression aXpathExpression = _compileXPath(aXPathContext, sPath);
                    ret.add(new PSXPathBoundElement(aName, sPath, aXpathExpression));
                } catch (final XPathExpressionException ex) {
                    error(aName, "Failed to compile XPath expression in <name>: '" + sPath + "'", ex.getCause() != null ? ex.getCause() : ex);
                    bHasAnyError = true;
                }
            } else {
                // No XPath required
                ret.add(new PSXPathBoundElement(aName));
            }
        } else if (aContentElement instanceof PSValueOf) {
            final PSValueOf aValueOf = (PSValueOf) aContentElement;
            // Replace variables
            final String sSelect = aVariables.getAppliedReplacement(aValueOf.getSelect());
            try {
                final XPathExpression aXPathExpression = _compileXPath(aXPathContext, sSelect);
                ret.add(new PSXPathBoundElement(aValueOf, sSelect, aXPathExpression));
            } catch (final XPathExpressionException ex) {
                error(aValueOf, "Failed to compile XPath expression in <value-of>: '" + sSelect + "'", ex);
                bHasAnyError = true;
            }
        } else {
            // No XPath compilation necessary
            if (aContentElement instanceof String)
                ret.add(new PSXPathBoundElement((String) aContentElement));
            else
                ret.add(new PSXPathBoundElement((IPSElement) aContentElement));
        }
    }
    if (bHasAnyError)
        return null;
    return ret;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) IPSElement(com.helger.schematron.pure.model.IPSElement) PSName(com.helger.schematron.pure.model.PSName) XPathExpressionException(javax.xml.xpath.XPathExpressionException) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) PSValueOf(com.helger.schematron.pure.model.PSValueOf) Nullable(javax.annotation.Nullable)

Aggregations

IPSElement (com.helger.schematron.pure.model.IPSElement)5 PSLet (com.helger.schematron.pure.model.PSLet)3 PSRule (com.helger.schematron.pure.model.PSRule)2 Nullable (javax.annotation.Nullable)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)1 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)1 PSActive (com.helger.schematron.pure.model.PSActive)1 PSAssertReport (com.helger.schematron.pure.model.PSAssertReport)1 PSExtends (com.helger.schematron.pure.model.PSExtends)1 PSName (com.helger.schematron.pure.model.PSName)1 PSPattern (com.helger.schematron.pure.model.PSPattern)1 PSPhase (com.helger.schematron.pure.model.PSPhase)1 PSValueOf (com.helger.schematron.pure.model.PSValueOf)1 Nonnull (javax.annotation.Nonnull)1 XPathExpression (javax.xml.xpath.XPathExpression)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 Test (org.junit.Test)1