Search in sources :

Example 1 with CliContext

use of org.hl7.fhir.validation.cli.model.CliContext in project org.hl7.fhir.core by hapifhir.

the class ValidationService method determineVersion.

public String determineVersion(CliContext cliContext, String sessionId) throws Exception {
    if (cliContext.getMode() != EngineMode.VALIDATION) {
        return "current";
    }
    System.out.println("Scanning for versions (no -version parameter):");
    VersionSourceInformation versions = scanForVersions(cliContext);
    for (String s : versions.getReport()) {
        if (!s.equals("(nothing found)")) {
            System.out.println("  " + s);
        }
    }
    if (versions.isEmpty()) {
        System.out.println("  No Version Info found: Using Default version '" + VersionUtilities.CURRENT_DEFAULT_VERSION + "'");
        return VersionUtilities.CURRENT_DEFAULT_FULL_VERSION;
    }
    if (versions.size() == 1) {
        System.out.println("-> use version " + versions.version());
        return versions.version();
    }
    throw new Exception("-> Multiple versions found. Specify a particular version using the -version parameter");
}
Also used : VersionSourceInformation(org.hl7.fhir.validation.cli.utils.VersionSourceInformation) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with CliContext

use of org.hl7.fhir.validation.cli.model.CliContext in project org.hl7.fhir.core by hapifhir.

the class ValidationService method transform.

