Search in sources :

Example 1 with PSPreprocessor

use of com.helger.schematron.pure.preprocess.PSPreprocessor 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 PSPreprocessor

use of com.helger.schematron.pure.preprocess.PSPreprocessor in project ph-schematron by phax.

the class Issue48Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
    final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematron)).readSchema();
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    final String sSCH = new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).getXMLString(aPreprocessedSchema);
    if (false)
        System.out.println(sSCH);
    final SchematronResourceSCH aSCH = new SchematronResourceSCH(new ReadableResourceString(sSCH, StandardCharsets.UTF_8));
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) PSWriter(com.helger.schematron.pure.exchange.PSWriter) PSWriterSettings(com.helger.schematron.pure.exchange.PSWriterSettings) ReadableResourceString(com.helger.commons.io.resource.inmemory.ReadableResourceString) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) ReadableResourceString(com.helger.commons.io.resource.inmemory.ReadableResourceString) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 3 with PSPreprocessor

use of com.helger.schematron.pure.preprocess.PSPreprocessor in project ph-schematron by phax.

the class PSBoundSchemaCacheKey method createPreprocessedSchema.

/**
 * Pre-process the read schema, using the determined query binding.
 *
 * @param aSchema
 *        The read schema. Never <code>null</code>.
 * @param aQueryBinding
 *        The determined query binding. Never <code>null</code>.
 * @return The pre-processed schema and never <code>null</code>.
 * @throws SchematronException
 *         In case pre-processing fails
 */
@Nonnull
@OverrideOnDemand
public PSSchema createPreprocessedSchema(@Nonnull final PSSchema aSchema, @Nonnull final IPSQueryBinding aQueryBinding) throws SchematronException {
    final PSPreprocessor aPreprocessor = createPreprocessor(aQueryBinding);
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    if (aPreprocessedSchema == null)
        throw new SchematronPreprocessException("Failed to preprocess schema " + aSchema + " with query binding " + aQueryBinding);
    if (SchematronDebug.isShowPreprocessedSchematron())
        s_aLogger.info("Preprocessed Schematron:\n" + MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement()));
    return aPreprocessedSchema;
}
Also used : SchematronPreprocessException(com.helger.schematron.pure.preprocess.SchematronPreprocessException) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema) Nonnull(javax.annotation.Nonnull) OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand)

Example 4 with PSPreprocessor

use of com.helger.schematron.pure.preprocess.PSPreprocessor in project ph-schematron by phax.

the class SchematronPreprocess method execute.

@Override
public void execute() throws BuildException {
    boolean bCanRun = false;
    if (m_aSrcFile == null)
        _error("No source Schematron file specified!");
    else if (m_aSrcFile.exists() && !m_aSrcFile.isFile())
        _error("The specified source Schematron file " + m_aSrcFile + " is not a file!");
    else if (m_aDstFile == null)
        _error("No destination Schematron file specified!");
    else if (m_aDstFile.exists() && !m_aDstFile.isFile())
        _error("The specified destination Schematron file " + m_aDstFile + " is not a file!");
    else
        bCanRun = true;
    if (bCanRun)
        try {
            // Read source
            final PSSchema aSchema = new PSReader(new FileSystemResource(m_aSrcFile)).readSchema();
            // Setup preprocessor
            final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
            aPreprocessor.setKeepTitles(m_bKeepTitles);
            aPreprocessor.setKeepDiagnostics(m_bKeepDiagnostics);
            aPreprocessor.setKeepReports(m_bKeepReports);
            aPreprocessor.setKeepEmptyPatterns(m_bKeepEmptyPatterns);
            aPreprocessor.setKeepEmptySchema(true);
            // Main pre-processing
            final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
            // Write the result file
            new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).writeToFile(aPreprocessedSchema, m_aDstFile);
            log("Successfully pre-processed Schematron " + m_aSrcFile + " to " + m_aDstFile);
        } catch (final SchematronReadException | SchematronPreprocessException ex) {
            _error("Error processing Schemtron " + m_aSrcFile.getAbsolutePath(), ex);
        }
}
Also used : XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) PSWriter(com.helger.schematron.pure.exchange.PSWriter) PSWriterSettings(com.helger.schematron.pure.exchange.PSWriterSettings) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 5 with PSPreprocessor

use of com.helger.schematron.pure.preprocess.PSPreprocessor 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)

Aggregations

PSSchema (com.helger.schematron.pure.model.PSSchema)5 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)5 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)3 PSReader (com.helger.schematron.pure.exchange.PSReader)3 PSWriter (com.helger.schematron.pure.exchange.PSWriter)2 PSWriterSettings (com.helger.schematron.pure.exchange.PSWriterSettings)2 XMLWriterSettings (com.helger.xml.serialize.write.XMLWriterSettings)2 Nonnull (javax.annotation.Nonnull)2 OverrideOnDemand (com.helger.commons.annotation.OverrideOnDemand)1 ReadableResourceString (com.helger.commons.io.resource.inmemory.ReadableResourceString)1 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)1 SchematronBindException (com.helger.schematron.pure.binding.SchematronBindException)1 IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)1 PSXPathBoundSchema (com.helger.schematron.pure.bound.xpath.PSXPathBoundSchema)1 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)1 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)1 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)1 SchematronPreprocessException (com.helger.schematron.pure.preprocess.SchematronPreprocessException)1 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 SchematronResourceSCH (com.helger.schematron.xslt.SchematronResourceSCH)1