Search in sources :

Example 1 with VersionSourceInformation

use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.

the class IgLoader method scanForVersions.

public void scanForVersions(List<String> sources, VersionSourceInformation versions) throws FHIRException, IOException {
    List<String> refs = new ArrayList<String>();
    ValidatorUtils.parseSources(sources, refs, context);
    for (String ref : refs) {
        Content cnt = loadContent(ref, "validate", false);
        String s = TextFile.bytesToString(cnt.focus);
        if (s.contains("http://hl7.org/fhir/3.0")) {
            versions.see("3.0", "Profile in " + ref);
        }
        if (s.contains("http://hl7.org/fhir/1.0")) {
            versions.see("1.0", "Profile in " + ref);
        }
        if (s.contains("http://hl7.org/fhir/4.0")) {
            versions.see("4.0", "Profile in " + ref);
        }
        if (s.contains("http://hl7.org/fhir/1.4")) {
            versions.see("1.4", "Profile in " + ref);
        }
        try {
            if (s.startsWith("{")) {
                JsonObject json = JsonTrackingParser.parse(s, null);
                if (json.has("fhirVersion")) {
                    versions.see(VersionUtilities.getMajMin(JSONUtil.str(json, "fhirVersion")), "fhirVersion in " + ref);
                }
            } else {
                Document doc = ValidatorUtils.parseXml(cnt.focus);
                String v = XMLUtil.getNamedChildValue(doc.getDocumentElement(), "fhirVersion");
                if (v != null) {
                    versions.see(VersionUtilities.getMajMin(v), "fhirVersion in " + ref);
                }
            }
        } catch (Exception e) {
        // nothing
        }
    }
}
Also used : ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Document(org.w3c.dom.Document) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 2 with VersionSourceInformation

use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation 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 3 with VersionSourceInformation

use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.

the class ValidationService method scanForVersions.

public VersionSourceInformation scanForVersions(CliContext cliContext) throws Exception {
    VersionSourceInformation versions = new VersionSourceInformation();
    IgLoader igLoader = new IgLoader(new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION), new SimpleWorkerContext.SimpleWorkerContextBuilder().fromNothing(), null);
    for (String src : cliContext.getIgs()) {
        igLoader.scanForIgVersion(src, cliContext.isRecursive(), versions);
    }
    igLoader.scanForVersions(cliContext.getSources(), versions);
    return versions;
}
Also used : IgLoader(org.hl7.fhir.validation.IgLoader) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) VersionSourceInformation(org.hl7.fhir.validation.cli.utils.VersionSourceInformation)

Example 4 with VersionSourceInformation

use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.

the class IgLoader method loadIgSourceForVersion.

private Map<String, byte[]> loadIgSourceForVersion(String src, boolean recursive, boolean explore, VersionSourceInformation versions) throws FHIRException, IOException {
    if (Common.isNetworkPath(src)) {
        String v = null;
        if (src.contains("|")) {
            v = src.substring(src.indexOf("|") + 1);
            src = src.substring(0, src.indexOf("|"));
        }
        String pid = getPackageCacheManager().getPackageId(src);
        if (!Utilities.noString(pid)) {
            versions.see(fetchVersionByPackage(pid + (v == null ? "" : "#" + v)), "Package " + src);
            return null;
        } else {
            return fetchVersionFromUrl(src + (v == null ? "" : "|" + v), explore, versions);
        }
    }
    File f = new File(Utilities.path(src));
    if (f.exists()) {
        if (f.isDirectory() && new File(Utilities.path(src, "package.tgz")).exists()) {
            versions.see(loadPackageForVersion(new FileInputStream(Utilities.path(src, "package.tgz"))), "Package " + src);
            return null;
        }
        if (f.isDirectory() && new File(Utilities.path(src, "igpack.zip")).exists())
            return readZip(new FileInputStream(Utilities.path(src, "igpack.zip")));
        if (f.isDirectory() && new File(Utilities.path(src, "validator.pack")).exists())
            return readZip(new FileInputStream(Utilities.path(src, "validator.pack")));
        if (f.isDirectory())
            return scanDirectory(f, recursive);
        if (src.endsWith(".tgz")) {
            versions.see(loadPackageForVersion(new FileInputStream(src)), "Package " + src);
            return null;
        }
        if (src.endsWith(".pack"))
            return readZip(new FileInputStream(src));
        if (src.endsWith("igpack.zip"))
            return readZip(new FileInputStream(src));
        Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), TextFile.fileToBytes(f), src, true);
        if (fmt != null) {
            Map<String, byte[]> res = new HashMap<String, byte[]>();
            res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), TextFile.fileToBytesNCS(src));
            return res;
        }
    } else if ((src.matches(FilesystemPackageCacheManager.PACKAGE_REGEX) || src.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX)) && !src.endsWith(".zip") && !src.endsWith(".tgz")) {
        versions.see(fetchVersionByPackage(src), "Package " + src);
        return null;
    }
    throw new FHIRException("Unable to find/resolve/read -ig " + src);
}
Also used : HashMap(java.util.HashMap) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) Manager(org.hl7.fhir.r5.elementmodel.Manager) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 5 with VersionSourceInformation

