Search in sources :

Example 81 with Package

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

the class NpmPackage method loadFiles.

public void loadFiles(String path, File source, String... exemptions) throws FileNotFoundException, IOException {
    this.npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json")));
    this.path = path;
    File dir = new File(path);
    for (File f : dir.listFiles()) {
        if (!isInternalExemptFile(f) && !Utilities.existsInList(f.getName(), exemptions)) {
            if (f.isDirectory()) {
                String d = f.getName();
                if (!d.equals("package")) {
                    d = Utilities.path("package", d);
                }
                NpmPackageFolder folder = this.new NpmPackageFolder(d);
                folder.folder = f;
                this.folders.put(d, folder);
                File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
                if (ij.exists()) {
                    try {
                        if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) {
                            indexFolder(folder.getName(), folder);
                        }
                    } catch (Exception e) {
                        throw new IOException("Error parsing " + ij.getAbsolutePath() + ": " + e.getMessage(), e);
                    }
                }
                loadSubFolders(dir.getAbsolutePath(), f);
            } else {
                NpmPackageFolder folder = this.new NpmPackageFolder(Utilities.path("package", "$root"));
                folder.folder = dir;
                this.folders.put(Utilities.path("package", "$root"), folder);
            }
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 82 with Package

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

the class NpmPackage method save.

public void save(File directory) throws IOException {
    File dir = new File(Utilities.path(directory.getAbsolutePath(), name()));
    if (!dir.exists()) {
        Utilities.createDirectory(dir.getAbsolutePath());
    } else {
        Utilities.clearDirectory(dir.getAbsolutePath());
    }
    for (NpmPackageFolder folder : folders.values()) {
        String n = folder.name;
        File pd = new File(Utilities.path(dir.getAbsolutePath(), n));
        if (!pd.exists()) {
            Utilities.createDirectory(pd.getAbsolutePath());
        }
        NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
        indexer.start();
        for (String s : folder.content.keySet()) {
            byte[] b = folder.content.get(s);
            indexer.seeFile(s, b);
            if (!s.equals(".index.json") && !s.equals("package.json")) {
                TextFile.bytesToFile(b, Utilities.path(dir.getAbsolutePath(), n, s));
            }
        }
        byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
        TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json"));
    }
    byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
    TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), "package", "package.json"));
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 83 with Package

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

Example 84 with Package

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

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

the class BaseTestingUtilities method loadTestResource.

public static String loadTestResource(String... paths) throws IOException {
    /**
     * This 'if' condition checks to see if the fhir-test-cases project (https://github.com/FHIR/fhir-test-cases) is
     * installed locally at the same directory level as the core library project is. If so, the test case data is read
     * directly from that project, instead of the imported maven dependency jar. It is important, that if you want to
     * test against the dependency imported from sonatype nexus, instead of your local copy, you need to either change
     * the name of the project directory to something other than 'fhir-test-cases', or move it to another location, not
     * at the same directory level as the core project.
     */
    String dir = System.getenv("FHIR-TEST-CASES");
    if (dir == null && ToolGlobalSettings.hasTestsPath()) {
        dir = ToolGlobalSettings.getTestsPath();
    }
    if (dir != null && new CSFile(dir).exists()) {
        String n = Utilities.path(dir, Utilities.path(paths));
        // ok, we'll resolve this locally
        return TextFile.fileToString(new CSFile(n));
    } else {
        // resolve from the package
        String contents;
        String classpath = ("/org/hl7/fhir/testcases/" + Utilities.pathURL(paths));
        try (InputStream inputStream = BaseTestingUtilities.class.getResourceAsStream(classpath)) {
            if (inputStream == null) {
                throw new IOException("Can't find file on classpath: " + classpath);
            }
            contents = IOUtils.toString(inputStream, java.nio.charset.StandardCharsets.UTF_8);
        }
        return contents;
    }
}
Also used : CSFile(org.hl7.fhir.utilities.CSFile)

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