fix up rendering

This commit is contained in:
Grahame Grieve 2022-08-29 17:52:39 +10:00
parent 3b4260d955
commit 832da9fc6b
2 changed files with 26 additions and 6 deletions

View File

@ -10,8 +10,10 @@ import org.hl7.fhir.r5.elementmodel.Element;
import org.hl7.fhir.r5.model.Base;
import org.hl7.fhir.r5.model.DataType;
import org.hl7.fhir.r5.model.DomainResource;
import org.hl7.fhir.r5.model.Reference;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.r5.renderers.utils.BaseWrappers.ResourceWrapper;
import org.hl7.fhir.r5.renderers.LiquidRenderer.LiquidRendererContxt;
import org.hl7.fhir.r5.renderers.utils.RenderingContext;
import org.hl7.fhir.r5.renderers.utils.Resolver.ResourceContext;
import org.hl7.fhir.r5.utils.EOperationOutcome;
@ -25,6 +27,22 @@ import org.hl7.fhir.utilities.xhtml.XhtmlParser;
public class LiquidRenderer extends ResourceRenderer implements ILiquidRenderingSupport {
public class LiquidRendererContxt {
private ResourceContext rcontext;
private ResourceWrapper resource;
public LiquidRendererContxt(ResourceContext rcontext, ResourceWrapper r) {
this.rcontext = rcontext;
this.resource = r;
}
public ResourceWrapper getResource() {
return resource;
}
}
private String liquidTemplate;
public LiquidRenderer(RenderingContext context, String liquidTemplate) {
@ -97,7 +115,7 @@ public class LiquidRenderer extends ResourceRenderer implements ILiquidRendering
try {
LiquidDocument doc = engine.parse(liquidTemplate, "template");
engine.setRenderingSupport(this);
String html = engine.evaluate(doc, r.getBase(), rcontext);
String html = engine.evaluate(doc, r.getBase(), new LiquidRendererContxt(rcontext, r));
xn = new XhtmlParser().parseFragment(html);
if (!x.getName().equals("div"))
throw new FHIRException("Error in template: Root element is not 'div'");
@ -114,13 +132,15 @@ public class LiquidRenderer extends ResourceRenderer implements ILiquidRendering
}
@Override
public String renderForLiquid(Base base) throws FHIRException {
public String renderForLiquid(Object appContext, Base base) throws FHIRException {
try {
if (base instanceof Element) {
base = context.getParser().parseType((Element) base);
}
XhtmlNode x = new XhtmlNode(NodeType.Element);
if (base instanceof DataType) {
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
if (base instanceof Reference) {
renderReference(((LiquidRendererContxt) appContext).getResource(), x, (Reference) base);
} else if (base instanceof DataType) {
render(x, (DataType) base);
} else {
x.tx(base.toString());

View File

@ -60,7 +60,7 @@ import org.hl7.fhir.utilities.xhtml.XhtmlNode;
public class LiquidEngine implements IEvaluationContext {
public interface ILiquidRenderingSupport {
String renderForLiquid(Base i) throws FHIRException;
String renderForLiquid(Object appContext, Base i) throws FHIRException;
}
public interface ILiquidEngineIncludeResolver {
@ -165,7 +165,7 @@ public class LiquidEngine implements IEvaluationContext {
boolean first = true;
for (Base i : items) {
if (first) first = false; else b.append(", ");
String s = renderingSupport != null ? renderingSupport.renderForLiquid(i) : null;
String s = renderingSupport != null ? renderingSupport.renderForLiquid(ctxt.externalContext, i) : null;
b.append(s != null ? s : engine.convertToString(i));
}
}