Search in sources :

Example 1 with MapBasedXPathFunctionResolver

use of com.helger.xml.xpath.MapBasedXPathFunctionResolver in project ph-schematron by phax.

the class SchematronResourcePureTest method testResolveFunctions.

@Test
public void testResolveFunctions() throws SchematronException, SAXException {
    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 SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).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(org.oclc.purl.dsdl.svrl.SchematronOutputType) Node(org.w3c.dom.Node) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) List(java.util.List) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 2 with MapBasedXPathFunctionResolver

use of com.helger.xml.xpath.MapBasedXPathFunctionResolver in project ph-schematron by phax.

the class SchematronResourcePureTest method testFunctXAreDistinctValuesWithXSD.

@Test
public void testFunctXAreDistinctValuesWithXSD() throws Exception {
    final String sTest = "<?xml version='1.0' encoding='iso-8859-1'?>\n" + "<schema xmlns='http://purl.oclc.org/dsdl/schematron'>\n" + "  <ns prefix=\"xs\" uri=\"http://www.w3.org/2001/XMLSchema\"/>\n" + "  <ns prefix='fn' uri='http://www.w3.org/2005/xpath-functions' />\n" + "  <ns prefix='functx' uri='http://www.functx.com' />\n" + "  <pattern name='toto'>\n" + "    <title>A very simple pattern with a title</title>\n" + "    <rule context='chapter'>\n" + "      <assert test='fn:count(fn:distinct-values(para)) = fn:count(para)'>Should have distinct values</assert>\n" + "      </rule>\n" + "  </pattern>\n" + "</schema>";
    final MapBasedXPathFunctionResolver aFunctionResolver = new XQueryAsXPathFunctionConverter().loadXQuery(ClassPathResource.getInputStream("xquery/functx-1.0-nodoc-2007-01.xq"));
    final Schema aSchema = XMLSchemaCache.getInstance().getSchema(new ClassPathResource("issues/20141124/chapter.xsd"));
    final Document aTestDoc = DOMReader.readXMLDOM("<?xml version='1.0'?>" + "<chapter>" + " <title />" + " <para>09</para>" + " <para>9</para>" + "</chapter>", new DOMReaderSettings().setSchema(aSchema));
    final SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).applySchematronValidationToSVRL(aTestDoc, null);
    assertNotNull(aOT);
    if (SVRLHelper.getAllFailedAssertions(aOT).size() != 0) {
        System.out.println(SVRLHelper.getAllFailedAssertions(aOT).get(0).getText());
    }
    assertTrue(SVRLHelper.getAllFailedAssertions(aOT).isEmpty());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) Schema(javax.xml.validation.Schema) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.xpath.XQueryAsXPathFunctionConverter) DOMReaderSettings(com.helger.xml.serialize.read.DOMReaderSettings) Document(org.w3c.dom.Document) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Example 3 with MapBasedXPathFunctionResolver

use of com.helger.xml.xpath.MapBasedXPathFunctionResolver in project ph-schematron by phax.

the class SchematronResourcePureTest method testResolveXQueryFunctions.

@Test
public void testResolveXQueryFunctions() 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='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='title'>Chapter should have a title</iso:assert>\n" + "      <iso:report test='count(para) = 2'>\n" + "      Node kind: <iso:value-of select='functx:node-kind(para)'/> - end</iso:report>\n" + "    </iso:rule>\n" + "  </iso:pattern>\n" + "\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>First para</para>" + "<para>Second para</para>" + "</chapter>");
    final SchematronOutputType aOT = SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setFunctionResolver(aFunctionResolver).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 kind: element - end".trim(), SVRLHelper.getAllSuccessfulReports(aOT).get(0).getText());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) XQueryAsXPathFunctionConverter(com.helger.schematron.xpath.XQueryAsXPathFunctionConverter) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 4 with MapBasedXPathFunctionResolver

use of com.helger.xml.xpath.MapBasedXPathFunctionResolver 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 5 with MapBasedXPathFunctionResolver

use of com.helger.xml.xpath.MapBasedXPathFunctionResolver 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

MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)7 Test (org.junit.Test)6 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)5 Document (org.w3c.dom.Document)5 XQueryAsXPathFunctionConverter (com.helger.schematron.xpath.XQueryAsXPathFunctionConverter)4 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)2 List (java.util.List)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)1 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)1 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)1 DOMReaderSettings (com.helger.xml.serialize.read.DOMReaderSettings)1 MapBasedXPathVariableResolver (com.helger.xml.xpath.MapBasedXPathVariableResolver)1 Nonnull (javax.annotation.Nonnull)1 Schema (javax.xml.validation.Schema)1 Configuration (net.sf.saxon.Configuration)1 Controller (net.sf.saxon.Controller)1 UserFunction (net.sf.saxon.expr.instruct.UserFunction)1 ExecutableFunctionLibrary (net.sf.saxon.functions.ExecutableFunctionLibrary)1 FunctionLibrary (net.sf.saxon.functions.FunctionLibrary)1