Search in sources :

Example 1 with NPMPackageGenerator

use of org.hl7.fhir.r4.utils.NPMPackageGenerator in project org.hl7.fhir.core by hapifhir.

the class ExtensionDefinitionGenerator method savePackage.

private void savePackage(List<StructureDefinition> extensions) throws FHIRException, IOException {
    JsonObject npm = new JsonObject();
    npm.addProperty("name", "hl7.fhir.extensions.r" + sourceVersion.toCode(1));
    npm.addProperty("version", targetVersion.toCode(3));
    npm.addProperty("tools-version", ToolsVersion.TOOLS_VERSION);
    npm.addProperty("type", PackageType.IG.getCode());
    npm.addProperty("license", SPDXLicense.CC01_0.toCode());
    npm.addProperty("canonical", "http://hl7.org/fhir/" + sourceVersion.toCode(3) + "/extensions/" + targetVersion.toCode(3));
    npm.addProperty("url", "http://hl7.org/fhir/" + sourceVersion.toCode(3) + "/extensions/" + targetVersion.toCode(3));
    npm.addProperty("title", "Extension Definitions for representing elements from " + sourceVersion.toCode() + " in " + targetVersion.toCode());
    npm.addProperty("description", "Extension Definitions for representing elements from " + sourceVersion.toCode() + " in " + targetVersion.toCode() + " built " + new SimpleDateFormat("EEE, MMM d, yyyy HH:mmZ", new Locale("en", "US")).format(Calendar.getInstance().getTime()) + timezone() + ")");
    JsonObject dep = new JsonObject();
    npm.add("dependencies", dep);
    dep.addProperty("hl7.fhir.core", targetVersion.toCode());
    npm.addProperty("author", "FHIR Project");
    JsonArray m = new JsonArray();
    JsonObject md = new JsonObject();
    m.add(md);
    md.addProperty("name", "FHIR Project");
    md.addProperty("url", "http://hl7.org/fhir");
    NPMPackageGenerator pi = new NPMPackageGenerator(filename, npm);
    for (StructureDefinition sd : extensions) {
        byte[] cnt = saveResource(sd, targetVersion);
        pi.addFile(Category.RESOURCE, "StructureDefinition-" + sd.getId() + ".json", cnt);
    }
    pi.finish();
}
Also used : JsonArray(com.google.gson.JsonArray) StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) JsonObject(com.google.gson.JsonObject) NPMPackageGenerator(org.hl7.fhir.r4.utils.NPMPackageGenerator) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with NPMPackageGenerator

use of org.hl7.fhir.r4.utils.NPMPackageGenerator in project org.hl7.fhir.core by hapifhir.

the class DicomPackageBuilder method execute.

public void execute() throws FileNotFoundException, ParserConfigurationException, SAXException, IOException {
    CodeSystem cs = buildCodeSystem();
    String fn = Utilities.path(dest, pattern.replace("{version}", version));
    Utilities.createDirectory(Utilities.getDirectoryForFile(fn));
    NPMPackageGenerator gen = new NPMPackageGenerator(fn, buildPackage());
    int i = 2;
    gen.addFile(Category.RESOURCE, "CodeSystem-" + cs.getId() + ".json", new JsonParser().setOutputStyle(OutputStyle.NORMAL).composeBytes(cs));
    ValueSet vs = buildAllValueSet();
    gen.addFile(Category.RESOURCE, "ValueSet-" + vs.getId() + ".json", new JsonParser().setOutputStyle(OutputStyle.NORMAL).composeBytes(vs));
    Set<String> ids = new HashSet<>();
    ids.add(vs.getId());
    for (File f : new File(Utilities.path(source, "Resources", "valuesets", "fhir", "json")).listFiles()) {
        vs = (ValueSet) JsonParser.loadFile(new FileInputStream(f));
        vs.setVersion(version);
        if (vs.getId().length() > 64) {
            vs.setId(vs.getId().substring(0, 64));
        }
        if (ids.contains(vs.getId())) {
            throw new Error("Duplicate Id (note Ids cut off at 64 char): " + vs.getId());
        }
        ids.add(vs.getId());
        gen.addFile(Category.RESOURCE, "ValueSet-" + vs.getId() + ".json", new JsonParser().setOutputStyle(OutputStyle.NORMAL).composeBytes(vs));
        i++;
    }
    gen.finish();
    System.out.println("Finished - " + i + " resources");
}
Also used : NPMPackageGenerator(org.hl7.fhir.r4.utils.NPMPackageGenerator) CodeSystem(org.hl7.fhir.r4.model.CodeSystem) ValueSet(org.hl7.fhir.r4.model.ValueSet) File(java.io.File) FileInputStream(java.io.FileInputStream) JsonParser(org.hl7.fhir.r4.formats.JsonParser) HashSet(java.util.HashSet)

Example 3 with NPMPackageGenerator

use of org.hl7.fhir.r4.utils.NPMPackageGenerator in project kindling by HL7.

the class SpecNPMPackageGenerator method generate.

