Search in sources :

Example 86 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class IgLoader method loadIgSource.

/**
 * @param src can be one of the following:
 *      <br> - a canonical url for an ig - this will be converted to a package id and loaded into the cache
 *      <br> - a package id for an ig - this will be loaded into the cache
 *      <br> - a direct reference to a package ("package.tgz") - this will be extracted by the cache manager, but not put in the cache
 *      <br> - a folder containing resources - these will be loaded directly
 * @param recursive if true and src resolves to a folder, recursively find and load IgSources from that directory
 * @param explore should be true if we're trying to load an -ig parameter, and false if we're loading source
 *
 * @return
 * @throws FHIRException
 * @throws IOException
 */
public Map<String, byte[]> loadIgSource(String src, boolean recursive, boolean explore) 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 = explore ? getPackageCacheManager().getPackageId(src) : null;
        if (!Utilities.noString(pid))
            return fetchByPackage(pid + (v == null ? "" : "#" + v));
        else
            return fetchFromUrl(src + (v == null ? "" : "|" + v), explore);
    }
    File f = new File(Utilities.path(src));
    if (f.exists()) {
        if (f.isDirectory() && new File(Utilities.path(src, "package.tgz")).exists())
            return loadPackage(new FileInputStream(Utilities.path(src, "package.tgz")), Utilities.path(src, "package.tgz"));
        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"))
            return loadPackage(new FileInputStream(src), src);
        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")) {
        return fetchByPackage(src);
    }
    throw new FHIRException("Unable to find/resolve/read " + (explore ? "-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 87 with Package

use of org.hl7.fhir.utilities.graphql.Package 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 88 with Package

use of org.hl7.fhir.utilities.graphql.Package 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)

Example 89 with Package

use of org.hl7.fhir.utilities.graphql.Package in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method loadFromPackageInt.

public int loadFromPackageInt(NpmPackage pi, IContextResourceLoader loader, String... types) throws IOException, FHIRException {
    int t = 0;
    if (progress) {
        System.out.println("Load Package " + pi.name() + "#" + pi.version());
    }
    if (loadedPackages.contains(pi.id() + "#" + pi.version())) {
        return 0;
    }
    loadedPackages.add(pi.id() + "#" + pi.version());
    if ((types == null || types.length == 0) && loader != null) {
        types = loader.getTypes();
    }
    if (VersionUtilities.isR2Ver(pi.fhirVersion()) || !pi.canLazyLoad()) {
        // can't lazy load R2 because of valueset/codesystem implementation
        if (types.length == 0) {
            types = new String[] { "StructureDefinition", "ValueSet", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem" };
        }
        for (String s : pi.listResources(types)) {
            try {
                loadDefinitionItem(s, pi.load("package", s), loader, null, new PackageVersion(pi.id(), pi.version(), pi.dateAsDate()));
                t++;
            } catch (Exception e) {
                throw new FHIRException(formatMessage(I18nConstants.ERROR_READING__FROM_PACKAGE__, s, pi.name(), pi.version(), e.getMessage()), e);
            }
        }
    } else {
        if (types.length == 0) {
            types = new String[] { "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter", "OperationDefinition", "Questionnaire", "ConceptMap", "StructureMap", "NamingSystem", "Measures" };
        }
        for (PackageResourceInformation pri : pi.listIndexedResources(types)) {
            if (!pri.getFilename().contains("ig-r4")) {
                try {
                    registerResourceFromPackage(new PackageResourceLoader(pri, loader), new PackageVersion(pi.id(), pi.version(), pi.dateAsDate()));
                    t++;
                } catch (FHIRException e) {
                    throw new FHIRException(formatMessage(I18nConstants.ERROR_READING__FROM_PACKAGE__, pri.getFilename(), pi.name(), pi.version(), e.getMessage()), e);
                }
            }
        }
    }
    for (String s : pi.list("other")) {
        binaries.put(s, TextFile.streamToBytes(pi.load("other", s)));
    }
    if (version == null) {
        version = pi.version();
    }
    return t;
}
Also used : PackageResourceInformation(org.hl7.fhir.utilities.npm.NpmPackage.PackageResourceInformation) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) DataFormatException(ca.uhn.fhir.parser.DataFormatException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 90 with Package

use of org.hl7.fhir.utilities.graphql.Package in project CRD by HL7-DaVinci.

the class CqlRule method build.

private void build(HashMap<String, byte[]> cqlFiles, HashMap<String, byte[]> jsonElmFiles, HashMap<String, byte[]> xmlElmFiles) {
    // build a list of all of the CQL Libraries
    List<CqlRule.CqlLibrary> cqlLibraries = new ArrayList<>();
    for (String fileName : cqlFiles.keySet()) {
        logger.debug("CqlRule: file: " + fileName);
        CqlRule.CqlLibrary cqlLibrary = new CqlRule.CqlLibrary();
        cqlLibrary.cql = cqlFiles.get(fileName);
        // only add those that are the right fhir version
        String fhirVersionFromCql = getFhirVersionFromCqlFile(cqlLibrary.cql);
        // last character of fhirVersion ("R4" => "4") and first character of FHIR version from file ("3.0.0" => "3")
        if (fhirVersion.substring(fhirVersion.length() - 1).equalsIgnoreCase(fhirVersionFromCql.substring(0, 1))) {
            String fileNameWithoutExtension = fileName.substring(0, fileName.length() - 4);
            String xmlElmName = fileNameWithoutExtension + ".xml";
            if (xmlElmFiles.containsKey(xmlElmName)) {
                cqlLibrary.xlmElm = true;
                cqlLibrary.elm = xmlElmFiles.get(xmlElmName);
            }
            String jsonElmName = fileNameWithoutExtension + ".json";
            if (jsonElmFiles.containsKey(jsonElmName)) {
                cqlLibrary.xlmElm = false;
                cqlLibrary.elm = jsonElmFiles.get(jsonElmName);
            }
            cqlLibraries.add(cqlLibrary);
        }
    }
    precompiled = false;
    for (CqlLibrary cqlLibrary : cqlLibraries) {
        if (precompiled) {
            if (cqlLibrary.elm == null) {
                throw new RuntimeException("Package indicated CQL was precompiled, but elm xml missing.");
            }
        // TODO: need to set rulePackage.elmLibraries and mainCqlLibraryId
        } else {
            InputStream cqlStream = new ByteArrayInputStream(cqlLibrary.cql);
            VersionedIdentifier id = getIdFromCqlFile(cqlLibrary.cql);
            String fhirVersionFromFile = getFhirVersionFromCqlFile(cqlLibrary.cql);
            logger.info("CqlRule::Constructor() add id: " + id.getId() + ", fhir version: " + fhirVersionFromFile);
            if (rawCqlLibraries.containsKey(fhirVersionFromFile)) {
                // logger.info("CqlRule::Constructor() add rawCqlLibraries add: " + id.getId());
                rawCqlLibraries.get(fhirVersionFromFile).put(id, cqlStream);
            } else {
                HashMap<VersionedIdentifier, InputStream> map = new HashMap<>();
                map.put(id, cqlStream);
                // logger.info("CqlRule::Constructor() add rawCqlLibraries new: " + id.getId());
                rawCqlLibraries.put(fhirVersionFromFile, map);
            }
            if (id.getId().equals(mainCqlLibraryName)) {
                // logger.info("CqlRule::Constructor() add mainCqlLibraryId: " + id.getId());
                mainCqlLibraryId.put(fhirVersionFromFile, id);
            }
        }
    }
}
Also used : VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList)

Aggregations

File (java.io.File)25 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 TextFile (org.hl7.fhir.utilities.TextFile)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)15 JsonObject (com.google.gson.JsonObject)12 FileNotFoundException (java.io.FileNotFoundException)11 FileInputStream (java.io.FileInputStream)9 IniFile (org.hl7.fhir.utilities.IniFile)9 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)9 ParseException (java.text.ParseException)8 HashMap (java.util.HashMap)7 Gson (com.google.gson.Gson)6 GsonBuilder (com.google.gson.GsonBuilder)6 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 JsonElement (com.google.gson.JsonElement)5 FileOutputStream (java.io.FileOutputStream)5 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)5 CSFile (org.hl7.fhir.utilities.CSFile)5