Search in sources :

Example 1 with CollectingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.

the class Schematron method execute.

@Override
public void execute() throws BuildException {
    boolean bCanRun = false;
    if (m_aSchematronFile == null)
        _error("No Schematron file specified!");
    else if (m_aSchematronFile.exists() && !m_aSchematronFile.isFile())
        _error("The specified Schematron file " + m_aSchematronFile + " is not a file!");
    else if (m_eSchematronProcessingEngine == null)
        _error("An invalid Schematron processing instance is specified! Only one of the following values is allowed: " + StringHelper.getImplodedMapped(", ", ESchematronMode.values(), x -> "'" + x.getID() + "'"));
    else if (m_aResCollections.isEmpty())
        _error("No XML resources to be validated specified! Add e.g. a <fileset> element.");
    else if (m_aSvrlDirectory != null && !m_aSvrlDirectory.exists() && !m_aSvrlDirectory.mkdirs())
        _error("Failed to create the SVRL directory " + m_aSvrlDirectory);
    else
        bCanRun = true;
    if (bCanRun) {
        // 1. Parse Schematron file
        final Locale aDisplayLocale = Locale.US;
        ISchematronResource aSch = null;
        IErrorList aSCHErrors = null;
        switch(m_eSchematronProcessingEngine) {
            case PURE:
                {
                    // pure
                    final CollectingPSErrorHandler aErrorHdl = new CollectingPSErrorHandler();
                    final SchematronResourcePure aRealSCH = new SchematronResourcePure(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setErrorHandler(aErrorHdl);
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.validateCompletely();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getAllErrors();
                    break;
                }
            case SCHEMATRON:
                {
                    // SCH
                    final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceSCH aRealSCH = new SchematronResourceSCH(new FileSystemResource(m_aSchematronFile));
                    aRealSCH.setPhase(m_sPhaseName);
                    aRealSCH.setLanguageCode(m_sLanguageCode);
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            case XSLT:
                {
                    // SCH
                    final CollectingTransformErrorListener aErrorHdl = new CollectingTransformErrorListener();
                    final SchematronResourceXSLT aRealSCH = new SchematronResourceXSLT(new FileSystemResource(m_aSchematronFile));
                    // phase and language are ignored because this was decided when the
                    // XSLT
                    // was created
                    aRealSCH.setErrorListener(aErrorHdl);
                    aRealSCH.setURIResolver(getURIResolver());
                    aRealSCH.setEntityResolver(getEntityResolver());
                    aRealSCH.isValidSchematron();
                    aSch = aRealSCH;
                    aSCHErrors = aErrorHdl.getErrorList();
                    break;
                }
            default:
                _error("No handler for processing engine '" + m_eSchematronProcessingEngine + "'");
                break;
        }
        if (aSCHErrors != null) {
            // Error validating the Schematrons!!
            boolean bAnyParsingError = false;
            for (final IError aError : aSCHErrors) if (aError.getErrorLevel().isGE(EErrorLevel.ERROR)) {
                log("Error in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_ERR);
                bAnyParsingError = true;
            } else if (aError.getErrorLevel().isGE(EErrorLevel.WARN))
                log("Warning in Schematron: " + aError.getAsString(aDisplayLocale), Project.MSG_WARN);
            if (bAnyParsingError)
                _error("The provided Schematron file contains errors. See log for details.");
            else {
                log("Successfully parsed Schematron file '" + m_aSchematronFile.getPath() + "'", Project.MSG_INFO);
                // 2. for all XML files that match the pattern
                _performValidation(aSch, m_aResCollections, m_aSvrlDirectory, m_bExpectSuccess);
            }
        }
    }
}
Also used : OverrideOnDemand(com.helger.commons.annotation.OverrideOnDemand) Task(org.apache.tools.ant.Task) IError(com.helger.commons.error.IError) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) URIResolver(javax.xml.transform.URIResolver) ResourceCollection(org.apache.tools.ant.types.ResourceCollection) FileResource(org.apache.tools.ant.types.resources.FileResource) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) AbstractSVRLMessage(com.helger.schematron.svrl.AbstractSVRLMessage) SVRLHelper(com.helger.schematron.svrl.SVRLHelper) ESchematronMode(com.helger.schematron.ESchematronMode) Locale(java.util.Locale) Project(org.apache.tools.ant.Project) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) Nonnull(javax.annotation.Nonnull) TransformSourceFactory(com.helger.xml.transform.TransformSourceFactory) Nullable(javax.annotation.Nullable) EntityResolver(org.xml.sax.EntityResolver) ErrorTextProvider(com.helger.commons.error.ErrorTextProvider) Resource(org.apache.tools.ant.types.Resource) EErrorLevel(com.helger.commons.error.level.EErrorLevel) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) StringHelper(com.helger.commons.string.StringHelper) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) File(java.io.File) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) XMLCatalog(org.apache.tools.ant.types.XMLCatalog) ISchematronResource(com.helger.schematron.ISchematronResource) CommonsHashMap(com.helger.commons.collection.impl.CommonsHashMap) ICommonsList(com.helger.commons.collection.impl.ICommonsList) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ICommonsMap(com.helger.commons.collection.impl.ICommonsMap) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) IErrorList(com.helger.commons.error.list.IErrorList) ResourceUtils(org.apache.tools.ant.util.ResourceUtils) Locale(java.util.Locale) SchematronResourceXSLT(com.helger.schematron.xslt.SchematronResourceXSLT) ISchematronResource(com.helger.schematron.ISchematronResource) SchematronResourceSCH(com.helger.schematron.xslt.SchematronResourceSCH) IErrorList(com.helger.commons.error.list.IErrorList) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) CollectingTransformErrorListener(com.helger.xml.transform.CollectingTransformErrorListener) IError(com.helger.commons.error.IError) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)

