From 832da9fc6bf15d79ffcf15c0dbfee8421a031bde Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Mon, 29 Aug 2022 17:52:39 +1000 Subject: [PATCH] fix up rendering --- .../hl7/fhir/r5/renderers/LiquidRenderer.java | 28 ++++++++++++++++--- .../org/hl7/fhir/r5/utils/LiquidEngine.java | 4 +-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/LiquidRenderer.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/LiquidRenderer.java index 76cee540e..938f32bdc 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/LiquidRenderer.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/renderers/LiquidRenderer.java @@ -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()); diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/LiquidEngine.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/LiquidEngine.java index 7521c4107..f502dccba 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/LiquidEngine.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/utils/LiquidEngine.java @@ -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)); } }