Repository: incubator-freemarker Updated Branches: refs/heads/2.3-gae df4dc52f2 -> eacd51689
Cleanup related to [=...], also some missing functionality added. Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/eacd5168 Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/eacd5168 Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/eacd5168 Branch: refs/heads/2.3-gae Commit: eacd51689fb301d4d128bc26dd9e96781474fd89 Parents: df4dc52 Author: ddekany <ddek...@apache.org> Authored: Sun Mar 18 18:37:23 2018 +0100 Committer: ddekany <ddek...@apache.org> Committed: Sun Mar 18 18:37:23 2018 +0100 ---------------------------------------------------------------------- .../java/freemarker/core/DollarVariable.java | 11 ++++-- .../java/freemarker/core/NumericalOutput.java | 5 ++- .../java/freemarker/core/StringLiteral.java | 6 +-- .../ext/beans/OverloadedNumberUtil.java | 2 +- .../java/freemarker/template/Configuration.java | 3 +- src/main/java/freemarker/template/Template.java | 22 +++++++++++ src/main/javacc/FTL.jj | 41 ++++++++------------ .../core/InterpolationSyntaxTest.java | 8 ++++ 8 files changed, 64 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/core/DollarVariable.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/DollarVariable.java b/src/main/java/freemarker/core/DollarVariable.java index 7e7bd1f..12fa02a 100644 --- a/src/main/java/freemarker/core/DollarVariable.java +++ b/src/main/java/freemarker/core/DollarVariable.java @@ -22,11 +22,15 @@ package freemarker.core; import java.io.IOException; import java.io.Writer; +import freemarker.template.Configuration; import freemarker.template.TemplateException; import freemarker.template.utility.StringUtil; /** - * An instruction that outputs the value of an <tt>Expression</tt>. + * An interpolation like <code>${exp}</code> or {@code [=exp]}. The class name is the remnant of old times, but as + * some users are using the package-visible AST API, it wasn't renamed. + * + * @see NumericalOutput */ final class DollarVariable extends Interpolation { @@ -99,10 +103,11 @@ final class DollarVariable extends Interpolation { @Override protected String dump(boolean canonical, boolean inStringLiteral) { StringBuilder sb = new StringBuilder(); - sb.append("${"); + int syntax = getTemplate().getInterpolationSyntax(); + sb.append(syntax != Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX ? "${" : "[="); final String exprCF = expression.getCanonicalForm(); sb.append(inStringLiteral ? StringUtil.FTLStringLiteralEnc(exprCF, '"') : exprCF); - sb.append("}"); + sb.append(syntax != Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX ? "}" : "]"); if (!canonical && expression != escapedExpression) { sb.append(" auto-escaped"); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/core/NumericalOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/NumericalOutput.java b/src/main/java/freemarker/core/NumericalOutput.java index 08e8301..c897bd1 100644 --- a/src/main/java/freemarker/core/NumericalOutput.java +++ b/src/main/java/freemarker/core/NumericalOutput.java @@ -28,7 +28,10 @@ import freemarker.template.TemplateException; import freemarker.template.utility.StringUtil; /** - * An instruction that outputs the value of a numerical expression. + * An interpolation like <code>#{numericalExp; format}</code>; it's deprecated, but still supported. The class name is + * the remnant of old times, but as some users are using the package-visible AST API, it wasn't renamed. + * + * @see DollarVariable */ final class NumericalOutput extends Interpolation { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/core/StringLiteral.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/core/StringLiteral.java b/src/main/java/freemarker/core/StringLiteral.java index 6d53c17..c116498 100644 --- a/src/main/java/freemarker/core/StringLiteral.java +++ b/src/main/java/freemarker/core/StringLiteral.java @@ -54,9 +54,9 @@ final class StringLiteral extends Expression implements TemplateScalarModel { if (value.length() > 3 && ( (intSyn == Configuration.LEGACY_INTERPOLATION_SYNTAX || intSyn == Configuration.DOLLAR_INTERPOLATION_SYNTAX) - && (value.indexOf("${") >= 0 - || intSyn == Configuration.LEGACY_INTERPOLATION_SYNTAX && value.indexOf("#{") >= 0) - || intSyn == Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX && value.indexOf("[=") >= 0)) { + && (value.indexOf("${") != -1 + || intSyn == Configuration.LEGACY_INTERPOLATION_SYNTAX && value.indexOf("#{") != -1) + || intSyn == Configuration.SQUARE_BRACKET_INTERPOLATION_SYNTAX && value.indexOf("[=") != -1)) { try { SimpleCharStream simpleCharacterStream = new SimpleCharStream( new StringReader(value), http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/ext/beans/OverloadedNumberUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/ext/beans/OverloadedNumberUtil.java b/src/main/java/freemarker/ext/beans/OverloadedNumberUtil.java index 352d1de..fbb2fc0 100644 --- a/src/main/java/freemarker/ext/beans/OverloadedNumberUtil.java +++ b/src/main/java/freemarker/ext/beans/OverloadedNumberUtil.java @@ -89,7 +89,7 @@ class OverloadedNumberUtil { * @param num the number to coerce * @param typeFlags the type flags of the target parameter position; see {@link TypeFlags} * - * @returns The original number or a {@link NumberWithFallbackType}, depending on the actual value and the types + * @return The original number or a {@link NumberWithFallbackType}, depending on the actual value and the types * indicated in the {@code targetNumTypes} parameter. */ static Number addFallbackType(final Number num, final int typeFlags) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/template/Configuration.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/Configuration.java b/src/main/java/freemarker/template/Configuration.java index 53fcd49..3114747 100644 --- a/src/main/java/freemarker/template/Configuration.java +++ b/src/main/java/freemarker/template/Configuration.java @@ -2409,8 +2409,7 @@ public class Configuration extends Configurable implements Cloneable, ParserConf } /** - * Determines the interpolation syntax (like <code>${x}</code> VS <code>[=x]</code>) of the template files in which - * there's no {@code #ftl} hader with {@code interpolation_syntax} parameter. The + * Determines the interpolation syntax (like <code>${x}</code> VS <code>[=x]</code>) of the template files. The * {@code interpolationSyntax} parameter must be one of {@link Configuration#LEGACY_INTERPOLATION_SYNTAX}, * {@link Configuration#DOLLAR_INTERPOLATION_SYNTAX}, and {@link Configuration#SQUARE_BRACKET_INTERPOLATION_SYNTAX}. * http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/java/freemarker/template/Template.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/Template.java b/src/main/java/freemarker/template/Template.java index c95be5e..f21fffc 100644 --- a/src/main/java/freemarker/template/Template.java +++ b/src/main/java/freemarker/template/Template.java @@ -87,6 +87,7 @@ public class Template extends Configurable { private TemplateElement rootElement; private String encoding, defaultNS; private Object customLookupCondition; + private int interpolationSyntax; private int actualTagSyntax; private int actualNamingConvention; private boolean autoEscaping; @@ -258,6 +259,7 @@ public class Template extends Configurable { rootElement = null; } this.actualTagSyntax = parser._getLastTagSyntax(); + this.interpolationSyntax = actualParserConfiguration.getInterpolationSyntax(); this.actualNamingConvention = parser._getLastNamingConvention(); } catch (TokenMgrError exc) { // TokenMgrError VS ParseException is not an interesting difference for the user, so we just convert it @@ -640,6 +642,8 @@ public class Template extends Configurable { * returns whatever the default is in the current configuration, so it's maybe * {@link Configuration#AUTO_DETECT_TAG_SYNTAX}. * + * @see Configuration#setTagSyntax(int) + * * @since 2.3.20 */ public int getActualTagSyntax() { @@ -647,12 +651,30 @@ public class Template extends Configurable { } /** + * Returns the interpolation syntax the parser has used for this template. Because the interpolation syntax is + * never auto-detected, it's not called "getActualInterpolationSyntax" (unlike {@link #getActualTagSyntax()}). + * + * @return A constant like {@link Configuration#LEGACY_INTERPOLATION_SYNTAX}, + * {@link Configuration#DOLLAR_INTERPOLATION_SYNTAX}, or + * {@link Configuration#SQUARE_BRACKET_INTERPOLATION_SYNTAX}. + * + * @see Configuration#setInterpolationSyntax(int) + * + * @since 2.3.28 + */ + public int getInterpolationSyntax() { + return interpolationSyntax; + } + + /** * Returns the naming convention the parser has chosen for this template. If it could be determined, it's * {@link Configuration#LEGACY_NAMING_CONVENTION} or {@link Configuration#CAMEL_CASE_NAMING_CONVENTION}. If it * couldn't be determined (like because there no identifier that's part of the template language was used where * the naming convention matters), this returns whatever the default is in the current configuration, so it's maybe * {@link Configuration#AUTO_DETECT_TAG_SYNTAX}. * + * @see Configuration#setNamingConvention(int) + * * @since 2.3.23 */ public int getActualNamingConvention() { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/main/javacc/FTL.jj ---------------------------------------------------------------------- diff --git a/src/main/javacc/FTL.jj b/src/main/javacc/FTL.jj index 4134b70..d3be697 100644 --- a/src/main/javacc/FTL.jj +++ b/src/main/javacc/FTL.jj @@ -1194,7 +1194,7 @@ TOKEN: <#ESCAPED_CHAR : "\\" ( - ("n" | "t" | "r" | "f" | "b" | "g" | "l" | "a" | "\\" | "'" | "\"" | "$" | "{" | "=") + ("n" | "t" | "r" | "f" | "b" | "g" | "l" | "a" | "\\" | "'" | "\"" | "{" | "=") | ("x" ["0"-"9", "A"-"F", "a"-"f"]) ) @@ -2329,11 +2329,11 @@ StringLiteral StringLiteral(boolean interpolate) : int interpolationSyntax = pCfg.getInterpolationSyntax(); if ((interpolationSyntax == LEGACY_INTERPOLATION_SYNTAX || interpolationSyntax == DOLLAR_INTERPOLATION_SYNTAX) - && t.image.indexOf("${") >= 0 + && t.image.indexOf("${") != -1 || interpolationSyntax == LEGACY_INTERPOLATION_SYNTAX - && t.image.indexOf("#{") >= 0 + && t.image.indexOf("#{") != -1 || interpolationSyntax == SQUARE_BRACKET_INTERPOLATION_SYNTAX - && t.image.indexOf("[=") >= 0) { + && t.image.indexOf("[=") != -1) { result.parseValue(this, outputFormat); } } @@ -2400,7 +2400,7 @@ HashLiteral HashLiteral() : /** * A production representing the ${...} or [=...] that outputs a variable. */ -DollarVariable StringOutput() : +DollarVariable NormalInterpolation() : { Expression exp; Token begin, end; @@ -2410,26 +2410,19 @@ DollarVariable StringOutput() : ( begin = <DOLLAR_INTERPOLATION_OPENING> exp = Expression() - { - notHashLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); - notListLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); - } - end = <CLOSING_CURLY_BRACKET> ) | ( begin = <SQUARE_BRACKET_INTERPOLATION_OPENING> exp = Expression() - { - notHashLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); - notListLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); - } - end = <CLOSE_BRACKET> ) ) { + notHashLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); + notListLiteral(exp, NonStringException.STRING_COERCABLE_TYPES_DESC); + DollarVariable result = new DollarVariable( exp, escapedExpression(exp), outputFormat, @@ -2439,7 +2432,7 @@ DollarVariable StringOutput() : } } -NumericalOutput NumericalOutput() : +NumericalOutput NumericalInterpolation() : { Expression exp; Token fmt = null, begin, end; @@ -4140,9 +4133,9 @@ TemplateElements MixedContentElements() : ( elem = PCData() | - elem = StringOutput() + elem = NormalInterpolation() | - elem = NumericalOutput() + elem = NumericalInterpolation() | elem = FreemarkerDirective() ) @@ -4186,9 +4179,9 @@ MixedContent MixedContent() : ( elem = PCData() | - elem = StringOutput() + elem = NormalInterpolation() | - elem = NumericalOutput() + elem = NumericalInterpolation() | elem = FreemarkerDirective() ) @@ -4243,9 +4236,9 @@ TemplateElement FreeMarkerText() : ( elem = PCData() | - elem = StringOutput() + elem = NormalInterpolation() | - elem = NumericalOutput() + elem = NumericalInterpolation() ) { if (begin == null) { @@ -4495,12 +4488,12 @@ List<Object> StaticTextAndInterpolations() : ( LOOKAHEAD(<DOLLAR_INTERPOLATION_OPENING>|<SQUARE_BRACKET_INTERPOLATION_OPENING>) ( - interpolation = StringOutput() + interpolation = NormalInterpolation() ) | LOOKAHEAD(<HASH_INTERPOLATION_OPENING>) ( - interpolation = NumericalOutput() + interpolation = NumericalInterpolation() ) ) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/eacd5168/src/test/java/freemarker/core/InterpolationSyntaxTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/core/InterpolationSyntaxTest.java b/src/test/java/freemarker/core/InterpolationSyntaxTest.java index adea7c9..8c57d36 100644 --- a/src/test/java/freemarker/core/InterpolationSyntaxTest.java +++ b/src/test/java/freemarker/core/InterpolationSyntaxTest.java @@ -1,9 +1,13 @@ package freemarker.core; import static freemarker.template.Configuration.*; +import static org.junit.Assert.*; + +import java.io.StringWriter; import org.junit.Test; +import freemarker.template.Template; import freemarker.test.TemplateTest; public class InterpolationSyntaxTest extends TemplateTest { @@ -78,6 +82,10 @@ public class InterpolationSyntaxTest extends TemplateTest { assertOutput("[='[\\=1]']", "[=1]"); assertOutput("[='[\\=1][=2]']", "12"); // Usual legacy interpolation escaping glitch... assertOutput("[=r'[=1]']", "[=1]"); + + StringWriter sw = new StringWriter(); + new Template(null, "[= 1 + '[= 2 ]' ]", getConfiguration()).dump(sw); + assertEquals("[=1 + \"[=2]\"]", sw.toString()); } @Test