public void transform(CliContext cliContext, ValidationEngine validator) throws Exception {
    if (cliContext.getSources().size() > 1)
        throw new Exception("Can only have one source when doing a transform (found " + cliContext.getSources() + ")");
    if (cliContext.getTxServer() == null)
        throw new Exception("Must provide a terminology server when doing a transform");
    if (cliContext.getMap() == null)
        throw new Exception("Must provide a map when doing a transform");
    try {
        List<StructureDefinition> structures = validator.getContext().allStructures();
        for (StructureDefinition sd : structures) {
            if (!sd.hasSnapshot()) {
                if (sd.getKind() != null && sd.getKind() == StructureDefinitionKind.LOGICAL) {
                    validator.getContext().generateSnapshot(sd, true);
                } else {
                    validator.getContext().generateSnapshot(sd, false);
                }
            }
        }
        validator.setMapLog(cliContext.getMapLog());
        org.hl7.fhir.r5.elementmodel.Element r = validator.transform(cliContext.getSources().get(0), cliContext.getMap());
        System.out.println(" ...success");
        if (cliContext.getOutput() != null) {
            FileOutputStream s = new FileOutputStream(cliContext.getOutput());
            if (cliContext.getOutput() != null && cliContext.getOutput().endsWith(".json"))
                new org.hl7.fhir.r5.elementmodel.JsonParser(validator.getContext()).compose(r, s, IParser.OutputStyle.PRETTY, null);
            else
                new org.hl7.fhir.r5.elementmodel.XmlParser(validator.getContext()).compose(r, s, IParser.OutputStyle.PRETTY, null);
            s.close();
        }
    } catch (Exception e) {
        System.out.println(" ...Failure: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 3 with CliContext

use of org.hl7.fhir.validation.cli.model.CliContext in project org.hl7.fhir.core by hapifhir.

the class ValidationService method generateSpreadsheet.

public void generateSpreadsheet(CliContext cliContext, ValidationEngine validator) throws Exception {
    CanonicalResource cr = validator.loadCanonicalResource(cliContext.getSources().get(0), cliContext.getSv());
    boolean ok = true;
    if (cr instanceof StructureDefinition) {
        new StructureDefinitionSpreadsheetGenerator(validator.getContext(), false, false).renderStructureDefinition((StructureDefinition) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof CodeSystem) {
        new CodeSystemSpreadsheetGenerator(validator.getContext()).renderCodeSystem((CodeSystem) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof ValueSet) {
        new ValueSetSpreadsheetGenerator(validator.getContext()).renderValueSet((ValueSet) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else if (cr instanceof ConceptMap) {
        new ConceptMapSpreadsheetGenerator(validator.getContext()).renderConceptMap((ConceptMap) cr).finish(new FileOutputStream(cliContext.getOutput()));
    } else {
        ok = false;
        System.out.println(" ...Unable to generate spreadsheet for " + cliContext.getSources().get(0) + ": no way to generate a spreadsheet for a " + cr.fhirType());
    }
    if (ok) {
        System.out.println(" ...generated spreadsheet successfully");
    }
}
Also used : StructureDefinitionSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.StructureDefinitionSpreadsheetGenerator) FileOutputStream(java.io.FileOutputStream) CodeSystemSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.CodeSystemSpreadsheetGenerator) ValueSetSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.ValueSetSpreadsheetGenerator) ConceptMapSpreadsheetGenerator(org.hl7.fhir.r5.renderers.spreadsheets.ConceptMapSpreadsheetGenerator)

Example 4 with CliContext

use of org.hl7.fhir.validation.cli.model.CliContext in project org.hl7.fhir.core by hapifhir.

the class ValidatorCli method main.

public static void main(String[] args) throws Exception {
    TimeTracker tt = new TimeTracker();
    TimeTracker.Session tts = tt.start("Loading");
    args = preProcessArgs(args);
    Display.displayVersion();
    Display.displaySystemInfo();
    if (Params.hasParam(args, Params.PROXY)) {
        assert Params.getParam(args, Params.PROXY) != null : "PROXY arg passed in was NULL";
        String[] p = Params.getParam(args, Params.PROXY).split(":");
        System.setProperty(HTTP_PROXY_HOST, p[0]);
        System.setProperty(HTTP_PROXY_PORT, p[1]);
    }
    if (Params.hasParam(args, Params.PROXY_AUTH)) {
        assert Params.getParam(args, Params.PROXY) != null : "Cannot set PROXY_AUTH without setting PROXY...";
        assert Params.getParam(args, Params.PROXY_AUTH) != null : "PROXY_AUTH arg passed in was NULL...";
        String[] p = Params.getParam(args, Params.PROXY_AUTH).split(":");
        String authUser = p[0];
        String authPass = p[1];
        /*
       * For authentication, use java.net.Authenticator to set proxy's configuration and set the system properties
       * http.proxyUser and http.proxyPassword
       */
        Authenticator.setDefault(new Authenticator() {

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(authUser, authPass.toCharArray());
            }
        });
        System.setProperty(HTTP_PROXY_USER, authUser);
        System.setProperty(HTTP_PROXY_PASS, authPass);
        System.setProperty(JAVA_USE_SYSTEM_PROXIES, "true");
        /*
       * For Java 1.8 and higher you must set
       * -Djdk.http.auth.tunneling.disabledSchemes=
       * to make proxies with Basic Authorization working with https along with Authenticator
       */
        System.setProperty(JAVA_DISABLED_TUNNELING_SCHEMES, "");
        System.setProperty(JAVA_DISABLED_PROXY_SCHEMES, "");
    }
    CliContext cliContext = Params.loadCliContext(args);
    if (Params.hasParam(args, Params.TEST)) {
        Common.runValidationEngineTests();
    } else if (shouldDisplayHelpToUser(args)) {
        Display.displayHelpDetails();
    } else if (Params.hasParam(args, Params.COMPARE)) {
        if (destinationDirectoryValid(Params.getParam(args, Params.DESTINATION))) {
            doLeftRightComparison(args, cliContext, tt);
        }
    } else {
        Display.printCliArgumentsAndInfo(args);
        doValidation(tt, tts, cliContext);
    }
}
Also used : TimeTracker(org.hl7.fhir.utilities.TimeTracker) CliContext(org.hl7.fhir.validation.cli.model.CliContext) Authenticator(java.net.Authenticator) PasswordAuthentication(java.net.PasswordAuthentication)

Example 5 with CliContext

use of org.hl7.fhir.validation.cli.model.CliContext in project org.hl7.fhir.core by hapifhir.

the class Params method loadCliContext.

/**
 * TODO Don't do this all in one for loop. Use the above methods.
 */
public static CliContext loadCliContext(String[] args) throws Exception {
    CliContext cliContext = new CliContext();
    // load the parameters - so order doesn't matter
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals(VERSION)) {
            cliContext.setSv(VersionUtilities.getCurrentPackageVersion(args[++i]));
        } else if (args[i].equals(OUTPUT)) {
            if (i + 1 == args.length)
                throw new Error("Specified -output without indicating output file");
            else
                cliContext.setOutput(args[++i]);
        } else if (args[i].equals(HTML_OUTPUT)) {
            if (i + 1 == args.length)
                throw new Error("Specified -html-output without indicating output file");
            else
                cliContext.setHtmlOutput(args[++i]);
        } else if (args[i].equals(PROXY)) {
            // ignore next parameter
            i++;
        } else if (args[i].equals(PROXY_AUTH)) {
            i++;
        } else if (args[i].equals(PROFILE)) {
            String p = null;
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating profile source");
            } else {
                p = args[++i];
                cliContext.addProfile(p);
            }
        } else if (args[i].equals(BUNDLE)) {
            String p = null;
            String r = null;
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating bundle rule ");
            } else {
                r = args[++i];
            }
            if (i + 1 == args.length) {
                throw new Error("Specified -profile without indicating profile source");
            } else {
                p = args[++i];
            }
            cliContext.getBundleValidationRules().add(new BundleValidationRule(r, p));
        } else if (args[i].equals(QUESTIONNAIRE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -questionnaire without indicating questionnaire mode");
            else {
                String q = args[++i];
                cliContext.setQuestionnaireMode(QuestionnaireMode.fromCode(q));
            }
        } else if (args[i].equals(LEVEL)) {
            if (i + 1 == args.length)
                throw new Error("Specified -level without indicating level mode");
            else {
                String q = args[++i];
                cliContext.setLevel(ValidationLevel.fromCode(q));
            }
        } else if (args[i].equals(NATIVE)) {
            cliContext.setDoNative(true);
        } else if (args[i].equals(ASSUME_VALID_REST_REF)) {
            cliContext.setAssumeValidRestReferences(true);
        } else if (args[i].equals(DEBUG)) {
            cliContext.setDoDebug(true);
        } else if (args[i].equals(SCT)) {
            cliContext.setSnomedCT(args[++i]);
        } else if (args[i].equals(RECURSE)) {
            cliContext.setRecursive(true);
        } else if (args[i].equals(SHOW_MESSAGES_FROM_REFERENCES)) {
            cliContext.setShowMessagesFromReferences(true);
        } else if (args[i].equals(LOCALE)) {
            if (i + 1 == args.length) {
                throw new Error("Specified -locale without indicating locale");
            } else {
                cliContext.setLocale(new Locale(args[++i]));
            }
        } else if (args[i].equals(EXTENSIONS)) {
            cliContext.getExtensions().add(args[++i]);
        } else if (args[i].equals(NO_INTERNAL_CACHING)) {
            cliContext.setNoInternalCaching(true);
        } else if (args[i].equals(NO_EXTENSIBLE_BINDING_WARNINGS)) {
            cliContext.setNoExtensibleBindingMessages(true);
        } else if (args[i].equals(NO_UNICODE_BIDI_CONTROL_CHARS)) {
            cliContext.setNoUnicodeBiDiControlChars(true);
        } else if (args[i].equals(NO_INVARIANTS)) {
            cliContext.setNoInvariants(true);
        } else if (args[i].equals(WANT_INVARIANTS_IN_MESSAGES)) {
            cliContext.setWantInvariantsInMessages(true);
        } else if (args[i].equals(HINT_ABOUT_NON_MUST_SUPPORT)) {
            cliContext.setHintAboutNonMustSupport(true);
        } else if (args[i].equals(TO_VERSION)) {
            cliContext.setTargetVer(args[++i]);
            cliContext.setMode(EngineMode.VERSION);
        } else if (args[i].equals(DO_NATIVE)) {
            cliContext.setCanDoNative(true);
        } else if (args[i].equals(NO_NATIVE)) {
            cliContext.setCanDoNative(false);
        } else if (args[i].equals(TRANSFORM)) {
            cliContext.setMap(args[++i]);
            cliContext.setMode(EngineMode.TRANSFORM);
        } else if (args[i].equals(COMPILE)) {
            cliContext.setMap(args[++i]);
            cliContext.setMode(EngineMode.COMPILE);
        } else if (args[i].equals(NARRATIVE)) {
            cliContext.setMode(EngineMode.NARRATIVE);
        } else if (args[i].equals(SPREADSHEET)) {
            cliContext.setMode(EngineMode.SPREADSHEET);
        } else if (args[i].equals(SNAPSHOT)) {
            cliContext.setMode(EngineMode.SNAPSHOT);
        } else if (args[i].equals(SECURITY_CHECKS)) {
            cliContext.setSecurityChecks(true);
        } else if (args[i].equals(CRUMB_TRAIL)) {
            cliContext.setCrumbTrails(true);
        } else if (args[i].equals(VERBOSE)) {
            cliContext.setCrumbTrails(true);
        } else if (args[i].equals(ALLOW_EXAMPLE_URLS)) {
            String bl = args[++i];
            if ("true".equals(bl)) {
                cliContext.setAllowExampleUrls(true);
            } else if ("false".equals(bl)) {
                cliContext.setAllowExampleUrls(false);
            } else {
                throw new Error("Value for " + ALLOW_EXAMPLE_URLS + " not understood: " + bl);
            }
        } else if (args[i].equals(SHOW_TIMES)) {
            cliContext.setShowTimes(true);
        } else if (args[i].equals(OUTPUT_STYLE)) {
            cliContext.setOutputStyle(args[++i]);
        } else if (args[i].equals(SCAN)) {
            cliContext.setMode(EngineMode.SCAN);
        } else if (args[i].equals(TERMINOLOGY)) {
            if (i + 1 == args.length)
                throw new Error("Specified -tx without indicating terminology server");
            else
                cliContext.setTxServer("n/a".equals(args[++i]) ? null : args[i]);
        } else if (args[i].equals(TERMINOLOGY_LOG)) {
            if (i + 1 == args.length)
                throw new Error("Specified -txLog without indicating file");
            else
                cliContext.setTxLog(args[++i]);
        } else if (args[i].equals(TERMINOLOGY_CACHE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -txCache without indicating file");
            else
                cliContext.setTxCache(args[++i]);
        } else if (args[i].equals(LOG)) {
            if (i + 1 == args.length)
                throw new Error("Specified -log without indicating file");
            else
                cliContext.setMapLog(args[++i]);
        } else if (args[i].equals(LANGUAGE)) {
            if (i + 1 == args.length)
                throw new Error("Specified -language without indicating language");
            else
                cliContext.setLang(args[++i]);
        } else if (args[i].equals(IMPLEMENTATION_GUIDE) || args[i].equals(DEFINITION)) {
            if (i + 1 == args.length)
                throw new Error("Specified " + args[i] + " without indicating ig file");
            else {
                String s = args[++i];
                String version = Common.getVersionFromIGName(null, s);
                if (version == null) {
                    cliContext.addIg(s);
                } else {
                    cliContext.setSv(version);
                }
            }
        } else if (args[i].equals(MAP)) {
            if (cliContext.getMap() == null) {
                if (i + 1 == args.length)
                    throw new Error("Specified -map without indicating map file");
                else
                    cliContext.setMap(args[++i]);
            } else {
                throw new Exception("Can only nominate a single -map parameter");
            }
        } else if (args[i].startsWith(X)) {
            i++;
        } else if (args[i].equals(CONVERT)) {
            cliContext.setMode(EngineMode.CONVERT);
        } else if (args[i].equals(FHIRPATH)) {
            cliContext.setMode(EngineMode.FHIRPATH);
            if (cliContext.getFhirpath() == null)
                if (i + 1 == args.length)
                    throw new Error("Specified -fhirpath without indicating a FHIRPath expression");
                else
                    cliContext.setFhirpath(args[++i]);
            else
                throw new Exception("Can only nominate a single -fhirpath parameter");
        } else {
            cliContext.addSource(args[i]);
        }
    }
    return cliContext;
}
Also used : Locale(java.util.Locale) BundleValidationRule(org.hl7.fhir.r5.utils.validation.BundleValidationRule) CliContext(org.hl7.fhir.validation.cli.model.CliContext)

Aggregations

FileOutputStream (java.io.FileOutputStream)3 IOException (java.io.IOException)3 FHIRException (org.hl7.fhir.exceptions.FHIRException)3 CliContext (org.hl7.fhir.validation.cli.model.CliContext)3 ArrayList (java.util.ArrayList)2 IgLoader (org.hl7.fhir.validation.IgLoader)2 ValidationEngine (org.hl7.fhir.validation.ValidationEngine)2 VersionSourceInformation (org.hl7.fhir.validation.cli.utils.VersionSourceInformation)2 PrintStream (java.io.PrintStream)1 MemoryMXBean (java.lang.management.MemoryMXBean)1 Authenticator (java.net.Authenticator)1 PasswordAuthentication (java.net.PasswordAuthentication)1 Locale (java.util.Locale)1 SystemOutLoggingService (org.hl7.fhir.r5.context.SystemOutLoggingService)1 JsonParser (org.hl7.fhir.r5.formats.JsonParser)1 XmlParser (org.hl7.fhir.r5.formats.XmlParser)1 CodeSystemSpreadsheetGenerator (org.hl7.fhir.r5.renderers.spreadsheets.CodeSystemSpreadsheetGenerator)1 ConceptMapSpreadsheetGenerator (org.hl7.fhir.r5.renderers.spreadsheets.ConceptMapSpreadsheetGenerator)1 StructureDefinitionSpreadsheetGenerator (org.hl7.fhir.r5.renderers.spreadsheets.StructureDefinitionSpreadsheetGenerator)1 ValueSetSpreadsheetGenerator (org.hl7.fhir.r5.renderers.spreadsheets.ValueSetSpreadsheetGenerator)1