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.Map;
009
010import org.hl7.fhir.utilities.Utilities;
011
012import com.google.gson.GsonBuilder;
013import com.google.gson.JsonArray;
014import com.google.gson.JsonObject;
015
016/**
017 * intenral use only - set the file name to edit in main(), and fill out the edit routine
018 * 
019 * @author grahame
020 *
021 */
022public class PackageHacker {
023
024  public static void main(String[] args) throws FileNotFoundException, IOException {
025    new PackageHacker().edit("C:\\work\\fhir.org\\web-source\\source\\guides\\argonaut\\questionnaire\\package.tgz");
026  }
027
028  private void edit(String name) throws FileNotFoundException, IOException {
029    File f = new File(name);
030    if (!f.exists())
031      throw new Error("Unable to find "+f.getAbsolutePath());
032    
033    NpmPackage pck = NpmPackage.fromPackage(new FileInputStream(f));
034    System.out.println("Altering Package "+f.getAbsolutePath());
035    System.out.println(nice(pck.getNpm()));
036    
037    change(pck.getNpm(), pck.getContent());
038    
039    System.out.println("Revised Package");
040    System.out.println("=======================");
041    System.out.println(nice(pck.getNpm()));
042    System.out.println("=======================");
043    System.out.print("save? y/n: ");
044    int r = System.in.read();
045    if (r == 'y') {
046      f.renameTo(new File(Utilities.changeFileExt(name, ".tgz.bak")));
047      pck.save(new FileOutputStream(f));
048    }   
049    
050  }
051
052  private String nice(JsonObject json) {
053    return new GsonBuilder().setPrettyPrinting().create().toJson(json);
054  }
055
056  private void change(JsonObject npm, Map<String, byte[]> content) {
057    npm.remove("name");
058    npm.addProperty("name", "fhir.argonaut.questionnaire");
059//    JsonArray fhirVersions = new JsonArray();
060//    fhirVersions.add("1.9.0");
061//    npm.add("fhirVersions", fhirVersions);
062//    JsonObject dependencies = new JsonObject();
063//    dependencies.addProperty("hl7.fhir.r3.core", "3.0.2");
064//    npm.remove("dependencies");
065//    npm.add("dependencies", dependencies);
066  }
067
068}