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