mirror of
https://github.com/itplr-kosit/validator.git
synced 2026-05-25 16:55:39 +00:00
Compare commits
No commits in common. "d4d1aaea51b3dfa39166354f0568d3db29263b01" and "edde0f5134cba4f00e76c0fc52adab761130b59d" have entirely different histories.
d4d1aaea51
...
edde0f5134
9 changed files with 53 additions and 228 deletions
100
docs/api.md
100
docs/api.md
|
|
@ -12,8 +12,8 @@ Then you can declare the dependency as follows:
|
|||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.kosit</groupId>
|
||||
<artifactId>validator</artifactId>
|
||||
<groupId>de.kosit</groupId>
|
||||
<artifactId>validationtool</artifactId>
|
||||
<version>${validator.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
|
@ -22,12 +22,10 @@ Then you can declare the dependency as follows:
|
|||
|
||||
```js
|
||||
dependencies {
|
||||
compile group: 'org.kosit', name: 'validator', version: '1.5.1'
|
||||
compile group: 'de.kosit', name: 'validationtool', version: '1.1.0'
|
||||
}
|
||||
```
|
||||
|
||||
Hint: prior to v1.5.1 the group ID was `de.kosit` and the artifact ID was `validationtool`.
|
||||
|
||||
## Usage
|
||||
|
||||
Prerequisite for use is a valid [scenario definition](configurations.md) and the a folder with all necessary artifacts for validation (repository) either on the filesystem or on the classpath.
|
||||
|
|
@ -35,39 +33,34 @@ Prerequisite for use is a valid [scenario definition](configurations.md) and the
|
|||
The following example demonstrates loading scenario.xml and whole configuration from classpath and validating one XML document:
|
||||
|
||||
```java
|
||||
package de.kosit.validationtool.docs;
|
||||
package org.kosit.validator.example;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import de.kosit.validationtool.api.Check;
|
||||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.api.Result;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.impl.xml.ProcessorProvider;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
/**
|
||||
* Example code that is used in the docs/api.md file
|
||||
*/
|
||||
public class StandardExample {
|
||||
|
||||
public void run(final Path testDocument) throws URISyntaxException {
|
||||
public void run(Path testDocument) throws URISyntaxException {
|
||||
// Load scenarios.xml from classpath
|
||||
final URL scenarios = this.getClass().getClassLoader().getResource("examples/simple/scenarios-with-relative-paths.xml");
|
||||
URL scenarios = this.getClass().getClassLoader().getResource("scenarios.xml");
|
||||
// Load the rest of the specific Validator configuration from classpath
|
||||
final Configuration config = Configuration.load(scenarios.toURI()).build(ProcessorProvider.getProcessor());
|
||||
Configuration config = Configuration.load(scenarios.toURI()).build();
|
||||
// Use the default validation procedure
|
||||
final Check validator = new DefaultCheck(config);
|
||||
Check validator = new DefaultCheck(config);
|
||||
// Validate a single document
|
||||
final Input document = InputFactory.read(testDocument);
|
||||
Input document = InputFactory.read(testDocument);
|
||||
// Get Result including information about the whole validation
|
||||
final Result report = validator.checkInput(document);
|
||||
Result report = validator.checkInput(document);
|
||||
System.out.println("Is processing succesful=" + report.isProcessingSuccessful());
|
||||
// Get report document if processing was successful
|
||||
Document result = null;
|
||||
|
|
@ -77,16 +70,13 @@ public class StandardExample {
|
|||
// continue processing results...
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
// Use e.g. "src/test/resources/examples/simple/input/foo.xml"
|
||||
if (args.length == 0) {
|
||||
throw new IllegalStateException("Provide a test document filename on the commandline");
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Path of document for validation
|
||||
final Path testDoc = Paths.get(args[0]);
|
||||
final StandardExample example = new StandardExample();
|
||||
Path testDoc = Paths.get(args[0]);
|
||||
StandardExample example = new StandardExample();
|
||||
// run example validation
|
||||
example.run(testDoc);
|
||||
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
@ -144,36 +134,26 @@ Instead of pre-configured [scenario files](configurations.md) it is possible to
|
|||
A simple configuration looks like this:
|
||||
|
||||
```java
|
||||
package de.kosit.validationtool.docs;
|
||||
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.fallback;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.report;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.scenario;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.schema;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.schematron;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import de.kosit.validationtool.api.Check;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.*;
|
||||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.impl.xml.ProcessorProvider;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Example code that is used in the docs/api.md file
|
||||
*/
|
||||
public class MyValidator {
|
||||
|
||||
public static void main(final String[] args) {
|
||||
final 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());
|
||||
final Check validator = new DefaultCheck(config);
|
||||
// .. run your checks
|
||||
}
|
||||
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();
|
||||
Check validator = new DefaultCheck(config);
|
||||
// .. run your checks
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
@ -208,17 +188,19 @@ which further opens the second to load resources also from remote locations via
|
|||
|
||||
You can configure usage of one of these implementations using the `ResolvingMode` via
|
||||
|
||||
```java
|
||||
final Configuration config = Configuration.load(URI.create("myscenarios.xml")).setResolvingMode(ResolvingMode.STRICT_LOCAL)
|
||||
.build(ProcessorProvider.getProcessor());
|
||||
```
|
||||
````java
|
||||
Conifuguration config = Configuration.load(URI.create("myscenarios.xml"))
|
||||
.resolvingMode(ResolvingMode.STRICT_LOCAL)
|
||||
.build();
|
||||
````
|
||||
|
||||
If you decide to implement your own strategy, you can configure this via:
|
||||
|
||||
```java
|
||||
final Configuration config = Configuration.load(URI.create("myscenarios.xml"))
|
||||
.setResolvingStrategy(new MyCustomResolvingConfigurationStrategy()).build(ProcessorProvider.getProcessor());
|
||||
```
|
||||
````java
|
||||
Conifuguration config = Configuration.load(URI.create("myscenarios.xml"))
|
||||
.resolvingStrategy(new MyCustomResolvingConfigurationStrategy())
|
||||
.build();
|
||||
````
|
||||
|
||||
---
|
||||
|
||||
|
|
|
|||
8
pom.xml
8
pom.xml
|
|
@ -22,7 +22,7 @@
|
|||
<name>KoSIT XML Prüftool Implementierung</name>
|
||||
|
||||
<groupId>org.kosit</groupId>
|
||||
<artifactId>validator</artifactId>
|
||||
<artifactId>validationtool</artifactId>
|
||||
<version>1.5.1-SNAPSHOT</version>
|
||||
|
||||
<description>KoSIT XML Validator against XSD and Schematron based on defined scenarios.</description>
|
||||
|
|
@ -464,12 +464,6 @@
|
|||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.5.3</version>
|
||||
<configuration>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>java.net.useSystemProxies</name>
|
||||
<value>true</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
<!--suppress MavenModelInspection -->
|
||||
<argLine>-Dfile.encoding=UTF-8 ${jacocoSurefire}</argLine>
|
||||
</configuration>
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import org.fusesource.jansi.AnsiRenderer.Code;
|
|||
|
||||
import de.kosit.validationtool.cmd.report.Line;
|
||||
import de.kosit.validationtool.impl.Printer;
|
||||
|
||||
import picocli.CommandLine;
|
||||
import picocli.CommandLine.ParseResult;
|
||||
|
||||
|
|
@ -78,11 +79,11 @@ public class CommandLineApplication {
|
|||
final CommandLine commandLine = new CommandLine(new CommandLineOptions());
|
||||
try {
|
||||
commandLine.setExecutionExceptionHandler(CommandLineApplication::logExecutionException);
|
||||
final int cmdlineRetVal = commandLine.execute(args);
|
||||
if (commandLine.isUsageHelpRequested() || cmdlineRetVal == CommandLine.ExitCode.USAGE) {
|
||||
commandLine.execute(args);
|
||||
if (commandLine.isUsageHelpRequested()) {
|
||||
resultStatus = ReturnValue.HELP_REQUEST;
|
||||
} else {
|
||||
resultStatus = ObjectUtils.getIfNull(commandLine.getExecutionResult(), ReturnValue.PARSING_ERROR);
|
||||
resultStatus = ObjectUtils.defaultIfNull(commandLine.getExecutionResult(), ReturnValue.PARSING_ERROR);
|
||||
if (resultStatus.isError()) {
|
||||
commandLine.usage(System.out);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,12 @@ import java.nio.file.Path;
|
|||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import de.kosit.validationtool.cmd.CommandLineApplication.Level;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import de.kosit.validationtool.cmd.CommandLineApplication.Level;
|
||||
|
||||
import picocli.CommandLine.ArgGroup;
|
||||
import picocli.CommandLine.Command;
|
||||
import picocli.CommandLine.Help.Visibility;
|
||||
|
|
@ -36,12 +38,10 @@ import picocli.CommandLine.Parameters;
|
|||
* @author Andreas Penski
|
||||
*/
|
||||
@Command(description = "Structural and semantic validation of xml files", name = "KoSIT Validator", mixinStandardHelpOptions = false,
|
||||
separator = " ", synopsisHeading = CommandLineOptions.SYNOSIS_HEADING)
|
||||
separator = " ")
|
||||
@Getter
|
||||
public class CommandLineOptions implements Callable<ReturnValue> {
|
||||
|
||||
static final String SYNOSIS_HEADING = "Usage: ";
|
||||
|
||||
/**
|
||||
* @author Andreas Penski
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -38,11 +38,7 @@ public class Printer {
|
|||
* @param params the params.
|
||||
*/
|
||||
public static void writeOut(final String message, final Object... params) {
|
||||
try {
|
||||
System.out.println(new MessageFormat(message, Locale.ENGLISH).format(params));
|
||||
} catch (final RuntimeException ex) {
|
||||
System.err.println("[Format error!] <" + message + "> with params <" + params + ">");
|
||||
}
|
||||
System.out.println(new MessageFormat(message, Locale.ENGLISH).format(params));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -52,11 +48,7 @@ public class Printer {
|
|||
* @param params the params.
|
||||
*/
|
||||
public static void writeErr(final String message, final Object... params) {
|
||||
try {
|
||||
System.err.println(new MessageFormat(message, Locale.ENGLISH).format(params));
|
||||
} catch (final RuntimeException ex) {
|
||||
System.err.println("[Format error!] <" + message + "> with params <" + params + ">");
|
||||
}
|
||||
System.err.println(new MessageFormat(message, Locale.ENGLISH).format(params));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -81,15 +81,7 @@ public class CommandlineApplicationTest {
|
|||
|
||||
private static void checkForHelp(final List<String> outputLines) {
|
||||
assertThat(outputLines.size()).isPositive();
|
||||
assertThat(outputLines.stream().filter(l -> l.startsWith(CommandLineOptions.SYNOSIS_HEADING))).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoArguments() {
|
||||
final String[] args = {};
|
||||
CommandLineApplication.mainProgram(args);
|
||||
assertThat(CommandLine.getErrorOutput()).isNotEmpty();
|
||||
checkForHelp(CommandLine.getErrorLines());
|
||||
assertThat(outputLines.stream().filter(l -> l.startsWith("Usage: KoSIT Validator"))).hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
|
|
@ -1,51 +0,0 @@
|
|||
package de.kosit.validationtool.docs;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.xml.transform.URIResolver;
|
||||
import javax.xml.validation.Schema;
|
||||
import javax.xml.validation.SchemaFactory;
|
||||
import javax.xml.validation.Validator;
|
||||
|
||||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.api.ResolvingConfigurationStrategy;
|
||||
import de.kosit.validationtool.impl.ResolvingMode;
|
||||
import de.kosit.validationtool.impl.xml.ProcessorProvider;
|
||||
import net.sf.saxon.lib.UnparsedTextURIResolver;
|
||||
|
||||
public class MiscDocExampleCodes {
|
||||
|
||||
void m1() {
|
||||
final Configuration config = Configuration.load(URI.create("myscenarios.xml")).setResolvingMode(ResolvingMode.STRICT_LOCAL)
|
||||
.build(ProcessorProvider.getProcessor());
|
||||
}
|
||||
|
||||
private static final class MyCustomResolvingConfigurationStrategy implements ResolvingConfigurationStrategy {
|
||||
|
||||
public SchemaFactory createSchemaFactory() {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public URIResolver createResolver(final URI scenarioRepository) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public UnparsedTextURIResolver createUnparsedTextURIResolver(final URI scenarioRepository) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
public Validator createValidator(final Schema schema) {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void m2() {
|
||||
final Configuration config = Configuration.load(URI.create("myscenarios.xml"))
|
||||
.setResolvingStrategy(new MyCustomResolvingConfigurationStrategy()).build(ProcessorProvider.getProcessor());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
package de.kosit.validationtool.docs;
|
||||
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.fallback;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.report;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.scenario;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.schema;
|
||||
import static de.kosit.validationtool.config.ConfigurationBuilder.schematron;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import de.kosit.validationtool.api.Check;
|
||||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.impl.xml.ProcessorProvider;
|
||||
|
||||
/**
|
||||
* Example code that is used in the docs/api.md file
|
||||
*/
|
||||
public class MyValidator {
|
||||
|
||||
public static void main(final String[] args) {
|
||||
final 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());
|
||||
final Check validator = new DefaultCheck(config);
|
||||
// .. run your checks
|
||||
}
|
||||
}
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
package de.kosit.validationtool.docs;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import de.kosit.validationtool.api.Check;
|
||||
import de.kosit.validationtool.api.Configuration;
|
||||
import de.kosit.validationtool.api.Input;
|
||||
import de.kosit.validationtool.api.InputFactory;
|
||||
import de.kosit.validationtool.api.Result;
|
||||
import de.kosit.validationtool.impl.DefaultCheck;
|
||||
import de.kosit.validationtool.impl.xml.ProcessorProvider;
|
||||
|
||||
/**
|
||||
* Example code that is used in the docs/api.md file
|
||||
*/
|
||||
public class StandardExample {
|
||||
|
||||
public void run(final Path testDocument) throws URISyntaxException {
|
||||
// Load scenarios.xml from classpath
|
||||
final URL scenarios = this.getClass().getClassLoader().getResource("examples/simple/scenarios-with-relative-paths.xml");
|
||||
// Load the rest of the specific Validator configuration from classpath
|
||||
final Configuration config = Configuration.load(scenarios.toURI()).build(ProcessorProvider.getProcessor());
|
||||
// Use the default validation procedure
|
||||
final Check validator = new DefaultCheck(config);
|
||||
// Validate a single document
|
||||
final Input document = InputFactory.read(testDocument);
|
||||
// Get Result including information about the whole validation
|
||||
final 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();
|
||||
}
|
||||
// continue processing results...
|
||||
}
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
// Use e.g. "src/test/resources/examples/simple/input/foo.xml"
|
||||
if (args.length == 0) {
|
||||
throw new IllegalStateException("Provide a test document filename on the commandline");
|
||||
}
|
||||
// Path of document for validation
|
||||
final Path testDoc = Paths.get(args[0]);
|
||||
final StandardExample example = new StandardExample();
|
||||
// run example validation
|
||||
example.run(testDoc);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue