Search in sources :

Example 6 with XPathConfigBuilder

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

the class SchematronResourcePureTest method testResolveVariables.

@Test
public void testResolveVariables() 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-element\">Chapter should have a title</iso:assert>\n" + "      <iso:report test=\"java:my-count(para) = 2\">\n" + "      <iso:value-of select=\"java:my-count(para)\"/> paragraphs found</iso:report>\n" + "    </iso:rule>\n" + "  </iso:pattern>\n" + "\n" + "</iso:schema>";
    // Test without variable and function resolver
    // -> an error is expected, but we don't need to log it
    assertFalse(SchematronResourcePure.fromString(sTest, StandardCharsets.UTF_8).setErrorHandler(new DoNothingPSErrorHandler()).isValidSchematron());
    // Test with variable and function resolver
    final MapBasedXPathVariableResolver aVarResolver = new MapBasedXPathVariableResolver();
    aVarResolver.addUniqueVariable("title-element", "title");
    final MapBasedXPathFunctionResolver aFunctionResolver = new MapBasedXPathFunctionResolver();
    aFunctionResolver.addUniqueFunction("http://helger.com/schematron/test", "my-count", 1, args -> {
        final List<?> aArg = (List<?>) args.get(0);
        return Integer.valueOf(aArg.size());
    });
    final IXPathConfig aXPathConfig = new XPathConfigBuilder().setXPathVariableResolver(aVarResolver).setXPathFunctionResolver(aFunctionResolver).build();
    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).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      2 paragraphs found".trim(), SVRLHelper.getAllSuccessfulReports(aOT).get(0).getText());
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) MapBasedXPathVariableResolver(com.helger.xml.xpath.MapBasedXPathVariableResolver) 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 7 with XPathConfigBuilder

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

the class Issue047Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
    IXPathConfig aXPathConfig = new XPathConfigBuilder().setXPathFunctionResolver((aFunctionName, aArity) -> {
        System.out.println(aFunctionName + " - " + aArity);
        return null;
    }).build();
    final SchematronResourcePure aSCH = new SchematronResourcePure(new FileSystemResource(aSchematron)).setXPathConfig(aXPathConfig);
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) Assert.assertNotNull(org.junit.Assert.assertNotNull) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) Test(org.junit.Test) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Nonnull(javax.annotation.Nonnull) File(java.io.File) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) XPathConfigBuilder(com.helger.schematron.pure.xpath.XPathConfigBuilder) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) IXPathConfig(com.helger.schematron.pure.xpath.IXPathConfig) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure)

Aggregations

IXPathConfig (com.helger.schematron.pure.xpath.IXPathConfig)7 XPathConfigBuilder (com.helger.schematron.pure.xpath.XPathConfigBuilder)7 Test (org.junit.Test)7 SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)6 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)6 Document (org.w3c.dom.Document)5 XQueryAsXPathFunctionConverter (com.helger.schematron.pure.xpath.XQueryAsXPathFunctionConverter)4 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)2 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)2 List (java.util.List)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)1 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)1 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)1 DOMReaderSettings (com.helger.xml.serialize.read.DOMReaderSettings)1 MapBasedXPathVariableResolver (com.helger.xml.xpath.MapBasedXPathVariableResolver)1 File (java.io.File)1 Nonnull (javax.annotation.Nonnull)1 Schema (javax.xml.validation.Schema)1