001package org.hl7.fhir.utilities.cache;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.io.OutputStream;
006import java.io.OutputStreamWriter;
007
008import org.hl7.fhir.utilities.TextFile;
009import org.hl7.fhir.utilities.Utilities;
010
011import com.google.gson.Gson;
012import com.google.gson.GsonBuilder;
013import com.google.gson.JsonArray;
014import com.google.gson.JsonObject;
015import com.google.gson.JsonParser;
016import com.google.gson.JsonPrimitive;
017import com.google.gson.JsonSyntaxException;
018
019public class PackageGenerator {
020
021  public enum PackageType {
022    CORE, IG, TOOL, TEMPLATE;
023
024    public String getCode() {
025      switch (this) {
026      case CORE: return "fhir.core";
027      case IG: return "fhir.ig";
028      case TOOL: return "fhir.tool";
029      case TEMPLATE: return "fhir.template";
030        
031      }
032      throw new Error("Unknown Type");
033    }
034  }
035  private OutputStream stream;
036  private JsonObject object;
037
038  public PackageGenerator(OutputStream stream) {
039    super();
040    this.stream = stream;
041    object = new JsonObject();
042  }
043  
044  public PackageGenerator(OutputStream stream, InputStream template) throws JsonSyntaxException, IOException {
045    super();
046    this.stream = stream;
047    JsonParser parser = new com.google.gson.JsonParser();
048    object = parser.parse(TextFile.streamToString(template)).getAsJsonObject();
049
050  }
051  
052  public void commit() throws IOException {
053    Gson gson = new GsonBuilder().setPrettyPrinting().create();
054    String json = gson.toJson(object);
055    OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8");
056    sw.write('\ufeff');  // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter
057    sw.write(json);
058    sw.flush();
059    sw.close();
060  }
061  
062  public PackageGenerator name(String value) {
063    object.addProperty("name", "@fhir/"+value);
064    return this;
065  }
066   
067  public PackageGenerator version(String value) {
068    object.addProperty("version", value);
069    return this;
070  }
071  
072  public PackageGenerator toolsVersion(int value) {
073    object.addProperty("tools-version", value);
074    return this;
075  }
076  
077  public PackageGenerator description(String value) {
078    object.addProperty("description", value);
079    return this;    
080  }
081  
082  public PackageGenerator license(String value) {
083    object.addProperty("license", value);
084    return this;        
085  }
086  
087  public PackageGenerator homepage(String value) {
088    object.addProperty("homepage", value);
089    return this;            
090  }
091  
092  public PackageGenerator bugs(String value) {
093    object.addProperty("bugs", value);
094    return this;            
095  }
096  
097  public PackageGenerator author(String name, String email, String url) {
098    JsonObject person = new JsonObject();
099    person.addProperty("name", name);
100    if (!Utilities.noString(email))
101      person.addProperty("email", email);
102    if (!Utilities.noString(url))
103      person.addProperty("url", url);
104    object.add("author", person);
105    return this;            
106  }
107  
108  public PackageGenerator contributor(String name, String email, String url) {
109    JsonObject person = new JsonObject();
110    person.addProperty("name", name);
111    if (!Utilities.noString(email))
112      person.addProperty("email", email);
113    if (!Utilities.noString(url))
114      person.addProperty("url", url);
115    JsonArray c = object.getAsJsonArray("contributors");
116    if (c == null) {
117      c = new JsonArray();
118      object.add("contributors", c);
119    }
120    c.add(person);
121    return this;
122  }
123  
124  public PackageGenerator dependency(String name, String version) {
125    JsonObject dep = object.getAsJsonObject("dependencies");
126    if (dep == null) {
127      dep = new JsonObject();
128      object.add("dependencies", dep);
129    }
130    dep.addProperty(name, version);
131    return this;
132  }
133  
134  public PackageGenerator file(String name) {
135    JsonArray files = object.getAsJsonArray("files");
136    if (files == null) {
137      files = new JsonArray();
138      object.add("files", files);
139    }
140    files.add(new JsonPrimitive(name));
141    return this;
142  }
143
144  public PackageGenerator kind(PackageType kind) {
145    object.addProperty("type", kind.getCode());
146    return this;     
147  }
148  
149  
150}