Search in sources :

Example 1 with DoNothingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler in project ph-schematron by phax.

the class DocumentationExamples method validateXMLViaPureSchematron2.

public static boolean validateXMLViaPureSchematron2(@Nonnull final File aSchematronFile, @Nonnull final File aXMLFile) throws Exception {
    // Read the schematron from file
    final PSSchema aSchema = new PSReader(new FileSystemResource(aSchematronFile)).readSchema();
    if (!aSchema.isValid(new DoNothingPSErrorHandler()))
        throw new IllegalArgumentException("Invalid Schematron!");
    // Resolve the query binding to use
    final IPSQueryBinding aQueryBinding = PSQueryBindingRegistry.getQueryBindingOfNameOrThrow(aSchema.getQueryBinding());
    // Pre-process schema
    final PSPreprocessor aPreprocessor = new PSPreprocessor(aQueryBinding);
    aPreprocessor.setKeepTitles(true);
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    // Bind the pre-processed schema
    final IPSBoundSchema aBoundSchema = aQueryBinding.bind(aPreprocessedSchema, null, null, null, null);
    // Read the XML file
    final Document aXMLNode = DOMReader.readXMLDOM(aXMLFile);
    if (aXMLNode == null)
        return false;
    // Perform the validation
    return aBoundSchema.validatePartially(aXMLNode, FileHelper.getAsURLString(aXMLFile)).isValid();
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) IPSQueryBinding(com.helger.schematron.pure.binding.IPSQueryBinding) IPSBoundSchema(com.helger.schematron.pure.bound.IPSBoundSchema) PSReader(com.helger.schematron.pure.exchange.PSReader) FileSystemResource(com.helger.commons.io.resource.FileSystemResource) PSPreprocessor(com.helger.schematron.pure.preprocess.PSPreprocessor) Document(org.w3c.dom.Document) PSSchema(com.helger.schematron.pure.model.PSSchema)

Example 2 with DoNothingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler in project ph-schematron by phax.

the class SchematronResourcePure method validateCompletely.

/**
 * Use the internal error handler to validate all elements in the schematron.
 * It tries to catch as many errors as possible.
 */
public void validateCompletely() {
    // Use the provided error handler (if any)
    final IPSErrorHandler aErrorHandler = m_aErrorHandler != null ? m_aErrorHandler : new DoNothingPSErrorHandler();
    validateCompletely(aErrorHandler);
}
Also used : IPSErrorHandler(com.helger.schematron.pure.errorhandler.IPSErrorHandler) DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)

Example 3 with DoNothingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler 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 4 with DoNothingPSErrorHandler

use of com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler in project ph-schematron by phax.

the class PSPreprocessorTest method testWithTitle.

@Test
public void testWithTitle() throws SchematronException {
    final PSPreprocessor aPreprocessor = new PSPreprocessor(PSXPathQueryBinding.getInstance()).setKeepTitles(true).setKeepDiagnostics(true);
    final IReadableResource aRes = new ClassPathResource("test-sch/example-3-5.sch");
    final IMicroDocument aDoc = SchematronHelper.getWithResolvedSchematronIncludes(aRes);
    final PSReader aReader = new PSReader(aRes);
    final PSSchema aSchema = aReader.readSchemaFromXML(aDoc.getDocumentElement());
    final PSSchema aPreprocessedSchema = aPreprocessor.getAsPreprocessedSchema(aSchema);
    assertNotNull(aPreprocessedSchema);
    assertTrue(aPreprocessedSchema.isValid(new DoNothingPSErrorHandler()));
    // Because titles are not in minimal mode
    assertFalse(aPreprocessedSchema.isMinimal());
}
Also used : DoNothingPSErrorHandler(com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler) IReadableResource(com.helger.commons.io.resource.IReadableResource) IMicroDocument(com.helger.xml.microdom.IMicroDocument) PSReader(com.helger.schematron.pure.exchange.PSReader) PSSchema(com.helger.schematron.pure.model.PSSchema) ClassPathResource(com.helger.commons.io.resource.ClassPathResource) Test(org.junit.Test)

Aggregations

DoNothingPSErrorHandler (com.helger.schematron.pure.errorhandler.DoNothingPSErrorHandler)4 PSReader (com.helger.schematron.pure.exchange.PSReader)2 PSSchema (com.helger.schematron.pure.model.PSSchema)2 Test (org.junit.Test)2 Document (org.w3c.dom.Document)2 ClassPathResource (com.helger.commons.io.resource.ClassPathResource)1 FileSystemResource (com.helger.commons.io.resource.FileSystemResource)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 IPSQueryBinding (com.helger.schematron.pure.binding.IPSQueryBinding)1 IPSBoundSchema (com.helger.schematron.pure.bound.IPSBoundSchema)1 IPSErrorHandler (com.helger.schematron.pure.errorhandler.IPSErrorHandler)1 PSPreprocessor (com.helger.schematron.pure.preprocess.PSPreprocessor)1 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 MapBasedXPathFunctionResolver (com.helger.xml.xpath.MapBasedXPathFunctionResolver)1 MapBasedXPathVariableResolver (com.helger.xml.xpath.MapBasedXPathVariableResolver)1 List (java.util.List)1 SchematronOutputType (org.oclc.purl.dsdl.svrl.SchematronOutputType)1