Search in sources :

Example 1 with IPSQueryBinding

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

the class DocumentationExamples method validateXMLViaPureSchematron2.

public static boolean validateXMLViaPureSchematron2(@Nonnull final File aSchematronFile, @Nonnull final File aXMLFile) throws Exception {
    // Read the schematron from file
    final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematronFile)).readSchema();
    if (!aSchema.isValid(new DoNothingPSErrorHandler()))
        throw new IllegalArgumentException("Invalid Schematron!");
    // Resolve the query binding to use
    final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow(aSchema.getQueryBinding());
    // Pre-process schema
    final PSPreprocessor aPreprocessor = new PSPreprocessor(aQueryBinding);
    aPreprocessor.setKeepTitles(true);
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    // Bind the pre-processed schema
    final IPSBoundSchema aBoundSchema = aQueryBinding.bind(aPreprocessedSchema, null, null, null, null);
    // Read the XML file
    final Document aXMLNode = DOMReader.readXMLDOM(aXMLFile);
    if (aXMLNode == null)
        return false;
    // Perform the validation
    return aBoundSchema.validatePartially(aXMLNode, FileHelper.getAsURLString(aXMLFile)).isValid();
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) IPSBoundSchema(com.helger.schematron.pure.bound.IPSBoundSchema) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) Document(org.w3c.dom.Document) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 2 with IPSQueryBinding

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

the class PSBoundSchemaCacheKey method createBoundSchema.

/**
 * The main routine to create a bound schema from the passed resource and
 * phase. The usual routine is to
 * <ol>
 * <li>read the schema from the resource - see
 * {@link #readSchema(IReadableResource, IPSErrorHandler, EntityResolver)}</li>
 * <li>resolve the query binding - see {@link #getQueryBinding(PSSchema)}</li>
 * <li>pre-process the schema -
 * {@link #createPreprocessedSchema(PSSchema, IPSQueryBinding)}</li>
 * <li>and finally bind it -
 * {@link IPSQueryBinding#bind(PSSchema, String, IPSErrorHandler, javax.xml.xpath.XPathVariableResolver, javax.xml.xpath.XPathFunctionResolver)}
 * </li>
 * </ol>
 *
 * @return The bound schema. Never <code>null</code>.
 * @throws SchematronException
 *         In case reading or binding fails.
 */
@Nonnull
public IPSBoundSchema createBoundSchema() throws SchematronException {
    // Read schema from resource
    final PSSchema aSchema = readSchema(getResource(), getErrorHandler(), getEntityResolver());
    // Resolve the query binding to be used
    final IPSQueryBinding aQueryBinding = getQueryBinding(aSchema);
    // Pre-process schema
    final PSSchema aPreprocessedSchema = createPreprocessedSchema(aSchema, aQueryBinding);
    // And finally bind the pre-processed schema
    return aQueryBinding.bind(aPreprocessedSchema, getPhase(), getErrorHandler(), getVariableResolver(), getFunctionResolver());
}
Also used : IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) PSSchema(com.helger.schematron.pure.model.PSSchema) Nonnull(javax.annotation.Nonnull)

Aggregations

IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)2 PSSchema (com.helger.schematron.pure.model.PSSchema)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)1 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)1 PSReader (com.helger.schematron.pure.exchange.PSReader)1 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)1 Nonnull (javax.annotation.Nonnull)1 Document (org.w3c.dom.Document)1