Skip to content

Commit

Permalink
Add generation json from profile
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Aug 10, 2022
1 parent 2fb2907 commit 64e4c3f
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
24 changes: 24 additions & 0 deletions generation-json-from-profile/pom.xml
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>
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);
}
}
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();
}
}
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();
}
}
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;
}
}
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>pdf-decode-streams</module>
<module>generation-json-from-profile</module>
<module>pdf-decode-fonts</module>
<module>pdf-decode-streams</module>
<module>pdf-deprecated-finder</module>
<module>pdf-stream-change</module>
<module>pdf-stream-dump</module>
Expand Down

0 comments on commit 64e4c3f

Please sign in to comment.