Search in sources :

Example 6 with IParser

use of org.hl7.fhir.r5.formats.IParser in project kindling by HL7.

the class Publisher method generateCodeSystemPart2.

private void generateCodeSystemPart2(CodeSystem cs) throws Exception {
    String n = cs.getUserString("filename");
    if (n == null)
        n = "codesystem-" + cs.getId();
    ImplementationGuideDefn ig = (ImplementationGuideDefn) cs.getUserData(ToolResourceUtilities.NAME_RES_IG);
    if (ig != null)
        n = ig.getCode() + File.separator + n;
    if (cs.getText().getDiv().allChildrenAreText() && (Utilities.noString(cs.getText().getDiv().allText()) || !cs.getText().getDiv().allText().matches(".*\\w.*"))) {
        RenderingContext lrc = page.getRc().copy().setLocalPrefix(ig != null ? "../" : "").setTooCostlyNoteEmpty(PageProcessor.TOO_MANY_CODES_TEXT_EMPTY).setTooCostlyNoteNotEmpty(PageProcessor.TOO_MANY_CODES_TEXT_NOT_EMPTY);
        RendererFactory.factory(cs, lrc).render(cs);
    }
    page.getVsValidator().validate(page.getValidationErrors(), n, cs, true, false);
    if (isGenerate) {
        // page.log(" ... "+n, LogMessageType.Process);
        addToResourceFeed(cs, valueSetsFeed, null);
        if (cs.getUserData("path") == null)
            cs.setUserData("path", n + ".html");
        page.setId(cs.getId());
        String sf;
        WorkGroup wg = wg(cs, "vocab");
        try {
            sf = page.processPageIncludes(n + ".html", TextFile.fileToString(page.getFolders().templateDir + "template-cs.html"), "codeSystem", null, n + ".html", cs, null, "Value Set", ig, null, wg);
        } catch (Exception e) {
            throw new Exception("Error processing " + n + ".html: " + e.getMessage(), e);
        }
        sf = addSectionNumbers(n + ".html", "template-codesystem", sf, csCounter(), ig == null ? 0 : 1, null, ig);
        TextFile.stringToFile(sf, page.getFolders().dstDir + n + ".html");
        try {
            String src = page.processPageIncludesForBook(n + ".html", TextFile.fileToString(page.getFolders().templateDir + "template-cs-book.html"), "codeSystem", cs, ig, null);
            cachePage(n + ".html", src, "Code System " + n, false);
            page.setId(null);
        } catch (Exception e) {
            throw new Exception("Error processing " + n + ".html: " + e.getMessage(), e);
        }
        IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
        FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + n + ".json");
        json.compose(s, cs);
        s.close();
        json = new JsonParser().setOutputStyle(OutputStyle.CANONICAL);
        s = new FileOutputStream(page.getFolders().dstDir + n + ".canonical.json");
        json.compose(s, cs);
        s.close();
        IParser xml = new XmlParser().setOutputStyle(OutputStyle.PRETTY);
        s = new FileOutputStream(page.getFolders().dstDir + n + ".xml");
        xml.compose(s, cs);
        s.close();
        xml = new XmlParser().setOutputStyle(OutputStyle.CANONICAL);
        s = new FileOutputStream(page.getFolders().dstDir + n + ".canonical.xml");
        xml.compose(s, cs);
        s.close();
        // System.out.println(vs.getUrl());
        cloneToXhtml(n, "Definition for Code System " + cs.getName(), false, "codesystem-instance", "Code System", null, wg);
        jsonToXhtml(n, "Definition for Code System " + cs.getName(), resource2Json(cs), "codesystem-instance", "Code System", null, wg);
        ttlToXhtml(n, "Definition for Code System " + cs.getName(), resource2Ttl(cs), "codesystem-instance", "Code System", null, wg);
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) WorkGroup(org.hl7.fhir.definitions.model.WorkGroup) XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) ImplementationGuideDefn(org.hl7.fhir.definitions.model.ImplementationGuideDefn) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 7 with IParser

use of org.hl7.fhir.r5.formats.IParser in project kindling by HL7.

the class Publisher method generateConceptMap.

