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));
}
}
}
}
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);
}
}
}
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);
}
}
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);
}
}
}
}
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"));
}
Aggregations