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.Utilities; 008import org.hl7.fhir.utilities.json.model.JsonObject; 009import org.hl7.fhir.utilities.json.parser.JsonParser; 010 011 012public class ResourceRenamer { 013 014 public static void main(String[] args) throws IOException { 015 new ResourceRenamer().processArg(new File(args[0])); 016 } 017 018 private void processArg(File file) throws IOException { 019 if (file.isDirectory()) { 020 process(file); 021 } else { 022 unbundle(file); 023 } 024 } 025 026 private void unbundle(File f) throws IOException { 027 JsonObject j = JsonParser.parseObject(f); 028 for (JsonObject e : j.getJsonObjects("entry")) { 029 JsonObject r = e.getJsonObject("resource"); 030 String rt = r.asString("resourceType"); 031 String id = r.asString("id"); 032 String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); 033 JsonParser.compose(r, new FileOutputStream(nn), true); 034 } 035 } 036 037 private void process(File dir) { 038 039 for (File f : dir.listFiles()) { 040 if (f.getName().endsWith(".json")) { 041 try { 042 JsonObject j = JsonParser.parseObject(f); 043 String rt = j.asString("resourceType"); 044 String id = j.asString("id"); 045 String nn = Utilities.path(Utilities.getDirectoryForFile(f.getAbsolutePath()), rt+"-"+id+".json"); 046 File nf = new File(nn); 047 if (!nn.equals(f.getAbsolutePath())) { 048 System.out.println("Rename "+f.getName()+" to "+nf.getName()); 049 f.renameTo(nf); 050 } 051 } catch (Exception e) { 052 System.out.println("Error Processing "+f.getName()+" : "+e.getMessage()); 053 } 054 } 055 } 056 057 } 058 059}