// 
// public static void main(String[] args) throws Exception {
// //    generateForVersion("F:\\fhir\\web\\DSTU2", "http://hl7.org/fhir/DSTU2");
// generateForVersion("E:\\fhir\\web\\2016May", "http://hl7.org/fhir/2016May", new Date());
// //    generateForVersion("F:\\fhir\\web\\STU3", "http://hl7.org/fhir/2016STU3");
// System.out.println("Done");
// }
// private static void generateForVersion(String folder, String url, Date genDate) throws Exception {
// SpecNPMPackageGenerator self = new SpecNPMPackageGenerator();
// self.generate(folder, url, false, genDate);
// }
// 
public void generate(String folder, String url, boolean forWeb, Date genDate, String pidRoot) throws Exception {
    System.out.println("Generate Package for " + folder);
    Map<String, byte[]> files = loadZip(new FileInputStream(Utilities.path(folder, "igpack.zip")));
    FHIRVersion version = determineVersion(files);
    System.out.println(" .. Loading v" + version);
    SpecMapManager spm = new SpecMapManager(files.get("spec.internals"), version.toCode());
    System.out.println(" .. Conformance Resources");
    List<ResourceEntry> reslist = makeResourceList(files, version.toCode());
    System.out.println(" .. Other Resources");
    addToResList(folder, reslist, version.toCode());
    System.out.println(" .. building IG");
    ImplementationGuide ig = new ImplementationGuide();
    ig.setId("fhir");
    ig.setUrl("http://hl7.org/fhir/ImplementationGuide/fhir");
    ig.setVersion(version.toCode());
    ig.addFhirVersion(version);
    ig.setLicense(SPDXLicense.CC01_0);
    ig.setTitle("FHIR Core package");
    ig.setDescription("FHIR Core package - the NPM package that contains all the definitions for the base FHIR specification");
    ig.setPublisher("HL7 Inc");
    ig.getContactFirstRep().getTelecomFirstRep().setSystem(ContactPointSystem.URL).setValue("http://hl7.org/fhir");
    ig.setPackageId(pidRoot + ".core");
    ig.getManifest().setRendering(url);
    for (ResourceEntry e : reslist) {
        ManifestResourceComponent r = ig.getManifest().addResource();
        r.setReference(new Reference(e.type + "/" + e.id));
        if (e.conf)
            r.setExample(new BooleanType(true));
        r.setRelativePath(spm.getPath(e.canonical, null));
    }
    for (String k : files.keySet()) {
        if (k.endsWith(".png") || k.endsWith(".gif"))
            ig.getManifest().addImage(k);
        else if (k.endsWith(".css"))
            ig.getManifest().addOther(k);
    }
    Map<String, ManifestPageComponent> map = new HashMap<String, ManifestPageComponent>();
    for (String k : spm.getPages()) {
        ManifestPageComponent pp = ig.getManifest().addPage();
        pp.setName(k).setTitle(spm.getPage(k));
        map.put(pp.getName(), pp);
    }
    for (String k : spm.getTargets()) {
        String n = null;
        String f = null;
        if (k.contains("#")) {
            n = k.substring(0, k.indexOf("#"));
            f = k.substring(k.indexOf("#") + 1);
        } else
            n = k;
        ManifestPageComponent p = map.get(n);
        if (p == null) {
            p = ig.getManifest().addPage();
            p.setName(n);
            map.put(p.getName(), p);
        }
        if (f != null)
            p.addAnchor(f);
    }
    // ok ig is full loaded...
    System.out.println(" .. Building NPM Package");
    NPMPackageGenerator npm = new NPMPackageGenerator(Utilities.path(folder, pidRoot + ".core.tgz"), "http://hl7.org/fhir", url, PackageType.CORE, ig, genDate, true);
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    new org.hl7.fhir.r5.formats.JsonParser().setOutputStyle(OutputStyle.NORMAL).compose(bs, ig);
    // npm.addFile(Category.RESOURCE, "ig-r4.json", bs.toByteArray());
    addConvertedIg(npm, ig, version.toCode());
    for (ResourceEntry e : reslist) {
        npm.addFile(Category.RESOURCE, e.type + "-" + e.id + ".json", e.json);
    }
    for (String k : files.keySet()) {
        if (k.endsWith(".png") || k.endsWith(".css") || k.endsWith(".template") || k.endsWith(".zip") || k.endsWith(".gif") || k.equals("spec.internals") || k.equals("mappingSpaces.details"))
            npm.addFile(Category.OTHER, k, files.get(k));
    }
    for (String fn : new File(folder).list()) {
        if (fn.endsWith(".schema.json") || fn.endsWith(".openapi.json")) {
            byte[] b = TextFile.fileToBytes(Utilities.path(folder, fn));
            npm.addFile(Category.OPENAPI, fn, b);
        }
        if (fn.endsWith(".xsd") || fn.endsWith(".sch")) {
            byte[] b = TextFile.fileToBytes(Utilities.path(folder, fn));
            npm.addFile(Category.SCHEMATRON, fn, b);
        }
    }
    npm.finish();
    System.out.println(" .. Built");
    System.out.println(" .. Building NPM Package (xml)");
    ig.setId("fhir-xml");
    ig.setUrl("http://hl7.org/fhir/ImplementationGuide/fhir-xml");
    ig.setTitle("FHIR Core package (XML Conformance files)");
    ig.setDescription("FHIR Core package - the NPM package that contains all the definitions for the base FHIR specification (XML)");
    ig.setPackageId(pidRoot + ".corexml");
    npm = new NPMPackageGenerator(Utilities.path(folder, pidRoot + ".corexml.tgz"), "http://hl7.org/fhir", url, PackageType.CORE, ig, genDate, true);
    bs = new ByteArrayOutputStream();
    new org.hl7.fhir.r5.formats.XmlParser().setOutputStyle(OutputStyle.NORMAL).compose(bs, ig);
    npm.addFile(Category.RESOURCE, "ig-r4.json", bs.toByteArray());
    addConvertedIgX(npm, ig, version.toCode());
    for (ResourceEntry e : reslist) {
        if (e.xml != null)
            npm.addFile(Category.RESOURCE, e.type + "-" + e.id + ".xml", e.xml);
    }
    npm.finish();
    System.out.println(" .. Built");
}
Also used : SpecMapManager(org.hl7.fhir.tools.publisher.SpecMapManager) XmlParser(org.hl7.fhir.r5.formats.XmlParser) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) HashMap(java.util.HashMap) Reference(org.hl7.fhir.r5.model.Reference) BooleanType(org.hl7.fhir.r5.model.BooleanType) NPMPackageGenerator(org.hl7.fhir.r5.utils.NPMPackageGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream) FHIRVersion(org.hl7.fhir.r5.model.Enumerations.FHIRVersion) ManifestResourceComponent(org.hl7.fhir.r5.model.ImplementationGuide.ManifestResourceComponent) ManifestPageComponent(org.hl7.fhir.r5.model.ImplementationGuide.ManifestPageComponent) File(java.io.File) IniFile(org.hl7.fhir.utilities.IniFile) TextFile(org.hl7.fhir.utilities.TextFile) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 4 with NPMPackageGenerator

