Search in sources :

Example 31 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer 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("r5", "narrative", test.getId() + ".json")) {
        source = (Resource) new JsonParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".json"));
    } else {
        source = (Resource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".xml"));
    }
    XhtmlNode x = RendererFactory.factory(source, rc).build(source);
    String expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + ".html"));
    String actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
    String expectedFileName = CompareUtilities.tempFile("narrative", test.getId() + ".expected.html");
    String actualFileName = CompareUtilities.tempFile("narrative", test.getId() + ".actual.html");
    TextFile.stringToFile(expected, expectedFileName);
    TextFile.stringToFile(actual, actualFileName);
    String msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
    Assertions.assertTrue(msg == null, "Output does not match expected: " + msg);
    if (test.isMeta()) {
        org.hl7.fhir.r5.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));
        expected = TextFile.streamToString(TestingUtilities.loadTestResourceStream("r5", "narrative", test.getId() + "-meta.html"));
        actual = HEADER + new XhtmlComposer(true, true).compose(x) + FOOTER;
        actualFileName = CompareUtilities.tempFile("narrative", test.getId() + "-meta.actual.html");
        TextFile.stringToFile(actual, actualFileName);
        msg = CompareUtilities.checkXMLIsSame(expectedFileName, actualFileName);
        Assertions.assertTrue(msg == null, "Meta output does not match expected: " + msg);
    }
}
Also used : RenderingContext(org.hl7.fhir.r5.renderers.utils.RenderingContext) XmlParser(org.hl7.fhir.r5.formats.XmlParser) Resource(org.hl7.fhir.r5.model.Resource) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) JsonParser(org.hl7.fhir.r5.formats.JsonParser) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 32 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer in project org.hl7.fhir.core by hapifhir.

the class NarrativeGeneratorTests method checkDateTimeRendering.

private void checkDateTimeRendering(String src, String lang, String country, ZoneId tz, String fmt, ResourceRendererMode mode, String... expected) throws FHIRFormatError, DefinitionException, IOException {
    rc.setLocale(new java.util.Locale(lang, country));
    rc.setTimeZoneId(tz);
    if (fmt == null) {
        rc.setDateTimeFormat(null);
        rc.setDateFormat(null);
    } else {
        // really, it would be better to test patterns based on FormatStyle here, since
        // that's what will be used in the real world, but
        rc.setDateTimeFormat(DateTimeFormatter.ofPattern(fmt));
        rc.setDateFormat(DateTimeFormatter.ofPattern(fmt));
    }
    rc.setMode(mode);
    DateTimeType dt = new DateTimeType(src);
    String actual = new DataRenderer(rc).display(dt);
    Assert.assertTrue("Actual = " + actual + ", expected one of " + Utilities.toString(expected), Utilities.existsInList(actual, expected));
    XhtmlNode node = new XhtmlNode(NodeType.Element, "p");
    new DataRenderer(rc).render(node, dt);
    actual = new XhtmlComposer(true, false).compose(node);
    Assert.assertTrue(actual.startsWith("<p>"));
    Assert.assertTrue(actual.endsWith("</p>"));
    Assert.assertTrue("Actual = " + actual + ", expected one of " + Utilities.toString(expected), Utilities.existsInList(actual.substring(0, actual.length() - 4).substring(3), expected));
}
Also used : DateTimeType(org.hl7.fhir.r5.model.DateTimeType) DataRenderer(org.hl7.fhir.r5.renderers.DataRenderer) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 33 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer in project org.hl7.fhir.core by hapifhir.

the class JsonParserBase method composeXhtml.

protected void composeXhtml(String name, XhtmlNode html) throws IOException {
    if (!Utilities.noString(xhtmlMessage)) {
        prop(name, "<div>!-- " + xhtmlMessage + " --></div>");
    } else {
        XhtmlComposer comp = new XhtmlComposer(XhtmlComposer.XML, htmlPretty);
        prop(name, comp.compose(html));
    }
}
Also used : XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer)

Example 34 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer in project org.hl7.fhir.core by hapifhir.

the class ComparisonRenderer method renderProfile.

private void renderProfile(String id, ProfileComparison comp) throws IOException {
    String template = templates.get("Profile");
    Map<String, Base> vars = new HashMap<>();
    ProfileComparer cs = new ProfileComparer(session, new ProfileUtilities(session.getContextLeft(), null, session.getPkp()), new ProfileUtilities(session.getContextRight(), null, session.getPkp()));
    vars.put("left", new StringType(comp.getLeft().present()));
    vars.put("right", new StringType(comp.getRight().present()));
    vars.put("leftId", new StringType(comp.getLeft().getId()));
    vars.put("rightId", new StringType(comp.getRight().getId()));
    vars.put("leftUrl", new StringType(comp.getLeft().getUrl()));
    vars.put("rightUrl", new StringType(comp.getRight().getUrl()));
    vars.put("errors", new StringType(new XhtmlComposer(true).compose(cs.renderErrors(comp))));
    vars.put("metadata", new StringType(new XhtmlComposer(true).compose(cs.renderMetadata(comp, "", ""))));
    vars.put("structure", new StringType(new XhtmlComposer(true).compose(cs.renderStructure(comp, "", "", "http://hl7.org/fhir"))));
    String cnt = processTemplate(template, "CodeSystem", vars);
    TextFile.stringToFile(cnt, file(comp.getId() + ".html"));
    new org.hl7.fhir.r4b.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-union.json")), comp.getUnion());
    new org.hl7.fhir.r4b.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-intersection.json")), comp.getIntersection());
}
Also used : HashMap(java.util.HashMap) ProfileUtilities(org.hl7.fhir.r4b.conformance.ProfileUtilities) StringType(org.hl7.fhir.r4b.model.StringType) FileOutputStream(java.io.FileOutputStream) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) Base(org.hl7.fhir.r4b.model.Base)

Example 35 with XhtmlComposer

use of org.hl7.fhir.utilities.xhtml.XhtmlComposer in project org.hl7.fhir.core by hapifhir.

the class ComparisonRenderer method renderCodeSystem.

private void renderCodeSystem(String id, CodeSystemComparison comp) throws IOException {
    String template = templates.get("CodeSystem");
    Map<String, Base> vars = new HashMap<>();
    CodeSystemComparer cs = new CodeSystemComparer(session);
    vars.put("left", new StringType(comp.getLeft().present()));
    vars.put("right", new StringType(comp.getRight().present()));
    vars.put("leftId", new StringType(comp.getLeft().getId()));
    vars.put("rightId", new StringType(comp.getRight().getId()));
    vars.put("leftUrl", new StringType(comp.getLeft().getUrl()));
    vars.put("rightUrl", new StringType(comp.getRight().getUrl()));
    vars.put("errors", new StringType(new XhtmlComposer(true).compose(cs.renderErrors(comp))));
    vars.put("metadata", new StringType(new XhtmlComposer(true).compose(cs.renderMetadata(comp, "", ""))));
    vars.put("concepts", new StringType(new XhtmlComposer(true).compose(cs.renderConcepts(comp, "", ""))));
    String cnt = processTemplate(template, "CodeSystem", vars);
    TextFile.stringToFile(cnt, file(comp.getId() + ".html"));
    new org.hl7.fhir.r4b.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-union.json")), comp.getUnion());
    new org.hl7.fhir.r4b.formats.JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(folder, comp.getId() + "-intersection.json")), comp.getIntersection());
}
Also used : HashMap(java.util.HashMap) StringType(org.hl7.fhir.r4b.model.StringType) FileOutputStream(java.io.FileOutputStream) XhtmlComposer(org.hl7.fhir.utilities.xhtml.XhtmlComposer) Base(org.hl7.fhir.r4b.model.Base)

Aggregations

XhtmlComposer (org.hl7.fhir.utilities.xhtml.XhtmlComposer)78 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)31 FHIRException (org.hl7.fhir.exceptions.FHIRException)15 IOException (java.io.IOException)14 FileOutputStream (java.io.FileOutputStream)13 NotImplementedException (org.apache.commons.lang3.NotImplementedException)11 XhtmlParser (org.hl7.fhir.utilities.xhtml.XhtmlParser)11 File (java.io.File)10 FileNotFoundException (java.io.FileNotFoundException)10 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 RenderingContext (org.hl7.fhir.r5.renderers.utils.RenderingContext)10 FileInputStream (java.io.FileInputStream)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 HashMap (java.util.HashMap)7 TransformerException (javax.xml.transform.TransformerException)7 XmlParser (org.hl7.fhir.r5.formats.XmlParser)7 ValueSet (org.hl7.fhir.r5.model.ValueSet)7 CanonicalResource (org.hl7.fhir.r5.model.CanonicalResource)6 URISyntaxException (java.net.URISyntaxException)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5