From e5a709e8e8cdccfbf3c3fd80587edbb2154c50ac Mon Sep 17 00:00:00 2001 From: Grahame Grieve Date: Wed, 18 Dec 2019 11:30:33 +1100 Subject: [PATCH] add xhtml processing directly --- .../org/hl7/fhir/r5/utils/LiquidEngine.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) 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 ae3250b4d..04831b3ce 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 @@ -37,6 +37,8 @@ import org.hl7.fhir.r5.model.ValueSet; import org.hl7.fhir.r5.utils.FHIRPathEngine.ExpressionNodeWithOffset; import org.hl7.fhir.r5.utils.FHIRPathEngine.IEvaluationContext; import org.hl7.fhir.utilities.Utilities; +import org.hl7.fhir.utilities.xhtml.NodeType; +import org.hl7.fhir.utilities.xhtml.XhtmlNode; public class LiquidEngine implements IEvaluationContext { @@ -427,4 +429,42 @@ public class LiquidEngine implements IEvaluationContext { return engine.getWorker().fetchResource(ValueSet.class, url); } + /** + * Lightweight method to replace fixed constants in resources + * + * @param node + * @param vars + * @return + */ + public boolean replaceInHtml(XhtmlNode node, Map vars) { + boolean replaced = false; + if (node.getNodeType() == NodeType.Text || node.getNodeType() == NodeType.Comment) { + String cnt = node.getContent(); + for (String n : vars.keySet()) { + cnt = cnt.replace(n, vars.get(n)); + } + if (!cnt.equals(node.getContent())) { + node.setContent(cnt); + replaced = true; + } + } else if (node.getNodeType() == NodeType.Element || node.getNodeType() == NodeType.Document) { + for (XhtmlNode c : node.getChildNodes()) { + if (replaceInHtml(c, vars)) { + replaced = true; + } + } + for (String an : node.getAttributes().keySet()) { + String cnt = node.getAttributes().get(an); + for (String n : vars.keySet()) { + cnt = cnt.replace(n, vars.get(n)); + } + if (!cnt.equals(node.getAttributes().get(an))) { + node.getAttributes().put(an, cnt); + replaced = true; + } + } + } + return replaced; + } + }