use of org.hl7.fhir.r4.utils.NPMPackageGenerator in project kindling by HL7.

the class Publisher method produceSpec.

private void produceSpec() throws Exception {
    for (ImplementationGuideDefn ig : page.getDefinitions().getSortedIgs()) {
        for (LogicalModel lm : ig.getLogicalModels()) {
            page.log(" ...logical model " + lm.getId(), LogMessageType.Process);
            produceLogicalModel(lm, ig);
        }
    }
    for (StructureDefinition ed : page.getWorkerContext().getExtensionDefinitions()) {
        String filename = "extension-" + (ed.getUrl().startsWith("http://fhir-registry.smarthealthit.org/StructureDefinition/") ? ed.getUrl().substring(59).toLowerCase() : ed.getUrl().substring(40).toLowerCase());
        ed.setUserData("filename", filename);
        ImplementationGuideDefn ig = page.getDefinitions().getIgs().get(ed.getUserString(ToolResourceUtilities.NAME_RES_IG));
        if (ig == null) {
            ig = page.getDefinitions().getIgs().get("core");
        }
        ed.setUserData("path", (ig.isCore() ? "" : ig.getCode() + File.separator) + filename + ".html");
    }
    page.updateDiffEngineDefinitions();
    loadValueSets2();
    page.log(" ...extensions", LogMessageType.Process);
    for (StructureDefinition ae : page.getWorkerContext().getExtensionDefinitions()) produceExtensionDefinition(ae);
    checkAllOk();
    page.log(" ...resource identities", LogMessageType.Process);
    for (String rname : page.getDefinitions().getBaseResources().keySet()) {
        ResourceDefn r = page.getDefinitions().getBaseResources().get(rname);
        produceResource1(r, r.isAbstract());
    }
    for (String rname : page.getDefinitions().sortedResourceNames()) {
        if (!rname.equals("ValueSet") && !rname.equals("CodeSystem") && wantBuild(rname)) {
            ResourceDefn r = page.getDefinitions().getResources().get(rname);
            produceResource1(r, false);
        }
    }
    if (buildFlags.get("all")) {
        page.log(" ...base profiles", LogMessageType.Process);
        produceBaseProfile();
    }
    for (String rname : page.getDefinitions().getBaseResources().keySet()) {
        ResourceDefn r = page.getDefinitions().getBaseResources().get(rname);
        page.log(" ...resource " + r.getName(), LogMessageType.Process);
        produceResource2(r, !rname.equals("Parameters"), rname.equals("Resource") ? "Meta" : null, false);
    }
    for (String rname : page.getDefinitions().sortedResourceNames()) {
        if (!rname.equals("ValueSet") && !rname.equals("CodeSystem") && wantBuild(rname)) {
            ResourceDefn r = page.getDefinitions().getResources().get(rname);
            page.log(" ...resource " + r.getName(), LogMessageType.Process);
            produceResource2(r, false, null, false);
        }
    }
    for (Compartment c : page.getDefinitions().getCompartments()) {
        if (buildFlags.get("all")) {
            page.log(" ...compartment " + c.getName(), LogMessageType.Process);
            produceCompartment(c);
        }
    }
    Regenerator regen = new Regenerator(page.getFolders().srcDir, page.getDefinitions(), page.getWorkerContext());
    regen.generate();
    Bundle searchParamsFeed = new Bundle();
    searchParamsFeed.setId("searchParams");
    searchParamsFeed.setType(BundleType.COLLECTION);
    searchParamsFeed.setMeta(new Meta().setLastUpdated(page.getResourceBundle().getMeta().getLastUpdated()));
    Set<String> uris = new HashSet<String>();
    for (ResourceDefn rd : page.getDefinitions().getBaseResources().values()) addSearchParams(uris, searchParamsFeed, rd);
    for (String n : page.getDefinitions().sortedResourceNames()) {
        ResourceDefn rd = page.getDefinitions().getResources().get(n);
        addSearchParams(uris, searchParamsFeed, rd);
    }
    for (Profile cp : page.getDefinitions().getPackList()) {
        addSearchParams(uris, searchParamsFeed, cp);
    }
    checkBundleURLs(searchParamsFeed);
    for (String n : page.getIni().getPropertyNames("pages")) {
        if (buildFlags.get("all") || buildFlags.get("page-" + n.toLowerCase())) {
            page.log(" ...page " + n, LogMessageType.Process);
            producePage(n, page.getIni().getStringProperty("pages", n));
        }
    }
    for (ImplementationGuideDefn ig : page.getDefinitions().getSortedIgs()) {
        for (String n : ig.getPageList()) {
            page.log(" ...ig page " + n, LogMessageType.Process);
            produceIgPage(n, ig);
        }
        for (ImplementationGuideDefinitionPageComponent page : ig.getSpecialPages()) {
            produceIgPage(ig, page);
        }
        for (Profile p : ig.getProfiles()) {
            if (!p.getOperations().isEmpty()) {
                produceIgOperations(ig, p);
            }
        }
    }
    if (page.getIni().getPropertyNames("ig-pages") != null) {
        for (String n : page.getIni().getPropertyNames("ig-pages")) {
            page.log(" ...page " + n, LogMessageType.Process);
            for (ImplementationGuideDefn ig : page.getDefinitions().getSortedIgs()) {
                if (!ig.isCore())
                    produceIgPage(n, ig, page.getIni().getStringProperty("ig-pages", n));
            }
        }
    }
    for (String n : page.getDefinitions().getDictionaries().keySet()) {
        if (buildFlags.get("all")) {
            // || buildFlags.get("dict-" + n.toLowerCase())) {
            page.log(" ...dictionary " + n, LogMessageType.Process);
            produceDictionary(page.getDefinitions().getDictionaries().get(n));
        }
    }
    int i = 0;
    for (String n : page.getIni().getPropertyNames("sid")) {
        page.log(" ...sid " + n, LogMessageType.Process);
        produceSid(i, n, page.getIni().getStringProperty("sid", n));
        i++;
    }
    if (buildFlags.get("all")) {
        page.log(" ...check Fragments", LogMessageType.Process);
        checkFragments();
        for (Profile p : page.getDefinitions().getPackList()) {
            // if (!n.startsWith("http://")) {
            page.log(" ...Profile " + p.getId(), LogMessageType.Process);
            produceConformancePackage(null, p, null);
        // }
        }
        produceUml();
        page.getVsValidator().checkDuplicates(page.getValidationErrors());
        if (buildFlags.get("all")) {
            // if (page.getToc().containsKey("1.1"))
            // throw new Exception("Duplicate DOC Entry "+"1.1");
            page.getToc().put("1.1", new TocEntry("1.1", "Table Of Contents", "toc.html", false));
            page.log(" ...page toc.html", LogMessageType.Process);
            producePage("toc.html", null);
        }
        checkAllOk();
        page.log(" ...collections ", LogMessageType.Process);
        com.google.gson.JsonObject diff = new com.google.gson.JsonObject();
        page.getDiffEngine().getDiffAsJson(diff);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String json = gson.toJson(diff);
        TextFile.stringToFile(json, Utilities.path(page.getFolders().dstDir, "fhir.diff.json"));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element element = doc.createElement("difference");
        doc.appendChild(element);
        page.getDiffEngine().getDiffAsXml(doc, element);
        prettyPrint(doc, Utilities.path(page.getFolders().dstDir, "fhir.diff.xml"));
        checkBundleURLs(page.getResourceBundle());
        checkStructureDefinitions(page.getResourceBundle());
        page.getResourceBundle().getEntry().sort(new ProfileBundleSorter());
        FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + "profiles-resources.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, page.getResourceBundle());
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "profiles-resources.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, page.getResourceBundle());
        s.close();
        checkBundleURLs(page.getTypeBundle());
        checkStructureDefinitions(page.getTypeBundle());
        page.getTypeBundle().getEntry().sort(new ProfileBundleSorter());
        s = new FileOutputStream(page.getFolders().dstDir + "profiles-types.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, page.getTypeBundle());
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "profiles-types.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, page.getTypeBundle());
        s.close();
        Bundle extensionsFeed = new Bundle();
        extensionsFeed.setId("extensions");
        extensionsFeed.setType(BundleType.COLLECTION);
        extensionsFeed.setMeta(new Meta().setLastUpdated(page.getResourceBundle().getMeta().getLastUpdated()));
        Set<String> urls = new HashSet<String>();
        for (StructureDefinition ed : page.getWorkerContext().getExtensionDefinitions()) {
            if (!urls.contains(ed.getUrl())) {
                urls.add(ed.getUrl());
                extensionsFeed.getEntry().add(new BundleEntryComponent().setResource(ed).setFullUrl("http://hl7.org/fhir/" + ed.fhirType() + "/" + ed.getId()));
            }
        }
        checkBundleURLs(extensionsFeed);
        checkStructureDefinitions(extensionsFeed);
        s = new FileOutputStream(page.getFolders().dstDir + "extension-definitions.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, extensionsFeed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "extension-definitions.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, extensionsFeed);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "extension-definitions.xml", page.getFolders().dstDir + "examples" + File.separator + "extension-definitions.xml");
        s = new FileOutputStream(page.getFolders().dstDir + "search-parameters.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, searchParamsFeed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "search-parameters.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, searchParamsFeed);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "search-parameters.xml", page.getFolders().dstDir + "examples" + File.separator + "search-parameters.xml");
        for (ResourceDefn rd : page.getDefinitions().getResources().values()) addOtherProfiles(profileBundle, rd);
        for (Profile cp : page.getDefinitions().getPackList()) {
            addOtherProfiles(profileBundle, cp);
        }
        checkBundleURLs(profileBundle);
        checkStructureDefinitions(profileBundle);
        s = new FileOutputStream(page.getFolders().dstDir + "profiles-others.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, profileBundle);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "profiles-others.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, profileBundle);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "profiles-others.xml", page.getFolders().dstDir + "examples" + File.separator + "profiles-others.xml");
        // todo-bundle - should this be checked?
        // int ec = 0;
        // for (Resource e : valueSetsFeed.getItem()) {
        // ValueSet vs = (ValueSet) e;
        // if (!vs.getUrl().equals(e.getId())) {
        // ec++;
        // page.log("Valueset id mismatch: atom entry has '"+e.getId()+"', but value set is '"+vs.getUrl()+"'", LogMessageType.Error);
        // }
        // }
        // if (ec > 0)
        // throw new Exception("Cannot continue due to value set mis-identification");
        checkBundleURLs(dataElements);
        s = new FileOutputStream(page.getFolders().dstDir + "dataelements.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, dataElements);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "dataelements.xml", page.getFolders().dstDir + "examples" + File.separator + "dataelements.xml");
        s = new FileOutputStream(page.getFolders().dstDir + "dataelements.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, dataElements);
        s.close();
        checkBundleURLs(valueSetsFeed);
        s = new FileOutputStream(page.getFolders().dstDir + "valuesets.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, valueSetsFeed);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "valuesets.xml", page.getFolders().dstDir + "examples" + File.separator + "valuesets.xml");
        s = new FileOutputStream(page.getFolders().dstDir + "valuesets.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, valueSetsFeed);
        s.close();
        checkBundleURLs(conceptMapsFeed);
        s = new FileOutputStream(page.getFolders().dstDir + "conceptmaps.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, conceptMapsFeed);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "conceptmaps.xml", page.getFolders().dstDir + "examples" + File.separator + "conceptmaps.xml");
        s = new FileOutputStream(page.getFolders().dstDir + "conceptmaps.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, conceptMapsFeed);
        s.close();
        checkBundleURLs(externals);
        s = new FileOutputStream(page.getFolders().dstDir + "external-resources.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, externals);
        s.close();
        Utilities.copyFile(page.getFolders().dstDir + "external-resources.xml", page.getFolders().dstDir + "examples" + File.separator + "external-resources.xml");
        s = new FileOutputStream(page.getFolders().dstDir + "external-resources.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, externals);
        s.close();
        ImplementationGuide expIg = new ImplementationGuide();
        expIg.addFhirVersion(page.getVersion());
        expIg.setPackageId(pidRoot() + ".expansions");
        expIg.setVersion(page.getVersion().toCode());
        expIg.setLicense(ImplementationGuide.SPDXLicense.CC01_0);
        expIg.setTitle("FHIR " + page.getVersion().getDisplay() + " package : Expansions");
        expIg.setDescription("Expansions for the " + page.getVersion().getDisplay() + " version of the FHIR standard");
        NPMPackageGenerator npm = new NPMPackageGenerator(Utilities.path(page.getFolders().dstDir, pidRoot() + ".expansions.tgz"), "http://hl7.org/fhir", "http://hl7.org/fhir", PackageType.CORE, expIg, page.getGenDate().getTime(), true);
        Bundle expansionFeed = new Bundle();
        Set<String> urlset = new HashSet<>();
        expansionFeed.setId("valueset-expansions");
        expansionFeed.setType(BundleType.COLLECTION);
        expansionFeed.setMeta(new Meta().setLastUpdated(page.getGenDate().getTime()));
        expansionFeed.getFormatCommentsPre().add("This collection contains expansions for all the value sets that are used on an element of type \r\n" + "'code', to help with code generation (saves the code generator having to figure out how to \r\n" + "do the expansions or find a terminology server that supports the same version of the value sets");
        for (ValueSet vs : page.getValueSets().getList()) {
            if (!urlset.contains(vs.getUrl())) {
                urlset.add(vs.getUrl());
                if (vs.getUserData(ToolResourceUtilities.NAME_VS_USE_MARKER) != null) {
                    ValueSet evs = null;
                    if (vs.hasUserData("expansion"))
                        evs = (ValueSet) vs.getUserData("expansion");
                    else {
                        ValueSetExpansionOutcome vse = page.getWorkerContext().expandVS(vs, true, false);
                        if (vse.getValueset() != null) {
                            evs = vse.getValueset();
                            vs.setUserData("expansion", evs);
                        }
                    }
                    if (evs != null) {
                        ValueSet vsc = vs.copy();
                        vsc.setText(null);
                        vsc.setExpansion(evs.getExpansion());
                        expansionFeed.addEntry().setFullUrl("http://hl7.org/fhir/" + vsc.fhirType() + "/" + vsc.getId()).setResource(vsc);
                        npm.addFile(Category.RESOURCE, "ValueSet-" + vsc.getId() + ".json", new JsonParser().composeBytes(vsc));
                    }
                }
            }
        }
        npm.finish();
        if (!isCIBuild) {
            String id = pidRoot() + ".expansions";
            new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).addPackageToCache(id, "current", new FileInputStream(Utilities.path(page.getFolders().dstDir, id + ".tgz")), Utilities.path(page.getFolders().dstDir, id + ".tgz"));
        }
        s = new FileOutputStream(page.getFolders().dstDir + "expansions.xml");
        new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, expansionFeed);
        s.close();
        s = new FileOutputStream(page.getFolders().dstDir + "expansions.json");
        new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, expansionFeed);
        s.close();
        produceComparisons();
        produceSpecMap();
        processRDF();
        page.log("....version maps", LogMessageType.Process);
        ZipGenerator zip = new ZipGenerator(page.getFolders().dstDir + "r3r4maps.zip");
        zip.addFiles(Utilities.path(page.getFolders().rootDir, "implementations", "r3maps", "R3toR4", ""), "r3/", null, null);
        zip.addFiles(Utilities.path(page.getFolders().rootDir, "implementations", "r3maps", "R4toR3", ""), "r4/", null, null);
        zip.close();
        page.log("....definitions", LogMessageType.Process);
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions.xml.zip");
        zip.addFileName("version.info", page.getFolders().dstDir + "version.info", false);
        zip.addFileName("profiles-types.xml", page.getFolders().dstDir + "profiles-types.xml", false);
        zip.addFileName("profiles-resources.xml", page.getFolders().dstDir + "profiles-resources.xml", false);
        zip.addFileName("profiles-others.xml", page.getFolders().dstDir + "profiles-others.xml", false);
        zip.addFileName("extension-definitions.xml", page.getFolders().dstDir + "extension-definitions.xml", false);
        zip.addFileName("search-parameters.xml", page.getFolders().dstDir + "search-parameters.xml", false);
        zip.addFileName("valuesets.xml", page.getFolders().dstDir + "valuesets.xml", false);
        zip.addFileName("conceptmaps.xml", page.getFolders().dstDir + "conceptmaps.xml", false);
        zip.addFileName("dataelements.xml", page.getFolders().dstDir + "dataelements.xml", false);
        zip.addFileName("fhir-all-xsd.zip", page.getFolders().dstDir + "fhir-all-xsd.zip", false);
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions.json.zip");
        zip.addFileName("version.info", page.getFolders().dstDir + "version.info", false);
        zip.addFileName("profiles-types.json", page.getFolders().dstDir + "profiles-types.json", false);
        zip.addFileName("profiles-resources.json", page.getFolders().dstDir + "profiles-resources.json", false);
        zip.addFileName("profiles-others.json", page.getFolders().dstDir + "profiles-others.json", false);
        zip.addFileName("extension-definitions.json", page.getFolders().dstDir + "extension-definitions.json", false);
        zip.addFileName("search-parameters.json", page.getFolders().dstDir + "search-parameters.json", false);
        zip.addFileName("valuesets.json", page.getFolders().dstDir + "valuesets.json", false);
        zip.addFileName("conceptmaps.json", page.getFolders().dstDir + "conceptmaps.json", false);
        zip.addFileName("dataelements.json", page.getFolders().dstDir + "dataelements.json", false);
        zip.addFileName("fhir.schema.json.zip", page.getFolders().dstDir + "fhir.schema.json.zip", false);
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions.xlsx.zip");
        for (String rn : page.getDefinitions().sortedResourceNames()) {
            zip.addFileName(rn.toLowerCase() + ".xlsx", page.getFolders().dstDir + rn.toLowerCase() + ".xlsx", false);
        }
        zip.close();
        // this is the actual package used by the validator.
        zip = new ZipGenerator(page.getFolders().dstDir + "validator.pack");
        // conformance resources
        zip.addFileName("profiles-types.json", page.getFolders().dstDir + "profiles-types.json", false);
        zip.addFileName("profiles-resources.json", page.getFolders().dstDir + "profiles-resources.json", false);
        zip.addFileName("profiles-others.json", page.getFolders().dstDir + "profiles-others.json", false);
        zip.addFileName("extension-definitions.json", page.getFolders().dstDir + "extension-definitions.json", false);
        zip.addFileName("valuesets.json", page.getFolders().dstDir + "valuesets.json", false);
        zip.addFileName("conceptmaps.json", page.getFolders().dstDir + "conceptmaps.json", false);
        // native schema
        zip.addFileName("fhir-all-xsd.zip", page.getFolders().dstDir + "fhir-all-xsd.zip", false);
        zip.addFileName("fhir.schema.json.zip", page.getFolders().dstDir + "fhir.schema.json.zip", false);
        zip.addFileName("fhir.shex", page.getFolders().dstDir + "fhir.shex", false);
        zip.close();
        page.log("....dstu3 format (xml)", LogMessageType.Process);
        DSTU3ValidationConvertor dstu3 = new DSTU3ValidationConvertor();
        dstu3.convert(page.getFolders().dstDir + "profiles-types.xml", page.getFolders().tmpDir + "profiles-types-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "profiles-resources.xml", page.getFolders().tmpDir + "profiles-resources-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "profiles-others.xml", page.getFolders().tmpDir + "profiles-others-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "extension-definitions.xml", page.getFolders().tmpDir + "extension-definitions-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "search-parameters.xml", page.getFolders().tmpDir + "search-parameters-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "valuesets.xml", page.getFolders().tmpDir + "valuesets-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "conceptmaps.xml", page.getFolders().tmpDir + "conceptmaps-r3.xml");
        dstu3.convert(page.getFolders().dstDir + "dataelements.xml", page.getFolders().tmpDir + "dataelements-r3.xml");
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions-r3.xml.zip");
        zip.addFileName("profiles-types.xml", page.getFolders().tmpDir + "profiles-types-r3.xml", false);
        zip.addFileName("profiles-resources.xml", page.getFolders().tmpDir + "profiles-resources-r3.xml", false);
        zip.addFileName("profiles-others.xml", page.getFolders().tmpDir + "profiles-others-r3.xml", false);
        zip.addFileName("extension-definitions.xml", page.getFolders().tmpDir + "extension-definitions-r3.xml", false);
        zip.addFileName("search-parameters.xml", page.getFolders().tmpDir + "search-parameters-r3.xml", false);
        zip.addFileName("valuesets.xml", page.getFolders().tmpDir + "valuesets-r3.xml", false);
        zip.addFileName("conceptmaps.xml", page.getFolders().tmpDir + "conceptmaps-r3.xml", false);
        zip.addFileName("dataelements.xml", page.getFolders().tmpDir + "dataelements-r3.xml", false);
        zip.close();
        page.log("....dstu3 format (json)", LogMessageType.Process);
        dstu3.convertJ(page.getFolders().dstDir + "profiles-types.xml", page.getFolders().tmpDir + "profiles-types-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "profiles-resources.xml", page.getFolders().tmpDir + "profiles-resources-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "profiles-others.xml", page.getFolders().tmpDir + "profiles-others-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "extension-definitions.xml", page.getFolders().tmpDir + "extension-definitions-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "search-parameters.xml", page.getFolders().tmpDir + "search-parameters-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "valuesets.xml", page.getFolders().tmpDir + "valuesets-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "conceptmaps.xml", page.getFolders().tmpDir + "conceptmaps-r3.json");
        dstu3.convertJ(page.getFolders().dstDir + "dataelements.xml", page.getFolders().tmpDir + "dataelements-r3.json");
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions-r3.json.zip");
        zip.addFileName("profiles-types.json", page.getFolders().tmpDir + "profiles-types-r3.json", false);
        zip.addFileName("profiles-resources.json", page.getFolders().tmpDir + "profiles-resources-r3.json", false);
        zip.addFileName("profiles-others.json", page.getFolders().tmpDir + "profiles-others-r3.json", false);
        zip.addFileName("extension-definitions.json", page.getFolders().tmpDir + "extension-definitions-r3.json", false);
        zip.addFileName("search-parameters.json", page.getFolders().tmpDir + "search-parameters-r3.json", false);
        zip.addFileName("valuesets.json", page.getFolders().tmpDir + "valuesets-r3.json", false);
        zip.addFileName("conceptmaps.json", page.getFolders().tmpDir + "conceptmaps-r3.json", false);
        zip.addFileName("dataelements.json", page.getFolders().tmpDir + "dataelements-r3.json", false);
        zip.close();
        System.gc();
        page.log("....r4 in r5 format", LogMessageType.Process);
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions-r2asr3.xml.zip");
        page.getDiffEngine().saveR4AsR5(zip, FhirFormat.XML);
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "definitions-r2asr3.json.zip");
        page.getDiffEngine().saveR4AsR5(zip, FhirFormat.JSON);
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "all-valuesets.zip");
        zip.addFileName("valuesets.xml", page.getFolders().dstDir + "valuesets.xml", false);
        zip.addFileName("valuesets.json", page.getFolders().dstDir + "valuesets.json", false);
        zip.addFileName("conceptmaps.xml", page.getFolders().dstDir + "conceptmaps.xml", false);
        zip.addFileName("conceptmaps.json", page.getFolders().dstDir + "conceptmaps.json", false);
        zip.close();
        page.log("....IG Builder Resources", LogMessageType.Process);
        zip = new ZipGenerator(page.getFolders().tmpDir + "ig-template.zip");
        zip.addFolder(Utilities.path(page.getFolders().rootDir, "tools", "ig"), "", false, null);
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "igpack.zip");
        zip.addFileName("fhir.css", page.getFolders().dstDir + "fhir.css", false);
        zip.addFileName("spec.internals", page.getFolders().dstDir + "spec.internals", false);
        zip.addFileName("profiles-types.xml", page.getFolders().dstDir + "profiles-types.xml", false);
        zip.addFileName("profiles-resources.xml", page.getFolders().dstDir + "profiles-resources.xml", false);
        zip.addFileName("profiles-others.xml", page.getFolders().dstDir + "profiles-others.xml", false);
        zip.addFileName("extension-definitions.xml", page.getFolders().dstDir + "extension-definitions.xml", false);
        zip.addFileName("search-parameters.xml", page.getFolders().dstDir + "search-parameters.xml", false);
        zip.addFileName("valuesets.xml", page.getFolders().dstDir + "valuesets.xml", false);
        zip.addFileName("conceptmaps.xml", page.getFolders().dstDir + "conceptmaps.xml", false);
        zip.addFileName("dataelements.xml", page.getFolders().dstDir + "dataelements.xml", false);
        zip.addFileName("version.info", page.getFolders().dstDir + "version.info", false);
        zip.addFileName("mappingSpaces.details", page.getFolders().srcDir + "mappingSpaces.xml", false);
        zip.addFileName("redirect.asp.template", page.getFolders().srcDir + "redirect.asp", false);
        zip.addFileName("redirect.cgi.template", page.getFolders().srcDir + "redirect.cgi", false);
        zip.addFileName("redirect.php.template", page.getFolders().srcDir + "redirect.php", false);
        zip.addFileName("ig-template.zip", Utilities.path(page.getFolders().tmpDir, "ig-template.zip"), false);
        zip.addFiles(Utilities.path(page.getFolders().rootDir, "publish", ""), "", ".png", null);
        zip.addFiles(Utilities.path(page.getFolders().rootDir, "publish", ""), "", ".gif", null);
        zip.close();
        page.log("....IG Builder (2)", LogMessageType.Process);
        SpecNPMPackageGenerator self = new SpecNPMPackageGenerator();
        self.generate(page.getFolders().dstDir, page.getBaseURL(), false, page.getGenDate().getTime(), pidRoot());
        if (!isCIBuild) {
            new FilesystemPackageCacheManager(true, ToolsVersion.TOOLS_VERSION).addPackageToCache(pidRoot() + ".core", "current", new FileInputStream(Utilities.path(page.getFolders().dstDir, pidRoot() + ".core.tgz")), Utilities.path(page.getFolders().dstDir, pidRoot() + ".core.tgz"));
        }
        page.log(" ...zips", LogMessageType.Process);
        zip = new ZipGenerator(page.getFolders().dstDir + "examples.zip");
        zip.addFiles(page.getFolders().dstDir + "examples" + File.separator, "", null, "expansions.xml");
        zip.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "examples-json.zip");
        gson = new GsonBuilder().setPrettyPrinting().create();
        File f = new CSFile(page.getFolders().dstDir);
        File[] files = f.listFiles();
        String[] noExt = new String[] { ".schema.json", ".canonical.json", ".diff.json", "expansions.json", "package.json", "choice-elements.json", "backbone-elements.json" };
        for (int fi = 0; fi < files.length; fi++) {
            if (files[fi].isFile() && (files[fi].getName().endsWith(".json"))) {
                boolean ok = true;
                for (String n : noExt) {
                    ok = ok && !files[fi].getName().endsWith(n);
                }
                if (ok) {
                    JsonObject jr = JSONUtil.parse(TextFile.fileToString(files[fi]));
                    if (!jr.has("url")) {
                        JsonObject meta = JSONUtil.forceObject(jr, "meta");
                        JsonArray labels = JSONUtil.forceArray(meta, "tag");
                        JsonObject label = JSONUtil.addObj(labels);
                        label.addProperty("system", "http://terminology.hl7.org/CodeSystem/v3-ActReason");
                        label.addProperty("code", "HTEST");
                        label.addProperty("display", "test health data");
                    }
                    String jrs = gson.toJson(jr);
                    zip.addBytes(files[fi].getName(), jrs.getBytes(Charsets.UTF_8), true);
                }
            }
        }
        zip.close();
        NDJsonWriter ndjson = new NDJsonWriter(page.getFolders().dstDir + "examples-ndjson.zip", page.getFolders().tmpDir);
        ndjson.addFilesFiltered(page.getFolders().dstDir, ".json", new String[] { ".schema.json", ".canonical.json", ".diff.json", "expansions.json", "package.json" });
        ndjson.close();
        zip = new ZipGenerator(page.getFolders().dstDir + "examples-ttl.zip");
        zip.addFilesFiltered(page.getFolders().dstDir, "", ".ttl", new String[0]);
        zip.close();
        page.log("Check HTML Links", LogMessageType.Process);
        page.getHTMLChecker().produce();
        checkAllOk();
    } else
        page.log("Partial Build - terminating now", LogMessageType.Error);
}
Also used : Meta(org.hl7.fhir.r5.model.Meta) JsonObject(com.google.gson.JsonObject) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) NDJsonWriter(org.hl7.fhir.utilities.NDJsonWriter) Compartment(org.hl7.fhir.definitions.model.Compartment) Element(org.w3c.dom.Element) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) CSFile(org.hl7.fhir.utilities.CSFile) Document(org.w3c.dom.Document) XhtmlDocument(org.hl7.fhir.utilities.xhtml.XhtmlDocument) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) Profile(org.hl7.fhir.definitions.model.Profile) DSTU3ValidationConvertor(org.hl7.fhir.tools.converters.DSTU3ValidationConvertor) LogicalModel(org.hl7.fhir.definitions.model.LogicalModel) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ValueSet(org.hl7.fhir.r5.model.ValueSet) HashSet(java.util.HashSet) JsonParser(org.hl7.fhir.r5.formats.JsonParser) XmlParser(org.hl7.fhir.r5.formats.XmlParser) FilesystemPackageCacheManager(org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager) GsonBuilder(com.google.gson.GsonBuilder) SpecNPMPackageGenerator(org.hl7.fhir.tools.converters.SpecNPMPackageGenerator) Bundle(org.hl7.fhir.r5.model.Bundle) NPMPackageGenerator(org.hl7.fhir.r5.utils.NPMPackageGenerator) SpecNPMPackageGenerator(org.hl7.fhir.tools.converters.SpecNPMPackageGenerator) ImplementationGuideDefn(org.hl7.fhir.definitions.model.ImplementationGuideDefn) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) FileInputStream(java.io.FileInputStream) JsonArray(com.google.gson.JsonArray) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) Regenerator(org.hl7.fhir.definitions.parsers.Regenerator) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) ValueSetExpansionOutcome(org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome) ImplementationGuideDefinitionPageComponent(org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideDefinitionPageComponent) ZipGenerator(org.hl7.fhir.utilities.ZipGenerator) IniFile(org.hl7.fhir.utilities.IniFile) File(java.io.File) CSFile(org.hl7.fhir.utilities.CSFile) TextFile(org.hl7.fhir.utilities.TextFile)

Aggregations

File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 HashSet (java.util.HashSet)2 NPMPackageGenerator (org.hl7.fhir.r4.utils.NPMPackageGenerator)2 JsonParser (org.hl7.fhir.r5.formats.JsonParser)2 XmlParser (org.hl7.fhir.r5.formats.XmlParser)2 ImplementationGuide (org.hl7.fhir.r5.model.ImplementationGuide)2 NPMPackageGenerator (org.hl7.fhir.r5.utils.NPMPackageGenerator)2 IniFile (org.hl7.fhir.utilities.IniFile)2 TextFile (org.hl7.fhir.utilities.TextFile)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1