001package org.hl7.fhir.utilities.npm; 002 003import java.io.File; 004import java.io.FileOutputStream; 005import java.io.IOException; 006 007import org.hl7.fhir.utilities.FileUtilities; 008import org.hl7.fhir.utilities.Utilities; 009import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; 010import org.hl7.fhir.utilities.json.model.JsonObject; 011import org.hl7.fhir.utilities.json.parser.JsonParser; 012 013@SuppressWarnings("checkstyle:systemout") 014public class ResourceRenamer { 015 016 public static void main(String[] args) throws IOException { 017 new ResourceRenamer().processArg(ManagedFileAccess.file(args[0])); 018 } 019 020 private void processArg(File file) throws IOException { 021 if (file.isDirectory()) { 022 process(file); 023 } else { 024 unbundle(file); 025 } 026 } 027 028 private void unbundle(File f) throws IOException { 029 JsonObject j = JsonParser.parseObject(f); 030 for (JsonObject e : j.getJsonObjects("entry")) { 031 JsonObject r = e.getJsonObject("resource"); 032 String rt = r.asString("resourceType"); 033 String id = r.asString("id"); 034 String nn = Utilities.path(FileUtilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); 035 FileOutputStream fs = ManagedFileAccess.outStream(nn); 036 try { 037 JsonParser.compose(r, fs, true); 038 } finally { 039 fs.close(); 040 } 041 } 042 } 043 044 private void process(File dir) { 045 046 for (File f : dir.listFiles()) { 047 if (f.getName().endsWith(".json")) { 048 try { 049 JsonObject j = JsonParser.parseObject(f); 050 String rt = j.asString("resourceType"); 051 String id = j.asString("id"); 052 String nn = Utilities.path(FileUtilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); 053 File nf = ManagedFileAccess.file(nn); 054 if (!nn.equals(f.getAbsolutePath())) { 055 System.out.println("Rename "+f.getName()+" to "+nf.getName()); 056 f.renameTo(nf); 057 } 058 } catch (Exception e) { 059 System.out.println("Error Processing "+f.getName()+" : "+e.getMessage()); 060 } 061 } 062 } 063 064 } 065 066}