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