use of org.hl7.fhir.r5.model.Narrative in project org.hl7.fhir.core by hapifhir.
the class ResourceRenderer method renderReference.
public void renderReference(ResourceWrapper rw, XhtmlNode x, Reference r, boolean allowLinks) throws UnsupportedEncodingException, IOException {
if (r == null) {
x.tx("null!");
return;
}
XhtmlNode c = null;
ResourceWithReference tr = null;
if (r.hasReferenceElement() && allowLinks) {
tr = resolveReference(rw, r.getReference());
if (!r.getReference().startsWith("#")) {
if (tr != null && tr.getReference() != null)
c = x.ah(tr.getReference());
else
c = x.ah(r.getReference());
} else {
c = x.ah(r.getReference());
}
} else {
c = x.span(null, null);
}
if (tr != null && tr.getReference() != null && tr.getReference().startsWith("#")) {
c.tx("See above (");
}
// what to display: if text is provided, then that. if the reference was resolved, then show the name, or the generated narrative
String display = r.hasDisplayElement() ? r.getDisplay() : null;
String name = tr != null && tr.getResource() != null ? tr.getResource().getNameFromResource() : null;
if (display == null && (tr == null || tr.getResource() == null)) {
c.addText(r.getReference());
} else if (context.isTechnicalMode()) {
c.addText(r.getReference());
if (display != null) {
c.addText(": " + display);
}
if ((tr == null || !tr.getReference().startsWith("#")) && name != null) {
x.addText(" \"" + name + "\"");
}
if (r.hasExtension(ToolingExtensions.EXT_TARGET_ID)) {
x.addText("(#" + r.getExtensionString(ToolingExtensions.EXT_TARGET_ID) + ")");
} else if (r.hasExtension(ToolingExtensions.EXT_TARGET_PATH)) {
x.addText("(#/" + r.getExtensionString(ToolingExtensions.EXT_TARGET_PATH) + ")");
}
} else {
if (display != null) {
c.addText(display);
} else if (name != null) {
c.addText(name);
} else {
c.tx(". Generated Summary: ");
if (tr != null) {
new ProfileDrivenRenderer(context).generateResourceSummary(c, tr.getResource(), true, r.getReference().startsWith("#"), true);
}
}
}
if (tr != null && tr.getReference() != null && tr.getReference().startsWith("#")) {
c.tx(")");
}
}
use of org.hl7.fhir.r5.model.Narrative 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.r5.model.Narrative in project org.hl7.fhir.core by hapifhir.
the class ResourceRenderer method renderReference.
public void renderReference(ResourceWrapper rw, XhtmlNode x, BaseWrapper r) throws UnsupportedEncodingException, IOException {
XhtmlNode c = x;
ResourceWithReference tr = null;
String v;
if (r.has("reference")) {
v = r.get("reference").primitiveValue();
tr = resolveReference(rw, v);
if (!v.startsWith("#")) {
if (tr != null && tr.getReference() != null)
c = x.ah(tr.getReference());
else
c = x.ah(v);
}
} else {
v = "";
}
// what to display: if text is provided, then that. if the reference was resolved, then show the generated narrative
if (r.has("display")) {
c.addText(r.get("display").primitiveValue());
if (tr != null && tr.getResource() != null) {
c.tx(". Generated Summary: ");
new ProfileDrivenRenderer(context).generateResourceSummary(c, tr.getResource(), true, v.startsWith("#"), false);
}
} else if (tr != null && tr.getResource() != null) {
new ProfileDrivenRenderer(context).generateResourceSummary(c, tr.getResource(), v.startsWith("#"), v.startsWith("#"), false);
} else {
c.addText(v);
}
}
use of org.hl7.fhir.r5.model.Narrative in project org.hl7.fhir.core by hapifhir.
the class DataRenderer method addMarkdown.
// -- 2. Markdown support -------------------------------------------------------
protected void addMarkdown(XhtmlNode x, String text) throws FHIRFormatError, IOException, DefinitionException {
if (text != null) {
// 1. custom FHIR extensions
while (text.contains("[[[")) {
String left = text.substring(0, text.indexOf("[[["));
String link = text.substring(text.indexOf("[[[") + 3, text.indexOf("]]]"));
String right = text.substring(text.indexOf("]]]") + 3);
String url = link;
String[] parts = link.split("\\#");
StructureDefinition p = getContext().getWorker().fetchResource(StructureDefinition.class, parts[0]);
if (p == null)
p = getContext().getWorker().fetchTypeDefinition(parts[0]);
if (p == null)
p = getContext().getWorker().fetchResource(StructureDefinition.class, link);
if (p != null) {
url = p.getUserString("path");
if (url == null)
url = p.getUserString("filename");
} else
throw new DefinitionException("Unable to resolve markdown link " + link);
text = left + "[" + link + "](" + url + ")" + right;
}
// 2. markdown
String s = getContext().getMarkdown().process(Utilities.escapeXml(text), "narrative generator");
XhtmlParser p = new XhtmlParser();
XhtmlNode m;
try {
m = p.parse("<div>" + s + "</div>", "div");
} catch (org.hl7.fhir.exceptions.FHIRFormatError e) {
throw new FHIRFormatError(e.getMessage(), e);
}
x.getChildNodes().addAll(m.getChildNodes());
}
}
use of org.hl7.fhir.r5.model.Narrative in project org.hl7.fhir.core by hapifhir.
the class Factory method newNarrative.
public static Narrative newNarrative(NarrativeStatus status, String html) throws IOException, FHIRException {
Narrative n = new Narrative();
n.setStatus(status);
try {
n.setDiv(new XhtmlParser().parseFragment("<div>" + Utilities.escapeXml(html) + "</div>"));
} catch (org.hl7.fhir.exceptions.FHIRException e) {
throw new FHIRException(e.getMessage(), e);
}
return n;
}
Aggregations