validator icon indicating copy to clipboard operation
validator copied to clipboard

Outdated API Documentation for 1.5.0

Open SemikolonDEV opened this issue 2 years ago • 2 comments

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.

SemikolonDEV avatar Apr 18 '24 13:04 SemikolonDEV

@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
 }
}

wustudent avatar Aug 20 '24 20:08 wustudent

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).

fktrdui avatar Aug 22 '24 06:08 fktrdui