001package org.hl7.fhir.validation.cli.utils; 002 003import org.hl7.fhir.r5.model.Constants; 004import org.hl7.fhir.r5.model.FhirPublication; 005import org.hl7.fhir.utilities.TimeTracker; 006import org.hl7.fhir.utilities.VersionUtilities; 007import org.hl7.fhir.validation.ValidationEngine; 008 009public class Common { 010 011 public static final String DEFAULT_TX_SERVER = "http://tx.fhir.org"; 012 013 public static String getVersion(String[] args) { 014 String v = Params.getParam(args, "-version"); 015 if (v == null) { 016 v = "current"; 017 for (int i = 0; i < args.length; i++) { 018 if ("-ig".equals(args[i])) { 019 if (i + 1 == args.length) 020 throw new Error("Specified -ig without indicating ig file"); 021 else { 022 String n = args[i + 1]; 023 v = Common.getVersionFromIGName(v, n); 024 } 025 } 026 } 027 } else if ("1.0".equals(v)) { 028 v = "1.0"; 029 } else if ("1.4".equals(v)) { 030 v = "1.4"; 031 } else if ("3.0".equals(v)) { 032 v = "3.0"; 033 } else if ("4.0".equals(v)) { 034 v = "4.0"; 035 } else if (v.startsWith(Constants.VERSION)) { 036 v = "current"; 037 } 038 return v; 039 } 040 041 /** 042 * Evaluates the current implementation guide file name and sets the current version accordingly. 043 * <p> 044 * If igFileName is not one of the known patterns, will return whatever value is passed in as default. 045 * 046 * @param defaultValue Version to return if no associated version can be determined from passed in igFileName 047 * @param igFileName Name of the implementation guide 048 * @return 049 */ 050 public static String getVersionFromIGName(String defaultValue, String igFileName) { 051 if (igFileName.equals("hl7.fhir.core")) { 052 defaultValue = "current"; 053 } else if (igFileName.startsWith("hl7.fhir.core#")) { 054 defaultValue = VersionUtilities.getCurrentPackageVersion(igFileName.substring(14)); 055 } else if (igFileName.startsWith("hl7.fhir.r2.core#") || igFileName.equals("hl7.fhir.r2.core")) { 056 defaultValue = "1.0"; 057 } else if (igFileName.startsWith("hl7.fhir.r2b.core#") || igFileName.equals("hl7.fhir.r2b.core")) { 058 defaultValue = "1.4"; 059 } else if (igFileName.startsWith("hl7.fhir.r3.core#") || igFileName.equals("hl7.fhir.r3.core")) { 060 defaultValue = "3.0"; 061 } else if (igFileName.startsWith("hl7.fhir.r4.core#") || igFileName.equals("hl7.fhir.r4.core")) { 062 defaultValue = "4.0"; 063 } else if (igFileName.startsWith("hl7.fhir.r5.core#") || igFileName.equals("hl7.fhir.r5.core")) { 064 defaultValue = "current"; 065 } 066 return defaultValue; 067 } 068 069 /** 070 * Triggers the validation engine tests to run. 071 */ 072 public static void runValidationEngineTests() { 073 try { 074 Class<?> clazz = Class.forName("org.hl7.fhir.validation.r5.tests.ValidationEngineTests"); 075 clazz.getMethod("execute").invoke(clazz); 076 } catch (Exception e) { 077 e.printStackTrace(); 078 } 079 } 080 081 /** 082 * Default validation engine will point to "http://tx.fhir.org" terminology server. 083 */ 084 public static ValidationEngine getValidationEngine(String version, String definitions, String txLog, TimeTracker tt) throws Exception { 085 return getValidationEngine(version, DEFAULT_TX_SERVER, definitions, txLog, tt); 086 } 087 088 public static ValidationEngine getValidationEngine(String version, String txServer, String definitions, String txLog, TimeTracker tt) throws Exception { 089 System.out.println("Loading (v = " + version + ", tx server -> " + txServer + ")"); 090 ValidationEngine ve = new ValidationEngine.ValidationEngineBuilder().withVersion(version).withTimeTracker(tt).withUserAgent("fhir/validator").fromSource(definitions); 091 ve.connectToTSServer(txServer, txLog, FhirPublication.fromCode(version)); 092 return ve; 093 } 094 095 public static boolean isNetworkPath(String path) { 096 return path.startsWith("https:") || path.startsWith("http:"); 097 } 098 099 public static boolean isWildcardPath(String name) { 100 return name.contains("*"); 101 } 102 103}