Example 2 with CollectingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.

the class SchematronResourcePureTest method testParseWithXPathError.

@Test
public void testParseWithXPathError() {
    final String sTest = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n" + "<iso:schema xmlns=\"http://purl.oclc.org/dsdl/schematron\" \n" + "         xmlns:iso=\"http://purl.oclc.org/dsdl/schematron\" \n" + "         xmlns:sch=\"http://www.ascc.net/xml/schematron\"\n" + "         queryBinding='xslt2'\n" + "         schemaVersion=\"ISO19757-3\">\n" + "  <iso:title>Test ISO schematron file. Introduction mode</iso:title>\n" + "  <iso:ns prefix=\"dp\" uri=\"http://www.dpawson.co.uk/ns#\" />\n" + "  <iso:pattern>\n" + "    <iso:rule context=\"chapter\">\n" + "      <iso:assert test=\"title xor 55\">Chapter should have a title</iso:assert>\n" + "      <iso:assert test=\"title xor 33\">Chapter should have a title</iso:assert>\n" + "    </iso:rule>\n" + "  </iso:pattern>\n" + "</iso:schema>";
    final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler();
    final SchematronResourcePure aSch = SchematronResourcePure.fromByteArray(sTest.getBytes(StandardCharsets.UTF_8)).setErrorHandler(aErrorHandler);
    // Perform quick validation
    assertFalse(aSch.isValidSchematron());
    assertEquals("Expected three errors: " + aErrorHandler.getErrorList().toString(), 3, aErrorHandler.getErrorList().size());
    if (false)
        System.out.println(aErrorHandler.getErrorList().toString());
    // Perform complete validation
    aErrorHandler.clearResourceErrors();
    aSch.validateCompletely();
    assertEquals("Expected three errors: " + aErrorHandler.getErrorList().toString(), 3, aErrorHandler.getErrorList().size());
}
Also used : CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 3 with CollectingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.

the class SchematronResourcePureTest method testResolveFunctXAreDistinctValuesQueryFunctions.

