forked from veraPDF/veraPDF-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fb2907
commit 64e4c3f
Showing
6 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>verapdf-tools</artifactId> | ||
<groupId>org.verapdf</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>generation-json-from-profile</artifactId> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.verapdf</groupId> | ||
<artifactId>core</artifactId> | ||
<version>${verapdf.version}</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
32 changes: 32 additions & 0 deletions
32
generation-json-from-profile/src/main/java/org/verapdf/tools/cli/JSONGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.verapdf.tools.cli; | ||
|
||
import com.fasterxml.jackson.core.Version; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import org.verapdf.pdfa.validation.profiles.Profiles; | ||
import org.verapdf.pdfa.validation.profiles.ValidationProfile; | ||
|
||
import javax.xml.bind.JAXBException; | ||
import java.io.*; | ||
|
||
public class JSONGenerator { | ||
|
||
public static void main(String[] args) throws IOException { | ||
ValidationProfile profile; | ||
File file = new File(args[0]); | ||
try (InputStream is = new FileInputStream(file)) { | ||
profile = Profiles.profileFromXml(is); | ||
} catch (IOException | JAXBException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
Rules rules = new Rules(profile.getRules()); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
SimpleModule module = new SimpleModule("Serializer", new Version(2, 1, | ||
3, null, null, null)); | ||
RuleSerializer ruleSerializer = new RuleSerializer(Rules.class); | ||
module.addSerializer(Rules.class, ruleSerializer); | ||
objectMapper.registerModule(module); | ||
objectMapper.writeValue(System.out, rules); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
generation-json-from-profile/src/main/java/org/verapdf/tools/cli/RuleComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.verapdf.tools.cli; | ||
|
||
import org.verapdf.pdfa.validation.profiles.Rule; | ||
import org.verapdf.pdfa.validation.profiles.RuleId; | ||
|
||
import java.util.Comparator; | ||
|
||
public class RuleComparator implements Comparator<Rule> { | ||
|
||
@Override | ||
public int compare(Rule rule1, Rule rule2) { | ||
RuleId id1 = rule1.getRuleId(); | ||
RuleId id2 = rule2.getRuleId(); | ||
int c = id1.getSpecification().getId().compareTo(id2.getSpecification().getId()); | ||
if (c != 0) { | ||
return c; | ||
} | ||
c = id1.getClause().compareTo(id2.getClause()); | ||
if (c != 0) { | ||
return c; | ||
} | ||
return id1.getTestNumber() - id2.getTestNumber(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
generation-json-from-profile/src/main/java/org/verapdf/tools/cli/RuleSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.verapdf.tools.cli; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
import org.verapdf.pdfa.validation.profiles.Rule; | ||
import org.verapdf.pdfa.validation.profiles.RuleId; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class RuleSerializer extends StdSerializer<Rules> { | ||
protected RuleSerializer(Class<Rules> t) { | ||
super(t); | ||
} | ||
|
||
public void serialize(Rules rules, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) | ||
throws IOException { | ||
RuleId lastRuleId = null; | ||
jsonGenerator.writeStartObject(); | ||
for (Rule rule : rules.getRules()) { | ||
RuleId id = rule.getRuleId(); | ||
if (lastRuleId == null || !Objects.equals(lastRuleId.getSpecification(), id.getSpecification())) { | ||
if (lastRuleId != null) { | ||
jsonGenerator.writeEndObject(); | ||
jsonGenerator.writeEndObject(); | ||
} | ||
jsonGenerator.writeFieldName(id.getSpecification().getId()); | ||
jsonGenerator.writeStartObject(); | ||
} | ||
if (lastRuleId == null || !lastRuleId.getClause().equals(id.getClause())) { | ||
if (lastRuleId != null) { | ||
jsonGenerator.writeEndObject(); | ||
} | ||
jsonGenerator.writeFieldName(id.getClause()); | ||
jsonGenerator.writeStartObject(); | ||
} | ||
jsonGenerator.writeFieldName(String.valueOf(id.getTestNumber())); | ||
jsonGenerator.writeStartObject(); | ||
jsonGenerator.writeStringField("SUMMARY", rule.getError().getMessage()); | ||
jsonGenerator.writeStringField("DESCRIPTION", rule.getDescription()); | ||
jsonGenerator.writeEndObject(); | ||
lastRuleId = rule.getRuleId(); | ||
} | ||
jsonGenerator.writeEndObject(); | ||
jsonGenerator.writeEndObject(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
generation-json-from-profile/src/main/java/org/verapdf/tools/cli/Rules.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.verapdf.tools.cli; | ||
|
||
import org.verapdf.pdfa.validation.profiles.Rule; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class Rules { | ||
private final List<Rule> rules; | ||
|
||
public Rules(Set<Rule> rules) { | ||
this.rules = new ArrayList<>(rules); | ||
this.rules.sort(new RuleComparator()); | ||
} | ||
|
||
public List<Rule> getRules() { | ||
return rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters