Search in sources :

Example 26 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project kindling by HL7.

the class Publisher method generateIGValueSetsPart2.

private void generateIGValueSetsPart2(ImplementationGuideDefn ig) throws Exception {
    for (Resource ae : page.getIgResources().values()) {
        if (ae instanceof ValueSet) {
            ValueSet vs = (ValueSet) ae;
            String name = Utilities.fileTitle((String) ae.getUserData("path"));
            String title = vs.getName();
            if (vs.getText() == null || vs.getText().getDiv() == null || vs.getText().getDiv().allChildrenAreText() && (Utilities.noString(vs.getText().getDiv().allText()) || !vs.getText().getDiv().allText().matches(".*\\w.*"))) {
                RenderingContext lrc = page.getRc().copy().setLocalPrefix("").setTooCostlyNoteEmpty(PageProcessor.TOO_MANY_CODES_TEXT_EMPTY).setTooCostlyNoteNotEmpty(PageProcessor.TOO_MANY_CODES_TEXT_NOT_EMPTY);
                RendererFactory.factory(vs, lrc).render(vs);
            }
            page.getVsValidator().validate(page.getValidationErrors(), name, vs, true, false);
            // todo - what should the Oids be
            addToResourceFeed(vs, valueSetsFeed, null);
            String sf = page.processPageIncludes(title + ".html", TextFile.fileToString(page.getFolders().templateDir + "template-vs-ig.html"), "valueSet", null, name + ".html", vs, null, "Value Set", null, null, wg("vocab"));
            sf = addSectionNumbers(title + ".html", "template-valueset", sf, "??", 0, null, ig);
            TextFile.stringToFile(sf, page.getFolders().dstDir + name + ".html");
            String src = page.processPageIncludesForBook(title + ".html", TextFile.fileToString(page.getFolders().templateDir + "template-vs-ig-book.html"), "valueSet", vs, null, null);
            cachePage(name + ".html", src, "Value Set " + title, false);
            page.setId(null);
            IParser json = new JsonParser().setOutputStyle(OutputStyle.PRETTY);
            FileOutputStream s = new FileOutputStream(page.getFolders().dstDir + name + ".json");
            json.compose(s, vs);
            s.close();
            json = new JsonParser().setOutputStyle(OutputStyle.CANONICAL);
            s = new FileOutputStream(page.getFolders().dstDir + name + ".canonical.json");
            json.compose(s, vs);
            s.close();
            IParser xml = new XmlParser().setOutputStyle(OutputStyle.PRETTY);
            s = new FileOutputStream(page.getFolders().dstDir + name + ".xml");
            xml.compose(s, vs);
            s.close();
            xml = new XmlParser().setOutputStyle(OutputStyle.CANONICAL);
            s = new FileOutputStream(page.getFolders().dstDir + name + ".canonical.xml");
            xml.compose(s, vs);
            s.close();
            cloneToXhtml(name, "Definition for Value Set" + vs.getName(), false, "valueset-instance", "Value Set", null, wg("vocab"));
            jsonToXhtml(name, "Definition for Value Set" + vs.getName(), resource2Json(vs), "valueset-instance", "Value Set", null, wg("vocab"));
            ttlToXhtml(name, "Definition for Value Set" + vs.getName(), resource2Ttl(vs), "valueset-instance", "Value Set", null, wg("vocab"));
        }
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) FileOutputStream(java.io.FileOutputStream) Resource(org.hl7.fhir.r5.model.Resource) DomainResource(org.hl7.fhir.r5.model.DomainResource) CanonicalResource(org.hl7.fhir.r5.model.CanonicalResource) ValueSet(org.hl7.fhir.r5.model.ValueSet) IParser(org.hl7.fhir.r5.formats.IParser) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 27 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class NarrativeGenerationTests method test.

@ParameterizedTest(name = "{index}: file {0}")
@MethodSource("data")
public void test(String id, TestDetails test) throws Exception {
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    rc.setDestDir("");
    rc.setHeader(test.isHeader());
    rc.setDefinitionsTarget("test.html");
    rc.setTerminologyServiceOptions(TerminologyServiceOptions.defaults());
    rc.setParser(new TestTypeParser());
    // getting timezones correct (well, at least consistent, so tests pass on any computer)
    rc.setLocale(new java.util.Locale("en", "AU"));
    rc.setTimeZoneId(ZoneId.of("Australia/Sydney"));
    rc.setDateTimeFormatString("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
    rc.setDateFormatString("yyyy-MM-dd");
    rc.setMode(test.technical ? ResourceRendererMode.TECHNICAL : ResourceRendererMode.END_USER);
    Resource source;
    if (TestingUtilities.findTestResource("r4b", "narrative", test.getId() + ".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r4b", "narrative", test.getId() + ".json"));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r4b", "narrative", test.getId() + ".xml"));
    }
    XhtmlNode x = RendererFactory.factory(source, rc).build(source);
    String target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html"));
    String output = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
    String tfn = TestingUtilities.tempFile("narrative", test.getId() + ".target.html");
    String ofn = TestingUtilities.tempFile("narrative", test.getId() + ".output.html");
    TextFile.stringToFile(target, tfn);
    TextFile.stringToFile(output, ofn);
    String msg = TestingUtilities.checkXMLIsSame(ofn, tfn);
    Assertions.assertTrue(msg == null, "Output does not match expected: " + msg);
    if (test.isMeta()) {
        org.hl7.fhir.r4b.elementmodel.Element e = Manager.parseSingle(context, TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"), FhirFormat.XML);
        x = RendererFactory.factory(source, rc).render(new ElementWrappers.ResourceWrapperMetaElement(rc, e));
        target = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r4b", "narrative", test.getId() + "-meta.html"));
        output = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
        ofn = TestingUtilities.tempFile("narrative", test.getId() + "-meta.output.html");
        TextFile.stringToFile(output, ofn);
        msg = TestingUtilities.checkXMLIsSame(ofn, tfn);
        Assertions.assertTrue(msg == null, "Meta output does not match expected: " + msg);
    }
}
Also used : RenderingContext(org.hl7.fhir.r4b.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r4b.formats.XmlParser) Resource(org.hl7.fhir.r4b.model.Resource) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) JsonParser(org.hl7.fhir.r4b.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 28 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class ValidationEngine method generate.

public Resource generate(String source, String version) throws FHIRException, IOException, EOperationOutcome {
    Content cnt = igLoader.loadContent(source, "validate", false);
    Resource res = igLoader.loadResourceByVersion(version, cnt.focus, source);
    RenderingContext rc = new RenderingContext(context, null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
    genResource(res, rc);
    return (Resource) res;
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext)

Example 29 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class Scanner method genScanOutputItem.

protected void genScanOutputItem(ScanOutputItem item, String filename) throws IOException, FHIRException, EOperationOutcome {
    RenderingContext rc = new RenderingContext(getContext(), null, null, "http://hl7.org/fhir", "", null, RenderingContext.ResourceRendererMode.END_USER);
    rc.setNoSlowLookup(true);
    RendererFactory.factory(item.getOutcome(), rc).render(item.getOutcome());
    String s = new XhtmlComposer(XhtmlComposer.HTML).compose(item.getOutcome().getText().getDiv());
    String title = item.getTitle();
    StringBuilder b = new StringBuilder();
    b.append("<html>");
    b.append("<head>");
    b.append("<title>" + title + "</title>");
    b.append("<link rel=\"stylesheet\" href=\"fhir.css\"/>\r\n");
    b.append("</head>");
    b.append("<body>");
    b.append("<h2>" + title + "</h2>");
    b.append(s);
    b.append("</body>");
    b.append("</html>");
    TextFile.stringToFile(b.toString(), filename);
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 30 with RenderingContext

use of org.hl7.fhir.r4b.renderers.utils.RenderingContext in project org.hl7.fhir.core by hapifhir.

the class ProfileUtilities method genElementCells.

public List<Cell> genElementCells(HierarchicalTableGenerator gen, ElementDefinition element, String profileBaseFileName, boolean snapshot, String corePath, String imagePath, boolean root, boolean logicalModel, boolean allInvariants, StructureDefinition profile, Row typesRow, Row row, boolean hasDef, boolean ext, UnusedTracker used, String ref, String sName, Cell nameCell, boolean mustSupport, boolean allowSubRows, RenderingContext rc) throws IOException {
    List<Cell> res = new ArrayList<>();
    Cell gc = gen.new Cell();
    row.getCells().add(gc);
    res.add(gc);
    if (element != null && element.getIsModifier()) {
        checkForNoChange(element.getIsModifierElement(), gc.addStyledText(translate("sd.table", "This element is a modifier element"), "?!", null, null, null, false));
    }
    if (element != null && element.getMustSupport()) {
        checkForNoChange(element.getMustSupportElement(), gc.addStyledText(translate("sd.table", "This element must be supported"), "S", "white", "red", null, false));
    }
    if (element != null && element.getIsSummary()) {
        checkForNoChange(element.getIsSummaryElement(), gc.addStyledText(translate("sd.table", "This element is included in summaries"), "\u03A3", null, null, null, false));
    }
    if (element != null && (hasNonBaseConstraints(element.getConstraint()) || hasNonBaseConditions(element.getCondition()))) {
        gc.addStyledText(translate("sd.table", "This element has or is affected by some invariants (" + listConstraintsAndConditions(element) + ")"), "I", null, null, null, false);
    }
    ExtensionContext extDefn = null;
    if (ext) {
        if (element != null && element.getType().size() == 1 && element.getType().get(0).hasProfile()) {
            String eurl = element.getType().get(0).getProfile().get(0).getValue();
            extDefn = locateExtension(StructureDefinition.class, eurl);
            if (extDefn == null) {
                res.add(genCardinality(gen, element, row, hasDef, used, null));
                res.add(addCell(row, gen.new Cell(null, null, "?gen-e1? " + element.getType().get(0).getProfile(), null, null)));
                res.add(generateDescription(gen, row, element, (ElementDefinition) element.getUserData(DERIVATION_POINTER), used.used, profile == null ? "" : profile.getUrl(), eurl, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot, mustSupport, allowSubRows, rc));
            } else {
                String name = urltail(eurl);
                nameCell.getPieces().get(0).setText(name);
                // left.getPieces().get(0).setReference((String) extDefn.getExtensionStructure().getTag("filename"));
                nameCell.getPieces().get(0).setHint(translate("sd.table", "Extension URL") + " = " + extDefn.getUrl());
                res.add(genCardinality(gen, element, row, hasDef, used, extDefn.getElement()));
                ElementDefinition valueDefn = extDefn.getExtensionValueDefinition();
                if (valueDefn != null && !"0".equals(valueDefn.getMax()))
                    res.add(genTypes(gen, row, valueDefn, profileBaseFileName, profile, corePath, imagePath, root, mustSupport));
                else
                    // if it's complex, we just call it nothing
                    // genTypes(gen, row, extDefn.getSnapshot().getElement().get(0), profileBaseFileName, profile);
                    res.add(addCell(row, gen.new Cell(null, null, "(" + translate("sd.table", "Complex") + ")", null, null)));
                res.add(generateDescription(gen, row, element, extDefn.getElement(), used.used, null, extDefn.getUrl(), profile, corePath, imagePath, root, logicalModel, allInvariants, valueDefn, snapshot, mustSupport, allowSubRows, rc));
            }
        } else {
            res.add(genCardinality(gen, element, row, hasDef, used, null));
            if ("0".equals(element.getMax()))
                res.add(addCell(row, gen.new Cell()));
            else
                res.add(genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath, root, mustSupport));
            res.add(generateDescription(gen, row, element, (ElementDefinition) element.getUserData(DERIVATION_POINTER), used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot, mustSupport, allowSubRows, rc));
        }
    } else {
        res.add(genCardinality(gen, element, row, hasDef, used, null));
        if (hasDef && !"0".equals(element.getMax()) && typesRow == null)
            res.add(genTypes(gen, row, element, profileBaseFileName, profile, corePath, imagePath, root, mustSupport));
        else
            res.add(addCell(row, gen.new Cell()));
        res.add(generateDescription(gen, row, element, (ElementDefinition) element.getUserData(DERIVATION_POINTER), used.used, null, null, profile, corePath, imagePath, root, logicalModel, allInvariants, snapshot, mustSupport, allowSubRows, rc));
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r4b.model.StructureDefinition) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r4b.model.ElementDefinition) Cell(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)

Aggregations

RenderingContext (org.hl7.fhir.r5.renderers.utils.RenderingContext)22 FHIRException (org.hl7.fhir.exceptions.FHIRException)14 FileOutputStream (java.io.FileOutputStream)13 XmlParser (org.hl7.fhir.r5.formats.XmlParser)13 XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 FileNotFoundException (java.io.FileNotFoundException)9 JsonParser (org.hl7.fhir.r5.formats.JsonParser)8 Cell (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell)8 TransformerException (javax.xml.transform.TransformerException)6 NotImplementedException (org.apache.commons.lang3.NotImplementedException)6 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)6 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)6 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)6 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)6 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 IParser (org.hl7.fhir.r5.formats.IParser)4