001package org.hl7.fhir.utilities.cache; 002 003/*- 004 * #%L 005 * org.hl7.fhir.utilities 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023 024import java.io.IOException; 025import java.io.InputStream; 026import java.io.OutputStream; 027import java.io.OutputStreamWriter; 028 029import org.hl7.fhir.utilities.TextFile; 030import org.hl7.fhir.utilities.Utilities; 031 032import com.google.gson.Gson; 033import com.google.gson.GsonBuilder; 034import com.google.gson.JsonArray; 035import com.google.gson.JsonObject; 036import com.google.gson.JsonParser; 037import com.google.gson.JsonPrimitive; 038import com.google.gson.JsonSyntaxException; 039 040public class PackageGenerator { 041 042 public enum PackageType { 043 CORE, IG, TOOL, TEMPLATE, SUBSET; 044 045 public String getCode() { 046 switch (this) { 047 case CORE: return "fhir.core"; 048 case IG: return "fhir.ig"; 049 case TOOL: return "fhir.tool"; 050 case TEMPLATE: return "fhir.template"; 051 case SUBSET: return "fhir.subset"; 052 } 053 throw new Error("Unknown Type"); 054 } 055 } 056 private OutputStream stream; 057 private JsonObject object; 058 059 public PackageGenerator(OutputStream stream) { 060 super(); 061 this.stream = stream; 062 object = new JsonObject(); 063 } 064 065 public PackageGenerator(OutputStream stream, InputStream template) throws JsonSyntaxException, IOException { 066 super(); 067 this.stream = stream; 068 JsonParser parser = new com.google.gson.JsonParser(); 069 object = parser.parse(TextFile.streamToString(template)).getAsJsonObject(); 070 071 } 072 073 public void commit() throws IOException { 074 Gson gson = new GsonBuilder().setPrettyPrinting().create(); 075 String json = gson.toJson(object); 076 OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); 077 sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter 078 sw.write(json); 079 sw.flush(); 080 sw.close(); 081 } 082 083 public PackageGenerator name(String value) { 084 object.addProperty("name", "@fhir/"+value); 085 return this; 086 } 087 088 public PackageGenerator version(String value) { 089 object.addProperty("version", value); 090 return this; 091 } 092 093 public PackageGenerator toolsVersion(int value) { 094 object.addProperty("tools-version", value); 095 return this; 096 } 097 098 public PackageGenerator description(String value) { 099 object.addProperty("description", value); 100 return this; 101 } 102 103 public PackageGenerator license(String value) { 104 object.addProperty("license", value); 105 return this; 106 } 107 108 public PackageGenerator homepage(String value) { 109 object.addProperty("homepage", value); 110 return this; 111 } 112 113 public PackageGenerator bugs(String value) { 114 object.addProperty("bugs", value); 115 return this; 116 } 117 118 public PackageGenerator author(String name, String email, String url) { 119 JsonObject person = new JsonObject(); 120 person.addProperty("name", name); 121 if (!Utilities.noString(email)) 122 person.addProperty("email", email); 123 if (!Utilities.noString(url)) 124 person.addProperty("url", url); 125 object.add("author", person); 126 return this; 127 } 128 129 public PackageGenerator contributor(String name, String email, String url) { 130 JsonObject person = new JsonObject(); 131 person.addProperty("name", name); 132 if (!Utilities.noString(email)) 133 person.addProperty("email", email); 134 if (!Utilities.noString(url)) 135 person.addProperty("url", url); 136 JsonArray c = object.getAsJsonArray("contributors"); 137 if (c == null) { 138 c = new JsonArray(); 139 object.add("contributors", c); 140 } 141 c.add(person); 142 return this; 143 } 144 145 public PackageGenerator dependency(String name, String version) { 146 JsonObject dep = object.getAsJsonObject("dependencies"); 147 if (dep == null) { 148 dep = new JsonObject(); 149 object.add("dependencies", dep); 150 } 151 dep.addProperty(name, version); 152 return this; 153 } 154 155 public PackageGenerator file(String name) { 156 JsonArray files = object.getAsJsonArray("files"); 157 if (files == null) { 158 files = new JsonArray(); 159 object.add("files", files); 160 } 161 files.add(new JsonPrimitive(name)); 162 return this; 163 } 164 165 public PackageGenerator kind(PackageType kind) { 166 object.addProperty("type", kind.getCode()); 167 return this; 168 } 169 170 171}