Search in sources :

Example 1 with SchematronBindException

use of com.helger.schematron.pure.binding.SchematronBindException in project ph-schematron by phax.

the class PSXPathQueryBinding method bind.

@Nonnull
public IPSBoundSchema bind(@Nonnull final PSSchema aSchema, @Nullable final String sPhase, @Nullable final IPSErrorHandler aCustomErrorListener, @Nullable final XPathVariableResolver aVariableResolver, @Nullable final XPathFunctionResolver aFunctionResolver) throws SchematronException {
    ValueEnforcer.notNull(aSchema, "Schema");
    final IPSErrorHandler aErrorHandler = aCustomErrorListener != null ? aCustomErrorListener : new CollectingPSErrorHandler();
    if (!aSchema.isValid(aErrorHandler))
        throw new SchematronBindException("The passed schema is not valid and can therefore not be bound" + (aErrorHandler == aCustomErrorListener ? ". Errors are in the provided error handler." : ": " + ((CollectingPSErrorHandler) aErrorHandler).getErrorList().toString()));
    PSSchema aSchemaToUse = aSchema;
    if (!aSchemaToUse.isPreprocessed()) {
        // Required for parameter resolution
        final PSPreprocessor aPreprocessor = PSPreprocessor.createPreprocessorWithoutInformationLoss(this);
        // Apply preprocessing
        aSchemaToUse = aPreprocessor.getForcedPreprocessedSchema(aSchema);
    }
    final PSXPathBoundSchema ret = new PSXPathBoundSchema(this, aSchemaToUse, sPhase, aCustomErrorListener, aVariableResolver, aFunctionResolver);
    ret.bind();
    return ret;
}
Also used : SchematronBindException(com.helger.schematron.pure.binding.SchematronBindException) IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) PSXPathBoundSchema(com.helger.schematron.pure.bound.xpath.PSXPathBoundSchema) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Nonnull(javax.annotation.Nonnull)

Example 2 with SchematronBindException

use of com.helger.schematron.pure.binding.SchematronBindException in project ph-schematron by phax.

the class PSXPathBoundSchema method bind.

@Nonnull
public PSXPathBoundSchema bind() throws SchematronBindException {
    if (s_aLogger.isDebugEnabled())
        s_aLogger.debug("Binding pure Schematron");
    if (m_aBoundPatterns != null)
        throw new IllegalStateException("bind must only be called once!");
    final PSSchema aSchema = getOriginalSchema();
    final PSPhase aPhase = getPhase();
    // Get all "global" variables that are defined in the schema
    final PSXPathVariables aGlobalVariables = new PSXPathVariables();
    if (aSchema.hasAnyLet())
        for (final Map.Entry<String, String> aEntry : aSchema.getAllLetsAsMap().entrySet()) if (aGlobalVariables.add(aEntry).isUnchanged())
            error(aSchema, "Duplicate <let> with name '" + aEntry.getKey() + "' in global <schema>");
    if (aPhase != null) {
        // Get all variables that are defined in the specified phase
        for (final Map.Entry<String, String> aEntry : aPhase.getAllLetsAsMap().entrySet()) if (aGlobalVariables.add(aEntry).isUnchanged())
            error(aSchema, "Duplicate <let> with name '" + aEntry.getKey() + "' in <phase> with name '" + getPhaseID() + "'");
    }
    final XPath aXPathContext = _createXPathContext();
    // Pre-compile all diagnostics first
    final ICommonsMap<String, PSXPathBoundDiagnostic> aBoundDiagnostics = _createBoundDiagnostics(aXPathContext, aGlobalVariables);
    if (aBoundDiagnostics == null)
        throw new SchematronBindException("Failed to precompile the diagnostics of the supplied schema. Check the " + (isDefaultErrorHandler() ? "log output" : "error listener") + " for XPath errors!");
    // Perform the pre-compilation of all XPath expressions in the patterns,
    // rules, asserts/reports and the content elements
    m_aBoundPatterns = _createBoundPatterns(aXPathContext, aBoundDiagnostics, aGlobalVariables);
    if (m_aBoundPatterns == null)
        throw new SchematronBindException("Failed to precompile the supplied schema.");
    return this;
}
Also used : XPath(javax.xml.xpath.XPath) SchematronBindException(com.helger.schematron.pure.binding.SchematronBindException) PSPhase(com.helger.schematron.pure.model.PSPhase) PSXPathVariables(com.helger.schematron.pure.binding.xpath.PSXPathVariables) IPSXPathVariables(com.helger.schematron.pure.binding.xpath.IPSXPathVariables) PSSchema(com.helger.schematron.pure.model.PSSchema) Map(java.util.Map) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) Nonnull(javax.annotation.Nonnull)

Aggregations

SchematronBindException (com.helger.schematron.pure.binding.SchematronBindException)2 PSSchema (com.helger.schematron.pure.model.PSSchema)2 Nonnull (javax.annotation.Nonnull)2 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 PSXPathBoundSchema (com.helger.schematron.pure.bound.xpath.PSXPathBoundSchema)1 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)1 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)1 PSPhase (com.helger.schematron.pure.model.PSPhase)1 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)1 Map (java.util.Map)1 XPath (javax.xml.xpath.XPath)1