Search in sources :

Example 1 with IPSBoundSchema

use of com.helger.schematron.pure.bound.IPSBoundSchema 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 IPSBoundSchema

use of com.helger.schematron.pure.bound.IPSBoundSchema in project ph-schematron by phax.

the class PSXPathBoundSchemaTest method testSchematronValidation.

@Test
public void testSchematronValidation() throws SAXException, SchematronException {
    for (int i = 0; i < SCH.length; ++i) {
        final IReadableResource aSchRes = new ClassPathResource("test-sch/" + SCH[i]);
        final IReadableResource aXmlRes = new ClassPathResource("test-xml/" + XML[i]);
        // Resolve all includes
        final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aSchRes);
        assertNotNull(aDoc);
        // Read to domain object
        final PSReader aReader = new PSReader(aSchRes);
        final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
        assertNotNull(aSchema);
        // Create a compiled schema
        final String sPhaseID = null;
        final IPSErrorHandler aErrorHandler = null;
        final IPSBoundSchema aBoundSchema = PSXPathQueryBinding.getInstance().bind(aSchema, sPhaseID, aErrorHandler);
        // Validate completely
        final SchematronOutputType aSVRL = aBoundSchema.validateComplete(DOMReader.readXMLDOM(aXmlRes), aXmlRes.getAsURL().toExternalForm());
        assertNotNull(aSVRL);
        if (false)
            System.out.println(new SVRLMarshaller().getAsString(aSVRL));
    }
}
Also used : IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) IPSBoundSchema(com.helger.schematron.pure.bound.IPSBoundSchema) IReadableResource(com.helger.commons.io.resource.IReadableResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 3 with IPSBoundSchema

use of com.helger.schematron.pure.bound.IPSBoundSchema in project ph-schematron by phax.

the class PSXPathBoundSchemaTest method testBindAllValidSchematrons.

@Test
public void testBindAllValidSchematrons() throws SchematronException {
    for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
        // Parse the schema
        final PSSchema aSchema = new PSReader(aRes).readSchema();
        assertNotNull(aSchema);
        CommonsTestHelper.testToStringImplementation(aSchema);
        final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler();
        assertTrue(aRes.getPath(), aSchema.isValid(aLogger));
        assertTrue(aLogger.isEmpty());
        // Create a compiled schema
        final String sPhaseID = null;
        final IPSErrorHandler aErrorHandler = null;
        final IPSBoundSchema aBoundSchema = PSXPathQueryBinding.getInstance().bind(aSchema, sPhaseID, aErrorHandler);
        assertNotNull(aBoundSchema);
    }
}
Also used : IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) IPSBoundSchema(com.helger.schematron.pure.bound.IPSBoundSchema) IReadableResource(com.helger.commons.io.resource.IReadableResource) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Aggregations

IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)3 PSReader (com.helger.schematron.pure.exchange.PSReader)3 PSSchema (com.helger.schematron.pure.model.PSSchema)3 IReadableResource (com.helger.commons.io.resource.IReadableResource)2 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)2 Test (org.junit.Test)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)1 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)1 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)1 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)1 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)1 Document (org.w3c.dom.Document)1