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