From 1d193e7c87ce3f05fc28a80721682ca819b6d7b8 Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Sun, 10 Nov 2019 21:07:34 +1100 Subject: [PATCH] rework version loading when validating --- .../java/org/hl7/fhir/utilities/TextFile.java | 13 ++ .../hl7/fhir/utilities/VersionUtilities.java | 25 ++- .../fhir/r5/validation/ValidationEngine.java | 4 +- .../org/hl7/fhir/r5/validation/Validator.java | 142 ++++++++++-------- 4 files changed, 120 insertions(+), 64 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java index 4a871ed93..a9ead934c 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java @@ -192,6 +192,19 @@ public class TextFile { return streamToBytes(new FileInputStream(new CSFile(srcFile))); } + /** + * + * fileToBytes insists in case correctness to ensure that stuff works across linux and windows, but it's not always appropriate to ceheck case (e.g. validator parameters) + * + * @param srcFile + * @return + * @throws FileNotFoundException + * @throws IOException + */ + public static byte[] fileToBytesNCS(String srcFile) throws FileNotFoundException, IOException { + return streamToBytes(new FileInputStream(new File(srcFile))); + } + public static byte[] fileToBytes(File file) throws FileNotFoundException, IOException { return streamToBytes(new FileInputStream(file)); } diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java index 0eb777fb8..5739ab599 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/VersionUtilities.java @@ -39,6 +39,9 @@ public class VersionUtilities { if (isR4Ver(v)) { return "hl7.fhir.r4.core"; } + if ("current".equals(v)) { + return "hl7.fhir.r5.core"; + } if (v != null && v.startsWith(CURRENT_VERSION)) { return "hl7.fhir.r5.core"; } @@ -59,10 +62,30 @@ public class VersionUtilities { return "4.0.1"; } if (v != null && v.startsWith(CURRENT_VERSION)) { - return "Constants.VERSION"; + return "current"; } return v; } + + public static String getCurrentPackageVersion(String v) { + if (isR2Ver(v)) { + return "1.0"; + } + if (isR2BVer(v)) { + return "1.4"; + } + if (isR3Ver(v)) { + return "3.0"; + } + if (isR4Ver(v)) { + return "4.0"; + } + if (v != null && v.startsWith(CURRENT_VERSION)) { + return "current"; + } + return v; + } + public static boolean isSupportedVersion(String version) { return Utilities.existsInList(version, "1.0.2", "1.4.0", "3.0.2", "4.0.1", CURRENT_FULL_VERSION); } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java index bc090a7ed..ed97cceb5 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/ValidationEngine.java @@ -516,7 +516,7 @@ public class ValidationEngine implements IValidatorResourceFetcher { FhirFormat fmt = checkIsResource(src); if (fmt != null) { Map res = new HashMap(); - res.put(Utilities.changeFileExt(src, "."+fmt.getExtension()), TextFile.fileToBytes(src)); + res.put(Utilities.changeFileExt(src, "."+fmt.getExtension()), TextFile.fileToBytesNCS(src)); return res; } } else if ((src.matches(PackageCacheManager.PACKAGE_REGEX) || src.matches(PackageCacheManager.PACKAGE_VERSION_REGEX)) && !src.endsWith(".zip") && !src.endsWith(".tgz")) { @@ -1586,7 +1586,7 @@ public class ValidationEngine implements IValidatorResourceFetcher { return true; // we don't bother with those. if (context.fetchResource(Resource.class, url) != null) return true; - throw new Error("Not done yet - resolve "+url); + return false; } } diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java index cf2018e7e..bdc03be4e 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/r5/validation/Validator.java @@ -261,13 +261,42 @@ public class Validator { else { // first, prepare the context String v = getParam(args, "-version"); - if (v == null) v = Constants.VERSION; - else if ("1.0".equals(v)) v = "1.0.2"; - else if ("1.4".equals(v)) v = "1.4.0"; - else if ("3.0".equals(v)) v = "3.0.2"; - else if ("4.0".equals(v)) v = "4.0.1"; - else if (v.startsWith(Constants.VERSION)) v = Constants.VERSION; - String definitions = "hl7.fhir.core#"+v; + if (v == null) { + v = "current"; + for (int i = 0; i < args.length; i++) { + if ("-ig".equals(args[i])) { + if (i+1 == args.length) + throw new Error("Specified -ig without indicating ig file"); + else { + String n = args[i+1]; + if (n.startsWith("hl7.fhir.core#")) { + v = VersionUtilities.getCurrentPackageVersion(n.substring(14)); + } else if (n.startsWith("hl7.fhir.r2.core#") || n.equals("hl7.fhir.r2.core")) { + v = "1.0"; + } else if (n.startsWith("hl7.fhir.r2b.core#") || n.equals("hl7.fhir.r2b.core")) { + v = "1.4"; + } else if (n.startsWith("hl7.fhir.r3.core#") || n.equals("hl7.fhir.r3.core")) { + v = "3.0"; + } else if (n.startsWith("hl7.fhir.r4.core#") || n.equals("hl7.fhir.r4.core")) { + v = "4.0"; + } else if (n.startsWith("hl7.fhir.r5.core#") || n.equals("hl7.fhir.r5.core")) { + v = "current"; + } + } + } + } + } else if ("1.0".equals(v)) { + v = "1.0"; + } else if ("1.4".equals(v)) { + v = "1.4"; + } else if ("3.0".equals(v)) { + v = "3.0"; + } else if ("4.0".equals(v)) { + v = "4.0"; + } else if (v.startsWith(Constants.VERSION)) { + v = "current"; + } + String definitions = VersionUtilities.packageForVersion(v)+"#"+v; System.out.println("Loading (v = "+v+", tx server http://tx.fhir.org)"); ValidationEngine validator = new ValidationEngine(definitions, "http://tx.fhir.org", null, FhirPublication.fromCode(v)); for (int i = 0; i < args.length; i++) { @@ -344,7 +373,6 @@ public class Validator { System.out.println(); System.out.println("Directories: Current = "+System.getProperty("user.dir")+", Package Cache = "+PackageCacheManager.userDir()); - String definitions = "hl7.fhir.core#current"; String map = null; List igs = new ArrayList(); List questionnaires = new ArrayList(); @@ -358,7 +386,7 @@ public class Validator { String output = null; List sources= new ArrayList(); Map locations = new HashMap(); - String sv = null; + String sv = "current"; String txLog = null; String mapLog = null; String lang = null; @@ -366,39 +394,15 @@ public class Validator { // load the parameters - so order doesn't matter for (int i = 0; i < args.length; i++) { - if (args[i].equals("-defn")) - if (i+1 == args.length) - throw new Error("Specified -defn without indicating definition file"); - else - definitions = args[++i]; - else if (args[i].equals("-version")) { + if (args[i].equals("-version")) { sv = args[++i]; - if (sv.startsWith("1.0")) { - sv = "1.0.2"; - definitions = "hl7.fhir.r2.core#"+sv; - } - if (sv.startsWith("1.4")) { - sv = "1.4.0"; - definitions = "hl7.fhir.r2b.core#"+sv; - } - if (sv.startsWith("3.0")) { - sv = "3.0.2"; - definitions = "hl7.fhir.r3.core#"+sv; - } - if (sv.startsWith("4.0")) { - sv = "4.0.1"; - definitions = "hl7.fhir.r4.core#"+sv; - } - if (sv.startsWith(Constants.VERSION)) { - sv = Constants.VERSION; - definitions = "hl7.fhir.r5.core#"+sv; - } - } else if (args[i].equals("-output")) + sv = VersionUtilities.getCurrentPackageVersion(sv); + } else if (args[i].equals("-output")) { if (i+1 == args.length) throw new Error("Specified -output without indicating output file"); else output = args[++i]; - else if (args[i].equals("-profile")) { + } else if (args[i].equals("-profile")) { String p = null; if (i+1 == args.length) throw new Error("Specified -profile without indicating profile source"); @@ -413,79 +417,95 @@ public class Validator { else locations.put(p, args[++i]); } - } else if (args[i].equals("-questionnaire")) + } else if (args[i].equals("-questionnaire")) { if (i+1 == args.length) throw new Error("Specified -questionnaire without indicating questionnaire file"); else questionnaires.add(args[++i]); - else if (args[i].equals("-native")) + } else if (args[i].equals("-native")) { doNative = true; - else if (args[i].equals("-debug")) + } else if (args[i].equals("-debug")) { doDebug = true; - else if (args[i].equals("-recurse")) + } else if (args[i].equals("-recurse")) { recursive = true; - else if (args[i].equals("-strictExtensions")) + } else if (args[i].equals("-strictExtensions")) { anyExtensionsAllowed = false; - else if (args[i].equals("-hintAboutNonMustSupport")) + } else if (args[i].equals("-hintAboutNonMustSupport")) { hintAboutNonMustSupport = true; - else if (args[i].equals("-transform")) { + } else if (args[i].equals("-transform")) { map = args[++i]; mode = EngineMode.TRANSFORM; - } else if (args[i].equals("-narrative")) + } else if (args[i].equals("-narrative")) { mode = EngineMode.NARRATIVE; - else if (args[i].equals("-snapshot")) + } else if (args[i].equals("-snapshot")) { mode = EngineMode.SNAPSHOT; - else if (args[i].equals("-scan")) + } else if (args[i].equals("-scan")) { mode = EngineMode.SCAN; - else if (args[i].equals("-tx")) + } else if (args[i].equals("-tx")) { if (i+1 == args.length) throw new Error("Specified -tx without indicating terminology server"); else txServer = "n/a".equals(args[++i]) ? null : args[i]; - else if (args[i].equals("-txLog")) + } else if (args[i].equals("-txLog")) { if (i+1 == args.length) throw new Error("Specified -txLog without indicating file"); else txLog = args[++i]; - else if (args[i].equals("-log")) + } else if (args[i].equals("-log")) { if (i+1 == args.length) throw new Error("Specified -log without indicating file"); else mapLog = args[++i]; - else if (args[i].equals("-language")) + } else if (args[i].equals("-language")) { if (i+1 == args.length) throw new Error("Specified -language without indicating language"); else lang = args[++i]; - else if (args[i].equals("-ig")) + } else if (args[i].equals("-ig") || args[i].equals("-defn")) { if (i+1 == args.length) - throw new Error("Specified -ig without indicating ig file"); + throw new Error("Specified "+args[i]+" without indicating ig file"); else { String s = args[++i]; - if (s.startsWith("hl7.fhir.core-")) - definitions = s; + if (s.equals("hl7.fhir.core")) { + sv = "current"; + } else if (s.startsWith("hl7.fhir.core#")) { + sv = VersionUtilities.getCurrentPackageVersion(s.substring(14)); + } else if (s.startsWith("hl7.fhir.r2.core#") || s.equals("hl7.fhir.r2.core")) { + sv = "1.0"; + } else if (s.startsWith("hl7.fhir.r2b.core#") || s.equals("hl7.fhir.r2b.core")) { + sv = "1.4"; + } else if (s.startsWith("hl7.fhir.r3.core#") || s.equals("hl7.fhir.r3.core")) { + sv = "3.0"; + } else if (s.startsWith("hl7.fhir.r4.core#") || s.equals("hl7.fhir.r4.core")) { + sv = "4.0"; + } else if (s.startsWith("hl7.fhir.r5.core#") || s.equals("hl7.fhir.r5.core")) { + sv = "current"; + } else igs.add(s); } - else if (args[i].equals("-map")) - if (map == null) + } else if (args[i].equals("-map")) { + if (map == null) { if (i+1 == args.length) throw new Error("Specified -map without indicating map file"); else map = args[++i]; - else + } else { throw new Exception("Can only nominate a single -map parameter"); - else if (args[i].startsWith("-x")) + } + } else if (args[i].startsWith("-x")) { i++; - else + } else { sources.add(args[i]); } + } if (sources.isEmpty()) throw new Exception("Must provide at least one source file"); // Comment this out because definitions filename doesn't necessarily contain version (and many not even be 14 characters long). Version gets spit out a couple of lines later after we've loaded the context + String definitions = VersionUtilities.packageForVersion(sv)+"#"+VersionUtilities.getCurrentVersion(sv); + System.out.println(" .. FHIR Version "+sv+", definitions from "+definitions); System.out.println(" .. connect to tx server @ "+txServer); - System.out.println(" .. definitions from "+definitions); ValidationEngine validator = new ValidationEngine(definitions, txServer, txLog, FhirPublication.fromCode(sv)); validator.setDebug(doDebug); System.out.println(" (v"+validator.getContext().getVersion()+")");