Search in sources :

Example 36 with SchematronOutputType

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

the class Issue048Test method validateAndProduceSVRL.

public static void validateAndProduceSVRL(@Nonnull final File aSchematron, final File aXML) throws Exception {
    final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematron)).readSchema();
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance());
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    final String sSCH = new PSWriter(new PSWriterSettings().setXMLWriterSettings(new XMLWriterSettings())).getXMLString(aPreprocessedSchema);
    if (false)
        System.out.println(sSCH);
    final SchematronResourceSCH aSCH = new SchematronResourceSCH(new ReadableResourceString(sSCH, StandardCharsets.UTF_8));
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) XMLWriterSettings(com.helger.xml.serialize.write.XMLWriterSettings) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) PSWriter(com.helger.schematron.pure.exchange.PSWriter) PSWriterSettings(com.helger.schematron.pure.exchange.PSWriterSettings) ReadableResourceString(com.helger.commons.io.resource.inmemory.ReadableResourceString) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) ReadableResourceString(com.helger.commons.io.resource.inmemory.ReadableResourceString) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 37 with SchematronOutputType

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

the class Issue006Test method validateAndProduceSVRL.

@SuppressFBWarnings("BC_IMPOSSIBLE_INSTANCEOF")
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 aSCH = new SchematronResourceSCH(aSchematron);
    if (aSCH instanceof SchematronResourcePure)
        ((SchematronResourcePure) aSCH).setErrorHandler(new LoggingPSErrorHandler());
    else
        System.out.println(XMLWriter.getNodeAsString(((SchematronResourceSCH) aSCH).getXSLTProvider().getXSLTDocument()));
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(anXMLSource);
    assertNotNull(aSVRL);
    if (false)
        System.out.println(new SVRLMarshaller().getAsString(aSVRL));
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) SchematronResourceSCH(com.helger.schematron.sch.SchematronResourceSCH) LoggingPSErrorHandler(com.helger.schematron.pure.errorhandler.LoggingPSErrorHandler) 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) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 38 with SchematronOutputType

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

the class PSXPathValidationHandlerSVRL method onStart.

@Override
public void onStart(@Nonnull final PSSchema aSchema, @Nullable final PSPhase aActivePhase, @Nullable final String sBaseURI) throws SchematronValidationException {
    final SchematronOutputType aSchematronOutput = new SchematronOutputType();
    if (aActivePhase != null)
        aSchematronOutput.setPhase(aActivePhase.getID());
    aSchematronOutput.setSchemaVersion(aSchema.getSchemaVersion());
    aSchematronOutput.setTitle(_getTitleAsString(aSchema.getTitle()));
    // Add namespace prefixes
    for (final Map.Entry<String, String> aEntry : aSchema.getAsNamespaceContext().getPrefixToNamespaceURIMap().entrySet()) {
        final NsPrefixInAttributeValues aNsPrefix = new NsPrefixInAttributeValues();
        aNsPrefix.setPrefix(aEntry.getKey());
        aNsPrefix.setUri(aEntry.getValue());
        aSchematronOutput.getNsPrefixInAttributeValues().add(aNsPrefix);
    }
    m_aSchematronOutput = aSchematronOutput;
    m_aSchema = aSchema;
    m_sBaseURI = sBaseURI;
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) NsPrefixInAttributeValues(com.helger.schematron.svrl.jaxb.NsPrefixInAttributeValues) Map(java.util.Map)

Example 39 with SchematronOutputType

use of com.helger.schematron.svrl.jaxb.SchematronOutputType 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 40 with SchematronOutputType

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

the class Issue099Test method _validateAndProduceSVRL.

private static void _validateAndProduceSVRL(@Nonnull final File aSchematron, @Nonnull final File aXML) throws Exception {
    SchematronDebug.setSaveIntermediateXSLTFiles(true);
    final StopWatch aSW = StopWatch.createdStarted();
    LOGGER.info("Start");
    final ISchematronResource aSCH = SchematronResourcePure.fromFile(aSchematron);
    if (aSCH instanceof SchematronResourcePure)
        ((SchematronResourcePure) aSCH).setCustomValidationHandler(new LoggingPSValidationHandler());
    // Perform validation
    final SchematronOutputType aSVRL = aSCH.applySchematronValidationToSVRL(new FileSystemResource(aXML));
    assertNotNull(aSVRL);
    aSW.stop();
    LOGGER.info("Took " + aSW.getDuration());
    LOGGER.info("SVRL:\n" + new SVRLMarshaller().getAsString(aSVRL));
    assertTrue(SVRLHelper.getAllFailedAssertionsAndSuccessfulReports(aSVRL).isEmpty());
}
Also used : SchematronOutputType(com.helger.schematron.svrl.jaxb.SchematronOutputType) ISchematronResource(com.helger.schematron.ISchematronResource) SVRLMarshaller(com.helger.schematron.svrl.SVRLMarshaller) LoggingPSValidationHandler(com.helger.schematron.pure.validation.LoggingPSValidationHandler) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) SchematronResourcePure(com.helger.schematron.pure.SchematronResourcePure) StopWatch(com.helger.commons.timing.StopWatch)

Aggregations

SchematronOutputType (com.helger.schematron.svrl.jaxb.SchematronOutputType)59 SVRLMarshaller (com.helger.schematron.svrl.SVRLMarshaller)33 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)32 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 SchematronResourceSCH (com.helger.schematron.sch.SchematronResourceSCH)12 ISchematronResource (com.helger.schematron.ISchematronResource)11 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 Nonnull (javax.annotation.Nonnull)5 SVRLFailedAssert (com.helger.schematron.svrl.SVRLFailedAssert)4 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)4 File (java.io.File)4 LoggingPSValidationHandler (com.helger.schematron.pure.validation.LoggingPSValidationHandler)3 XQueryAsXPathFunctionConverter (com.helger.schematron.pure.xpath.XQueryAsXPathFunctionConverter)3