use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.
the class BundleRenderer method render.
public XhtmlNode render(Bundle b) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
if (b.getType() == BundleType.DOCUMENT) {
if (!b.hasEntry() || !(b.getEntryFirstRep().hasResource() && b.getEntryFirstRep().getResource() instanceof Composition)) {
throw new FHIRException("Invalid document - first entry is not a Composition");
}
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
renderDocument(x, b);
return x;
} else if ((b.getType() == BundleType.COLLECTION && allEntresAreHistoryProvenance(b))) {
return null;
} else {
XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ROOT, b.getId(), b.getType().toCode()));
int i = 0;
for (BundleEntryComponent be : b.getEntry()) {
i++;
if (be.hasFullUrl())
root.an(makeInternalBundleLink(be.getFullUrl()));
if (be.hasResource() && be.getResource().hasId())
root.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId());
root.hr();
if (be.hasFullUrl()) {
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY_URL, Integer.toString(i), be.getFullUrl()));
} else {
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY, Integer.toString(i)));
}
if (be.hasRequest())
renderRequest(root, be.getRequest());
if (be.hasSearch())
renderSearch(root, be.getSearch());
if (be.hasResponse())
renderResponse(root, be.getResponse());
if (be.hasResource()) {
root.para().addText(formatMessage(RENDER_BUNDLE_RESOURCE, be.getResource().fhirType()));
if (be.hasResource()) {
XhtmlNode xn = null;
if (be.getResource() instanceof DomainResource) {
DomainResource dr = (DomainResource) be.getResource();
xn = dr.getText().getDiv();
}
if (xn == null || xn.isEmpty()) {
ResourceRenderer rr = RendererFactory.factory(be.getResource(), context);
try {
xn = rr.build(be.getResource());
} catch (Exception e) {
xn = makeExceptionXhtml(e, "generating narrative");
}
}
root.blockquote().getChildNodes().addAll(checkInternalLinks(b, xn.getChildNodes()));
}
}
}
return root;
}
}
use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.
the class ResourceRenderer method inject.
public static void inject(DomainResource r, XhtmlNode x, NarrativeStatus status) {
if (!x.hasAttribute("xmlns"))
x.setAttribute("xmlns", "http://www.w3.org/1999/xhtml");
if (r.hasLanguage()) {
// use both - see https://www.w3.org/TR/i18n-html-tech-lang/#langvalues
x.setAttribute("lang", r.getLanguage());
x.setAttribute("xml:lang", r.getLanguage());
}
r.getText().setUserData("renderer.generated", true);
if (!r.hasText() || !r.getText().hasDiv() || r.getText().getDiv().getChildNodes().isEmpty()) {
r.setText(new Narrative());
r.getText().setDiv(x);
r.getText().setStatus(status);
} else {
XhtmlNode n = r.getText().getDiv();
n.clear();
n.getChildNodes().addAll(x.getChildNodes());
}
}
use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.
the class ResourceRenderer method render.
/**
* given a resource, update it's narrative with the best rendering available
*
* @param r - the domain resource in question
*
* @throws IOException
* @throws EOperationOutcome
* @throws FHIRException
*/
public void render(DomainResource r) throws IOException, FHIRException, EOperationOutcome {
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
boolean ofr = forResource;
boolean hasExtensions;
try {
forResource = true;
hasExtensions = render(x, r);
} finally {
forResource = ofr;
}
inject(r, x, hasExtensions ? NarrativeStatus.EXTENSIONS : NarrativeStatus.GENERATED);
}
use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.
the class ResourceRoundTripTests method test.
@Test
public void test() throws IOException, FHIRException, EOperationOutcome {
DomainResource res = (DomainResource) new XmlParser().parse(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"));
RenderingContext rc = new RenderingContext(TestingUtilities.context(), null, null, "http://hl7.org/fhir", "", null, ResourceRendererMode.END_USER);
RendererFactory.factory(res, rc).render(res);
IOUtils.copy(TestingUtilities.loadTestResourceStream("r5", "unicode.xml"), new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.xml")));
new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(TestingUtilities.tempFile("gen", "unicode.out.xml")), res);
}
use of org.hl7.fhir.r4.model.DomainResource in project org.hl7.fhir.core by hapifhir.
the class BundleRenderer method render.
public XhtmlNode render(Bundle b) throws FHIRFormatError, DefinitionException, IOException, FHIRException, EOperationOutcome {
if (b.getType() == BundleType.DOCUMENT) {
if (!b.hasEntry() || !(b.getEntryFirstRep().hasResource() && b.getEntryFirstRep().getResource() instanceof Composition)) {
throw new FHIRException("Invalid document - first entry is not a Composition");
}
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
renderDocument(x, b);
return x;
} else if ((b.getType() == BundleType.COLLECTION && allEntresAreHistoryProvenance(b))) {
return null;
} else {
XhtmlNode root = new XhtmlNode(NodeType.Element, "div");
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ROOT, b.getId(), b.getType().toCode()));
int i = 0;
for (BundleEntryComponent be : b.getEntry()) {
i++;
if (be.hasFullUrl())
root.an(makeInternalBundleLink(be.getFullUrl()));
if (be.hasResource() && be.getResource().hasId())
root.an(be.getResource().getResourceType().name() + "_" + be.getResource().getId());
root.hr();
if (be.hasFullUrl()) {
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY_URL, Integer.toString(i), be.getFullUrl()));
} else {
root.para().addText(formatMessage(RENDER_BUNDLE_HEADER_ENTRY, Integer.toString(i)));
}
if (be.hasRequest())
renderRequest(root, be.getRequest());
if (be.hasSearch())
renderSearch(root, be.getSearch());
if (be.hasResponse())
renderResponse(root, be.getResponse());
if (be.hasResource()) {
root.para().addText(formatMessage(RENDER_BUNDLE_RESOURCE, be.getResource().fhirType()));
if (be.hasResource()) {
XhtmlNode xn = null;
if (be.getResource() instanceof DomainResource) {
DomainResource dr = (DomainResource) be.getResource();
xn = dr.getText().getDiv();
}
if (xn == null || xn.isEmpty()) {
ResourceRenderer rr = RendererFactory.factory(be.getResource(), context);
try {
xn = rr.build(be.getResource());
} catch (Exception e) {
xn = makeExceptionXhtml(e, "generating narrative");
}
}
root.blockquote().para().getChildNodes().addAll(checkInternalLinks(b, xn.getChildNodes()));
}
}
}
return root;
}
}
Aggregations