private void generateConceptMap(ConceptMap cm) throws Exception {
    String filename = cm.getUserString("path");
    RenderingContext lrc = page.getRc().copy().setLocalPrefix("").setTooCostlyNoteEmpty(PageProcessor.TOO_MANY_CODES_TEXT_EMPTY).setTooCostlyNoteNotEmpty(PageProcessor.TOO_MANY_CODES_TEXT_NOT_EMPTY);
    RendererFactory.factory(cm, lrc).render(cm);
    IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
    FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(filename, ".json"));
    json.compose(s, cm);
    s.close();
    json = new JsonParser().setOutputStyle(OutputStyle.CANONICAL);
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(filename, ".canonical.json"));
    json.compose(s, cm);
    s.close();
    String n = Utilities.changeFileExt(filename, "");
    jsonToXhtml(n, cm.getName(), resource2Json(cm), "conceptmap-instance", "Concept Map", null, wg("vocab"));
    ttlToXhtml(n, cm.getName(), resource2Ttl(cm), "conceptmap-instance", "Concept Map", null, wg("vocab"));
    IParser xml = new XmlParser().setOutputStyle(OutputStyle.PRETTY);
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(filename, ".xml"));
    xml.compose(s, cm);
    s.close();
    xml = new XmlParser().setOutputStyle(OutputStyle.CANONICAL);
    s = new FileOutputStream(page.getFolders().dstDir + Utilities.changeFileExt(filename, ".canonical.xml"));
    xml.compose(s, cm);
    s.close();
    cloneToXhtml(n, cm.getName(), false, "conceptmap-instance", "Concept Map", null, wg("vocab"));
    // now, we create an html page from the narrative
    String narrative = new XhtmlComposer(XhtmlComposer.HTML).compose(cm.getText().getDiv());
    String html = TextFile.fileToString(page.getFolders().templateDir + "template-example.html").replace("<%example%>", narrative);
    html = page.processPageIncludes(Utilities.changeFileExt(filename, ".html"), html, "conceptmap-instance", null, null, null, "Concept Map", null, null, wg("vocab"));
    TextFile.stringToFile(html, page.getFolders().dstDir + Utilities.changeFileExt(filename, ".html"));
    conceptMapsFeed.getEntry().add(new BundleEntryComponent().setResource(cm).setFullUrl("http://hl7.org/fhir/" + cm.fhirType() + "/" + cm.getId()));
    page.getConceptMaps().see(cm, page.packageInfo());
    page.getHTMLChecker().registerFile(n + ".html", cm.getName(), HTMLLinkChecker.XHTML_TYPE, false);
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) BundleEntryComponent(org.hl7.fhir.r5.model.Bundle.BundleEntryComponent) FileOutputStream(java.io.FileOutputStream) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 8 with IParser

use of org.hl7.fhir.r5.formats.IParser in project kindling by HL7.

the class Publisher method resource2Ttl.

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

Example 9 with IParser

use of org.hl7.fhir.r5.formats.IParser in project kindling by HL7.

the class PageProcessor method r2Json.

private String r2Json(ValueSet vs) throws Exception {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
    json.setSuppressXhtml("Snipped for Brevity");
    json.compose(bytes, vs);
    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 10 with IParser

use of org.hl7.fhir.r5.formats.IParser in project kindling by HL7.

the class BindingsParser method loadValueSet.

private ValueSet loadValueSet(String ref, String committee) throws Exception {
    String folder = new File(filename).getParent();
    String srcName;
    IParser p;
    if (new File(Utilities.path(folder, ref + ".xml")).exists()) {
        p = new XmlParser();
        srcName = Utilities.path(folder, ref + ".xml");
    } else if (new File(Utilities.path(folder, ref + ".json")).exists()) {
        p = new JsonParser();
        srcName = Utilities.path(folder, ref + ".json");
    } else
        throw new Exception("Unable to find source for " + ref + " in " + filename + " (" + Utilities.path(folder, ref + ".xml/json)"));
    FileInputStream input = new FileInputStream(srcName);
    try {
        ValueSet result = ValueSetUtilities.makeShareable((ValueSet) p.parse(input));
        result.setId(ref.substring(9));
        if (!result.hasExperimental())
            result.setExperimental(false);
        // if (!result.hasUrl())
        result.setUrl("http://hl7.org/fhir/ValueSet/" + ref.substring(9));
        if (!result.hasVersion() || result.getUrl().startsWith("http://hl7.org/fhir"))
            result.setVersion(version);
        if (!Utilities.noString(committee)) {
            if (!result.hasExtension(ToolingExtensions.EXT_WORKGROUP)) {
                result.addExtension().setUrl(ToolingExtensions.EXT_WORKGROUP).setValue(new CodeType(committee));
            } else {
                String ec = ToolingExtensions.readStringExtension(result, ToolingExtensions.EXT_WORKGROUP);
                if (!ec.equals(committee))
                    System.out.println("ValueSet " + result.getUrl() + " WG mismatch 1: is " + ec + ", want to set to " + committee);
            }
        }
        result.setUserData("filename", "valueset-" + ref.substring(9));
        result.setUserData("path", "valueset-" + ref.substring(9) + ".html");
        new CodeSystemConvertor(codeSystems).convert(p, result, srcName, packageInfo);
        return result;
    } finally {
        IOUtils.closeQuietly(input);
    }
}
Also used : XmlParser(org.hl7.fhir.r5.formats.XmlParser) XLSXmlParser(org.hl7.fhir.utilities.xls.XLSXmlParser) CodeSystemConvertor(org.hl7.fhir.definitions.parsers.CodeSystemConvertor) CodeType(org.hl7.fhir.r5.model.CodeType) File(java.io.File) ValueSet(org.hl7.fhir.r5.model.ValueSet) URISyntaxException(java.net.URISyntaxException) FileInputStream(java.io.FileInputStream) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Aggregations

IParser (ca.uhn.fhir.parser.IParser)89 FhirContext (ca.uhn.fhir.context.FhirContext)43 IOException (java.io.IOException)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 Test (org.junit.Test)24 InputStream (java.io.InputStream)22 IParser (org.hl7.fhir.r5.formats.IParser)19 JsonParser (org.hl7.fhir.r5.formats.JsonParser)18 Test (org.junit.jupiter.api.Test)18 FHIRException (org.hl7.fhir.exceptions.FHIRException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 File (java.io.File)16 FileInputStream (java.io.FileInputStream)16 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)15 Bundle (org.hl7.fhir.r4.model.Bundle)14 FileOutputStream (java.io.FileOutputStream)12 Bundle (org.hl7.fhir.dstu3.model.Bundle)12 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)12 XmlParser (org.hl7.fhir.r5.formats.XmlParser)12 ArrayList (java.util.ArrayList)11