001package org.hl7.fhir.utilities;
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.File;
035import java.io.FileInputStream;
036import java.io.FileNotFoundException;
037import java.io.FileOutputStream;
038import java.io.IOException;
039import java.io.OutputStreamWriter;
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.HashMap;
043import java.util.HashSet;
044import java.util.List;
045import java.util.Map;
046import java.util.Set;
047
048import org.hl7.fhir.utilities.filesystem.CSFile;
049import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
050
051import com.google.gson.Gson;
052import com.google.gson.GsonBuilder;
053import com.google.gson.JsonElement;
054import com.google.gson.JsonObject;
055import com.google.gson.JsonSyntaxException;
056
057public class NDJsonWriter {
058
059  private class ResourceInfo {
060    private FileOutputStream stream;
061    private Set<String> ids = new HashSet<String>();
062    public OutputStreamWriter writer;
063  }
064  
065  private static com.google.gson.JsonParser  parser = new com.google.gson.JsonParser();
066  private Gson gson = new GsonBuilder().create();
067  private Map<String, ResourceInfo> outputs = new HashMap<String, ResourceInfo>();
068  private String filename;
069  private String scratch;
070  
071  public NDJsonWriter(String filename, String scratch) {
072    this.filename = filename;
073    this.scratch = scratch;
074    outputs.clear();
075  }
076
077  public void addFilesFiltered(String actualDir, String ext, String[] noExt) throws IOException {
078    File f = ManagedFileAccess.csfile(actualDir);
079
080    String files[] = f.list();
081    for (int i = 0; i < files.length; i++) {
082      if ( ManagedFileAccess.csfile(actualDir + files[i]).isFile() && ((ext == null || files[i].endsWith(ext)))) {
083        boolean ok = true;
084        for (String n : noExt) {
085          ok = ok && !files[i].endsWith(n);
086        }
087        if (ok) {
088          addFile(Utilities.path(actualDir, files[i]));
089        }
090      }
091    }
092  }
093
094  private void addFile(String path) throws JsonSyntaxException, FileNotFoundException, IOException {
095    JsonObject js = parser.parse(FileUtilities.fileToString(path)).getAsJsonObject();
096    if (js.has("resourceType")) {
097      addResource(js);  
098    }
099  }
100
101  private void addResource(JsonObject js) throws IOException {
102    String rn = js.get("resourceType").getAsString();    
103    if (rn.equals("Bundle")) {
104      if (js.has("entry")) {
105        for (JsonElement item : js.getAsJsonArray("entry")) {
106          if (item instanceof JsonObject && ((JsonObject) item).has("resource")) {
107            JsonObject r = (JsonObject) ((JsonObject) item).get("resource");
108            rn = r.get("resourceType").getAsString();
109            addResource(r);  
110          }
111        }
112      }
113    } else {
114      if (!js.has("id"))
115        return;
116
117      String id = js.get("id").getAsString();
118      String json = gson.toJson(js);
119      
120      if (outputs.containsKey(rn)) { 
121        ResourceInfo ri = outputs.get(rn);
122        if (!ri.ids.contains(id)) {
123          ri.ids.add(id);
124          ri.writer.append("\r\n");      
125          ri.writer.append(json);
126        }
127      } else {
128        ResourceInfo ri = new ResourceInfo();
129        outputs.put(rn, ri);
130        ri.ids.add(id);
131        ri.stream = ManagedFileAccess.outStream(Utilities.path(scratch, rn+".ndjson"));
132        ri.writer = new OutputStreamWriter(ri.stream, "UTF-8");
133        ri.writer.append(json);      
134      }
135    }
136  }
137
138  public void close() throws IOException {
139    ZipGenerator zip = new ZipGenerator(filename);
140    for (String rn : sorted(outputs.keySet())) {
141      ResourceInfo ri = outputs.get(rn);
142      ri.writer.flush();
143      ri.writer.close();
144      ri.stream.close();
145      
146      zip.addStream(rn+".ndjson", ManagedFileAccess.inStream(Utilities.path(scratch, rn+".ndjson")), false);
147    }
148    zip.close();
149  }
150
151  private List<String> sorted(Set<String> keys) {
152    List<String> res = new ArrayList<String>();
153    res.addAll(keys);
154    Collections.sort(res);
155    return res;
156  }
157
158  public static void main(String[] args) throws IOException {
159    String dstDir = "C:\\work\\org.hl7.fhir\\build\\publish\\";
160    NDJsonWriter ndjson = new NDJsonWriter(dstDir + "examples-ndjson.zip", Utilities.path("[tmp]", "ndjson"));
161    ndjson.addFilesFiltered(dstDir, ".json", new String[] {".schema.json", ".canonical.json", ".diff.json", "expansions.json", "package.json"});
162    ndjson.close();
163  }
164
165}