Fix R4B liquid processor

This commit is contained in:
Grahame Grieve 2023-07-27 20:07:36 +10:00
parent 06c865badf
commit 4677f319cb
4 changed files with 302 additions and 178 deletions

View File

@ -48,24 +48,34 @@ import org.hl7.fhir.utilities.Utilities;
public class FHIRLexer {
public class FHIRLexerException extends FHIRException {
public FHIRLexerException() {
super();
}
private SourceLocation location;
public FHIRLexerException(String message, Throwable cause) {
super(message, cause);
}
// public FHIRLexerException() {
// super();
// }
//
// public FHIRLexerException(String message, Throwable cause) {
// super(message, cause);
// }
//
// public FHIRLexerException(String message) {
// super(message);
// }
//
// public FHIRLexerException(Throwable cause) {
// super(cause);
// }
public FHIRLexerException(String message) {
public FHIRLexerException(String message, SourceLocation location) {
super(message);
this.location = location;
}
public FHIRLexerException(Throwable cause) {
super(cause);
public SourceLocation getLocation() {
return location;
}
}
private String source;
private int cursor;
private int currentStart;
@ -75,8 +85,10 @@ public class FHIRLexer {
private SourceLocation currentStartLocation;
private int id;
private String name;
private boolean liquidMode; // in liquid mode, || terminates the expression and hands the parser back to the
// host
private boolean liquidMode; // in liquid mode, || terminates the expression and hands the parser back to the host
private SourceLocation commentLocation;
private boolean metadataFormat;
private boolean allowDoubleQuotes;
public FHIRLexer(String source, String name) throws FHIRLexerException {
this.source = source == null ? "" : source;
@ -84,35 +96,44 @@ public class FHIRLexer {
currentLocation = new SourceLocation(1, 1);
next();
}
public FHIRLexer(String source, int i) throws FHIRLexerException {
this.source = source;
this.cursor = i;
currentLocation = new SourceLocation(1, 1);
next();
}
public FHIRLexer(String source, int i, boolean allowDoubleQuotes) throws FHIRLexerException {
this.source = source;
this.cursor = i;
this.allowDoubleQuotes = allowDoubleQuotes;
currentLocation = new SourceLocation(1, 1);
next();
}
public FHIRLexer(String source, String name, boolean metadataFormat, boolean allowDoubleQuotes) throws FHIRLexerException {
this.source = source == null ? "" : source;
this.name = name == null ? "??" : name;
this.metadataFormat = metadataFormat;
this.allowDoubleQuotes = allowDoubleQuotes;
currentLocation = new SourceLocation(1, 1);
next();
}
public String getCurrent() {
return current;
}
public SourceLocation getCurrentLocation() {
return currentLocation;
}
public boolean isConstant() {
return !Utilities.noString(current) && ((current.charAt(0) == '\'' || current.charAt(0) == '"')
|| current.charAt(0) == '@' || current.charAt(0) == '%' || current.charAt(0) == '-' || current.charAt(0) == '+'
|| (current.charAt(0) >= '0' && current.charAt(0) <= '9') || current.equals("true") || current.equals("false")
|| current.equals("{}"));
return FHIRPathConstant.isFHIRPathConstant(current);
}
public boolean isFixedName() {
return current != null && (current.charAt(0) == '`');
return FHIRPathConstant.isFHIRPathFixedName(current);
}
public boolean isStringConstant() {
return current.charAt(0) == '\'' || current.charAt(0) == '"' || current.charAt(0) == '`';
return FHIRPathConstant.isFHIRPathStringConstant(current);
}
public String take() throws FHIRLexerException {
@ -139,12 +160,10 @@ public class FHIRLexer {
if (current.equals("*") || current.equals("**"))
return true;
if ((current.charAt(0) >= 'A' && current.charAt(0) <= 'Z')
|| (current.charAt(0) >= 'a' && current.charAt(0) <= 'z')) {
if ((current.charAt(0) >= 'A' && current.charAt(0) <= 'Z') || (current.charAt(0) >= 'a' && current.charAt(0) <= 'z')) {
for (int i = 1; i < current.length(); i++)
if (!((current.charAt(1) >= 'A' && current.charAt(1) <= 'Z')
|| (current.charAt(1) >= 'a' && current.charAt(1) <= 'z')
|| (current.charAt(1) >= '0' && current.charAt(1) <= '9')))
if (!( (current.charAt(1) >= 'A' && current.charAt(1) <= 'Z') || (current.charAt(1) >= 'a' && current.charAt(1) <= 'z') ||
(current.charAt(1) >= '0' && current.charAt(1) <= '9')))
return false;
return true;
}
@ -152,11 +171,11 @@ public class FHIRLexer {
}
public FHIRLexerException error(String msg) {
return error(msg, currentLocation.toString());
return error(msg, currentLocation.toString(), currentLocation);
}
public FHIRLexerException error(String msg, String location) {
return new FHIRLexerException("Error @" + location + ": " + msg);
public FHIRLexerException error(String msg, String location, SourceLocation loc) {
return new FHIRLexerException("Error @"+location+": "+msg, loc);
}
public void next() throws FHIRLexerException {
@ -168,9 +187,7 @@ public class FHIRLexer {
char ch = source.charAt(cursor);
if (ch == '!' || ch == '>' || ch == '<' || ch == ':' || ch == '-' || ch == '=') {
cursor++;
if (cursor < source.length()
&& (source.charAt(cursor) == '=' || source.charAt(cursor) == '~' || source.charAt(cursor) == '-')
|| (ch == '-' && source.charAt(cursor) == '>'))
if (cursor < source.length() && (source.charAt(cursor) == '=' || source.charAt(cursor) == '~' || source.charAt(cursor) == '-') || (ch == '-' && source.charAt(cursor) == '>'))
cursor++;
current = source.substring(currentStart, cursor);
} else if (ch == '.' ) {
@ -181,8 +198,7 @@ public class FHIRLexer {
} else if (ch >= '0' && ch <= '9') {
cursor++;
boolean dotted = false;
while (cursor < source.length() && ((source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9')
|| (source.charAt(cursor) == '.') && !dotted)) {
while (cursor < source.length() && ((source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || (source.charAt(cursor) == '.') && !dotted)) {
if (source.charAt(cursor) == '.')
dotted = true;
cursor++;
@ -191,9 +207,8 @@ public class FHIRLexer {
cursor--;
current = source.substring(currentStart, cursor);
} else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z')
|| (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z')
|| (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == '_'))
while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') ||
(source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == '_'))
cursor++;
current = source.substring(currentStart, cursor);
} else if (ch == '%') {
@ -204,19 +219,20 @@ public class FHIRLexer {
cursor++;
cursor++;
} else
while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z')
|| (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z')
|| (source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == ':'
|| source.charAt(cursor) == '-'))
while (cursor < source.length() && ((source.charAt(cursor) >= 'A' && source.charAt(cursor) <= 'Z') || (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z') ||
(source.charAt(cursor) >= '0' && source.charAt(cursor) <= '9') || source.charAt(cursor) == ':' || source.charAt(cursor) == '-'))
cursor++;
current = source.substring(currentStart, cursor);
} else if (ch == '/') {
cursor++;
if (cursor < source.length() && (source.charAt(cursor) == '/')) {
// this is en error - should already have been skipped
error("This shouldn't happen?");
}
// we've run into metadata
cursor++;
cursor++;
current = source.substring(currentStart, cursor);
} else {
current = source.substring(currentStart, cursor);
}
} else if (ch == '$') {
cursor++;
while (cursor < source.length() && (source.charAt(cursor) >= 'a' && source.charAt(cursor) <= 'z'))
@ -228,7 +244,7 @@ public class FHIRLexer {
if (ch == '}')
cursor++;
current = source.substring(currentStart, cursor);
} else if (ch == '"') {
} else if (ch == '"' && allowDoubleQuotes) {
cursor++;
boolean escape = false;
while (cursor < source.length() && (escape || source.charAt(cursor) != '"')) {
@ -308,16 +324,23 @@ public class FHIRLexer {
private void skipWhitespaceAndComments() {
comments.clear();
commentLocation = null;
boolean last13 = false;
boolean done = false;
while (cursor < source.length() && !done) {
if (cursor < source.length() - 1 && "//".equals(source.substring(cursor, cursor + 2))) {
if (cursor < source.length() -1 && "//".equals(source.substring(cursor, cursor+2)) && !isMetadataStart()) {
if (commentLocation == null) {
commentLocation = currentLocation.copy();
}
int start = cursor+2;
while (cursor < source.length() && !((source.charAt(cursor) == '\r') || source.charAt(cursor) == '\n')) {
cursor++;
}
comments.add(source.substring(start, cursor).trim());
} else if (cursor < source.length() - 1 && "/*".equals(source.substring(cursor, cursor+2))) {
if (commentLocation == null) {
commentLocation = currentLocation.copy();
}
int start = cursor+2;
while (cursor < source.length() - 1 && !"*/".equals(source.substring(cursor, cursor+2))) {
last13 = currentLocation.checkChar(source.charAt(cursor), last13);
@ -329,7 +352,7 @@ public class FHIRLexer {
comments.add(source.substring(start, cursor).trim());
cursor = cursor + 2;
}
} else if (Character.isWhitespace(source.charAt(cursor))) {
} else if (Utilities.isWhitespace(source.charAt(cursor))) {
last13 = currentLocation.checkChar(source.charAt(cursor), last13);
cursor++;
} else {
@ -338,27 +361,25 @@ public class FHIRLexer {
}
}
private boolean isMetadataStart() {
return metadataFormat && cursor < source.length() - 2 && "///".equals(source.substring(cursor, cursor+3));
}
private boolean isDateChar(char ch,int start) {
int eot = source.charAt(start+1) == 'T' ? 10 : 20;
return ch == '-' || ch == ':' || ch == 'T' || ch == '+' || ch == 'Z' || Character.isDigit(ch)
|| (cursor - start == eot && ch == '.' && cursor < source.length() - 1
&& Character.isDigit(source.charAt(cursor + 1)));
return ch == '-' || ch == ':' || ch == 'T' || ch == '+' || ch == 'Z' || Character.isDigit(ch) || (cursor-start == eot && ch == '.' && cursor < source.length()-1&& Character.isDigit(source.charAt(cursor+1)));
}
public boolean isOp() {
return ExpressionNode.Operation.fromCode(current) != null;
}
public boolean done() {
return currentStart >= source.length();
}
public int nextId() {
id++;
return id;
}
public SourceLocation getCurrentStartLocation() {
return currentStartLocation;
}
@ -396,7 +417,6 @@ public class FHIRLexer {
public boolean hasToken(String kw) {
return !done() && kw.equals(current);
}
public boolean hasToken(String... names) {
if (done())
return false;
@ -468,7 +488,7 @@ public class FHIRLexer {
i = i + 4;
break;
default:
throw new FHIRLexerException("Unknown character escape \\" + s.charAt(i));
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation);
}
} else {
b.append(ch);
@ -517,7 +537,7 @@ public class FHIRLexer {
i = i + 4;
break;
default:
throw new FHIRLexerException("Unknown character escape \\" + s.charAt(i));
throw new FHIRLexerException("Unknown character escape \\"+s.charAt(i), currentLocation);
}
} else {
b.append(ch);
@ -532,7 +552,6 @@ public class FHIRLexer {
next();
}
public String takeDottedToken() throws FHIRLexerException {
StringBuilder b = new StringBuilder();
b.append(take());
@ -546,17 +565,39 @@ public class FHIRLexer {
public int getCurrentStart() {
return currentStart;
}
public String getSource() {
return source;
}
public boolean isLiquidMode() {
return liquidMode;
}
public void setLiquidMode(boolean liquidMode) {
this.liquidMode = liquidMode;
}
public SourceLocation getCommentLocation() {
return this.commentLocation;
}
public boolean isMetadataFormat() {
return metadataFormat;
}
public void setMetadataFormat(boolean metadataFormat) {
this.metadataFormat = metadataFormat;
}
public List<String> cloneComments() {
List<String> res = new ArrayList<>();
res.addAll(getComments());
return res;
}
public String tokenWithTrailingComment(String token) {
int line = getCurrentLocation().getLine();
token(token);
if (getComments().size() > 0 && getCommentLocation().getLine() == line) {
return getFirstComment();
} else {
return null;
}
}
public boolean isAllowDoubleQuotes() {
return allowDoubleQuotes;
}
}

View File

@ -0,0 +1,20 @@
package org.hl7.fhir.r4b.utils;
import org.hl7.fhir.utilities.Utilities;
public class FHIRPathConstant {
public static boolean isFHIRPathConstant(String string) {
return !Utilities.noString(string) && ((string.charAt(0) == '\'' || string.charAt(0) == '"') || string.charAt(0) == '@' || string.charAt(0) == '%' ||
string.charAt(0) == '-' || string.charAt(0) == '+' || (string.charAt(0) >= '0' && string.charAt(0) <= '9') ||
string.equals("true") || string.equals("false") || string.equals("{}"));
}
public static boolean isFHIRPathFixedName(String string) {
return string != null && (string.charAt(0) == '`');
}
public static boolean isFHIRPathStringConstant(String string) {
return string.charAt(0) == '\'' || string.charAt(0) == '"' || string.charAt(0) == '`';
}
}

View File

@ -282,6 +282,7 @@ public class FHIRPathEngine {
// host
private boolean doNotEnforceAsSingletonRule;
private boolean doNotEnforceAsCaseSensitive;
private boolean allowDoubleQuotes;
// if the fhir path expressions are allowed to use constants beyond those
// defined in the specification
@ -531,7 +532,7 @@ public class FHIRPathEngine {
}
public ExpressionNode parse(String path, String name) throws FHIRLexerException {
FHIRLexer lexer = new FHIRLexer(path, name);
FHIRLexer lexer = new FHIRLexer(path, name, false, allowDoubleQuotes);
if (lexer.done()) {
throw lexer.error("Path cannot be empty");
}
@ -572,7 +573,7 @@ public class FHIRPathEngine {
* @throws Exception
*/
public ExpressionNodeWithOffset parsePartial(String path, int i) throws FHIRLexerException {
FHIRLexer lexer = new FHIRLexer(path, i);
FHIRLexer lexer = new FHIRLexer(path, i, allowDoubleQuotes);
if (lexer.done()) {
throw lexer.error("Path cannot be empty");
}
@ -1400,8 +1401,7 @@ public class FHIRPathEngine {
private boolean checkParamCount(FHIRLexer lexer, SourceLocation location, ExpressionNode exp, int count)
throws FHIRLexerException {
if (exp.getParameters().size() != count) {
throw lexer.error("The function \"" + exp.getName() + "\" requires " + Integer.toString(count) + " parameters",
location.toString());
throw lexer.error("The function \"" + exp.getName() + "\" requires " + Integer.toString(count) + " parameters");
}
return true;
}
@ -1410,7 +1410,7 @@ public class FHIRPathEngine {
int countMax) throws FHIRLexerException {
if (exp.getParameters().size() < countMin || exp.getParameters().size() > countMax) {
throw lexer.error("The function \"" + exp.getName() + "\" requires between " + Integer.toString(countMin)
+ " and " + Integer.toString(countMax) + " parameters", location.toString());
+ " and " + Integer.toString(countMax) + " parameters");
}
return true;
}
@ -6722,4 +6722,14 @@ public class FHIRPathEngine {
this.liquidMode = liquidMode;
}
public ProfileUtilities getProfileUtilities() {
return profileUtilities;
}
public boolean isAllowDoubleQuotes() {
return allowDoubleQuotes;
}
public void setAllowDoubleQuotes(boolean allowDoubleQuotes) {
this.allowDoubleQuotes = allowDoubleQuotes;
}
}

View File

@ -50,6 +50,7 @@ import org.hl7.fhir.r4b.model.ValueSet;
import org.hl7.fhir.r4b.utils.FHIRPathEngine.ExpressionNodeWithOffset;
import org.hl7.fhir.r4b.utils.FHIRPathEngine.IEvaluationContext;
import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.i18n.I18nConstants;
import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode;
@ -70,17 +71,27 @@ public class LiquidEngine implements IEvaluationContext {
private class LiquidEngineContext {
private Object externalContext;
private Map<String, Base> vars = new HashMap<>();
private Map<String, Base> loopVars = new HashMap<>();
private Map<String, Base> globalVars = new HashMap<>();
public LiquidEngineContext(Object externalContext) {
super();
this.externalContext = externalContext;
globalVars = new HashMap<>();
}
public LiquidEngineContext(Object externalContext, LiquidEngineContext existing) {
super();
this.externalContext = externalContext;
loopVars.putAll(existing.loopVars);
globalVars = existing.globalVars;
}
public LiquidEngineContext(LiquidEngineContext existing) {
super();
externalContext = existing.externalContext;
vars.putAll(existing.vars);
loopVars.putAll(existing.loopVars);
globalVars = existing.globalVars;
}
}
@ -121,6 +132,7 @@ public class LiquidEngine implements IEvaluationContext {
return b.toString();
}
private abstract class LiquidNode {
protected void closeUp() {
}
@ -163,7 +175,6 @@ public class LiquidEngine implements IEvaluationContext {
private class LiquidExpressionNode {
private LiquidFilter filter; // null at root
private ExpressionNode expression; // null for some filters
public LiquidExpressionNode(LiquidFilter filter, ExpressionNode expression) {
super();
this.filter = filter;
@ -179,7 +190,7 @@ public class LiquidEngine implements IEvaluationContext {
@Override
public void evaluate(StringBuilder b, Base resource, LiquidEngineContext ctxt) throws FHIRException {
if (compiled.size() == 0) {
FHIRLexer lexer = new FHIRLexer(statement, "liquid statement");
FHIRLexer lexer = new FHIRLexer(statement, "liquid statement", false, true);
lexer.setLiquidMode(true);
compiled.add(new LiquidExpressionNode(null, engine.parse(lexer)));
while (!lexer.done()) {
@ -188,7 +199,7 @@ public class LiquidEngine implements IEvaluationContext {
String f = lexer.getCurrent();
LiquidFilter filter = LiquidFilter.fromCode(f);
if (filter == null) {
lexer.error("Unknown Liquid filter '" + f + "'");
lexer.error(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_FILTER, f));
}
lexer.next();
if (!lexer.done() && lexer.getCurrent().equals(":")) {
@ -198,7 +209,7 @@ public class LiquidEngine implements IEvaluationContext {
compiled.add(new LiquidExpressionNode(filter, null));
}
} else {
lexer.error("Unexpected syntax parsing liquid statement");
lexer.error(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_SYNTAX));
}
}
}
@ -207,8 +218,7 @@ public class LiquidEngine implements IEvaluationContext {
for (LiquidExpressionNode i : compiled) {
if (i.filter == null) { // first
t = stmtToString(ctxt, engine.evaluate(ctxt, resource, resource, resource, i.expression));
} else
switch (i.filter) {
} else switch (i.filter) {
case PREPEND:
t = stmtToString(ctxt, engine.evaluate(ctxt, resource, resource, resource, i.expression)) + t;
break;
@ -221,10 +231,7 @@ public class LiquidEngine implements IEvaluationContext {
StringBuilder b = new StringBuilder();
boolean first = true;
for (Base i : items) {
if (first)
first = false;
else
b.append(", ");
if (first) first = false; else b.append(", ");
String s = renderingSupport != null ? renderingSupport.renderForLiquid(ctxt.externalContext, i) : null;
b.append(s != null ? s : engine.convertToString(i));
}
@ -312,6 +319,30 @@ public class LiquidEngine implements IEvaluationContext {
}
}
private class LiquidAssign extends LiquidNode {
private String varName;
private String expression;
private ExpressionNode compiled;
@Override
public void evaluate(StringBuilder b, Base resource, LiquidEngineContext ctxt) throws FHIRException {
if (compiled == null) {
boolean dbl = engine.isAllowDoubleQuotes();
engine.setAllowDoubleQuotes(true);
ExpressionNodeWithOffset po = engine.parsePartial(expression, 0);
compiled = po.getNode();
engine.setAllowDoubleQuotes(dbl);
}
List<Base> list = engine.evaluate(ctxt, resource, resource, resource, compiled);
if (list.isEmpty()) {
ctxt.globalVars.remove(varName);
} else if (list.size() == 1) {
ctxt.globalVars.put(varName, list.get(0));
} else {
throw new Error("Assign returned a list?");
}
}
}
private class LiquidFor extends LiquidNode {
private String varName;
private String condition;
@ -350,7 +381,10 @@ public class LiquidEngine implements IEvaluationContext {
if (limit >= 0 && i == limit) {
break;
}
lctxt.vars.put(varName, o);
if (lctxt.globalVars.containsKey(varName)) {
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ALREADY_ASSIGNED, varName));
}
lctxt.loopVars.put(varName, o);
boolean wantBreak = false;
for (LiquidNode n : body) {
try {
@ -379,7 +413,7 @@ public class LiquidEngine implements IEvaluationContext {
} else if (cnt.startsWith("limit")) {
cnt = cnt.substring(5).trim();
if (!cnt.startsWith(":")) {
throw new FHIRException("Exception evaluating " + src + ": limit is not followed by ':'");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_COLON, src));
}
cnt = cnt.substring(1).trim();
int i = 0;
@ -387,14 +421,14 @@ public class LiquidEngine implements IEvaluationContext {
i++;
}
if (i == 0) {
throw new FHIRException("Exception evaluating " + src + ": limit is not followed by a number");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NUMBER, src));
}
limit = Integer.parseInt(cnt.substring(0, i));
cnt = cnt.substring(i);
} else if (cnt.startsWith("offset")) {
cnt = cnt.substring(6).trim();
if (!cnt.startsWith(":")) {
throw new FHIRException("Exception evaluating " + src + ": limit is not followed by ':'");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_COLON, src));
}
cnt = cnt.substring(1).trim();
int i = 0;
@ -402,12 +436,12 @@ public class LiquidEngine implements IEvaluationContext {
i++;
}
if (i == 0) {
throw new FHIRException("Exception evaluating " + src + ": limit is not followed by a number");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NUMBER, src));
}
offset = Integer.parseInt(cnt.substring(0, i));
cnt = cnt.substring(i);
} else {
throw new FHIRException("Exception evaluating " + src + ": unexpected content at " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_UNEXPECTED, cnt));
}
}
}
@ -422,9 +456,9 @@ public class LiquidEngine implements IEvaluationContext {
String src = includeResolver.fetchInclude(LiquidEngine.this, page);
LiquidParser parser = new LiquidParser(src);
LiquidDocument doc = parser.parse(page);
LiquidEngineContext nctxt = new LiquidEngineContext(ctxt.externalContext);
LiquidEngineContext nctxt = new LiquidEngineContext(ctxt.externalContext, ctxt);
Tuple incl = new Tuple();
nctxt.vars.put("include", incl);
nctxt.loopVars.put("include", incl);
for (String s : params.keySet()) {
incl.addProperty(s, engine.evaluate(ctxt, resource, resource, resource, params.get(s)));
}
@ -481,13 +515,11 @@ public class LiquidEngine implements IEvaluationContext {
cnt = "," + cnt.substring(5).trim();
while (!Utilities.noString(cnt)) {
if (!cnt.startsWith(",")) {
throw new FHIRException(
"Script " + name + ": Script " + name + ": Found " + cnt.charAt(0) + " expecting ',' parsing cycle");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_EXPECTING, name, cnt.charAt(0), ','));
}
cnt = cnt.substring(1).trim();
if (!cnt.startsWith("\"")) {
throw new FHIRException(
"Script " + name + ": Script " + name + ": Found " + cnt.charAt(0) + " expecting '\"' parsing cycle");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_EXPECTING, name, cnt.charAt(0), '"'));
}
cnt = cnt.substring(1);
int i = 0;
@ -495,7 +527,7 @@ public class LiquidEngine implements IEvaluationContext {
i++;
}
if (i == cnt.length()) {
throw new FHIRException("Script " + name + ": Script " + name + ": Found unterminated string parsing cycle");
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_UNTERMINATED, name));
}
res.list.add(cnt.substring(0, i));
cnt = cnt.substring(i + 1).trim();
@ -527,9 +559,10 @@ public class LiquidEngine implements IEvaluationContext {
list.add(parseCycle(cnt));
else if (cnt.startsWith("include "))
list.add(parseInclude(cnt.substring(7).trim()));
else if (cnt.startsWith("assign "))
list.add(parseAssign(cnt.substring(6).trim()));
else
throw new FHIRException(
"Script " + name + ": Script " + name + ": Unknown flow control statement " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_FLOW_STMT,name, cnt));
} else { // next2() == '{'
list.add(parseStatement());
}
@ -543,8 +576,7 @@ public class LiquidEngine implements IEvaluationContext {
n.closeUp();
if (terminators.length > 0)
if (!isTerminator(close, terminators))
throw new FHIRException(
"Script " + name + ": Script " + name + ": Found end of script looking for " + terminators);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_UNKNOWN_NOEND, name, terminators));
return close;
}
@ -588,7 +620,7 @@ public class LiquidEngine implements IEvaluationContext {
while (i < cnt.length() && !Character.isWhitespace(cnt.charAt(i)))
i++;
if (i == cnt.length() || i == 0)
throw new FHIRException("Script " + name + ": Error reading include: " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name + ": Error reading include: " + cnt));
LiquidInclude res = new LiquidInclude();
res.page = cnt.substring(0, i);
while (i < cnt.length() && Character.isWhitespace(cnt.charAt(i)))
@ -598,10 +630,10 @@ public class LiquidEngine implements IEvaluationContext {
while (i < cnt.length() && cnt.charAt(i) != '=')
i++;
if (i >= cnt.length() || j == i)
throw new FHIRException("Script " + name + ": Error reading include: " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name, cnt));
String n = cnt.substring(j, i);
if (res.params.containsKey(n))
throw new FHIRException("Script " + name + ": Error reading include: " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_INCLUDE, name, cnt));
i++;
ExpressionNodeWithOffset t = engine.parsePartial(cnt, i);
i = t.getOffset();
@ -618,13 +650,16 @@ public class LiquidEngine implements IEvaluationContext {
i++;
LiquidFor res = new LiquidFor();
res.varName = cnt.substring(0, i);
if ("include".equals(res.varName)) {
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ILLEGAL, res.varName));
}
while (Character.isWhitespace(cnt.charAt(i)))
i++;
int j = i;
while (!Character.isWhitespace(cnt.charAt(i)))
i++;
if (!"in".equals(cnt.substring(j, i)))
throw new FHIRException("Script " + name + ": Script " + name + ": Error reading loop: " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_LOOP, name, cnt));
res.condition = cnt.substring(i).trim();
parseList(res.body, false, new String[] { "endloop" });
return res;
@ -636,13 +671,16 @@ public class LiquidEngine implements IEvaluationContext {
i++;
LiquidFor res = new LiquidFor();
res.varName = cnt.substring(0, i);
if ("include".equals(res.varName)) {
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_VARIABLE_ILLEGAL, res.varName));
}
while (Character.isWhitespace(cnt.charAt(i)))
i++;
int j = i;
while (!Character.isWhitespace(cnt.charAt(i)))
i++;
if (!"in".equals(cnt.substring(j, i)))
throw new FHIRException("Script " + name + ": Script " + name + ": Error reading loop: " + cnt);
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_LOOP, name, cnt));
res.condition = cnt.substring(i).trim();
String term = parseList(res.body, true, new String[] { "endfor", "else" });
if ("else".equals(term)) {
@ -651,6 +689,21 @@ public class LiquidEngine implements IEvaluationContext {
return res;
}
private LiquidNode parseAssign(String cnt) throws FHIRException {
int i = 0;
while (!Character.isWhitespace(cnt.charAt(i)))
i++;
LiquidAssign res = new LiquidAssign();
res.varName = cnt.substring(0, i);
while (Character.isWhitespace(cnt.charAt(i)))
i++;
int j = i;
while (!Character.isWhitespace(cnt.charAt(i)))
i++;
res.expression = cnt.substring(i).trim();
return res;
}
private String parseTag(char ch) throws FHIRException {
grab();
grab();
@ -659,7 +712,7 @@ public class LiquidEngine implements IEvaluationContext {
b.append(grab());
}
if (!(next1() == '%' && next2() == '}'))
throw new FHIRException("Script " + name + ": Unterminated Liquid statement {% " + b.toString());
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NOTERM, name, "{% " + b.toString()));
grab();
grab();
return b.toString().trim();
@ -673,7 +726,7 @@ public class LiquidEngine implements IEvaluationContext {
b.append(grab());
}
if (!(next1() == '}' && next2() == '}'))
throw new FHIRException("Script " + name + ": Unterminated Liquid statement {{ " + b.toString());
throw new FHIRException(engine.getWorker().formatMessage(I18nConstants.LIQUID_SYNTAX_NOTERM, name, "{{ " + b.toString()));
grab();
grab();
LiquidStatement res = new LiquidStatement();
@ -686,8 +739,10 @@ public class LiquidEngine implements IEvaluationContext {
@Override
public List<Base> resolveConstant(Object appContext, String name, boolean beforeContext) throws PathEngineException {
LiquidEngineContext ctxt = (LiquidEngineContext) appContext;
if (ctxt.vars.containsKey(name))
return new ArrayList<Base>(Arrays.asList(ctxt.vars.get(name)));
if (ctxt.loopVars.containsKey(name))
return new ArrayList<Base>(Arrays.asList(ctxt.loopVars.get(name)));
if (ctxt.globalVars.containsKey(name))
return new ArrayList<Base>(Arrays.asList(ctxt.globalVars.get(name)));
if (externalHostServices == null)
return new ArrayList<Base>();
return externalHostServices.resolveConstant(ctxt.externalContext, name, beforeContext);
@ -716,8 +771,7 @@ public class LiquidEngine implements IEvaluationContext {
}
@Override
public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters)
throws PathEngineException {
public TypeDetails checkFunction(Object appContext, String functionName, List<TypeDetails> parameters) throws PathEngineException {
if (externalHostServices == null)
return null;
LiquidEngineContext ctxt = (LiquidEngineContext) appContext;
@ -725,8 +779,7 @@ public class LiquidEngine implements IEvaluationContext {
}
@Override
public List<Base> executeFunction(Object appContext, List<Base> focus, String functionName,
List<List<Base>> parameters) {
public List<Base> executeFunction(Object appContext, List<Base> focus, String functionName, List<List<Base>> parameters) {
if (externalHostServices == null)
return null;
LiquidEngineContext ctxt = (LiquidEngineContext) appContext;