001package org.hl7.fhir.utilities.cache; 002 003import java.io.File; 004import java.io.FileInputStream; 005import java.io.FileNotFoundException; 006import java.io.FileOutputStream; 007import java.io.IOException; 008import java.util.ArrayList; 009import java.util.List; 010import java.util.Map; 011 012import org.hl7.fhir.utilities.TextFile; 013import org.hl7.fhir.utilities.Utilities; 014 015import com.google.gson.GsonBuilder; 016import com.google.gson.JsonArray; 017import com.google.gson.JsonObject; 018 019/** 020 * intenral use only - set the file name to edit in main(), and fill out the edit routine 021 * 022 * @author grahame 023 * 024 */ 025public class PackageHacker { 026 027 public static void main(String[] args) throws FileNotFoundException, IOException { 028 new PackageHacker().edit("M:\\web\\terminology.hl7.org\\hl7.terminology.r3.tgz"); 029 } 030 031 private void edit(String name) throws FileNotFoundException, IOException { 032 File f = new File(name); 033 if (!f.exists()) 034 throw new Error("Unable to find "+f.getAbsolutePath()); 035 036 NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f)); 037 System.out.println("Altering Package "+f.getAbsolutePath()); 038 System.out.println(nice(pck.getNpm())); 039 040 change(pck.getNpm(), pck.getFolders().get("package").getContent()); 041 042 System.out.println("Revised Package"); 043 System.out.println("======================="); 044 System.out.println(nice(pck.getNpm())); 045 System.out.println("======================="); 046 System.out.print("save? y/n: "); 047 int r = System.in.read(); 048 if (r == 'y') { 049 f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak"))); 050 pck.save(new FileOutputStream(f)); 051 } 052 } 053 054 private String nice(JsonObject json) { 055 return new GsonBuilder().setPrettyPrinting().create().toJson(json); 056 } 057 058 private void change(JsonObject npm, Map<String, byte[]> content) throws FileNotFoundException, IOException { 059// fixVersions(npm); 060// npm.remove("name"); 061// npm.addProperty("name", "hl7.terminology.r5"); 062 npm.remove("version"); 063 npm.addProperty("version", "1.0.0"); 064// npm.remove("canonical"); 065// npm.addProperty("canonical", "http://hl7.org/fhir/us/davinci-drug-formulary"); 066//// npm.remove("description"); 067//// npm.addProperty("description", "Group Wrapper that includes all the R4 packages"); 068// npm.remove("url"); 069// npm.addProperty("url", "https://terminology.hl7.org/1.0.0/"); 070// JsonObject dep = new JsonObject(); 071// npm.add("dependencies", dep); 072// dep.addProperty("hl7.fhir.r4.core", "4.0.1"); 073// dep.addProperty("hl7.fhir.r4.examples", "4.0.1"); 074// dep.addProperty("hl7.fhir.r4.expansions", "4.0.1"); 075// dep.addProperty("hl7.fhir.r4.elements", "4.0.1"); 076 } 077 078 private void fixVersions(JsonObject npm) { 079 npm.remove("fhirVersions"); 080 JsonArray a = new JsonArray(); 081 npm.add("fhirVersions", a); 082 a.add("4.2.0"); 083 npm.remove("fhir-version-list"); 084 a = new JsonArray(); 085 npm.add("fhir-version-list", a); 086 a.add("4.2.0"); 087 } 088 089 private void setProperty(JsonObject npm, String name, String value) { 090 npm.remove("homepage"); 091 npm.addProperty("homepage", "http://hl7.org/fhir"); 092 } 093 094 private void fixNames(Map<String, byte[]> content) { 095 List<String> names = new ArrayList<>(); 096 names.addAll(content.keySet()); 097 for (String s : names) { 098 if (s.endsWith("json") && !s.endsWith(".json")) { 099 String n = s.substring(0, s.length()-4)+".json"; 100 content.put(n, content.get(s)); 101 content.remove(s); 102 } 103 } 104 } 105 106 private void addContentFrom(String folder, Map<String, byte[]> content) throws FileNotFoundException, IOException { 107 for (File f : new File(folder).listFiles()) { 108 if (f.getName().endsWith(".json") && !f.getName().endsWith(".canonical.json")) { 109 String cnt = TextFile.fileToString(f); 110 if (cnt.contains("\"resourceType\"")) { 111 content.put("package/"+f.getName(), TextFile.fileToBytes(f)); 112 } 113 } 114 } 115 } 116 117 public static String fixPackageUrl(String webref) { 118 if (webref == null) { 119 return null; 120 } 121 // workaround for past publishing problems 122 switch (webref) { 123 case "file://C:\\GitHub\\hl7.fhir.us.breast-radiology#0.2.0\\output": return "http://hl7.org/fhir/us/breast-radiology/2020May"; 124 case "file://C:\\GitHub\\hl7.fhir.us.bser#1.0.0\\output": return "http://hl7.org/fhir/us/bser/STU1"; 125 case "file://C:\\GitHub\\hl7.fhir.us.carin-bb#0.1.0\\output": return "http://hl7.org/fhir/us/carin-bb/2020Feb"; 126 case "file://C:\\GitHub\\hl7.fhir.us.carin-rtpbc#0.1.0\\output": return "http://hl7.org/fhir/us/carin-rtpbc/2020Feb"; 127 case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#1.1.0\\output": return "http://hl7.org/fhir/us/cqfmeasures/2020Feb"; 128 case "file://C:\\GitHub\\hl7.fhir.us.cqfmeasures#2.0.0\\output": return "http://hl7.org/fhir/us/cqfmeasures/STU2"; 129 case "file://C:\\GitHub\\hl7.fhir.us.davinci-alerts#0.2.0\\output": return "http://hl7.org/fhir/us/davinci-alerts/2020Feb"; 130 case "file://C:\\GitHub\\hl7.fhir.us.davinci-atr#0.1.0\\output": return "http://hl7.org/fhir/us/davinci-atr/2020Feb"; 131 case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.1.0\\output": return "http://hl7.org/fhir/us/davinci-deqm/2020Feb"; 132 case "file://C:\\GitHub\\hl7.fhir.us.davinci-deqm#1.0.0\\output": return "http://hl7.org/fhir/us/davinci-deqm/STU1"; 133 case "file://C:\\GitHub\\hl7.fhir.us.dme-orders#0.1.1\\output": return "http://hl7.org/fhir/us/dme-orders/2020May"; 134 case "file://C:\\GitHub\\hl7.fhir.us.ecr#1.0.0\\output": return "http://hl7.org/fhir/us/ecr/STU1"; 135 case "file://C:\\GitHub\\hl7.fhir.us.mcode#1.0.0\\output": return "http://hl7.org/fhir/us/mcode/STU1"; 136 case "file://C:\\GitHub\\hl7.fhir.us.odh#1.0.0\\output": return "http://hl7.org/fhir/us/odh/STU1"; 137 case "file://C:\\GitHub\\hl7.fhir.us.qicore#4.0.0\\output": return "http://hl7.org/fhir/us/qicore/STU4"; 138 case "file://C:\\GitHub\\hl7.fhir.uv.ips#1.0.0\\output": return "http://hl7.org/fhir/uv/ips/STU1"; 139 case "file://C:\\GitHub\\hl7.fhir.uv.mhealth-framework#0.1.0\\output": return "http://hl7.org/fhir/uv/mhealth-framework/2020May"; 140 case "file://C:\\GitHub\\hl7.fhir.uv.security-label-ds4p#0.1.0\\output": return "http://hl7.org/fhir/uv/security-label-ds4p/2020May"; 141 case "file://C:\\GitHub\\hl7.fhir.uv.shorthand#0.12.0\\output": return "http://hl7.org/fhir/uv/shorthand/2020May"; 142 } 143 return webref; 144 } 145 146}