001package org.hl7.fhir.utilities.cache; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import java.io.IOException; 035import java.io.InputStream; 036import java.io.OutputStream; 037import java.io.OutputStreamWriter; 038 039import org.hl7.fhir.utilities.TextFile; 040import org.hl7.fhir.utilities.Utilities; 041 042import com.google.gson.Gson; 043import com.google.gson.GsonBuilder; 044import com.google.gson.JsonArray; 045import com.google.gson.JsonObject; 046import com.google.gson.JsonParser; 047import com.google.gson.JsonPrimitive; 048import com.google.gson.JsonSyntaxException; 049 050public class PackageGenerator { 051 052 public enum PackageType { 053 CORE, IG, TOOL, TEMPLATE, SUBSET; 054 055 public String getCode() { 056 switch (this) { 057 case CORE: return "fhir.core"; 058 case IG: return "fhir.ig"; 059 case TOOL: return "fhir.tool"; 060 case TEMPLATE: return "fhir.template"; 061 case SUBSET: return "fhir.subset"; 062 } 063 throw new Error("Unknown Type"); 064 } 065 } 066 067 private OutputStream stream; 068 private JsonObject object; 069 070 public PackageGenerator() { 071 object = new JsonObject(); 072 } 073 074 public PackageGenerator(OutputStream stream) { 075 super(); 076 this.stream = stream; 077 object = new JsonObject(); 078 } 079 080 public PackageGenerator(OutputStream stream, InputStream template) throws JsonSyntaxException, IOException { 081 super(); 082 this.stream = stream; 083 JsonParser parser = new com.google.gson.JsonParser(); 084 object = parser.parse(TextFile.streamToString(template)).getAsJsonObject(); 085 086 } 087 088 public JsonObject getRootJsonObject() { 089 return object; 090 } 091 092 public void commit() throws IOException { 093 Gson gson = new GsonBuilder().setPrettyPrinting().create(); 094 String json = gson.toJson(object); 095 OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); 096 sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter 097 sw.write(json); 098 sw.flush(); 099 sw.close(); 100 } 101 102 public PackageGenerator name(String value) { 103 // NOTE: I removed a prefix of "@fhir/" here. What was this for? -JA 104 object.addProperty("name", value); 105 return this; 106 } 107 108 public PackageGenerator version(String value) { 109 object.addProperty("version", value); 110 return this; 111 } 112 113 public PackageGenerator toolsVersion(int value) { 114 object.addProperty("tools-version", value); 115 return this; 116 } 117 118 public PackageGenerator description(String value) { 119 object.addProperty("description", value); 120 return this; 121 } 122 123 public PackageGenerator license(String value) { 124 object.addProperty("license", value); 125 return this; 126 } 127 128 public PackageGenerator homepage(String value) { 129 object.addProperty("homepage", value); 130 return this; 131 } 132 133 public PackageGenerator bugs(String value) { 134 object.addProperty("bugs", value); 135 return this; 136 } 137 138 public PackageGenerator author(String name, String email, String url) { 139 JsonObject person = new JsonObject(); 140 person.addProperty("name", name); 141 if (!Utilities.noString(email)) 142 person.addProperty("email", email); 143 if (!Utilities.noString(url)) 144 person.addProperty("url", url); 145 object.add("author", person); 146 return this; 147 } 148 149 public PackageGenerator contributor(String name, String email, String url) { 150 JsonObject person = new JsonObject(); 151 person.addProperty("name", name); 152 if (!Utilities.noString(email)) 153 person.addProperty("email", email); 154 if (!Utilities.noString(url)) 155 person.addProperty("url", url); 156 JsonArray c = object.getAsJsonArray("contributors"); 157 if (c == null) { 158 c = new JsonArray(); 159 object.add("contributors", c); 160 } 161 c.add(person); 162 return this; 163 } 164 165 public PackageGenerator dependency(String name, String version) { 166 JsonObject dep = object.getAsJsonObject("dependencies"); 167 if (dep == null) { 168 dep = new JsonObject(); 169 object.add("dependencies", dep); 170 } 171 dep.addProperty(name, version); 172 return this; 173 } 174 175 public PackageGenerator file(String name) { 176 JsonArray files = object.getAsJsonArray("files"); 177 if (files == null) { 178 files = new JsonArray(); 179 object.add("files", files); 180 } 181 files.add(new JsonPrimitive(name)); 182 return this; 183 } 184 185 public PackageGenerator kind(PackageType kind) { 186 object.addProperty("type", kind.getCode()); 187 return this; 188 } 189 190 191}