use of org.hl7.fhir.validation.cli.utils.VersionSourceInformation in project org.hl7.fhir.core by hapifhir.

the class IgLoader method fetchVersionFromUrl.

private Map<String, byte[]> fetchVersionFromUrl(String src, boolean explore, VersionSourceInformation versions) throws FHIRException, IOException {
    if (src.endsWith(".tgz")) {
        versions.see(loadPackageForVersion(fetchFromUrlSpecific(src, false)), "From Package " + src);
        return null;
    }
    if (src.endsWith(".pack"))
        return readZip(fetchFromUrlSpecific(src, false));
    if (src.endsWith("igpack.zip"))
        return readZip(fetchFromUrlSpecific(src, false));
    InputStream stream = null;
    if (explore) {
        stream = fetchFromUrlSpecific(Utilities.pathURL(src, "package.tgz"), true);
        if (stream != null) {
            versions.see(loadPackageForVersion(stream), "From Package at " + src);
            return null;
        }
        // todo: these options are deprecated - remove once all IGs have been rebuilt post R4 technical correction
        stream = fetchFromUrlSpecific(Utilities.pathURL(src, "igpack.zip"), true);
        if (stream != null)
            return readZip(stream);
        stream = fetchFromUrlSpecific(Utilities.pathURL(src, "validator.pack"), true);
        if (stream != null)
            return readZip(stream);
        stream = fetchFromUrlSpecific(Utilities.pathURL(src, "validator.pack"), true);
    // // -----
    }
    // ok, having tried all that... now we'll just try to access it directly
    byte[] cnt;
    if (stream == null)
        cnt = fetchFromUrlSpecific(src, "application/json", true, null);
    else
        cnt = TextFile.streamToBytes(stream);
    Manager.FhirFormat fmt = ResourceChecker.checkIsResource(getContext(), isDebug(), cnt, src, true);
    if (fmt != null) {
        Map<String, byte[]> res = new HashMap<String, byte[]>();
        res.put(Utilities.changeFileExt(src, "." + fmt.getExtension()), cnt);
        return res;
    }
    String fn = Utilities.path("[tmp]", "fetch-resource-error-content.bin");
    TextFile.bytesToFile(cnt, fn);
    System.out.println("Error Fetching " + src);
    System.out.println("Some content was found, saved to " + fn);
    System.out.println("1st 100 bytes = " + presentForDebugging(cnt));
    throw new FHIRException("Unable to find/resolve/read " + (explore ? "-ig " : "") + src);
}
Also used : HashMap(java.util.HashMap) ZipInputStream(java.util.zip.ZipInputStream) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) Manager(org.hl7.fhir.r5.elementmodel.Manager) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)4 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)3 HashMap (java.util.HashMap)2 Manager (org.hl7.fhir.r5.elementmodel.Manager)2 VersionSourceInformation (org.hl7.fhir.validation.cli.utils.VersionSourceInformation)2 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 ZipInputStream (java.util.zip.ZipInputStream)1 IniFile (org.hl7.fhir.utilities.IniFile)1 TextFile (org.hl7.fhir.utilities.TextFile)1 IgLoader (org.hl7.fhir.validation.IgLoader)1 Document (org.w3c.dom.Document)1