001package org.hl7.fhir.utilities.cache;
002
003import java.io.File;
004import java.io.IOException;
005import java.util.ArrayList;
006import java.util.HashMap;
007import java.util.List;
008import java.util.Map;
009
010import org.hl7.fhir.exceptions.FHIRException;
011import org.hl7.fhir.utilities.TextFile;
012import org.hl7.fhir.utilities.Utilities;
013import org.hl7.fhir.utilities.json.JsonTrackingParser;
014
015import com.google.gson.GsonBuilder;
016import com.google.gson.JsonArray;
017import com.google.gson.JsonObject;
018
019/**
020 * This class builds the .index.json for a package 
021 * 
022 * @author grahame
023 *
024 */
025public class NpmPackageIndexBuilder {
026  
027  private JsonObject index;
028  private JsonArray files;
029  
030  public void start() {
031    index = new JsonObject();
032    index.addProperty("index-version", 1);
033    files = new JsonArray();
034    index.add("files", files);
035  }
036  
037  public void seeFile(String name, byte[] content) {
038    if (name.endsWith(".json")) {
039      try {
040        JsonObject json = JsonTrackingParser.parseJson(content);
041        if (json.has("resourceType")) {
042          // ok we treat it as a resource
043          JsonObject fi = new JsonObject();
044          files.add(fi);
045          fi.addProperty("filename", name);
046          fi.addProperty("resourceType", json.get("resourceType").getAsString()); 
047          if (json.has("id") && json.get("id").isJsonPrimitive())
048            fi.addProperty("id", json.get("id").getAsString());
049          if (json.has("url") && json.get("url").isJsonPrimitive())
050            fi.addProperty("url", json.get("url").getAsString());
051          if (json.has("version") && json.get("version").isJsonPrimitive())
052            fi.addProperty("version", json.get("version").getAsString());
053          if (json.has("kind") && json.get("kind").isJsonPrimitive())
054            fi.addProperty("kind", json.get("kind").getAsString());
055          if (json.has("type") && json.get("type").isJsonPrimitive())
056            fi.addProperty("type", json.get("type").getAsString());
057        }
058      } catch (Exception e) {
059        System.out.println("Error parsing "+name+": "+e.getMessage());
060      }
061    }
062  }
063  
064  public String build() {
065    String res = new GsonBuilder().setPrettyPrinting().create().toJson(index);
066    index = null;
067    files = null;
068    return res;
069  }
070  
071//  private Map<String, List<String>> types = new HashMap<>();
072//  private Map<String, String> canonicalMap = new HashMap<>();
073
074
075  public void executeWithStatus(String folder) throws IOException {
076    System.out.print("Index Package "+folder+" ... ");
077    execute(folder);
078    System.out.println("done");
079  }
080  
081  public void execute(String folder) throws IOException {
082    if (existsFolder(folder, "package")) {
083      folder = Utilities.path(folder, "package"); 
084    }
085    if (!existsFile(folder, "package.json")) {
086      throw new FHIRException("Not a proper package? (can't find package.json)");
087    }
088    start();
089    File dir = new File(folder);
090    for (File f : dir.listFiles()) {
091      seeFile(f.getName(), TextFile.fileToBytes(f));
092    }
093    TextFile.stringToFile(build(), Utilities.path(folder, ".index.json"));
094  }
095
096
097  private boolean existsFolder(String... args) throws IOException {
098    File f = new File(Utilities.path(args));
099    return f.exists() && f.isDirectory();
100  }
101
102  private boolean existsFile(String... args) throws IOException {
103    File f = new File(Utilities.path(args));
104    return f.exists() && !f.isDirectory();
105  }
106
107
108  public static void main(String[] args) throws IOException {
109    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.core");
110    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.examples");
111    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.expansions");
112    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r4.elements");
113    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.core");
114    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.examples");
115    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.expansions");
116    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r3.elements");
117    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.core");
118    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.examples");
119    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2b.expansions");
120    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.core");
121    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.examples");
122    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.expansions");
123    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.r2.elements");
124    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r2");
125    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r3");
126    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.test.data\\fhir.test.data.r4");
127    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r2");
128    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r3");
129    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\fhir.tx.support\\fhir.tx.support.r4");
130    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.0.2");
131    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#1.4.0");
132    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#3.0.2");
133    new NpmPackageIndexBuilder().executeWithStatus("C:\\work\\org.hl7.fhir\\packages\\hl7.fhir.rX\\hl7.fhir.core#4.0.1");
134  }
135
136}