Search in sources :

Example 1 with SchematronResourcePure

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

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

the class Issue11Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
    final SchematronResourcePure aSCH = SchematronResourcePure.fromFile(aSchematron);
    aSCH.setErrorHandler(new LoggingPSErrorHandler());
    assertTrue(aSCH.isValidSchematron());
    // 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) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure)

Example 3 with SchematronResourcePure

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

the class Issue36Test method testNestedExtends.

@Test
public void testNestedExtends() throws Exception {
    final SchematronResourcePure aResPure = SchematronResourcePure.fromFile("src/test/resources/issues/github36/test2.sch");
    aResPure.setErrorHandler(new LoggingPSErrorHandler());
    final SchematronOutputType aSOT = aResPure.applySchematronValidationToSVRL(new FileSystemResource("src/test/resources/issues/github36/test.xml"));
    assertNotNull(aSOT);
    assertFalse(SVRLHelper.getAllFailedAssertions(aSOT).isEmpty());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Test(org.junit.Test)

Example 4 with SchematronResourcePure

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

the class Issue36Test method test.

@Test
public void test() throws Exception {
    final SchematronResourcePure aResPure = SchematronResourcePure.fromFile("src/test/resources/issues/github36/test.sch");
    aResPure.setErrorHandler(new LoggingPSErrorHandler());
    final SchematronOutputType aSOT = aResPure.applySchematronValidationToSVRL(new FileSystemResource("src/test/resources/issues/github36/test.xml"));
    assertNotNull(aSOT);
    assertFalse(SVRLHelper.getAllFailedAssertions(aSOT).isEmpty());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Test(org.junit.Test)

Example 5 with SchematronResourcePure

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

the class Issue20150128Test method testEq.

@Test
public void testEq() throws Exception {
    final String sTest2 = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<schema xmlns='http://purl.oclc.org/dsdl/schematron'>\n" + "  <ns prefix='functx' uri='http://www.functx.com' />\n" + "  <pattern >\n" + "    <title>A very simple pattern with a title</title>\n" + "    <rule context='chapter'>\n" + "      <assert test='aaa eq 1'>aaa equals 1></assert>\n" + "      </rule>\n" + "  </pattern>\n" + "\n" + "</schema>";
    final CollectingPSErrorHandler aErrorHandler = new CollectingPSErrorHandler();
    final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
    final SchematronResourcePure resource = SchematronResourcePure.fromString(sTest2, StandardCharsets.ISO_8859_1);
    resource.setErrorHandler(aErrorHandler);
    resource.setFunctionResolver(aFunctionResolver);
    assertTrue(resource.isValidSchematron());
    assertEquals(1, aErrorHandler.getErrorList().size());
}
Also used : MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.xpath.XQueryAsXPathFunctionConverter) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Aggregations

SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)19 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)16 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)13 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)10 IReadableResource (com.helger.commons.io.resource.IReadableResource)9 Test (org.junit.Test)7 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)6 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)5 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)3 ISchematronResource (com.helger.schematron.ISchematronResource)3 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)3 SchematronResourceSCH (com.helger.schematron.xslt.SchematronResourceSCH)3 File (java.io.File)3 Nullable (javax.annotation.Nullable)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 StringHelper (com.helger.commons.string.StringHelper)2 ESchematronMode (com.helger.schematron.ESchematronMode)2