itplr-kosit / validator

Validates XML documents with XML Schema and Schematron
Apache License 2.0
72 stars 37 forks source link

Outdated API Documentation for 1.5.0 #130

Open SemikolonDEV opened 5 months ago

SemikolonDEV commented 5 months ago

The Description on https://github.com/itplr-kosit/validator/blob/main/docs/api.md is outdated and should be updated.

The code examples do not work with current version.

wustudent commented 1 month ago

@SemikolonDEV I also experienced the same thing as you did. After some research by looking into the source code, I found out how we can set the Processor parameter to the build function: You can use de.kosit.validationtool.impl.xml.ProcessorProvider.getProcessor() to get a processor.

import static de.kosit.validationtool.config.ConfigurationBuilder.*;
import de.kosit.validationtool.api.Configuration;
import java.net.URI;
import java.nio.file.Path;

public class MyValidator {

 public static void main(String[] args) {
    Configuration config = Configuration.create().name("myconfiguration")
                          .with(scenario("firstScenario")
                                          .match("//myNode")
                                          .validate(schema("Sample Schema").schemaLocation(URI.create("simple.xsd")))
                                          .validate(schematron("my rules").source("myRules.xsl"))
                                          .with(report("my report").source("report.xsl")))
                          .with(fallback().name("default-report").source("fallback.xsl"))
                          .useRepository(Paths.get("/opt/myrepository"))
                          .build(ProcessorProvider.getProcessor());
    Check validator = new DefaultCheck(config);
    // .. run your checks
 }
}
fktrdui commented 1 month ago

Thanks @wustudent , your comment was very helpful for me as it was the first time I use the KoSIT validator.

I am writing the update code, which also worked for me:

public void run(Path testDocument) throws URISyntaxException {
        // Load scenarios.xml from classpath
        URL scenarios = this.getClass().getClassLoader().getResource("scenarios.xml");

        // Load the rest of the specific Validator configuration from classpath
        Configuration config = Configuration.load(scenarios.toURI()).build(ProcessorProvider.getProcessor());

        // Use the default validation procedure
        Check validator = new DefaultCheck(config);
        // Validate a single document
        Input document = InputFactory.read(testDocument);
        // Get Result including information about the whole validation
        Result report = validator.checkInput(document);
        System.out.println("Is processing succesful=" + report.isProcessingSuccessful());
        // Get report document if processing was successful
        Document result = null;
        if (report.isProcessingSuccessful()) {
            result = report.getReportDocument();

            // Other available methods of the report (Result object) are for example:
            System.out.println("Report: " + report.getReport());
            System.out.println("isAcceptable: " + report.isAcceptable());
            System.out.println("isSchemaValid: " + report.isSchemaValid());
            System.out.println("ProcessingErrors: " + report.getProcessingErrors());
        }
        // continue processing results...
    }

Note: For the new users, loading scenarios(for XRechnung for example) from the class path will work with these steps:

  1. Download the latest XRechnung bundle from: https://xeinkauf.de/xrechnung/versionen-und-bundles/
  2. Unzip and locate the folder "validator-configuration-xrechnung_..."
  3. Copy the “scenarios.xml” file and “resources” folder to the root of your project under the “resources” folder (if not present, create the folder).