001package org.hl7.fhir.convertors.misc;
002
003import com.google.gson.JsonObject;
004import org.hl7.fhir.exceptions.FHIRFormatError;
005import org.hl7.fhir.utilities.Utilities;
006import org.hl7.fhir.utilities.json.JsonTrackingParser;
007import org.hl7.fhir.utilities.npm.NpmPackage;
008
009import java.io.File;
010import java.io.FileInputStream;
011import java.io.FileOutputStream;
012import java.io.IOException;
013
014public class CorePackageTools {
015
016  public static void main(String[] args) throws FHIRFormatError, IOException {
017    if ("-xml".equals(args[0])) {
018      new CorePackageTools().buildXml(args[1], args[2], args[3]);
019    }
020    if ("-pack".equals(args[0])) {
021      new CorePackageTools().buildPackage(args[1], args[2]);
022    }
023  }
024
025  private void buildPackage(String path, String output) throws IOException {
026    NpmPackage npm = NpmPackage.fromFolder(path);
027    npm.loadAllFiles();
028    npm.save(new FileOutputStream(output));
029
030  }
031
032  private void buildXml(String json, String xml, String version) throws FHIRFormatError, IOException {
033    for (File f : new File(Utilities.path(json, "package")).listFiles()) {
034      if (f.getName().endsWith(".json")) {
035        JsonObject j = new JsonTrackingParser().parseJson(f);
036        if (j.has("resourceType")) {
037          if ("1.4".equals(version)) {
038            String n = f.getName();
039            System.out.println(n);
040            String xn = Utilities.changeFileExt(n, ".xml");
041            org.hl7.fhir.dstu2016may.model.Resource r = new org.hl7.fhir.dstu2016may.formats.JsonParser().parse(new FileInputStream(f));
042            new org.hl7.fhir.dstu2016may.formats.XmlParser().setOutputStyle(org.hl7.fhir.dstu2016may.formats.IParser.OutputStyle.NORMAL).compose(new FileOutputStream(Utilities.path(xml, "package", xn)), r);
043          }
044        }
045      }
046    }
047  }
048}