Search in sources :

Example 11 with PSSchema

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

the class PSReaderTest method testReadAll.

@Test
public void testReadAll() throws Exception {
    for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
        final PSReader aReader = new PSReader(aRes);
        // Parse the schema
        final PSSchema aSchema1 = aReader.readSchema();
        assertNotNull(aSchema1);
        final CollectingPSErrorHandler aLogger = new CollectingPSErrorHandler();
        assertTrue(aRes.getPath(), aSchema1.isValid(aLogger));
        assertTrue(aLogger.isEmpty());
        // Convert back to XML
        final IMicroElement e1 = aSchema1.getAsMicroElement();
        final String sXML1 = MicroWriter.getNodeAsString(e1);
        // Re-read the created XML and re-create it
        final PSSchema aSchema2 = aReader.readSchemaFromXML(e1);
        final IMicroElement e2 = aSchema2.getAsMicroElement();
        final String sXML2 = MicroWriter.getNodeAsString(e2);
        // Originally created XML and re-created-written XML must match
        assertEquals(sXML1, sXML2);
    }
}
Also used : IMicroElement(com.helger.xml.microdom.IMicroElement) IReadableResource(com.helger.commons.io.resource.IReadableResource) PSSchema(com.helger.schematron.pure.model.PSSchema) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 12 with PSSchema

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

the class PSPreprocessorTest method testWithTitle.

@Test
public void testWithTitle() throws SchematronException {
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance()).setKeepTitles(true).setKeepDiagnostics(true);
    final IReadableResource aRes = new ClassPathResource("test-sch/example-3-5.sch");
    final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
    final PSReader aReader = new PSReader(aRes);
    final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    assertNotNull(aPreprocessedSchema);
    assertTrue(aPreprocessedSchema.isValid(new DoNothingPSErrorHandler()));
    // Because titles are not in minimal mode
    assertFalse(aPreprocessedSchema.isMinimal());
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) IReadableResource(com.helger.commons.io.resource.IReadableResource) 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 13 with PSSchema

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

the class PSPreprocessorTest method testBasic.

@Test
public void testBasic() throws Exception {
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
    for (final IReadableResource aRes : SchematronTestHelper.getAllValidSchematronFiles()) {
        // Resolve all includes
        final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
        assertNotNull(aDoc);
        // Read to domain object
        final PSReader aReader = new PSReader(aRes);
        final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
        assertNotNull(aSchema);
        // Ensure the schema is valid
        final CollectingPSErrorHandler aErrHdl = new CollectingPSErrorHandler();
        assertTrue(aRes.getPath(), aSchema.isValid(aErrHdl));
        assertTrue(aErrHdl.isEmpty());
        // Convert to minified schema if not-yet minimal
        final PSSchema aPreprocessedSchema = aPreprocessor.getAsMinimalSchema(aSchema);
        assertNotNull(aPreprocessedSchema);
        if (false) {
            final String sXML = MicroWriter.getNodeAsString(aPreprocessedSchema.getAsMicroElement());
            SimpleFileIO.writeFile(new File("test-minified", FilenameHelper.getWithoutPath(aRes.getPath()) + ".min-pure.sch"), sXML, XMLWriterSettings.DEFAULT_XML_CHARSET_OBJ);
        }
        // Ensure it is still valid and minimal
        assertTrue(aRes.getPath(), aPreprocessedSchema.isValid(aErrHdl));
        assertTrue(aRes.getPath(), aPreprocessedSchema.isMinimal());
    }
}
Also used : IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) File(java.io.File) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 14 with PSSchema

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

the class DocumentationExamples method readModifyAndWrite.

public static boolean readModifyAndWrite(@Nonnull final File aSchematronFile) throws Exception {
    final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematronFile)).readSchema();
    final PSTitle aTitle = new PSTitle();
    aTitle.addText("Created by ph-schematron");
    aSchema.setTitle(aTitle);
    return MicroWriter.writeToFile(aSchema.getAsMicroElement(), aSchematronFile).isSuccess();
}
Also used : PSTitle(com.helger.schematron.pure.model.PSTitle) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 15 with PSSchema

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

PSSchema (com.helger.schematron.pure.model.PSSchema)18 PSReader (com.helger.schematron.pure.exchange.PSReader)9 IReadableResource (com.helger.commons.io.resource.IReadableResource)7 Test (org.junit.Test)7 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)6 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)5 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)4 Nonnull (javax.annotation.Nonnull)4 IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)3 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)3 PSPhase (com.helger.schematron.pure.model.PSPhase)3 IMicroDocument (com.helger.xml.microdom.IMicroDocument)3 CommonsHashMap (com.helger.commons.collection.impl.CommonsHashMap)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)2 ReadableResourceString (com.helger.commons.io.resource.inmemory.ReadableResourceString)2 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)2 SchematronBindException (com.helger.schematron.pure.binding.SchematronBindException)2 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)2 PSWriter (com.helger.schematron.pure.exchange.PSWriter)2 PSWriterSettings (com.helger.schematron.pure.exchange.PSWriterSettings)2