Search in sources :

Example 6 with NpmPackageFolder

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

the class NpmPackage method loadAllFiles.

public void loadAllFiles() throws IOException {
    for (String folder : folders.keySet()) {
        NpmPackageFolder pf = folders.get(folder);
        String p = folder.contains("$") ? path : Utilities.path(path, folder);
        for (File f : new File(p).listFiles()) {
            if (!f.isDirectory() && !isInternalExemptFile(f)) {
                pf.getContent().put(f.getName(), TextFile.fileToBytes(f));
            }
        }
    }
}
Also used : File(java.io.File) TextFile(org.hl7.fhir.utilities.TextFile)

Example 7 with NpmPackageFolder

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

the class NpmPackage method loadSubFolders.

private void loadSubFolders(String rootPath, File dir) throws IOException {
    for (File f : dir.listFiles()) {
        if (f.isDirectory()) {
            String d = f.getAbsolutePath().substring(rootPath.length() + 1);
            if (!d.startsWith("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(rootPath, f);
        }
    }
}
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 8 with NpmPackageFolder

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

the class NpmPackage method indexFolder.

public void indexFolder(String desc, NpmPackageFolder folder) throws FileNotFoundException, IOException {
    List<String> remove = new ArrayList<>();
    NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
    indexer.start();
    for (String n : folder.listFiles()) {
        if (!indexer.seeFile(n, folder.fetchFile(n))) {
            remove.add(n);
        }
    }
    for (String n : remove) {
        folder.removeFile(n);
    }
    String json = indexer.build();
    try {
        folder.readIndex(JsonTrackingParser.parseJson(json));
        if (folder.folder != null) {
            TextFile.stringToFile(json, Utilities.path(folder.folder.getAbsolutePath(), ".index.json"));
        }
    } catch (Exception e) {
        TextFile.stringToFile(json, Utilities.path("[tmp]", ".index.json"));
        throw new IOException("Error parsing " + (desc == null ? "" : desc + "#") + "package/" + folder.name + "/.index.json: " + e.getMessage(), e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 9 with NpmPackageFolder

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

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

Aggregations

File (java.io.File)8 TextFile (org.hl7.fhir.utilities.TextFile)8 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 ParseException (java.text.ParseException)5 FHIRException (org.hl7.fhir.exceptions.FHIRException)5 GsonBuilder (com.google.gson.GsonBuilder)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayList (java.util.ArrayList)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)1 IniFile (org.hl7.fhir.utilities.IniFile)1 NpmPackageFolder (org.hl7.fhir.utilities.npm.NpmPackage.NpmPackageFolder)1