001package org.hl7.fhir.utilities.cache;
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.BufferedOutputStream;
035import java.io.ByteArrayInputStream;
036import java.io.ByteArrayOutputStream;
037import java.io.File;
038import java.io.FileNotFoundException;
039import java.io.IOException;
040import java.io.InputStream;
041import java.io.OutputStream;
042import java.nio.charset.Charset;
043import java.nio.charset.StandardCharsets;
044import java.util.ArrayList;
045import java.util.Collections;
046import java.util.Comparator;
047import java.util.HashMap;
048import java.util.List;
049import java.util.Map;
050import java.util.Map.Entry;
051import java.util.Set;
052import java.util.zip.ZipEntry;
053import java.util.zip.ZipInputStream;
054
055import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
056import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
057import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
058import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
059import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
060import org.hl7.fhir.exceptions.FHIRException;
061import org.hl7.fhir.utilities.CommaSeparatedStringBuilder;
062import org.hl7.fhir.utilities.TextFile;
063import org.hl7.fhir.utilities.Utilities;
064import org.hl7.fhir.utilities.cache.NpmPackage.PackageResourceInformationSorter;
065import org.hl7.fhir.utilities.cache.PackageGenerator.PackageType;
066import org.hl7.fhir.utilities.json.JSONUtil;
067import org.hl7.fhir.utilities.json.JsonTrackingParser;
068
069import com.google.gson.GsonBuilder;
070import com.google.gson.JsonArray;
071import com.google.gson.JsonElement;
072import com.google.gson.JsonObject;
073
074/**
075 * info and loader for a package 
076 * 
077 * Packages may exist on disk in the cache, or purely in memory when they are loaded on the fly
078 * 
079 * Packages are contained in subfolders (see the package spec). The FHIR resources will be in "package"
080 * 
081 * @author Grahame Grieve
082 *
083 */
084public class NpmPackage {
085
086  public class PackageResourceInformationSorter implements Comparator<PackageResourceInformation> {
087    @Override
088    public int compare(PackageResourceInformation o1, PackageResourceInformation o2) {
089      return o1.filename.compareTo(o2.filename);
090    }
091  }
092  
093  public class PackageResourceInformation {
094    private String id;
095    private String type;
096    private String url;
097    private String version;
098    private String filename;
099    private String supplements;
100    
101    public PackageResourceInformation(String root, JsonObject fi) throws IOException {
102      super();
103      id = JSONUtil.str(fi, "id");
104      type = JSONUtil.str(fi, "resourceType");
105      url = JSONUtil.str(fi, "url");
106      version = JSONUtil.str(fi, "version");
107      filename = Utilities.path(root, JSONUtil.str(fi, "filename"));
108      supplements = JSONUtil.str(fi, "supplements");
109    }
110    public String getId() {
111      return id;
112    }
113    public String getType() {
114      return type;
115    }
116    public String getUrl() {
117      return url;
118    }
119    public String getVersion() {
120      return version;
121    }
122    public String getFilename() {
123      return filename;
124    }
125    public String getSupplements() {
126      return supplements;
127    }
128    
129  }
130  public class IndexVersionSorter implements Comparator<JsonObject> {
131
132    @Override
133    public int compare(JsonObject o0, JsonObject o1) {
134      String v0 = JSONUtil.str(o0, "version"); 
135      String v1 = JSONUtil.str(o1, "version"); 
136      return v0.compareTo(v1);
137    }
138  }
139
140  public static boolean isValidName(String pid) {
141    return pid.matches("^[a-z][a-zA-Z0-9]*(\\.[a-z][a-zA-Z0-9\\-]*)+$");
142  }
143
144  public static boolean isValidVersion(String ver) {
145    return ver.matches("^[0-9]+\\.[0-9]+\\.[0-9]+$");
146  }
147
148  public class NpmPackageFolder {
149    private String name;
150    private Map<String, List<String>> types = new HashMap<>();
151    private Map<String, byte[]> content = new HashMap<>();
152    private JsonObject index;
153    private File folder;
154
155    public NpmPackageFolder(String name) {
156      super();
157      this.name = name;
158    }
159
160    public Map<String, List<String>> getTypes() {
161      return types;
162    }
163
164    public String getName() {
165      return name;
166    }
167
168    public boolean readIndex(JsonObject index) {
169      if (!index.has("index-version") || (index.get("index-version").getAsInt() != 1)) {
170        return false;
171      }
172      this.index = index;
173      for (JsonElement e : index.getAsJsonArray("files")) {
174        JsonObject file = (JsonObject) e;
175        String type = JSONUtil.str(file, "resourceType");
176        String name = JSONUtil.str(file, "filename");
177        if (!types.containsKey(type))
178          types.put(type, new ArrayList<>());
179        types.get(type).add(name);
180      }
181      return true;
182    }
183
184    public List<String> listFiles() {
185      List<String> res = new ArrayList<>();
186      if (folder != null) {
187        for (File f : folder.listFiles()) {
188          if (!f.isDirectory() && !Utilities.existsInList(f.getName(), "package.json", ".index.json")) {
189            res.add(f.getName());
190          }
191        }
192      } else {
193        for (String s : content.keySet()) {
194          if (!Utilities.existsInList(s, "package.json", ".index.json")) {
195            res.add(s);
196          }
197        }
198      }
199      Collections.sort(res);
200      return res;
201    }
202
203    public Map<String, byte[]> getContent() {
204      return content;
205    }
206
207    public byte[] fetchFile(String file) throws FileNotFoundException, IOException {
208      if (folder != null) {
209        File f = new File(Utilities.path(folder.getAbsolutePath(), file));
210        if (f.exists()) {
211          return TextFile.fileToBytes(f);
212        } else {
213          return null;
214        }
215      } else {
216        return content.get(file);
217      }
218    }
219
220    public boolean hasFile(String file) throws IOException {
221      if (folder != null) {
222        return new File(Utilities.path(folder.getAbsolutePath(), file)).exists();
223      } else {
224        return content.containsKey(file);
225      }
226
227    }
228
229    public String dump() {
230      return name + " ("+ (folder == null ? "null" : folder.toString())+") | "+Boolean.toString(index != null)+" | "+content.size()+" | "+types.size();
231    }
232
233    public void removeFile(String n) throws IOException {
234      if (folder != null) {
235        new File(Utilities.path(folder.getAbsolutePath(), n)).delete();
236      } else {
237        content.remove(n);
238      }
239      changedByLoader = true;      
240    }
241
242  }
243
244  private String path;
245  private JsonObject npm;
246  private Map<String, NpmPackageFolder> folders = new HashMap<>();
247  private boolean changedByLoader; // internal qa only!
248  private Map<String, Object> userData = new HashMap<>();
249
250  /**
251   * Constructor
252   */
253  private NpmPackage() {
254    super();
255  }
256
257  /**
258   * Factory method that parses a package from an extracted folder
259   */
260  public static NpmPackage fromFolder(String path) throws IOException {
261    NpmPackage res = new NpmPackage();
262    res.loadFiles(path, new File(path));
263    res.checkIndexed(path);
264    return res;
265  }
266
267  /**
268   * Factory method that starts a new empty package using the given PackageGenerator to create the manifest
269   */
270  public static NpmPackage empty(PackageGenerator thePackageGenerator) {
271    NpmPackage retVal = new NpmPackage();
272    retVal.npm = thePackageGenerator.getRootJsonObject();
273    return retVal;
274  }
275
276  public Map<String, Object> getUserData() {
277    return userData;
278  }
279
280  public void loadFiles(String path, File source, String... exemptions) throws FileNotFoundException, IOException {
281    this.npm = (JsonObject) new com.google.gson.JsonParser().parse(TextFile.fileToString(Utilities.path(path, "package", "package.json")));
282    this.path = path;
283    
284    File dir = new File(path);
285    for (File f : dir.listFiles()) {
286      if (!isInternalExemptFile(f) && !Utilities.existsInList(f.getName(), exemptions)) {
287        if (f.isDirectory()) {
288          String d = f.getName();
289          if (!d.equals("package")) {
290            d = Utilities.path("package", d);
291          }
292          NpmPackageFolder folder = this.new NpmPackageFolder(d);
293          folder.folder = f;
294          this.folders.put(d, folder);
295          File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
296          if (ij.exists()) {
297            try {
298              if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) {
299                indexFolder(folder.getName(), folder);
300              }
301            } catch (Exception e) {
302              throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e);
303            }
304          }
305          loadSubFolders(dir.getAbsolutePath(), f);
306        } else {
307          NpmPackageFolder folder = this.new NpmPackageFolder(Utilities.path("package", "$root"));
308          folder.folder = dir;
309          this.folders.put(Utilities.path("package", "$root"), folder);        
310        }
311      }
312    }
313  }
314
315  public static boolean isInternalExemptFile(File f) {
316    return Utilities.existsInList(f.getName(), ".git", ".svn") || Utilities.existsInList(f.getName(), "package-list.json");
317  }
318
319  private void loadSubFolders(String rootPath, File dir) throws IOException {
320    for (File f : dir.listFiles()) {
321      if (f.isDirectory()) {
322        String d = f.getAbsolutePath().substring(rootPath.length()+1);
323        if (!d.startsWith("package")) {
324          d = Utilities.path("package", d);
325        }
326        NpmPackageFolder folder = this.new NpmPackageFolder(d);
327        folder.folder = f;
328        this.folders.put(d, folder);
329        File ij = new File(Utilities.path(f.getAbsolutePath(), ".index.json"));
330        if (ij.exists()) {
331          try {
332            if (!folder.readIndex(JsonTrackingParser.parseJson(ij))) {
333              indexFolder(folder.getName(), folder);
334            }
335          } catch (Exception e) {
336            throw new IOException("Error parsing "+ij.getAbsolutePath()+": "+e.getMessage(), e);
337          }
338        }
339        loadSubFolders(rootPath, f);        
340      }
341    }    
342  }
343
344  public static NpmPackage fromFolder(String folder, PackageType defType, String... exemptions) throws IOException {
345    NpmPackage res = new NpmPackage();
346    res.loadFiles(folder, new File(folder), exemptions);
347    if (!res.folders.containsKey("package")) {
348      res.folders.put("package", res.new NpmPackageFolder("package"));
349    }
350    if (!res.folders.get("package").hasFile("package.json") && defType != null) {
351      TextFile.stringToFile("{ \"type\" : \""+defType.getCode()+"\"}", Utilities.path(res.folders.get("package").folder.getAbsolutePath(), "package.json"));
352    }
353    res.npm = (JsonObject) new com.google.gson.JsonParser().parse(new String(res.folders.get("package").fetchFile("package.json")));
354    return res;
355  }
356
357  private static final int BUFFER_SIZE = 1024;
358
359  public static NpmPackage fromPackage(InputStream tgz) throws IOException {
360    return fromPackage(tgz, null, false);
361  }
362
363  public static NpmPackage fromPackage(InputStream tgz, String desc) throws IOException {
364    return fromPackage(tgz, desc, false);
365  }
366
367  public static NpmPackage fromPackage(InputStream tgz, String desc, boolean progress) throws IOException {
368    NpmPackage res = new NpmPackage();
369    res.readStream(tgz, desc, progress);
370    return res;
371  }
372
373  public void readStream(InputStream tgz, String desc, boolean progress) throws IOException {
374    GzipCompressorInputStream gzipIn;
375    try {
376      gzipIn = new GzipCompressorInputStream(tgz);
377    } catch (Exception e) {
378      throw new IOException("Error reading "+(desc == null ? "package" : desc)+": "+e.getMessage(), e);      
379    }
380    try (TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn)) {
381      TarArchiveEntry entry;
382
383      int i = 0;
384      int c = 12;
385      while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {
386        i++;
387        String n = entry.getName();
388        if (entry.isDirectory()) {
389          String dir = n.substring(0, n.length()-1);
390          if (dir.startsWith("package/")) {
391            dir = dir.substring(8);
392          }
393          folders.put(dir, new NpmPackageFolder(dir));
394        } else {
395          int count;
396          byte data[] = new byte[BUFFER_SIZE];
397          ByteArrayOutputStream fos = new ByteArrayOutputStream();
398          try (BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
399            while ((count = tarIn.read(data, 0, BUFFER_SIZE)) != -1) {
400              dest.write(data, 0, count);
401            }
402          }
403          fos.close();
404          loadFile(n, fos.toByteArray());
405        }
406        if (progress && i % 50 == 0) {
407          c++;
408          System.out.print(".");
409          if (c == 120) {
410            System.out.println("");
411            System.out.print("  ");
412            c = 2;
413          }
414        }
415      }
416    } 
417    try {
418      npm = JsonTrackingParser.parseJson(folders.get("package").fetchFile("package.json"));
419    } catch (Exception e) {
420      throw new IOException("Error parsing "+(desc == null ? "" : desc+"#")+"package/package.json: "+e.getMessage(), e);
421    }
422    checkIndexed(desc);
423  }
424
425  public void loadFile(String n, byte[] data) throws IOException {
426    String dir = n.contains("/") ? n.substring(0, n.lastIndexOf("/")) : "$root";
427    if (dir.startsWith("package/")) {
428      dir = dir.substring(8);
429    }
430    n = n.substring(n.lastIndexOf("/")+1);
431    NpmPackageFolder index = folders.get(dir);
432    if (index == null) {
433      index = new NpmPackageFolder(dir);
434      folders.put(dir, index);
435    }
436    index.content.put(n, data);
437  }
438
439  private void checkIndexed(String desc) throws IOException {
440    for (NpmPackageFolder folder : folders.values()) {
441      if (folder.index == null) {
442        indexFolder(desc, folder);
443      }
444    }
445  }
446
447  public void indexFolder(String desc, NpmPackageFolder folder) throws FileNotFoundException, IOException {
448    List<String> remove = new ArrayList<>();
449    NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
450    indexer.start();
451    for (String n : folder.listFiles()) {
452      if (!indexer.seeFile(n, folder.fetchFile(n))) {
453        remove.add(n);
454      }
455    } 
456    for (String n : remove) {
457      folder.removeFile(n);
458    }
459    String json = indexer.build();
460    try {
461      folder.readIndex(JsonTrackingParser.parseJson(json));
462      if (folder.folder != null) {
463        TextFile.stringToFile(json, Utilities.path(folder.folder.getAbsolutePath(), ".index.json"));
464      }
465    } catch (Exception e) {
466      TextFile.stringToFile(json, Utilities.path("[tmp]", ".index.json"));
467      throw new IOException("Error parsing "+(desc == null ? "" : desc+"#")+"package/"+folder.name+"/.index.json: "+e.getMessage(), e);
468    }
469  }
470
471
472  public static NpmPackage fromZip(InputStream stream, boolean dropRootFolder, String desc) throws IOException {
473    NpmPackage res = new NpmPackage();
474    ZipInputStream zip = new ZipInputStream(stream);
475    ZipEntry ze;
476    while ((ze = zip.getNextEntry()) != null) {
477      int size;
478      byte[] buffer = new byte[2048];
479
480      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
481      BufferedOutputStream bos = new BufferedOutputStream(bytes, buffer.length);
482
483      while ((size = zip.read(buffer, 0, buffer.length)) != -1) {
484        bos.write(buffer, 0, size);
485      }
486      bos.flush();
487      bos.close();
488      if (bytes.size() > 0) {
489        if (dropRootFolder) {
490          res.loadFile(ze.getName().substring(ze.getName().indexOf("/")+1), bytes.toByteArray());
491        } else {
492          res.loadFile(ze.getName(), bytes.toByteArray());
493        }
494      }
495      zip.closeEntry();
496    }
497    zip.close();         
498    try {
499      res.npm = JsonTrackingParser.parseJson(res.folders.get("package").fetchFile("package.json"));
500    } catch (Exception e) {
501      throw new IOException("Error parsing "+(desc == null ? "" : desc+"#")+"package/package.json: "+e.getMessage(), e);
502    }
503    res.checkIndexed(desc);
504    return res;
505  }
506
507
508  /**
509   * Accessing the contents of the package - get a list of files in a subfolder of the package 
510   *
511   * @param folder
512   * @return
513   * @throws IOException 
514   */
515  public List<String> list(String folder) throws IOException {
516    List<String> res = new ArrayList<String>();
517    if (folders.containsKey(folder)) {
518      res.addAll(folders.get(folder).listFiles());
519    } else if (folders.containsKey(Utilities.path("package", folder))) {
520      res.addAll(folders.get(Utilities.path("package", folder)).listFiles());
521    }
522    return res;
523  }
524
525  public List<String> listResources(String... types) throws IOException {
526    List<String> res = new ArrayList<String>();
527    NpmPackageFolder folder = folders.get("package");
528    for (String s : types) {
529      if (folder.types.containsKey(s))
530        res.addAll(folder.types.get(s));
531    }
532    Collections.sort(res);
533    return res;
534  }
535
536  public List<PackageResourceInformation> listIndexedResources(String... types) throws IOException {
537    List<PackageResourceInformation> res = new ArrayList<PackageResourceInformation>();
538    for (NpmPackageFolder folder : folders.values()) {
539      if (folder.index != null) {
540        for (JsonElement e : folder.index.getAsJsonArray("files")) {
541          JsonObject fi = e.getAsJsonObject();
542          if (Utilities.existsInList(JSONUtil.str(fi, "resourceType"), types)) {
543            res.add(new PackageResourceInformation(folder.folder.getAbsolutePath(), fi));
544          }
545        }
546      }
547    } 
548    //    Collections.sort(res, new PackageResourceInformationSorter());
549    return res;
550  }
551
552  /**
553   * use the name from listResources()
554   * 
555   * @param id
556   * @return
557   * @throws IOException
558   */
559  public InputStream loadResource(String file) throws IOException {
560    NpmPackageFolder folder = folders.get("package");
561    return new ByteArrayInputStream(folder.fetchFile(file));
562  }
563
564  /**
565   * get a stream that contains the contents of a resource in the base folder, by it's canonical URL
566   * 
567   * @param url - the canonical URL of the resource (exact match only)
568   * @return null if it is not found
569   * @throws IOException
570   */
571  public InputStream loadByCanonical(String canonical) throws IOException {
572    return loadByCanonicalVersion("package", canonical, null);    
573  }
574  
575  /**
576   * get a stream that contains the contents of a resource in the nominated folder, by it's canonical URL
577   * 
578   * @param folder - one of the folders in the package (main folder is "package")
579   * @param url - the canonical URL of the resource (exact match only)
580   * @return null if it is not found
581   * @throws IOException
582   */
583  public InputStream loadByCanonical(String folder, String canonical) throws IOException {
584    return loadByCanonicalVersion(folder, canonical, null);    
585  }
586    
587  /**
588   * get a stream that contains the contents of a resource in the base folder, by it's canonical URL
589   * 
590   * @param url - the canonical URL of the resource (exact match only)
591   * @param version - the specified version (or null if the most recent)
592   * 
593   * @return null if it is not found
594   * @throws IOException
595   */
596  public InputStream loadByCanonicalVersion(String canonical, String version) throws IOException {
597    return loadByCanonicalVersion("package", canonical, version);
598  }
599  
600  /**
601   * get a stream that contains the contents of a resource in the nominated folder, by it's canonical URL
602   * 
603   * @param folder - one of the folders in the package (main folder is "package")
604   * @param url - the canonical URL of the resource (exact match only)
605   * @param version - the specified version (or null if the most recent)
606   * 
607   * @return null if it is not found
608   * @throws IOException
609   */
610  public InputStream loadByCanonicalVersion(String folder, String canonical, String version) throws IOException {
611    NpmPackageFolder f = folders.get(folder);
612    List<JsonObject> matches = new ArrayList<>();
613    for (JsonElement e : f.index.getAsJsonArray("files")) {
614      JsonObject file = (JsonObject) e;
615      if (canonical.equals(JSONUtil.str(file, "url"))) {
616        if (version != null && version.equals(JSONUtil.str(file, "version"))) {
617          return load("package", JSONUtil.str(file, "filename"));
618        } else if (version == null) {
619          matches.add(file);
620        }
621      }
622      if (matches.size() > 0) {
623        if (matches.size() == 1) {
624          return load("package", JSONUtil.str(matches.get(0), "filename"));          
625        } else {
626          Collections.sort(matches, new IndexVersionSorter());
627          return load("package", JSONUtil.str(matches.get(matches.size()-1), "filename"));          
628        }
629      }
630    }
631    return null;        
632  }
633    
634  /**
635   * get a stream that contains the contents of one of the files in the base package
636   * 
637   * @param file
638   * @return
639   * @throws IOException
640   */
641  public InputStream load(String file) throws IOException {
642    return load("package", file);
643  }
644  /**
645   * get a stream that contains the contents of one of the files in a folder
646   * 
647   * @param folder
648   * @param file
649   * @return
650   * @throws IOException
651   */
652  public InputStream load(String folder, String file) throws IOException {
653    NpmPackageFolder f = folders.get(folder);
654    if (f == null) {
655      f = folders.get(Utilities.path("package", folder));
656    }
657    if (f != null && f.hasFile(file)) {
658      return new ByteArrayInputStream(f.fetchFile(file));
659    } else {
660      throw new IOException("Unable to find the file "+folder+"/"+file+" in the package "+name());
661    }
662  }
663
664  public boolean hasFile(String folder, String file) throws IOException {
665    NpmPackageFolder f = folders.get(folder);
666    if (f == null) {
667      f = folders.get(Utilities.path("package", folder));
668    }
669    return f != null && f.hasFile(file);
670  }
671
672
673  /**
674   * Handle to the package json file
675   * 
676   * @return
677   */
678  public JsonObject getNpm() {
679    return npm;
680  }
681
682  /**
683   * convenience method for getting the package name
684   * @return
685   */
686  public String name() {
687    return JSONUtil.str(npm, "name");
688  }
689
690  /**
691   * convenience method for getting the package id (which in NPM language is the same as the name)
692   * @return
693   */
694  public String id() {
695    return JSONUtil.str(npm, "name");
696  }
697
698  public String date() {
699    return JSONUtil.str(npm, "date");
700  }
701
702  public String canonical() {
703    return JSONUtil.str(npm, "canonical");
704  }
705
706  /**
707   * convenience method for getting the package version
708   * @return
709   */
710  public String version() {
711    return JSONUtil.str(npm, "version");
712  }
713
714  /**
715   * convenience method for getting the package fhir version
716   * @return
717   */
718  public String fhirVersion() {
719    if ("hl7.fhir.core".equals(JSONUtil.str(npm, "name")))
720      return JSONUtil.str(npm, "version");
721    else if (JSONUtil.str(npm, "name").startsWith("hl7.fhir.r2.") || JSONUtil.str(npm, "name").startsWith("hl7.fhir.r2b.") || JSONUtil.str(npm, "name").startsWith("hl7.fhir.r3.") || JSONUtil.str(npm, "name").startsWith("hl7.fhir.r4.") || JSONUtil.str(npm, "name").startsWith("hl7.fhir.r5."))
722      return JSONUtil.str(npm, "version");
723    else {        
724      JsonObject dep = npm.getAsJsonObject("dependencies");
725      if (dep != null) {
726        for (Entry<String, JsonElement> e : dep.entrySet()) {
727          if (Utilities.existsInList(e.getKey(), "hl7.fhir.r2.core", "hl7.fhir.r2b.core", "hl7.fhir.r3.core", "hl7.fhir.r4.core"))
728            return e.getValue().getAsString();
729          if (Utilities.existsInList(e.getKey(), "hl7.fhir.core")) // while all packages are updated
730            return e.getValue().getAsString();
731        }
732      }
733      if (npm.has("fhirVersions")) {
734        JsonElement e = npm.get("fhirVersions");
735        if (e.isJsonArray() && e.getAsJsonArray().size() > 0) {
736          return npm.getAsJsonArray("fhirVersions").get(0).getAsString();
737        }
738      }
739      if (dep != null) {
740        // legacy simplifier support:
741        if (dep.has("simplifier.core.r4"))
742          return "4.0";
743        if (dep.has("simplifier.core.r3"))
744          return "3.0";
745        if (dep.has("simplifier.core.r2"))
746          return "2.0";
747      }
748      throw new FHIRException("no core dependency or FHIR Version found in the Package definition");
749    }
750  }
751
752  public String summary() {
753    if (path != null)
754      return path;
755    else
756      return "memory";
757  }
758
759  public boolean isType(PackageType template) {
760    return template.getCode().equals(type());
761  }
762
763  public String type() {
764    return JSONUtil.str(npm, "type");
765  }
766
767  public String description() {
768    return JSONUtil.str(npm, "description");
769  }
770
771  public String getPath() {
772    return path;
773  }
774
775  public List<String> dependencies() {
776    List<String> res = new ArrayList<>();
777    if (npm.has("dependencies")) {
778      for (Entry<String, JsonElement> e : npm.getAsJsonObject("dependencies").entrySet()) {
779        res.add(e.getKey()+"#"+e.getValue().getAsString());
780      }
781    }
782    return res;
783  }
784
785  public String homepage() {
786    return JSONUtil.str(npm, "homepage");
787  }
788
789  public String url() {
790    return JSONUtil.str(npm, "url");
791  }
792
793
794  public String title() {
795    return JSONUtil.str(npm, "title");
796  }
797
798  public String toolsVersion() {
799    return JSONUtil.str(npm, "tools-version");
800  }
801
802  public String license() {
803    return JSONUtil.str(npm, "license");
804  }
805
806  //  /**
807  //   * only for use by the package manager itself
808  //   * 
809  //   * @param path
810  //   */
811  //  public void setPath(String path) {
812  //    this.path = path;
813  //  }
814
815  public String getWebLocation() {
816    if (npm.has("url")) {
817      return npm.get("url").getAsString();
818    } else {
819      return JSONUtil.str(npm, "canonical");
820    }
821  }
822
823  public InputStream loadResource(String type, String id) throws IOException {
824    NpmPackageFolder f = folders.get("package");
825    JsonArray files = f.index.getAsJsonArray("files");
826    for (JsonElement e : files) {
827      JsonObject i = (JsonObject) e;
828      if (type.equals(JSONUtil.str(i, "resourceType")) && id.equals(JSONUtil.str(i, "id"))) {
829        return load("package", JSONUtil.str(i, "filename"));
830      }
831    }
832    return null;
833  }
834
835  public InputStream loadExampleResource(String type, String id) throws IOException {
836    NpmPackageFolder f = folders.get("example");
837    if (f != null) {
838      JsonArray files = f.index.getAsJsonArray("files");
839      for (JsonElement e : files) {
840        JsonObject i = (JsonObject) e;
841        if (type.equals(JSONUtil.str(i, "resourceType")) && id.equals(JSONUtil.str(i, "id"))) {
842          return load("example", JSONUtil.str(i, "filename"));
843        }
844      }
845    }
846    return null;
847  }
848
849  /** special case when playing around inside the package **/
850  public Map<String, NpmPackageFolder> getFolders() {
851    return folders;
852  }
853
854  public void save(File directory) throws IOException {
855    File dir = new File(Utilities.path(directory.getAbsolutePath(), name()));
856    if (!dir.exists()) {
857      Utilities.createDirectory(dir.getAbsolutePath());
858    } else {
859      Utilities.clearDirectory(dir.getAbsolutePath());
860    }
861    
862    for (NpmPackageFolder folder : folders.values()) {
863      String n = folder.name;
864
865      File pd = new File(Utilities.path(dir.getAbsolutePath(), n));
866      if (!pd.exists()) {
867        Utilities.createDirectory(pd.getAbsolutePath());
868      }
869      NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
870      indexer.start();
871      for (String s : folder.content.keySet()) {
872        byte[] b = folder.content.get(s);
873        indexer.seeFile(s, b);
874        if (!s.equals(".index.json") && !s.equals("package.json")) {
875          TextFile.bytesToFile(b, Utilities.path(dir.getAbsolutePath(), n, s));
876        }
877      }
878      byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
879      TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), n, ".index.json"));
880    }
881    byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
882    TextFile.bytesToFile(cnt, Utilities.path(dir.getAbsolutePath(), "package", "package.json"));
883  }
884  
885  public void save(OutputStream stream) throws IOException {
886    TarArchiveOutputStream tar;
887    ByteArrayOutputStream OutputStream;
888    BufferedOutputStream bufferedOutputStream;
889    GzipCompressorOutputStream gzipOutputStream;
890
891    OutputStream = new ByteArrayOutputStream();
892    bufferedOutputStream = new BufferedOutputStream(OutputStream);
893    gzipOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
894    tar = new TarArchiveOutputStream(gzipOutputStream);
895
896
897    for (NpmPackageFolder folder : folders.values()) {
898      String n = folder.name;
899      if (!"package".equals(n) && !(n.startsWith("package/") || n.startsWith("package\\"))) {
900        n = "package/"+n;
901      }
902      NpmPackageIndexBuilder indexer = new NpmPackageIndexBuilder();
903      indexer.start();
904      for (String s : folder.content.keySet()) {
905        byte[] b = folder.content.get(s);
906        String name = n+"/"+s;
907        indexer.seeFile(s, b);
908        if (!s.equals(".index.json") && !s.equals("package.json")) {
909          TarArchiveEntry entry = new TarArchiveEntry(name);
910          entry.setSize(b.length);
911          tar.putArchiveEntry(entry);
912          tar.write(b);
913          tar.closeArchiveEntry();
914        }
915      }
916      byte[] cnt = indexer.build().getBytes(StandardCharsets.UTF_8);
917      TarArchiveEntry entry = new TarArchiveEntry(n+"/.index.json");
918      entry.setSize(cnt.length);
919      tar.putArchiveEntry(entry);
920      tar.write(cnt);
921      tar.closeArchiveEntry();
922    }
923    byte[] cnt = TextFile.stringToBytes(new GsonBuilder().setPrettyPrinting().create().toJson(npm), false);
924    TarArchiveEntry entry = new TarArchiveEntry("package/package.json");
925    entry.setSize(cnt.length);
926    tar.putArchiveEntry(entry);
927    tar.write(cnt);
928    tar.closeArchiveEntry();
929
930    tar.finish();
931    tar.close();
932    gzipOutputStream.close();
933    bufferedOutputStream.close();
934    OutputStream.close();
935    byte[] b = OutputStream.toByteArray();
936    stream.write(b);
937  }
938
939  /**
940   * Keys are resource type names, values are filenames
941   */
942  public Map<String, List<String>> getTypes() {
943    return folders.get("package").types;
944  }
945
946  public String fhirVersionList() {
947    if (npm.has("fhirVersions")) {
948      CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
949      for (JsonElement n : npm.getAsJsonArray("fhirVersions")) {
950        b.append(n.getAsString());
951      }
952      return b.toString();
953    } else
954      return "";
955  }
956
957  public String dependencySummary() {
958    if (npm.has("dependencies")) {
959      CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
960      for (Entry<String, JsonElement> e : npm.getAsJsonObject("dependencies").entrySet()) {
961        b.append(e.getKey()+"#"+e.getValue().getAsString());
962      }
963      return b.toString();
964    } else
965      return "";
966  }
967
968  public void unPack(String dir) throws IOException {
969    unPack (dir, false);
970  }
971
972  public void unPackWithAppend(String dir) throws IOException {
973    unPack (dir, true);
974  }
975
976  public void unPack(String dir, boolean withAppend) throws IOException {
977    for (NpmPackageFolder folder : folders.values()) {
978      String dn = folder.getName();
979      if (!dn.equals("package") && (dn.startsWith("package/") || dn.startsWith("package\\"))) {
980        dn = dn.substring(8);
981      }
982      if (dn.equals("$root")) {
983        dn = dir;
984      } else {
985         dn = Utilities.path(dir, dn);
986      }
987      Utilities.createDirectory(dn);
988      for (String s : folder.listFiles()) {
989        String fn = Utilities.path(dn, s);
990        File f = new File(fn);
991        if (withAppend && f.getName().startsWith("_append.")) {
992          String appendFn = Utilities.path(dn, s.substring(8));
993          if (new File(appendFn).exists())
994            TextFile.appendBytesToFile(folder.fetchFile(s), appendFn);        
995          else
996            TextFile.bytesToFile(folder.fetchFile(s), appendFn);        
997        } else
998          TextFile.bytesToFile(folder.fetchFile(s), fn);
999      }
1000//      if (path != null)
1001//        FileUtils.copyDirectory(new File(path), new File(dir));      
1002    }
1003  }
1004
1005  public void debugDump(String purpose) {
1006//    System.out.println("Debug Dump of Package for '"+purpose+"'. Path = "+path);
1007//    System.out.println("  npm = "+name()+"#"+version()+", canonical = "+canonical());
1008//    System.out.println("  folders = "+folders.size());
1009//    for (String s : sorted(folders.keySet())) {
1010//      NpmPackageFolder folder = folders.get(s);
1011//      System.out.println("    "+folder.dump());
1012//    }
1013  }
1014
1015  private List<String> sorted(Set<String> keys) {
1016    List<String> res = new ArrayList<String>();
1017    res.addAll(keys);
1018    Collections.sort(res);
1019    return res ;
1020  }
1021
1022  public void clearFolder(String folderName) {
1023    NpmPackageFolder folder = folders.get(folderName);
1024    folder.content.clear();
1025    folder.types.clear();    
1026  }
1027
1028  public void deleteFolder(String folderName) {
1029    folders.remove(folderName);
1030  }
1031
1032  public void addFile(String folderName, String name, byte[] cnt, String type) {
1033    if (!folders.containsKey(folderName)) {
1034      folders.put(folderName, new NpmPackageFolder(folderName));
1035    }
1036    NpmPackageFolder folder = folders.get(folderName);
1037    folder.content.put(name, cnt);
1038    if (!folder.types.containsKey(type))
1039      folder.types.put(type, new ArrayList<>());
1040    folder.types.get(type).add(name);
1041  }
1042
1043  public void loadAllFiles() throws IOException {
1044    for (String folder : folders.keySet()) {
1045      NpmPackageFolder pf = folders.get(folder);
1046      String p = folder.contains("$") ? path : Utilities.path(path, folder);
1047      for (File f : new File(p).listFiles()) {
1048        if (!f.isDirectory() && !isInternalExemptFile(f)) {
1049          pf.getContent().put(f.getName(), TextFile.fileToBytes(f));
1050        }
1051      }
1052    }
1053  }
1054
1055  public boolean isChangedByLoader() {
1056    return changedByLoader;
1057  }
1058
1059  public boolean isCore() {
1060    return "fhir.core".equals(JSONUtil.str(npm, "type"));
1061  }
1062
1063  public boolean hasCanonical(String url) {
1064    if (url == null) {
1065      return false;
1066    }
1067    String u = url.contains("|") ?  url.substring(0, url.indexOf("|")) : url;
1068    String v = url.contains("|") ?  url.substring(url.indexOf("|")+1) : null;
1069    NpmPackageFolder folder = folders.get("package");
1070    if (folder != null) {
1071      for (JsonElement e : folder.index.getAsJsonArray("files")) {
1072        JsonObject o = (JsonObject) e;
1073        if (u.equals(JSONUtil.str(o, "url"))) {
1074          if (v == null || v.equals(JSONUtil.str(o, "version"))) {
1075            return true;
1076          }
1077        }
1078      }
1079    }
1080    return false;
1081  }
1082
1083  public boolean canLazyLoad() {
1084    for (NpmPackageFolder folder : folders.values()) {
1085      if (folder.folder == null) {        
1086        return false;
1087      }
1088    }
1089    return true;
1090  }
1091  
1092  
1093}