Search in sources :

Example 1 with JsonParser

use of org.hl7.fhir.r4.formats.JsonParser in project kindling by HL7.

the class Publisher method produceProfiledTypeProfile.

private void produceProfiledTypeProfile(ProfiledType pt) throws Exception {
    String fn = pt.getName().toLowerCase() + ".profile.xml";
    StructureDefinition rp = pt.getProfile();
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + fn);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".canonical.xml"));
    new XmlParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".json"));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".canonical.json"));
    new JsonParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    Utilities.copyFile(new CSFile(page.getFolders().dstDir + fn), new CSFile(Utilities.path(page.getFolders().dstDir, "examples", fn)));
    addToResourceFeed(rp, page.getTypeBundle(), (fn));
    cloneToXhtml(pt.getName().toLowerCase() + ".profile", "StructureDefinition for " + pt.getName(), false, "profile-instance:type:" + pt.getName(), "Type", null, wg("mnm"));
    jsonToXhtml(pt.getName().toLowerCase() + ".profile", "StructureDefinition for " + pt.getName(), resource2Json(rp), "profile-instance:type:" + pt.getName(), "Type", null, wg("mnm"));
    ttlToXhtml(pt.getName().toLowerCase() + ".profile", "StructureDefinition for " + pt.getName(), resource2Ttl(rp), "profile-instance:type:" + pt.getName(), "Type", null, wg("mnm"));
    String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, rp);
    TextFile.stringToFile(shex, Utilities.changeFileExt(page.getFolders().dstDir + fn, ".shex"));
    shexToXhtml(pt.getName().toLowerCase(), "ShEx statement for " + pt.getName(), shex, "profile-instance:type:" + pt.getName(), "Type", null, wg("mnm"));
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) FileOutputStream(java.io.FileOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ShExGenerator(org.hl7.fhir.r5.conformance.ShExGenerator)

Example 2 with JsonParser

use of org.hl7.fhir.r4.formats.JsonParser in project kindling by HL7.

the class Publisher method resource2Json.

private String resource2Json(Resource r) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
    json.setSuppressXhtml("Snipped for Brevity");
    json.compose(bytes, r);
    bytes.close();
    return new String(bytes.toByteArray());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 3 with JsonParser

use of org.hl7.fhir.r4.formats.JsonParser in project kindling by HL7.

the class Publisher method generateCodeSystemRegistry.

private void generateCodeSystemRegistry() throws FileNotFoundException, IOException, Exception {
    XmlParser xml = new XmlParser();
    xml.setOutputStyle(OutputStyle.PRETTY);
    Bundle bnd = (Bundle) xml.parse(new CSFileInputStream(Utilities.path(page.getFolders().srcDir, "namingsystem", "namingsystem-terminologies.xml")));
    for (BundleEntryComponent entry : bnd.getEntry()) {
        NamingSystem ns = (NamingSystem) entry.getResource();
        entry.setFullUrl("http://hl7.org/fhir/NamingSystem/" + ns.getId());
        String url = null;
        for (NamingSystemUniqueIdComponent t : ns.getUniqueId()) {
            if (t.getType() == NamingSystemIdentifierType.URI)
                url = t.getValue();
        }
        if (url != null) {
            if (url.startsWith("http://hl7.org/fhir"))
                page.getDefinitions().addNs(url, "System " + ns.getName(), "terminologies-systems.html#" + url);
            page.getDefinitions().addNs(entry.getFullUrl(), ns.getId(), "terminologies-systems.html#" + url);
        }
    }
    List<String> names = new ArrayList<String>();
    Set<String> urls = new HashSet<>();
    names.addAll(page.getCodeSystems().keys());
    Collections.sort(names);
    for (String n : names) {
        CodeSystem cs = page.getCodeSystems().get(n);
        if (cs != null && !urls.contains(cs.getUrl()) && cs.hasUrl() && !cs.getUrl().startsWith("http://terminology.hl7.org")) {
            urls.add(cs.getUrl());
            if (cs.hasName()) {
                NamingSystem ns = new NamingSystem();
                ns.setId(cs.getId());
                ns.setName(cs.getName());
                ns.setStatus(cs.getStatus());
                if (!ns.hasStatus())
                    ns.setStatus(PublicationStatus.DRAFT);
                ns.setKind(NamingSystemType.CODESYSTEM);
                ns.setPublisher(cs.getPublisher());
                for (ContactDetail c : cs.getContact()) {
                    ContactDetail nc = ns.addContact();
                    nc.setName(c.getName());
                    for (ContactPoint cc : c.getTelecom()) {
                        nc.getTelecom().add(cc);
                    }
                }
                ns.setDate(cs.getDate());
                if (!ns.hasDate())
                    ns.setDate(page.getGenDate().getTime());
                ns.setDescription(cs.getDescription());
                ns.addUniqueId().setType(NamingSystemIdentifierType.URI).setValue(cs.getUrl()).setPreferred(true);
                String oid = CodeSystemUtilities.getOID(cs);
                if (oid != null) {
                    if (oid.startsWith("urn:oid:"))
                        oid = oid.substring(8);
                    ns.addUniqueId().setType(NamingSystemIdentifierType.OID).setValue(oid).setPreferred(false);
                }
                ns.setUserData("path", cs.getUserData("path"));
                bnd.addEntry().setResource(ns).setFullUrl("http://hl7.org/fhir/" + ns.fhirType() + "/" + ns.getId());
            }
        }
    }
    xml.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.xml")), bnd);
    cloneToXhtml("namingsystem-terminologies", "Terminology Registry", false, "resource-instance:NamingSystem", "Terminology Registry", null, wg("vocab"));
    xml.setOutputStyle(OutputStyle.CANONICAL);
    xml.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.canonical.xml")), bnd);
    JsonParser json = new JsonParser();
    json.setOutputStyle(OutputStyle.PRETTY);
    json.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.json")), bnd);
    jsonToXhtml("namingsystem-terminologies", "Terminology Registry", TextFile.fileToString(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.json")), "resource-instance:NamingSystem", "Terminology Registry", null, wg("vocab"));
    json.setOutputStyle(OutputStyle.CANONICAL);
    json.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.canonical.json")), bnd);
    RdfParser rdf = new RdfParser();
    rdf.setOutputStyle(OutputStyle.PRETTY);
    rdf.compose(new FileOutputStream(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.ttl")), bnd);
    ttlToXhtml("namingsystem-terminologies", "Terminology Registry", TextFile.fileToString(Utilities.path(page.getFolders().dstDir, "namingsystem-terminologies.ttl")), "resource-instance:NamingSystem", "Terminology Registry", null, wg("vocab"));
    StringBuilder b = new StringBuilder();
    b.append("<table class=\"grid\">\r\n");
    b.append(" <tr>");
    b.append("<td><b>Name</b></td>");
    b.append("<td><b>Uri</b></td>");
    b.append("<td><b>OID</b></td>");
    b.append("</tr>\r\n");
    for (BundleEntryComponent entry : bnd.getEntry()) {
        NamingSystem ns = (NamingSystem) entry.getResource();
        String uri = "";
        String oid = "";
        for (NamingSystemUniqueIdComponent id : ns.getUniqueId()) {
            if (id.getType() == NamingSystemIdentifierType.URI)
                uri = id.getValue();
            if (id.getType() == NamingSystemIdentifierType.OID)
                oid = id.getValue();
        }
        String link = "terminologies-systems.html#" + uri;
        if (ns.getUserData("path") != null)
            link = ns.getUserString("path");
        b.append(" <tr>");
        b.append("<td><a href=\"" + link + "\">" + Utilities.escapeXml(ns.getName()) + "</a></td>");
        b.append("<td>" + Utilities.escapeXml(uri) + "</td>");
        b.append("<td>" + Utilities.escapeXml(oid) + "</td>");
        b.append("</tr>\r\n");
    }
    b.append("</table>\r\n");
    String html = TextFile.fileToString(page.getFolders().templateDir + "template-example.html").replace("<%example%>", b.toString()).replace("<%example-usage%>", "");
    html = page.processPageIncludes("namingsystem-terminologies.html", html, "resource-instance:NamingSystem", null, bnd, null, "Example", null, null, page.getDefinitions().getWorkgroups().get("fhir"));
    TextFile.stringToFile(html, page.getFolders().dstDir + "namingsystem-terminologies.html");
    cachePage("namingsystem-terminologies.html", html, "Registered Code Systems", false);
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) NamingSystemUniqueIdComponent(org.hl7.fhir.r5.model.NamingSystem.NamingSystemUniqueIdComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) Bundle(org.hl7.fhir.r5.model.Bundle) ArrayList(java.util.ArrayList) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) ContactDetail(org.hl7.fhir.r5.model.ContactDetail) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) NamingSystem(org.hl7.fhir.r5.model.NamingSystem) FileOutputStream(java.io.FileOutputStream) CSFileInputStream(org.hl7.fhir.utilities.CSFileInputStream) HashSet(java.util.HashSet) JsonParser(org.hl7.fhir.r5.formats.JsonParser) RdfParser(org.hl7.fhir.r5.formats.RdfParser)

Example 4 with JsonParser

use of org.hl7.fhir.r4.formats.JsonParser in project kindling by HL7.

the class Publisher method generateProfile.

private StructureDefinition generateProfile(ResourceDefn root, String n, String xmlSpec, String jsonSpec, String ttlSpec, boolean gen) throws Exception, FileNotFoundException {
    StructureDefinition rp = root.getProfile();
    page.getProfiles().see(rp, page.packageInfo());
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + n + ".profile.xml");
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + n + ".profile.canonical.xml");
    new XmlParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + n + ".profile.json");
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + n + ".profile.canonical.json");
    new JsonParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    Utilities.copyFile(new CSFile(page.getFolders().dstDir + n + ".profile.xml"), new CSFile(page.getFolders().dstDir + "examples" + File.separator + n + ".profile.xml"));
    if (buildFlags.get("all")) {
        addToResourceFeed(rp, page.getResourceBundle(), null);
    }
    if (gen) {
        saveAsPureHtml(rp, new FileOutputStream(page.getFolders().dstDir + "html" + File.separator + n + ".html"));
        cloneToXhtml(n + ".profile", "StructureDefinition for " + n, true, "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg());
        jsonToXhtml(n + ".profile", "StructureDefinition for " + n, resource2Json(rp), "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg());
        ttlToXhtml(n + ".profile", "StructureDefinition for " + n, resource2Ttl(rp), "profile-instance:resource:" + root.getName(), "Profile", root, root.getWg());
        String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, rp);
        TextFile.stringToFile(shex, page.getFolders().dstDir + n + ".shex");
        shexToXhtml(n, "ShEx statement for " + n, shex, "profile-instance:type:" + root.getName(), "Type", root, root.getWg());
    }
    return rp;
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) FileOutputStream(java.io.FileOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ShExGenerator(org.hl7.fhir.r5.conformance.ShExGenerator)

Example 5 with JsonParser

use of org.hl7.fhir.r4.formats.JsonParser in project kindling by HL7.

the class Publisher method producePrimitiveTypeProfile.

private void producePrimitiveTypeProfile(DefinedCode type) throws Exception {
    String fn = type.getCode().toLowerCase() + ".profile.xml";
    StructureDefinition rp = type.getProfile();
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + fn);
    new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".canonical.xml"));
    new XmlParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".json"));
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(s, rp);
    s.close();
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(fn, ".canonical.json"));
    new JsonParser().setOutputStyle(OutputStyle.CANONICAL).compose(s, rp);
    s.close();
    String shex = new ShExGenerator(page.getWorkerContext()).generate(HTMLLinkPolicy.NONE, rp);
    TextFile.stringToFile(shex, Utilities.changeFileExt(page.getFolders().dstDir + fn, ".shex"));
    Utilities.copyFile(new CSFile(page.getFolders().dstDir + fn), new CSFile(Utilities.path(page.getFolders().dstDir, "examples", fn)));
    addToResourceFeed(rp, page.getTypeBundle(), (fn));
    // saveAsPureHtml(rp, new FileOutputStream(page.getFolders().dstDir+ "html"
    // + File.separator + "datatypes.html"));
    cloneToXhtml(type.getCode().toLowerCase() + ".profile", "StructureDefinition for " + type.getCode(), false, "profile-instance:type:" + type.getCode(), "Type", null, wg("mnm"));
    jsonToXhtml(type.getCode().toLowerCase() + ".profile", "StructureDefinition for " + type.getCode(), resource2Json(rp), "profile-instance:type:" + type.getCode(), "Type", null, wg("mnm"));
    ttlToXhtml(type.getCode().toLowerCase() + ".profile", "StructureDefinition for " + type.getCode(), resource2Ttl(rp), "profile-instance:type:" + type.getCode(), "Type", null, wg("mnm"));
    shexToXhtml(type.getCode().toLowerCase(), "ShEx statement for " + type.getCode(), shex, "profile-instance:type:" + type.getCode(), "Type", null, wg("mnm"));
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) FileOutputStream(java.io.FileOutputStream) CSFile(org.hl7.fhir.utilities.CSFile) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ShExGenerator(org.hl7.fhir.r5.conformance.ShExGenerator)

Aggregations

FileOutputStream (java.io.FileOutputStream)82 JsonParser (org.hl7.fhir.r5.formats.JsonParser)66 FHIRException (org.hl7.fhir.exceptions.FHIRException)58 FileInputStream (java.io.FileInputStream)53 IOException (java.io.IOException)49 XmlParser (org.hl7.fhir.r5.formats.XmlParser)44 ByteArrayOutputStream (java.io.ByteArrayOutputStream)41 File (java.io.File)36 JsonParser (org.hl7.fhir.r4.formats.JsonParser)35 CSFileInputStream (org.hl7.fhir.utilities.CSFileInputStream)29 JsonParser (org.hl7.fhir.dstu3.formats.JsonParser)26 JsonParser (org.hl7.fhir.r4b.formats.JsonParser)26 CSFile (org.hl7.fhir.utilities.CSFile)26 ArrayList (java.util.ArrayList)23 TextFile (org.hl7.fhir.utilities.TextFile)20 InputStream (java.io.InputStream)18 IParser (org.hl7.fhir.r5.formats.IParser)18 Resource (org.hl7.fhir.dstu3.model.Resource)17 FileNotFoundException (java.io.FileNotFoundException)16 Resource (org.hl7.fhir.r4.model.Resource)16