diff --git a/Releasenotes.md b/Releasenotes.md index 87d3e13..9f6f076 100644 --- a/Releasenotes.md +++ b/Releasenotes.md @@ -190,4 +190,6 @@ Major Release 5.0.0 * Breaking Change: Removed all API deprecated in version 4 * Breaking Change: Switch to Java 8 language level * Bug fix / Enhancement: if statement more robust (https://github.com/DJCordhose/jmte/issues/8) -* Refactoring / Cleanup: Removed all caliper tests and real life test \ No newline at end of file +* Refactoring / Cleanup: Removed all caliper tests and real life test +* Deprecation: Configurable expression syntax, will be fixed to ${...} +* Enhancement: transform using a *null* template will yield *null* diff --git a/src/com/floreysoft/jmte/token/Lexer.java b/src/com/floreysoft/jmte/token/Lexer.java index 70757aa..f7ddba7 100644 --- a/src/com/floreysoft/jmte/token/Lexer.java +++ b/src/com/floreysoft/jmte/token/Lexer.java @@ -69,7 +69,7 @@ private AbstractToken innerNextToken(final String untrimmedInput) { negated = false; ifExpression = objectExpression; } - if (!ifExpression.contains("=") && !ifExpression.contains(";")) { + if (!input.contains("=") && !input.contains(";")) { return new IfToken(ifExpression, negated); } else { // HACK: if the value we compare to contains a space, it is cut off @@ -86,11 +86,16 @@ private AbstractToken innerNextToken(final String untrimmedInput) { String operand = null; if (hasCmp) { operand = completeIfExpression.substring(posLastEq + 1); + // heuristic: when there is leading or trailing space and after that a quote begins, + // it must be ignorable white space + if (isQuoted(operand.trim())) { + operand = operand.trim(); + } // remove optional quotations - if (operand.startsWith("'") || operand.startsWith("\"")) { + if (isQuoted(operand)) { operand = operand.substring(1, operand.length() - 1); } - complexVariable = completeIfExpression.substring(0, posLastEq); + complexVariable = completeIfExpression.substring(0, posLastEq).trim(); } else { complexVariable = completeIfExpression; } @@ -231,4 +236,8 @@ private AbstractToken innerNextToken(final String untrimmedInput) { } + private static boolean isQuoted(String operand) { + return operand.startsWith("'") || operand.startsWith("\""); + } + } diff --git a/test/com/floreysoft/jmte/EngineTest.java b/test/com/floreysoft/jmte/EngineTest.java index 28e31f4..04cdc9b 100644 --- a/test/com/floreysoft/jmte/EngineTest.java +++ b/test/com/floreysoft/jmte/EngineTest.java @@ -269,6 +269,13 @@ public String toString() { DEFAULT_MODEL.put("bigDecimal1", new BigDecimal("1.0")); } + @Test + public void transformRobustToNull() { + String output = newEngine() + .transform(null, new HashMap<>()); + assertNull(output); + } + @Test public void variableName() throws Exception { Map simpleModel = new HashMap(); @@ -1731,6 +1738,13 @@ public void ifEqSpacesSample() throws Exception { assertEquals("Filbert", output); } + @Test + public void ifEqSpacesElseSample() throws Exception { + String input = "${if address = 'Filbert2'}${address}${else}NIX${end}"; + String output = newEngine().transform(input, DEFAULT_MODEL); + assertEquals("NIX", output); + } + @Test public void ifEqSpacesRendererSample() throws Exception { String input = "${if address;string = 'Filbert'}${address}${else}NIX${end}";