Search in sources :

Example 41 with NpmPackage

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

the class PackageVisitor method processPackage.

private void processPackage(String pid, String v) throws IOException {
    NpmPackage npm = null;
    String fv = null;
    try {
        npm = pcm.loadPackage(pid, v);
        fv = npm.fhirVersion();
    } catch (Throwable e) {
        System.out.println("Unable to process: " + pid + "#" + v + ": " + e.getMessage());
    }
    int c = 0;
    if (fv != null && (versions.isEmpty() || versions.contains(fv))) {
        for (String type : resourceTypes) {
            for (String s : npm.listResources(type)) {
                c++;
                processor.processResource(pid + "#" + v, fv, type, TextFile.streamToBytes(npm.load("package", s)));
            }
        }
    }
    System.out.println("Processed: " + pid + "#" + v + ": " + c + " resources");
}
Also used : NpmPackage(org.hl7.fhir.utilities.npm.NpmPackage)

Example 42 with NpmPackage

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

the class TestingUtilities method getWorkerContext.

public static SimpleWorkerContext getWorkerContext(NpmPackage npmPackage) throws Exception {
    SimpleWorkerContext swc = new SimpleWorkerContext.SimpleWorkerContextBuilder().withAllowLoadingDuplicates(true).withUserAgent(TestConstants.USER_AGENT).withTerminologyCachePath(TestConstants.TX_CACHE).fromPackage(npmPackage);
    TerminologyCache.setCacheErrors(true);
    return swc;
}
Also used : SimpleWorkerContext(org.hl7.fhir.r5.context.SimpleWorkerContext)

Example 43 with NpmPackage

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

the class FilesystemPackageCacheManager method loadPackageFromCacheOnly.

/**
 * Load the identified package from the cache - if it exists
 * <p>
 * This is for special purpose only (testing, control over speed of loading).
 * Generally, use the loadPackage method
 *
 * @param id
 * @param version
 * @return
 * @throws IOException
 */
@Override
public NpmPackage loadPackageFromCacheOnly(String id, String version) throws IOException {
    if (!Utilities.noString(version) && version.startsWith("file:")) {
        return loadPackageFromFile(id, version.substring(5));
    }
    for (NpmPackage p : temporaryPackages) {
        if (p.name().equals(id) && ("current".equals(version) || "dev".equals(version) || p.version().equals(version))) {
            return p;
        }
        if (p.name().equals(id) && Utilities.noString(version)) {
            return p;
        }
    }
    String foundPackage = null;
    String foundVersion = null;
    for (String f : reverseSorted(new File(cacheFolder).list())) {
        File cf = new File(Utilities.path(cacheFolder, f));
        if (cf.isDirectory()) {
            if (f.equals(id + "#" + version) || (Utilities.noString(version) && f.startsWith(id + "#"))) {
                return loadPackageInfo(Utilities.path(cacheFolder, f));
            }
            if (version != null && version.endsWith(".x") && f.contains("#")) {
                String[] parts = f.split("#");
                if (parts[0].equals(id) && VersionUtilities.isMajMinOrLaterPatch((foundVersion != null ? foundVersion : version), parts[1])) {
                    foundVersion = parts[1];
                    foundPackage = f;
                }
            }
        }
    }
    if (foundPackage != null) {
        return loadPackageInfo(Utilities.path(cacheFolder, foundPackage));
    }
    if ("dev".equals(version))
        return loadPackageFromCacheOnly(id, "current");
    else
        return null;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 44 with NpmPackage

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

the class NpmPackage method fromFolder.

/**
 * Factory method that parses a package from an extracted folder
 */
public static NpmPackage fromFolder(String path) throws IOException {
    NpmPackage res = new NpmPackage();
    res.loadFiles(path, new File(path));
    res.checkIndexed(path);
    return res;
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 45 with NpmPackage

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

the class NpmPackage method fromZip.

public static NpmPackage fromZip(InputStream stream, boolean dropRootFolder, String desc) throws IOException {
    NpmPackage res = new NpmPackage();
    ZipInputStream zip = new ZipInputStream(stream);
    ZipEntry ze;
    while ((ze = zip.getNextEntry()) != null) {
        int size;
        byte[] buffer = new byte[2048];
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(bytes, buffer.length);
        while ((size = zip.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
        if (bytes.size() > 0) {
            if (dropRootFolder) {
                res.loadFile(ze.getName().substring(ze.getName().indexOf("/") + 1), bytes.toByteArray());
            } else {
                res.loadFile(ze.getName(), bytes.toByteArray());
            }
        }
        zip.closeEntry();
    }
    zip.close();
    try {
        res.npm = JsonTrackingParser.parseJson(res.folders.get("package").fetchFile("package.json"));
    } catch (Exception e) {
        throw new IOException("Error parsing " + (desc == null ? "" : desc + "#") + "package/package.json: " + e.getMessage(), e);
    }
    res.checkIndexed(desc);
    return res;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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