Search in sources :

Example 46 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class PackageHacker method edit.

private void edit(String name) throws FileNotFoundException, IOException {
    File f = new File(name);
    if (!f.exists())
        throw new Error("Unable to find " + f.getAbsolutePath());
    NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
    System.out.println("Altering Package " + f.getAbsolutePath());
    System.out.println(nice(pck.getNpm()));
    change(pck.getNpm(), pck.getFolders().get("package").getContent());
    System.out.println("Revised Package");
    System.out.println("=======================");
    System.out.println(nice(pck.getNpm()));
    System.out.println("=======================");
    System.out.print("save? y/n: ");
    int r = System.in.read();
    if (r == 'y') {
        f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
        pck.save(new FileOutputStream(f));
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) FileInputStream(java.io.FileInputStream)

Example 47 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class IgLoader method loadIg.

/**
 * @param igs
 * @param binaries
 * @param src Source of the IG
 *
 * @param recursive
 * @throws IOException
 * @throws FHIRException
 *
 * @see IgLoader#loadIgSource(String, boolean, boolean) loadIgSource for detailed description of the src parameter
 */
public void loadIg(List<ImplementationGuide> igs, Map<String, byte[]> binaries, String src, boolean recursive) throws IOException, FHIRException {
    final String explicitFhirVersion;
    final String srcPackage;
    if (src.startsWith("[") && src.indexOf(']', 1) > 1) {
        explicitFhirVersion = src.substring(1, src.indexOf(']', 1));
        srcPackage = src.substring(src.indexOf(']', 1) + 1);
        if (!VersionUtilities.isSupportedVersion(explicitFhirVersion)) {
            throw new FHIRException("Unsupported FHIR Version: " + explicitFhirVersion + " valid versions are " + VersionUtilities.listSupportedVersions());
        }
    } else {
        explicitFhirVersion = null;
        srcPackage = src;
    }
    NpmPackage npm = srcPackage.matches(FilesystemPackageCacheManager.PACKAGE_VERSION_REGEX_OPT) && !new File(srcPackage).exists() ? getPackageCacheManager().loadPackage(srcPackage, null) : null;
    if (npm != null) {
        for (String s : npm.dependencies()) {
            if (!getContext().getLoadedPackages().contains(s)) {
                if (!VersionUtilities.isCorePackage(s)) {
                    loadIg(igs, binaries, s, false);
                }
            }
        }
        System.out.print("  Load " + srcPackage);
        if (!srcPackage.contains("#")) {
            System.out.print("#" + npm.version());
        }
        int count = getContext().loadFromPackage(npm, ValidatorUtils.loaderForVersion(npm.fhirVersion()));
        System.out.println(" - " + count + " resources (" + getContext().clock().milestone() + ")");
    } else {
        System.out.print("  Load " + srcPackage);
        String canonical = null;
        int count = 0;
        Map<String, byte[]> source = loadIgSource(srcPackage, recursive, true);
        String version = Constants.VERSION;
        if (getVersion() != null) {
            version = getVersion();
        }
        if (source.containsKey("version.info")) {
            version = readInfoVersion(source.get("version.info"));
        }
        if (explicitFhirVersion != null) {
            version = explicitFhirVersion;
        }
        for (Map.Entry<String, byte[]> t : source.entrySet()) {
            String fn = t.getKey();
            if (!exemptFile(fn)) {
                Resource r = loadFileWithErrorChecking(version, t, fn);
                if (r != null) {
                    count++;
                    getContext().cacheResource(r);
                    if (r instanceof ImplementationGuide) {
                        canonical = ((ImplementationGuide) r).getUrl();
                        igs.add((ImplementationGuide) r);
                        if (canonical.contains("/ImplementationGuide/")) {
                            Resource r2 = r.copy();
                            ((ImplementationGuide) r2).setUrl(canonical.substring(0, canonical.indexOf("/ImplementationGuide/")));
                            getContext().cacheResource(r2);
                        }
                    }
                }
            }
        }
        if (canonical != null) {
            ValidatorUtils.grabNatives(binaries, source, canonical);
        }
        System.out.println(" - " + count + " resources (" + getContext().clock().milestone() + ")");
    }
}
Also used : ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage) Resource(org.hl7.fhir.r5.model.Resource) FHIRException(org.hl7.fhir.exceptions.FHIRException) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage 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 49 with NpmPackage

use of org.hl7.fhir.utilities.npm.NpmPackage in project org.hl7.fhir.core by hapifhir.

the class SimpleWorkerContext method loadFromPackageAndDependenciesInt.

public int loadFromPackageAndDependenciesInt(NpmPackage pi, IContextResourceLoader loader, BasePackageCacheManager pcm, String path) throws IOException, FHIRException {
    int t = 0;
    for (String e : pi.dependencies()) {
        if (!loadedPackages.contains(e) && !VersionUtilities.isCorePackage(e)) {
            NpmPackage npm = pcm.loadPackage(e);
            if (!VersionUtilities.versionsMatch(version, npm.fhirVersion())) {
                System.out.println(formatMessage(I18nConstants.PACKAGE_VERSION_MISMATCH, e, version, npm.fhirVersion(), path));
            }
            t = t + loadFromPackageAndDependenciesInt(npm, loader.getNewLoader(npm), pcm, path + " -> " + npm.name() + "#" + npm.version());
        }
    }
    t = t + loadFromPackageInt(pi, loader, loader.getTypes());
    return t;
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage)

Aggregations

NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)34 File (java.io.File)15 FHIRException (org.hl7.fhir.exceptions.FHIRException)15 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)13 IOException (java.io.IOException)11 Test (org.junit.jupiter.api.Test)11 FileNotFoundException (java.io.FileNotFoundException)9 TextFile (org.hl7.fhir.utilities.TextFile)8 FileOutputStream (java.io.FileOutputStream)7 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 PackageResourceInformation (org.hl7.fhir.utilities.npm.NpmPackage.PackageResourceInformation)6 SimpleWorkerContext (org.hl7.fhir.r5.context.SimpleWorkerContext)4 IniFile (org.hl7.fhir.utilities.IniFile)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 URISyntaxException (java.net.URISyntaxException)3 ParseException (java.text.ParseException)3 NotImplementedException (org.apache.commons.lang3.NotImplementedException)3 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)3