@Test
public void testResolveFunctXAreDistinctValuesQueryFunctions() throws Exception {
    final String sTest = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<iso:schema xmlns='http://purl.oclc.org/dsdl/schematron' \n" + "         xmlns:iso='http://purl.oclc.org/dsdl/schematron' \n" + "         xmlns:sch='http://www.ascc.net/xml/schematron'\n" + "         queryBinding='xslt2'\n" + "         schemaVersion='ISO19757-3'>\n" + "  <iso:title>Test ISO schematron file. Introduction mode</iso:title>\n" + "  <iso:ns prefix='dp' uri='http://www.dpawson.co.uk/ns#' />\n" + "  <iso:ns prefix='fn' uri='http://www.w3.org/2005/xpath-functions' />\n" + "  <iso:ns prefix='functx' uri='http://www.functx.com' />\n" + "    <iso:pattern >\n" + "    <iso:title>A very simple pattern with a title</iso:title>\n" + "    <iso:rule context='chapter'>\n" + "      <iso:assert test='functx:are-distinct-values(para)'>Should have distinct values</iso:assert>\n" + "      </iso:rule>\n" + "  </iso:pattern>\n" + "</iso:schema>";
    final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
    // Test with variable and function resolver
    final Document aTestDoc = DOMReader.readXMLDOM("<?xml version='1.0'?>" + "<chapter>" + "<title />" + "<para>100</para>" + "<para>200</para>" + "</chapter>");
    final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler(new LoggingPSErrorHandler());
    final SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).setErrorHandler(aErrorHandler).applySchematronValidationToSVRL(aTestDoc, null);
    assertNotNull(aOT);
    // XXX fails :(
    if (false)
        assertTrue(aErrorHandler.getAllErrors().toString(), aErrorHandler.isEmpty());
    assertEquals(0, SVRLHelper.getAllFailedAssertions(aOT).size());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.xpath.XQueryAsXPathFunctionConverter) Document(org.w3c.dom.Document) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 4 with CollectingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.

the class PSXPathBoundSchemaTest method testBindAllInvalidSchematrons.

@Test
public void testBindAllInvalidSchematrons() {
    for (final IReadableResource aRes : SchematronTestHelper.getAllInvalidSchematronFiles()) {
        System.out.println(aRes);
        try {
            // Parse the schema
            final PSSchema aSchema = new PSReader(aRes).readSchema();
            final CollectingPSErrorHandler aCEH = new CollectingPSErrorHandler();
            PSXPathQueryBinding.getInstance().bind(aSchema, null, aCEH);
            // Either an ERROR was collected or an exception was thrown
            assertTrue(aCEH.getErrorList().getMostSevereErrorLevel().isGE(EErrorLevel.ERROR));
        } catch (final SchematronException ex) {
            System.out.println("  " + ex.getMessage());
        }
    }
}
Also used : SchematronException(com.helger.schematron.SchematronException) 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)

Example 5 with CollectingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler in project ph-schematron by phax.

the class PSWriterTest method testReadAll.

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

Aggregations

CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)11 Test (org.junit.Test)8 PSSchema (com.helger.schematron.pure.model.PSSchema)6 IReadableResource (com.helger.commons.io.resource.IReadableResource)5 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)3 PSReader (com.helger.schematron.pure.exchange.PSReader)3 File (java.io.File)3 Nonnull (javax.annotation.Nonnull)3 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)3 ICommonsList (com.helger.commons.collection.impl.ICommonsList)2 IError (com.helger.commons.error.IError)2 EErrorLevel (com.helger.commons.error.level.EErrorLevel)2 IErrorList (com.helger.commons.error.list.IErrorList)2 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)2 StringHelper (com.helger.commons.string.StringHelper)2 ESchematronMode (com.helger.schematron.ESchematronMode)2 ISchematronResource (com.helger.schematron.ISchematronResource)2 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)2 SVRLHelper (com.helger.schematron.svrl.SVRLHelper)2 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)2