001package org.hl7.fhir.utilities.npm; 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; 038import java.util.List; 039 040import org.hl7.fhir.utilities.TextFile; 041import org.hl7.fhir.utilities.Utilities; 042import org.hl7.fhir.utilities.json.model.JsonArray; 043import org.hl7.fhir.utilities.json.model.JsonObject; 044import org.hl7.fhir.utilities.json.parser.JsonParser; 045 046public class PackageGenerator { 047 048 public enum PackageType { 049 CONFORMANCE, IG, CORE, EXAMPLES, GROUP, TOOL, IG_TEMPLATE; 050 // CORE, IG, TOOL, TEMPLATE, SUBSET; 051 052 public String getCode() { 053 switch (this) { 054 case CONFORMANCE: return "Conformance"; 055 case IG: return "IG"; 056 case CORE: return "Core"; 057 case EXAMPLES: return "Examples"; 058 case GROUP: return "Group"; 059 case TOOL: return "Tool"; 060 case IG_TEMPLATE: return "IG-Template"; 061 } 062 throw new Error("Unknown Type"); 063 } 064 065 String getOldCode() { 066 switch (this) { 067 case CONFORMANCE: return "xxx"; 068 case IG: return "fhir.ig"; 069 case CORE: return "fhir.core"; 070 case EXAMPLES: return "fhir.examples"; 071 case GROUP: return "fhir.group"; 072 case TOOL: return "fhir.tool"; 073 case IG_TEMPLATE: return "fhir.template"; 074 } 075 throw new Error("Unknown Type"); 076 } 077 } 078 079 private OutputStream stream; 080 private JsonObject object; 081 082 public PackageGenerator() { 083 object = new JsonObject(); 084 } 085 086 public PackageGenerator(OutputStream stream) { 087 super(); 088 this.stream = stream; 089 object = new JsonObject(); 090 } 091 092 public PackageGenerator(OutputStream stream, InputStream template) throws IOException { 093 super(); 094 this.stream = stream; 095 object = JsonParser.parseObject(TextFile.streamToString(template)); 096 097 } 098 099 public JsonObject getRootJsonObject() { 100 return object; 101 } 102 103 public void commit() throws IOException { 104 String json = JsonParser.compose(object, true); 105 OutputStreamWriter sw = new OutputStreamWriter(stream, "UTF-8"); 106 sw.write('\ufeff'); // Unicode BOM, translates to UTF-8 with the configured outputstreamwriter 107 sw.write(json); 108 sw.flush(); 109 sw.close(); 110 } 111 112 public PackageGenerator name(String value) { 113 // NOTE: I removed a prefix of "@fhir/" here. What was this for? -JA 114 object.add("name", value); 115 return this; 116 } 117 118 public PackageGenerator version(String value) { 119 object.add("version", value); 120 return this; 121 } 122 123 public PackageGenerator toolsVersion(int value) { 124 object.add("tools-version", value); 125 return this; 126 } 127 128 public PackageGenerator fhirVersions(List<String> versions) { 129 JsonArray fhirVersionsArray = new JsonArray(); 130 for (String version : versions) { 131 fhirVersionsArray.add(version); 132 } 133 object.add("fhirVersions", fhirVersionsArray); 134 return this; 135 } 136 137 public PackageGenerator description(String value) { 138 object.add("description", value); 139 return this; 140 } 141 142 public PackageGenerator license(String value) { 143 object.add("license", value); 144 return this; 145 } 146 147 public PackageGenerator homepage(String value) { 148 object.add("homepage", value); 149 return this; 150 } 151 152 public PackageGenerator bugs(String value) { 153 object.add("bugs", value); 154 return this; 155 } 156 157 public PackageGenerator author(String name, String email, String url) { 158 JsonObject person = new JsonObject(); 159 person.add("name", name); 160 if (!Utilities.noString(email)) 161 person.add("email", email); 162 if (!Utilities.noString(url)) 163 person.add("url", url); 164 object.add("author", person); 165 return this; 166 } 167 168 public PackageGenerator contributor(String name, String email, String url) { 169 JsonObject person = new JsonObject(); 170 person.add("name", name); 171 if (!Utilities.noString(email)) 172 person.add("email", email); 173 if (!Utilities.noString(url)) 174 person.add("url", url); 175 JsonArray c = object.getJsonArray("contributors"); 176 if (c == null) { 177 c = new JsonArray(); 178 object.add("contributors", c); 179 } 180 c.add(person); 181 return this; 182 } 183 184 public PackageGenerator dependency(String name, String version) { 185 JsonObject dep = object.getJsonObject("dependencies"); 186 if (dep == null) { 187 dep = new JsonObject(); 188 object.add("dependencies", dep); 189 } 190 dep.add(name, version); 191 return this; 192 } 193 194 public PackageGenerator file(String name) { 195 JsonArray files = object.forceArray("files"); 196 files.add(name); 197 return this; 198 } 199 200 public PackageGenerator kind(PackageType kind) { 201 object.add("type", kind.getCode()); 202 return this; 203 } 204 205 206}