Search in sources :

Example 21 with SchematronOutputType

use of org.oclc.purl.dsdl.svrl.SchematronOutputType in project ph-schematron by phax.

the class SchematronResourcePureTest method testResolveVariables.

@Test
public void testResolveVariables() 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-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 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).setVariableResolver(aVarResolver).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      2 paragraphs found".trim(), SVRLHelper.getAllSuccessfulReports(aOT).get(0).getText());
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) MapBasedXPathVariableResolver(com.helger.xml.xpath.MapBasedXPathVariableResolver) MapBasedXPathFunctionResolver(com.helger.xml.xpath.MapBasedXPathFunctionResolver) List(java.util.List) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 22 with SchematronOutputType

use of org.oclc.purl.dsdl.svrl.SchematronOutputType in project ph-schematron by phax.

the class Issue20140523Test 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(org.oclc.purl.dsdl.svrl.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 23 with SchematronOutputType

use of org.oclc.purl.dsdl.svrl.SchematronOutputType in project ph-schematron by phax.

the class Issue29Test method testGood.

@Test
@Ignore("Takes too long - more than 1 min")
public void testGood() throws Exception {
    final SchematronOutputType aSOT = validateXmlUsingSchematron(new GZIPReadableResource(new ClassPathResource("issues/github29/sample.xml.gz")));
    assertNotNull(aSOT);
    final ICommonsList<SVRLFailedAssert> aErrors = SVRLHelper.getAllFailedAssertions(aSOT);
    assertNotNull(aErrors);
    s_aLogger.info("Errors found: " + aErrors);
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) SVRLFailedAssert(com.helger.schematron.svrl.SVRLFailedAssert) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) GZIPReadableResource(com.helger.commons.io.resource.wrapped.GZIPReadableResource) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with SchematronOutputType

use of org.oclc.purl.dsdl.svrl.SchematronOutputType in project ph-schematron by phax.

the class Issue29Test 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(new ResourceStreamSource(aRes));
        if (aDoc != null) {
            final SVRLMarshaller marshaller = new SVRLMarshaller();
            ob = marshaller.read(aDoc);
        }
    } catch (final Exception pE) {
        pE.printStackTrace();
    }
    return ob;
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) ResourceStreamSource(com.helger.xml.transform.ResourceStreamSource) 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 25 with SchematronOutputType

use of org.oclc.purl.dsdl.svrl.SchematronOutputType in project ph-schematron by phax.

the class Issue25Test method testIssue25Invalid.

@Test
public void testIssue25Invalid() throws Exception {
    final IReadableResource aSCH = new ClassPathResource("test-sch/xfront/example05/check-classifications.sch");
    final IReadableResource aXML = new ClassPathResource("test-sch/xfront/example05/invalid-document.xml");
    final SchematronOutputType aSOT = SchematronHelper.applySchematron(new SchematronResourcePure(aSCH), aXML);
    assertNotNull(aSOT);
    assertTrue(SVRLHelper.getAllFailedAssertions(aSOT).isNotEmpty());
}
Also used : SchematronOutputType(org.oclc.purl.dsdl.svrl.SchematronOutputType) IReadableResource(com.helger.commons.io.resource.IReadableResource) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) Test(org.junit.Test)

Aggregations

SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)41 Test (org.junit.Test)20 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)18 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)17 SchematronResourcePure (com.helger.schematron.pure.SchematronResourcePure)15 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)14 IReadableResource (com.helger.commons.io.resource.IReadableResource)11 Document (org.w3c.dom.Document)8 AbstractSchematronResource (com.helger.schematron.AbstractSchematronResource)6 LoggingPSErrorHandler (com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler)6 ISchematronResource (com.helger.schematron.ISchematronResource)5 SchematronResourceSCH (com.helger.schematron.xslt.SchematronResourceSCH)5 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)5 SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)4 File (java.io.File)4 XQueryAsXPathFunctionConverter (com.helger.schematron.xpath.XQueryAsXPathFunctionConverter)3 Nonnull (javax.annotation.Nonnull)3 Nullable (javax.annotation.Nullable)3 CollectingPSErrorHandler (com.helger.schematron.pure.errorhandler.CollectingPSErrorHandler)2 PSReader (com.helger.schematron.pure.exchange.PSReader)2