Skip to content

Commit

Permalink
Merge pull request #31 from openEHR/fix_some_json_problems
Browse files Browse the repository at this point in the history
Add archetype editing functionality, fix some bugs
  • Loading branch information
pieterbos authored Jun 11, 2018
2 parents 6f3484d + 2534469 commit 12fb462
Show file tree
Hide file tree
Showing 79 changed files with 1,862 additions and 380 deletions.
4 changes: 2 additions & 2 deletions adlchecker/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
}

run {
args = ['archetypes']//, '--outputFlat']
args = ['archetypes', '--lint']//, '--outputFlat']
}

mainClassName='com.nedap.archie.adlchecker.AdlChecker'
mainClassName='com.nedap.archie.adlchecker.AdlChecker'
107 changes: 83 additions & 24 deletions adlchecker/src/main/java/com/nedap/archie/adlchecker/AdlChecker.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nedap.archie.adlchecker;

import com.google.common.io.CharStreams;
import com.nedap.archie.adlparser.ADLParser;
import com.nedap.archie.antlr.errors.ANTLRParserMessage;
import com.nedap.archie.aom.Archetype;
Expand All @@ -10,6 +11,7 @@
import com.nedap.archie.rminfo.ArchieRMInfoLookup;
import com.nedap.archie.rminfo.ReferenceModels;
import com.nedap.archie.serializer.adl.ADLArchetypeSerializer;
import com.nedap.archie.serializer.adl.ADLDefinitionSerializer;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.ArgumentParser;
Expand All @@ -19,7 +21,10 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -39,6 +44,10 @@ public static void main(String[] args) {
.action(Arguments.storeTrue())
.help("if the --outputFlat flag is present, also output the flat ADL");

parser.addArgument("-l", "--lint")
.action(Arguments.storeTrue())
.help("if the --lint flag is present, also output the linted ADL, which formats and adds missing id codes");

Namespace ns = null;
try {
ns = parser.parseArgs(args);
Expand All @@ -51,11 +60,12 @@ public static void main(String[] args) {
parser.printUsage();
parser.printHelp();
} else {
validateArchetypes(ns.getList("path"), ns.getBoolean("outputFlat"));
validateArchetypes(ns.getList("path"), ns.getBoolean("outputFlat"), ns.getBoolean("lint"));
}
}

private static void validateArchetypes(List<String> directories, boolean printFlatAdl) {
private static void validateArchetypes(List<String> directories, boolean printFlatAdl, boolean lint) {

InMemoryFullArchetypeRepository repository = new InMemoryFullArchetypeRepository();
for (String directory : directories) {
System.out.println("step 1: parsing archetypes");
Expand All @@ -78,20 +88,42 @@ private static void validateArchetypes(List<String> directories, boolean printFl
}

}

if(lint) {
System.out.println("step 3: running archetypes through linter");
System.out.println();
for (String directory : directories) {
try {
Files.walk(Paths.get(directory)).forEach((path) -> lint(path));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

private static void printValidationResult(ValidationResult result) {
System.out.println();
System.out.print("============= ");
System.out.print(result.getArchetypeId());
System.out.print(" ");
if(result.passes()) {
System.out.print("PASSED");
} else {
System.out.print("FAILED");
private static void lint(Path path) {
File file = path.toFile();
if(file.isDirectory()) {
return;
}
System.out.print(" =============");
System.out.println();
try (FileInputStream stream = new FileInputStream(file)) {
String fileContent = CharStreams.toString(new InputStreamReader(stream));
System.out.println("linting " + file.getAbsolutePath());
System.out.println();
TerminologyContentGenerator generator = new TerminologyContentGenerator(BuiltinReferenceModels.getMetaModels());
Archetype resultingArchetype = generator.addTerms(fileContent);
System.out.println(ADLArchetypeSerializer.serialize(resultingArchetype));
System.out.println();
} catch (Exception e) {
e.printStackTrace();
}


}

private static void printValidationResult(ValidationResult result) {
printHeader(result.getArchetypeId(), result.passes() ? "PASSED" : "FAILED");
for(ValidationMessage error:result.getErrors()) {
System.out.println(error.toString());
}
Expand All @@ -100,31 +132,35 @@ private static void printValidationResult(ValidationResult result) {
}
}

private static void printHeader(String archetypeId, String status) {
System.out.println();
System.out.print("============= ");
System.out.print(archetypeId);
System.out.print(" ");
System.out.print(status);
System.out.print(" =============");
System.out.println();
System.out.println();
}

private static void parseArchetype(Path path, InMemoryFullArchetypeRepository repository) {
File file = path.toFile();
if(file.isDirectory()) {
return;
}
ADLParser adlParser = new ADLParser();
adlParser.setLogEnabled(false);
try (FileInputStream stream = new FileInputStream(file)) {
try {
Archetype parsed = adlParser.parse(stream);
if(adlParser.getErrors().hasNoErrors()) {
repository.addArchetype(parsed);
}
if(adlParser.getErrors().hasNoMessages()){
System.out.println(path.getFileName() + " has no messages, ok!");
} else {
System.out.println("errors found for " + path.getFileName());

for(ANTLRParserMessage message:adlParser.getErrors().getWarnings()) {
System.err.println("warning: " + message.getMessage());
}
for(ANTLRParserMessage message:adlParser.getErrors().getErrors()) {
System.err.println("error: " + message.getMessage());
}
if(!adlParser.getErrors().hasNoMessages()){
printParseErrors(path, adlParser);
}
} catch (Exception e) {
printParseErrors(path, adlParser);
e.printStackTrace();
}
} catch (IOException e) {
Expand All @@ -133,4 +169,27 @@ private static void parseArchetype(Path path, InMemoryFullArchetypeRepository re
}
}

private static void printParseErrors(Path path, ADLParser adlParser) {
if(adlParser.getErrors() == null) {
printHeader(path.getFileName().toString(), "PARSING FAILED");
return;
}
else if(adlParser.getErrors().hasNoErrors()) {
printHeader(path.getFileName().toString(), "PARSING GENERATED WARNINGS");
} else {
printHeader(path.getFileName().toString(), "PARSING FAILED");
}
System.out.println("errors found for " + path.getFileName());

if(adlParser.getErrors() != null) {
for (ANTLRParserMessage message : adlParser.getErrors().getWarnings()) {
System.err.println("warning: " + message.getMessage());
}
for (ANTLRParserMessage message : adlParser.getErrors().getErrors()) {
System.err.println("error: " + message.getMessage());
}

}
}

}
Loading

0 comments on commit 12fb462

Please sign in to comment.