Search in sources :

Example 16 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType 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 IXPathConfig aXPathConfig = new XPathConfigBuilder().setXPathFunctionResolver(aFunctionResolver).build();
    final SchematronOutputType aOT = SchematronResourcePure.fromByteArray(sTest.getBytes(StandardCharsets.UTF_8)).setXPathConfig(aXPathConfig).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(com.helger.schematron.svrl.jaxb.SchematronOutputType) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.pure.xpath.XQueryAsXPathFunctionConverter) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) Document(org.w3c.dom.Document) IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) CollectingPSErrorHandler(com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler) Test(org.junit.Test)

Example 17 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType in project ph-schematron by phax.

the class SchematronResourcePureTest method testResolveFunctions.

@Test
public void testResolveFunctions() throws SchematronException, XPathFactoryConfigurationException {
    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=\"java\" uri=\"http://helger.com/schematron/test\" />\n" + "  <iso:pattern >\n" + "    <iso:title>A very simple pattern with a title</iso:title>\n" + "    <iso:rule context=\"chapter\">\n" + "      <iso:assert test=\"title\">Chapter should have a title</iso:assert>\n" + "      <iso:report test=\"count(para) = 2\">\n" + "      Node details: <iso:value-of select=\"java:get-nodelist-details(para)\"/> - end</iso:report>\n" + "    </iso:rule>\n" + "  </iso:pattern>\n" + "\n" + "</iso:schema>";
    // Test with variable and function resolver
    final MapBasedXPathFunctionResolver aFunctionResolver = new MapBasedXPathFunctionResolver();
    aFunctionResolver.addUniqueFunction("http://helger.com/schematron/test", "get-nodelist-details", 1, args -> {
        // We expect exactly one argument
        assertEquals(1, args.size());
        // The type of the first argument
        // itself is also a list
        final List<?> aFirstArg = (List<?>) args.get(0);
        // Ensure that the first argument
        // only contains Nodes
        final StringBuilder ret = new StringBuilder();
        boolean bFirst = true;
        for (final Object aFirstArgItem : aFirstArg) {
            assertTrue(aFirstArgItem instanceof Node);
            final Node aNode = (Node) aFirstArgItem;
            if (bFirst)
                bFirst = false;
            else
                ret.append(", ");
            ret.append(aNode.getNodeName()).append("[").append(aNode.getTextContent()).append("]");
        }
        return ret;
    });
    final Document aTestDoc = DOMReader.readXMLDOM("<?xml version='1.0'?>" + "<chapter>" + "<title />" + "<para>First para</para>" + "<para>Second para</para>" + "</chapter>");
    final IXPathConfig aXPathConfig = new XPathConfigBuilder().setXPathFunctionResolver(aFunctionResolver).build();
    final SchematronOutputType aOT = SchematronResourcePure.fromByteArray(sTest.getBytes(StandardCharsets.UTF_8)).setXPathConfig(aXPathConfig).applySchematronValidationToSVRL(aTestDoc, null);
    assertNotNull(aOT);
    assertEquals(0, SVRLHelper.getAllFailedAssertions(aOT).size());
    assertEquals(1, SVRLHelper.getAllSuccessfulReports(aOT).size());
    // Note: the text contains all whitespaces!
    assertEquals("\n      Node details: para[First para], para[Second para] - end".trim(), SVRLHelper.getAllSuccessfulReports(aOT).get(0).getText());
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) Node(org.w3c.dom.Node) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) List(java.util.List) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) Document(org.w3c.dom.Document) IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) Test(org.junit.Test)

Example 18 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType in project ph-schematron by phax.

the class IssueGC6Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(final File schematron, final File xml) throws Exception {
    final IReadableResource aSchematron = new FileSystemResource(schematron.getAbsoluteFile());
    final IReadableResource anXMLSource = new FileSystemResource(xml.getAbsoluteFile());
    final AbstractSchematronResource pure = new SchematronResourcePure(aSchematron);
    final SchematronOutputType aSVRL = pure.applySchematronValidationToSVRL(anXMLSource);
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) IReadableResource(com.helger.commons.io.resource.IReadableResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) AbstractSchematronResource(com.helger.schematron.AbstractSchematronResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure)

Example 19 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType in project ph-schematron by phax.

the class Issue029Test method validateXmlUsingSchematron.

@Nullable
static SchematronOutputType validateXmlUsingSchematron(@Nonnull final IReadableResource aRes) {
    SchematronOutputType ob = null;
    // Must use the XSLT based version, because of "key" usage
    final ISchematronResource aResSCH = new SchematronResourcePure(new ClassPathResource("issues/github29/pbs.sch"));
    if (!aResSCH.isValidSchematron())
        throw new IllegalArgumentException("Invalid Schematron!");
    try {
        final Document aDoc = aResSCH.applySchematronValidation(aRes);
        if (aDoc != null) {
            final SVRLMarshaller marshaller = new SVRLMarshaller();
            ob = marshaller.read(aDoc);
        }
    } catch (final Exception pE) {
        pE.printStackTrace();
    }
    return ob;
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) Document(org.w3c.dom.Document) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Nullable(javax.annotation.Nullable)

Example 20 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType in project ph-schematron by phax.

the class Issue036Test 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(com.helger.schematron.svrl.jaxb.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)

Aggregations

SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)56 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)31 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)30 Test (org.junit.Test)21 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)18 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)15 IReadableResource (com.helger.commons.io.resource.IReadableResource)13 ISchematronResource (com.helger.schematron.ISchematronResource)10 SchematronResourceSCH (com.helger.schematron.sch.SchematronResourceSCH)10 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)9 Document (org.w3c.dom.Document)9 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)7 IXPathConfig (com.helger.schematron.pure.xpath.IXPathConfig)6 XPathConfigBuilder (com.helger.schematron.pure.xpath.XPathConfigBuilder)6 SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)4 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)4 File (java.io.File)4 Nonnull (javax.annotation.Nonnull)4 LoggingPSValidationHandler (com.helger.schematron.pure.validation.LoggingPSValidationHandler)3 XQueryAsXPathFunctionConverter (com.helger.schematron.pure.xpath.XQueryAsXPathFunctionConverter)3