Skip to content

Commit 923065d

Browse files
committed
[ITB-828] Command line support
1 parent 138b047 commit 923065d

File tree

10 files changed

+551
-0
lines changed

10 files changed

+551
-0
lines changed

etc/dev/process.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
#
3+
# Bash script to facilitate the testing of the validator as a command-line tool for a given domain.
4+
#
5+
# To use this script:
6+
# 1. Build the project so that the JAR module's JAR is created.
7+
# 2. Make a copy of this script.
8+
# 3. Adapt in the copy the script's configuration variables (see script configuration block below) to match your environment.
9+
# 4. Execute this script from a bash shell. The script also requires the "zip" and "unzip" tools to be defined.
10+
#
11+
# Script configuration - START
12+
#
13+
# The path to the default JAR file for the command line tool (as produced given the maven build).
14+
JAR_PATH="/mnt/d/git/itb/json-validator/jsonvalidator-jar/target/validator.jar"
15+
# A temporary folder to be used for processing.
16+
TMP_FOLDER="/mnt/d/_dev/json/offline/tmp"
17+
# The folder in which the resulting JAR file will be placed.
18+
PROCESSED_FOLDER="/mnt/d/_dev/json/offline/processed"
19+
# The resource folder used to configure the validator (as would be provided to the validator fot the resource root.
20+
RESOURCE_FOLDER="/mnt/d/git/itb/docker/validator-json-any"
21+
# The name of the domain within the resource root.
22+
DOMAIN_NAME="resources"
23+
#
24+
# Script configuration - END
25+
#
26+
# No changes should be made in the commands that follow.
27+
echo "Processing $RESOURCE_FOLDER"
28+
rm -rf $TMP_FOLDER
29+
mkdir -p $TMP_FOLDER
30+
rm -rf $PROCESSED_FOLDER
31+
mkdir -p $PROCESSED_FOLDER
32+
cp $JAR_PATH $TMP_FOLDER
33+
(cd $RESOURCE_FOLDER; zip -r $TMP_FOLDER/resources.zip ./*)
34+
unzip $TMP_FOLDER/resources.zip -d $TMP_FOLDER/resources
35+
cp ./validator.jar $TMP_FOLDER
36+
unzip $TMP_FOLDER/validator.jar -d $TMP_FOLDER/validator
37+
rm -rf $TMP_FOLDER/validator.jar
38+
rm -rf $TMP_FOLDER/validator/BOOT-INF/classes/validator-resources.jar
39+
(cd $TMP_FOLDER/resources/; zip -r -0 $TMP_FOLDER/validator/BOOT-INF/classes/validator-resources.jar $DOMAIN_NAME)
40+
(cd $TMP_FOLDER/validator/; zip -r -0 $TMP_FOLDER/validator.jar .)
41+
cp $TMP_FOLDER/validator.jar $PROCESSED_FOLDER/validator.jar

jsonvalidator-common/src/main/java/eu/europa/ec/itb/json/DomainConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package eu.europa.ec.itb.json;
22

3+
import eu.europa.ec.itb.validation.commons.artifact.ExternalArtifactSupport;
4+
import eu.europa.ec.itb.validation.commons.artifact.TypedValidationArtifactInfo;
35
import eu.europa.ec.itb.validation.commons.artifact.ValidationArtifactInfo;
46
import eu.europa.ec.itb.validation.commons.config.LabelConfig;
57
import eu.europa.ec.itb.validation.commons.config.WebDomainConfig;
@@ -15,6 +17,15 @@ public ValidationArtifactInfo getSchemaInfo(String validationType) {
1517
return getArtifactInfo().get(validationType).get();
1618
}
1719

20+
public boolean definesTypeWithExternalSchemas() {
21+
for (TypedValidationArtifactInfo info : getArtifactInfo().values()) {
22+
if (info.get().getExternalArtifactSupport() != ExternalArtifactSupport.NONE) {
23+
return true;
24+
}
25+
}
26+
return false;
27+
}
28+
1829
public static class Label extends LabelConfig {
1930
private String externalSchemaLabel;
2031
private String externalSchemaPlaceholder;

jsonvalidator-jar/pom.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>jsonvalidator</artifactId>
7+
<groupId>eu.europa.ec.itb.json</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>jsonvalidator-jar</artifactId>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-maven-plugin</artifactId>
19+
</plugin>
20+
<plugin>
21+
<groupId>org.apache.maven.plugins</groupId>
22+
<artifactId>maven-antrun-plugin</artifactId>
23+
<executions>
24+
<execution>
25+
<phase>generate-resources</phase>
26+
<goals>
27+
<goal>run</goal>
28+
</goals>
29+
<configuration>
30+
<target>
31+
<copy file="${basedir}\..\jsonvalidator-resources\target\validator-resources.jar"
32+
tofile="${basedir}\target\classes\validator-resources.jar" />
33+
</target>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-compiler-plugin</artifactId>
41+
<configuration>
42+
<source>11</source>
43+
<target>11</target>
44+
</configuration>
45+
</plugin>
46+
</plugins>
47+
<finalName>validator</finalName>
48+
</build>
49+
50+
<dependencies>
51+
<dependency>
52+
<groupId>eu.europa.ec.itb.json</groupId>
53+
<artifactId>jsonvalidator-common</artifactId>
54+
</dependency>
55+
<dependency>
56+
<groupId>eu.europa.ec.itb.commons</groupId>
57+
<artifactId>validation-commons-report</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>eu.europa.ec.itb</groupId>
61+
<artifactId>gitb-types</artifactId>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-logging</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>commons-io</groupId>
73+
<artifactId>commons-io</artifactId>
74+
</dependency>
75+
</dependencies>
76+
77+
</project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package eu.europa.ec.itb.json.standalone;
2+
3+
import eu.europa.ec.itb.json.ApplicationConfig;
4+
import org.apache.commons.io.FileUtils;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.ApplicationContext;
8+
import org.springframework.context.annotation.ComponentScan;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.util.Enumeration;
14+
import java.util.Objects;
15+
import java.util.UUID;
16+
import java.util.jar.JarEntry;
17+
import java.util.jar.JarFile;
18+
19+
@SpringBootApplication
20+
@ComponentScan("eu.europa.ec.itb")
21+
public class Application {
22+
23+
public static void main(String[] args) throws IOException {
24+
System.out.print("Starting validator ...");
25+
File tempFolder = Files.createTempDirectory("jsonvalidator").toFile();
26+
Runtime.getRuntime().addShutdownHook(new Thread(() -> FileUtils.deleteQuietly(tempFolder)));
27+
// Set the resource root so that it can be used. This is done before app startup to avoid PostConstruct issues.
28+
String resourceRoot = tempFolder.getAbsolutePath();
29+
if (!resourceRoot.endsWith(File.separator)) {
30+
resourceRoot += File.separator;
31+
}
32+
System.setProperty("validator.resourceRoot", resourceRoot);
33+
prepareConfigForStandalone(tempFolder);
34+
// Start the application.
35+
ApplicationContext ctx = SpringApplication.run(Application.class, args);
36+
// Post process config.
37+
ApplicationConfig config = ctx.getBean(ApplicationConfig.class);
38+
// Set report folder for use as a temp file generation target.
39+
config.setTmpFolder(tempFolder.getAbsolutePath());
40+
System.out.println(" Done.");
41+
try {
42+
ValidationRunner runner = ctx.getBean(ValidationRunner.class);
43+
runner.bootstrap(args, new File(config.getTmpFolder(), UUID.randomUUID().toString()));
44+
} catch (Exception e) {
45+
// Ignore errors here.
46+
}
47+
}
48+
49+
private static void prepareConfigForStandalone(File tempFolder) throws IOException {
50+
// Explode invoice resources to temp folder
51+
File tempJarFile = new File(tempFolder, "validator-resources.jar");
52+
FileUtils.copyInputStreamToFile(Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResourceAsStream("validator-resources.jar")), tempJarFile);
53+
JarFile resourcesJar = new JarFile(tempJarFile);
54+
Enumeration<JarEntry> entries = resourcesJar.entries();
55+
while (entries.hasMoreElements()) {
56+
JarEntry entry = entries.nextElement();
57+
File f = new File(tempFolder, entry.getName());
58+
if (entry.isDirectory()) { // if its a directory, create it
59+
f.mkdir();
60+
continue;
61+
}
62+
FileUtils.copyInputStreamToFile(resourcesJar.getInputStream(entry), f);
63+
}
64+
}
65+
66+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package eu.europa.ec.itb.json.standalone;
2+
3+
import com.gitb.tr.TAR;
4+
5+
public class FileReport {
6+
7+
private final String fileName;
8+
private final TAR report;
9+
private final boolean requireType;
10+
private final String validationType;
11+
12+
public FileReport(String fileName, TAR report) {
13+
this(fileName, report, false, null);
14+
}
15+
16+
public FileReport(String fileName, TAR report, boolean requireType, String type) {
17+
this.fileName = fileName;
18+
this.report = report;
19+
this.requireType = requireType;
20+
this.validationType = type;
21+
}
22+
23+
@Override
24+
public String toString() {
25+
StringBuilder sb = new StringBuilder();
26+
sb.append("Validation report summary [").append(this.fileName).append("]:");
27+
if(requireType) {
28+
sb.append("\n- Validation type: ").append(this.validationType);
29+
}
30+
sb.append("\n- Date: ").append(report.getDate());
31+
sb.append("\n- Result: ").append(report.getResult());
32+
sb.append("\n- Errors: ").append(report.getCounters().getNrOfErrors());
33+
sb.append("\n- Warnings: ").append(report.getCounters().getNrOfWarnings());
34+
sb.append("\n- Messages: ").append(report.getCounters().getNrOfAssertions());
35+
36+
return sb.toString();
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package eu.europa.ec.itb.json.standalone;
2+
3+
import java.io.File;
4+
5+
public class ValidationInput {
6+
7+
private File inputFile;
8+
private String fileName;
9+
10+
public ValidationInput(File inputFile, String fileName) {
11+
this.inputFile = inputFile;
12+
this.fileName = fileName;
13+
}
14+
15+
public File getInputFile() {
16+
return inputFile;
17+
}
18+
19+
public String getFileName() {
20+
return fileName;
21+
}
22+
}

0 commit comments

Comments
 (0)