Search in sources :

Example 1 with PSValueOf

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

the class PSXPathValidationHandlerSVRL method _getErrorText.

/**
 * Get the error text from an assert or report element.
 *
 * @param aBoundContentElements
 *        The list of bound elements to be evaluated.
 * @param aSourceNode
 *        The XML node of the document currently validated.
 * @return A non-<code>null</code> String
 * @throws SchematronValidationException
 *         In case evaluating an XPath expression fails.
 */
@Nonnull
private String _getErrorText(@Nonnull final List<PSXPathBoundElement> aBoundContentElements, @Nonnull final Node aSourceNode) throws SchematronValidationException {
    final StringBuilder aSB = new StringBuilder();
    for (final PSXPathBoundElement aBoundElement : aBoundContentElements) {
        final Object aContent = aBoundElement.getElement();
        if (aContent instanceof String)
            aSB.append((String) aContent);
        else if (aContent instanceof PSName) {
            final PSName aName = (PSName) aContent;
            if (aName.hasPath()) {
                // XPath present
                try {
                    aSB.append((String) XPathEvaluationHelper.evaluate(aBoundElement.getBoundExpression(), aSourceNode, XPathConstants.STRING, m_sBaseURI));
                } catch (final XPathExpressionException ex) {
                    _error(aName, "Failed to evaluate XPath expression to a string: '" + aBoundElement.getExpression() + "'", ex.getCause() != null ? ex.getCause() : ex);
                    // Append the path so that something is present in the output
                    aSB.append(aName.getPath());
                }
            } else {
                // No XPath present
                aSB.append(aSourceNode.getNodeName());
            }
        } else if (aContent instanceof PSValueOf) {
            final PSValueOf aValueOf = (PSValueOf) aContent;
            try {
                aSB.append((String) XPathEvaluationHelper.evaluate(aBoundElement.getBoundExpression(), aSourceNode, XPathConstants.STRING, m_sBaseURI));
            } catch (final XPathExpressionException ex) {
                _error(aValueOf, "Failed to evaluate XPath expression to a string: '" + aBoundElement.getExpression() + "'", ex);
                // Append the path so that something is present in the output
                aSB.append(aValueOf.getSelect());
            }
        } else if (aContent instanceof PSEmph)
            aSB.append(((PSEmph) aContent).getAsText());
        else if (aContent instanceof PSDir)
            aSB.append(((PSDir) aContent).getAsText());
        else if (aContent instanceof PSSpan)
            aSB.append(((PSSpan) aContent).getAsText());
        else
            throw new SchematronValidationException("Unsupported assert/report content element: " + aContent);
    }
    return aSB.toString();
}
Also used : PSSpan(com.helger.schematron.pure.model.PSSpan) SchematronValidationException(com.helger.schematron.pure.validation.SchematronValidationException) PSName(com.helger.schematron.pure.model.PSName) PSDir(com.helger.schematron.pure.model.PSDir) PSEmph(com.helger.schematron.pure.model.PSEmph) XPathExpressionException(javax.xml.xpath.XPathExpressionException) PSXPathBoundElement(com.helger.schematron.pure.bound.xpath.PSXPathBoundElement) PSValueOf(com.helger.schematron.pure.model.PSValueOf) Nonnull(javax.annotation.Nonnull)

Example 2 with PSValueOf

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

the class PSPreprocessor method _getPreprocessedAssert.

@Nonnull
private PSAssertReport _getPreprocessedAssert(@Nonnull final PSAssertReport aAssertReport, @Nonnull final PreprocessorIDPool aIDPool, @Nullable final ICommonsMap<String, String> aParamValueMap) {
    String sTest = aAssertReport.getTest();
    if (aAssertReport.isReport() && !m_bKeepReports) {
        // Negate the expression!
        sTest = m_aQueryBinding.getNegatedTestExpression(sTest);
    }
    // Keep report or make it always an assert
    final PSAssertReport ret = new PSAssertReport(m_bKeepReports ? aAssertReport.isAssert() : true);
    ret.setTest(m_aQueryBinding.getWithParamTextsReplaced(sTest, aParamValueMap));
    ret.setFlag(aAssertReport.getFlag());
    ret.setID(aIDPool.getUniqueID(aAssertReport.getID()));
    if (m_bKeepDiagnostics)
        ret.setDiagnostics(aAssertReport.getAllDiagnostics());
    ret.setRich(aAssertReport.getRichClone());
    ret.setLinkable(aAssertReport.getLinkableClone());
    for (final Object aContent : aAssertReport.getAllContentElements()) {
        if (aContent instanceof String)
            ret.addText((String) aContent);
        else if (aContent instanceof PSName)
            ret.addName(((PSName) aContent).getClone());
        else if (aContent instanceof PSValueOf) {
            final PSValueOf aValueOf = ((PSValueOf) aContent).getClone();
            aValueOf.setSelect(m_aQueryBinding.getWithParamTextsReplaced(aValueOf.getSelect(), aParamValueMap));
            ret.addValueOf(aValueOf);
        } else if (aContent instanceof PSEmph)
            ret.addEmph(((PSEmph) aContent).getClone());
        else if (aContent instanceof PSDir)
            ret.addDir(((PSDir) aContent).getClone());
        else if (aContent instanceof PSSpan)
            ret.addSpan(((PSSpan) aContent).getClone());
    }
    ret.addForeignElements(aAssertReport.getAllForeignElements());
    ret.addForeignAttributes(aAssertReport.getAllForeignAttributes());
    return ret;
}
Also used : PSSpan(com.helger.schematron.pure.model.PSSpan) PSAssertReport(com.helger.schematron.pure.model.PSAssertReport) PSName(com.helger.schematron.pure.model.PSName) PSDir(com.helger.schematron.pure.model.PSDir) PSEmph(com.helger.schematron.pure.model.PSEmph) PSValueOf(com.helger.schematron.pure.model.PSValueOf) Nonnull(javax.annotation.Nonnull)

Example 3 with PSValueOf

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

PSName (com.helger.schematron.pure.model.PSName)3 PSValueOf (com.helger.schematron.pure.model.PSValueOf)3 PSDir (com.helger.schematron.pure.model.PSDir)2 PSEmph (com.helger.schematron.pure.model.PSEmph)2 PSSpan (com.helger.schematron.pure.model.PSSpan)2 Nonnull (javax.annotation.Nonnull)2 XPathExpressionException (javax.xml.xpath.XPathExpressionException)2 CommonsArrayList (com.helger.commons.collection.impl.CommonsArrayList)1 PSXPathBoundElement (com.helger.schematron.pure.bound.xpath.PSXPathBoundElement)1 IPSElement (com.helger.schematron.pure.model.IPSElement)1 PSAssertReport (com.helger.schematron.pure.model.PSAssertReport)1 SchematronValidationException (com.helger.schematron.pure.validation.SchematronValidationException)1 Nullable (javax.annotation.Nullable)1 XPathExpression (javax.xml.xpath.XPathExpression)1