001package org.hl7.fhir.utilities.npm;
002
003import java.io.IOException;
004import java.io.InputStream;
005import java.util.ArrayList;
006import java.util.HashSet;
007import java.util.List;
008import java.util.Set;
009
010import org.hl7.fhir.utilities.TextFile;
011import org.hl7.fhir.utilities.json.JSONUtil;
012import org.hl7.fhir.utilities.json.JsonTrackingParser;
013
014import com.google.gson.JsonObject;
015
016public class PackageScanner {
017
018  
019  public static void main(String[] args) throws IOException {
020    List<String> output = new ArrayList<>();
021    Set<String> packages = new HashSet<>();
022
023    processServer("http://packages.fhir.org", output, packages);
024    processServer("http://packages2.fhir.org/packages", output, packages);
025    
026    StringBuilder b = new StringBuilder();
027    for (String s : output) {
028      b.append(s);
029      b.append("\r\n");
030      System.out.println(s);
031    }
032    TextFile.stringToFile(b.toString(), "c:\\temp\\packages.csv");
033  }
034
035  public static void processServer(String server, List<String> output, Set<String> packages) throws IOException {
036    System.out.println("Server: "+server);
037    PackageClient client = new PackageClient(server);
038    List<PackageInfo> list = client.search(null, null, null, false);
039    output.add("id\tversion\tcanonica\tfhir version\tfhir-versions\tkind\ttype\tsource");
040    for (PackageInfo pi : list) {
041      System.out.print("  fetch: "+pi.getId());
042      List<PackageInfo> versions = null;
043      while (versions == null) {
044        System.out.print("-");
045        try {
046          versions = client.getVersions(pi.getId());
047        } catch (Exception e) {
048          // nothing
049        }
050      }
051      for (PackageInfo piv : versions) {
052        if (!packages.contains(pi.getId()+"#"+piv.getVersion())) {
053          packages.add(pi.getId()+"#"+piv.getVersion());
054          try {
055            System.out.print(".");
056            InputStream cnt = client.fetch(pi.getId(), piv.getVersion());
057            NpmPackage pck = NpmPackage.fromPackage(cnt);
058            JsonObject json = pck.getNpm();
059            String fv;
060            try {
061              fv = pck.fhirVersion();
062            } catch (Exception e) {
063              fv = "--";
064            }
065            output.add(pck.name()+"\t"+pck.version()+"\t"+pck.canonical()+"\t"+fv+'\t'+pck.fhirVersionList()+'\t'+JSONUtil.str(json, "kind")+'\t'+JSONUtil.str(json, "type")+'\t'+JsonTrackingParser.writeDense(json));          } catch (Exception e) {
066            System.out.println("Error acessing "+pi.getId()+"#"+piv.getVersion()+": "+e.getMessage());
067            e.printStackTrace();
068          }
069        }
070      }
071      System.out.println("!");
072    }
073  }
074
075}