Created published static utility class, o.a.f.core.util.FTLUtil, which contains some methods moved over from the now internal utility classes.
Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/1c4a73cf Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/1c4a73cf Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/1c4a73cf Branch: refs/heads/3 Commit: 1c4a73cf55dd1f3d3faaa7c98e91c3a65f030cf1 Parents: 06f4628 Author: ddekany <[email protected]> Authored: Fri Feb 17 21:22:10 2017 +0100 Committer: ddekany <[email protected]> Committed: Fri Feb 17 23:46:38 2017 +0100 ---------------------------------------------------------------------- .../freemarker/core/TemplateException.java | 4 +- .../core/ast/BuiltInBannedWhenAutoEscaping.java | 2 +- .../core/ast/BuiltInForLegacyEscaping.java | 2 +- .../freemarker/core/ast/Configurable.java | 3 +- .../freemarker/core/ast/DollarVariable.java | 4 +- .../freemarker/core/ast/NumericalOutput.java | 4 +- .../freemarker/core/ast/StringLiteral.java | 9 +- .../core/ast/_DelayedFTLTypeDescription.java | 4 +- .../ast/_ObjectBuilderSettingEvaluator.java | 3 +- .../freemarker/core/model/TemplateModel.java | 4 +- .../model/impl/beans/OverloadedMethods.java | 3 +- .../model/impl/beans/SimpleMethodModel.java | 4 +- .../apache/freemarker/core/util/FTLUtil.java | 802 +++++++++++++++++++ .../apache/freemarker/core/util/_ClassUtil.java | 168 ---- .../freemarker/core/util/_StringUtil.java | 578 +------------ src/manual/en_US/FM3-CHANGE-LOG.txt | 6 +- .../apache/freemarker/core/ast/ASTPrinter.java | 3 +- .../freemarker/core/util/FTLUtilTest.java | 118 +++ .../freemarker/core/util/StringUtilTest.java | 39 +- .../templatesuite/models/OverloadedMethods.java | 6 +- .../freemarker/test/util/AssertDirective.java | 4 +- 21 files changed, 975 insertions(+), 795 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/TemplateException.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/TemplateException.java b/src/main/java/org/apache/freemarker/core/TemplateException.java index ef50f23..a5f2740 100644 --- a/src/main/java/org/apache/freemarker/core/TemplateException.java +++ b/src/main/java/org/apache/freemarker/core/TemplateException.java @@ -329,7 +329,7 @@ public class TemplateException extends Exception { /** * @param heading should the heading at the top be printed - * @param ftlStackTrace should the FTL stack trace be printed + * @param ftlStackTrace should the FTL stack trace be printed * @param javaStackTrace should the Java stack trace be printed * * @since 2.3.20 @@ -342,7 +342,7 @@ public class TemplateException extends Exception { /** * @param heading should the heading at the top be printed - * @param ftlStackTrace should the FTL stack trace be printed + * @param ftlStackTrace should the FTL stack trace be printed * @param javaStackTrace should the Java stack trace be printed * * @since 2.3.20 http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java index c8701cf..d6fd349 100644 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java +++ b/src/main/java/org/apache/freemarker/core/ast/BuiltInBannedWhenAutoEscaping.java @@ -20,7 +20,7 @@ package org.apache.freemarker.core.ast; /** * A string built-in whose usage is banned when auto-escaping with a markup-output format is active. - * This is just a marker; the actual checking is in {@code FTL.jj}. + * This is just a marker; the actual checking is in {@code FTL.jj}. */ abstract class BuiltInBannedWhenAutoEscaping extends SpecialBuiltIn { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java index 4c79a9c..731f42b 100644 --- a/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java +++ b/src/main/java/org/apache/freemarker/core/ast/BuiltInForLegacyEscaping.java @@ -23,7 +23,7 @@ import org.apache.freemarker.core.model.TemplateModel; /** * A string built-in whose usage is banned when auto-escaping with a markup-output format is active. - * This is just a marker; the actual checking is in {@code FTL.jj}. + * This is just a marker; the actual checking is in {@code FTL.jj}. */ abstract class BuiltInForLegacyEscaping extends BuiltInBannedWhenAutoEscaping { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/Configurable.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/Configurable.java b/src/main/java/org/apache/freemarker/core/ast/Configurable.java index a00b808..48b04a7 100644 --- a/src/main/java/org/apache/freemarker/core/ast/Configurable.java +++ b/src/main/java/org/apache/freemarker/core/ast/Configurable.java @@ -65,6 +65,7 @@ import org.apache.freemarker.core.templateresolver.PathRegexMatcher; import org.apache.freemarker.core.templateresolver.TemplateLoader; import org.apache.freemarker.core.templateresolver.impl.DefaultTemplateNameFormat; import org.apache.freemarker.core.templateresolver.impl.DefaultTemplateNameFormatFM2; +import org.apache.freemarker.core.util.FTLUtil; import org.apache.freemarker.core.util._NullArgumentException; import org.apache.freemarker.core.util._SortedArraySet; import org.apache.freemarker.core.util._StringUtil; @@ -2875,7 +2876,7 @@ public class Configurable { if (w.startsWith("'") || w.startsWith("\"")) { w = w.substring(1, w.length() - 1); } - return _StringUtil.FTLStringLiteralDec(w); + return FTLUtil.unescapeStringLiteralPart(w); } String fetchKeyword() throws ParseException { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/DollarVariable.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/DollarVariable.java b/src/main/java/org/apache/freemarker/core/ast/DollarVariable.java index 318b66e..f892766 100644 --- a/src/main/java/org/apache/freemarker/core/ast/DollarVariable.java +++ b/src/main/java/org/apache/freemarker/core/ast/DollarVariable.java @@ -23,7 +23,7 @@ import java.io.IOException; import java.io.Writer; import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.util._StringUtil; +import org.apache.freemarker.core.util.FTLUtil; /** * An instruction that outputs the value of an <tt>Expression</tt>. @@ -101,7 +101,7 @@ final class DollarVariable extends Interpolation { StringBuilder sb = new StringBuilder(); sb.append("${"); final String exprCF = expression.getCanonicalForm(); - sb.append(inStringLiteral ? _StringUtil.FTLStringLiteralEnc(exprCF, '"') : exprCF); + sb.append(inStringLiteral ? FTLUtil.escapeStringLiteralPart(exprCF, '"') : exprCF); sb.append("}"); if (!canonical && expression != escapedExpression) { sb.append(" auto-escaped"); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/NumericalOutput.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/NumericalOutput.java b/src/main/java/org/apache/freemarker/core/ast/NumericalOutput.java index ac7b817..1824b99 100644 --- a/src/main/java/org/apache/freemarker/core/ast/NumericalOutput.java +++ b/src/main/java/org/apache/freemarker/core/ast/NumericalOutput.java @@ -25,7 +25,7 @@ import java.text.NumberFormat; import java.util.Locale; import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.util._StringUtil; +import org.apache.freemarker.core.util.FTLUtil; /** * An instruction that outputs the value of a numerical expression. @@ -104,7 +104,7 @@ final class NumericalOutput extends Interpolation { protected String dump(boolean canonical, boolean inStringLiteral) { StringBuilder buf = new StringBuilder("#{"); final String exprCF = expression.getCanonicalForm(); - buf.append(inStringLiteral ? _StringUtil.FTLStringLiteralEnc(exprCF, '"') : exprCF); + buf.append(inStringLiteral ? FTLUtil.escapeStringLiteralPart(exprCF, '"') : exprCF); if (hasFormat) { buf.append(" ; "); buf.append("m"); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/StringLiteral.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/StringLiteral.java b/src/main/java/org/apache/freemarker/core/ast/StringLiteral.java index 255d4f8..3d9ea96 100644 --- a/src/main/java/org/apache/freemarker/core/ast/StringLiteral.java +++ b/src/main/java/org/apache/freemarker/core/ast/StringLiteral.java @@ -24,13 +24,10 @@ import java.util.List; import org.apache.freemarker.core.Template; import org.apache.freemarker.core.TemplateException; -import org.apache.freemarker.core.ast.FMParser; -import org.apache.freemarker.core.ast.FMParserTokenManager; -import org.apache.freemarker.core.ast.SimpleCharStream; import org.apache.freemarker.core.model.TemplateModel; import org.apache.freemarker.core.model.TemplateScalarModel; import org.apache.freemarker.core.model.impl.SimpleScalar; -import org.apache.freemarker.core.util._StringUtil; +import org.apache.freemarker.core.util.FTLUtil; final class StringLiteral extends Expression implements TemplateScalarModel { @@ -149,7 +146,7 @@ final class StringLiteral extends Expression implements TemplateScalarModel { @Override public String getCanonicalForm() { if (dynamicValue == null) { - return _StringUtil.ftlQuote(value); + return FTLUtil.toStringLiteral(value); } else { StringBuilder sb = new StringBuilder(); sb.append('"'); @@ -157,7 +154,7 @@ final class StringLiteral extends Expression implements TemplateScalarModel { if (child instanceof Interpolation) { sb.append(((Interpolation) child).getCanonicalFormInStringLiteral()); } else { - sb.append(_StringUtil.FTLStringLiteralEnc((String) child, '"')); + sb.append(FTLUtil.escapeStringLiteralPart((String) child, '"')); } } sb.append('"'); http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/_DelayedFTLTypeDescription.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/_DelayedFTLTypeDescription.java b/src/main/java/org/apache/freemarker/core/ast/_DelayedFTLTypeDescription.java index 0bbc6cf..1551c46 100644 --- a/src/main/java/org/apache/freemarker/core/ast/_DelayedFTLTypeDescription.java +++ b/src/main/java/org/apache/freemarker/core/ast/_DelayedFTLTypeDescription.java @@ -20,7 +20,7 @@ package org.apache.freemarker.core.ast; import org.apache.freemarker.core.model.TemplateModel; -import org.apache.freemarker.core.util._ClassUtil; +import org.apache.freemarker.core.util.FTLUtil; /** Don't use this; used internally by FreeMarker, might changes without notice. */ public class _DelayedFTLTypeDescription extends _DelayedConversionToString { @@ -31,7 +31,7 @@ public class _DelayedFTLTypeDescription extends _DelayedConversionToString { @Override protected String doConversion(Object obj) { - return _ClassUtil.getFTLTypeDescription((TemplateModel) obj); + return FTLUtil.getFTLTypeDescription((TemplateModel) obj); } } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/ast/_ObjectBuilderSettingEvaluator.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/ast/_ObjectBuilderSettingEvaluator.java b/src/main/java/org/apache/freemarker/core/ast/_ObjectBuilderSettingEvaluator.java index 06ccd7d..214d511 100644 --- a/src/main/java/org/apache/freemarker/core/ast/_ObjectBuilderSettingEvaluator.java +++ b/src/main/java/org/apache/freemarker/core/ast/_ObjectBuilderSettingEvaluator.java @@ -54,6 +54,7 @@ import org.apache.freemarker.core.templateresolver.NotMatcher; import org.apache.freemarker.core.templateresolver.OrMatcher; import org.apache.freemarker.core.templateresolver.PathGlobMatcher; import org.apache.freemarker.core.templateresolver.PathRegexMatcher; +import org.apache.freemarker.core.util.FTLUtil; import org.apache.freemarker.core.util.WriteProtectable; import org.apache.freemarker.core.util._ClassUtil; import org.apache.freemarker.core.util._StringUtil; @@ -539,7 +540,7 @@ public class _ObjectBuilderSettingEvaluator { final String sInside = src.substring(startPos + (raw ? 2 : 1), pos); try { pos++; // skip closing quotation mark - return raw ? sInside : _StringUtil.FTLStringLiteralDec(sInside); + return raw ? sInside : FTLUtil.unescapeStringLiteralPart(sInside); } catch (ParseException e) { throw new _ObjectBuilderSettingEvaluationException("Malformed string literal: " + sInside, e); } http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/model/TemplateModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/model/TemplateModel.java b/src/main/java/org/apache/freemarker/core/model/TemplateModel.java index f92cd4a..9bfa63b 100644 --- a/src/main/java/org/apache/freemarker/core/model/TemplateModel.java +++ b/src/main/java/org/apache/freemarker/core/model/TemplateModel.java @@ -20,7 +20,7 @@ package org.apache.freemarker.core.model; import org.apache.freemarker.core.Configuration; -import org.apache.freemarker.core.util._ClassUtil; +import org.apache.freemarker.core.util.FTLUtil; /** * The common super-interface of the interfaces that stand for the FreeMarker Template Language (FTL) data types. @@ -42,7 +42,7 @@ import org.apache.freemarker.core.util._ClassUtil; * these types: string, number, boolean, date. The intended applications are like string+hash, string+method, * hash+sequence, etc. * - * @see _ClassUtil#getFTLTypeDescription(TemplateModel) + * @see FTLUtil#getFTLTypeDescription(TemplateModel) */ public interface TemplateModel { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/model/impl/beans/OverloadedMethods.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/model/impl/beans/OverloadedMethods.java b/src/main/java/org/apache/freemarker/core/model/impl/beans/OverloadedMethods.java index 91ce78c..726ed7d 100644 --- a/src/main/java/org/apache/freemarker/core/model/impl/beans/OverloadedMethods.java +++ b/src/main/java/org/apache/freemarker/core/model/impl/beans/OverloadedMethods.java @@ -31,6 +31,7 @@ import org.apache.freemarker.core.ast._ErrorDescriptionBuilder; import org.apache.freemarker.core.ast._TemplateModelException; import org.apache.freemarker.core.model.TemplateModel; import org.apache.freemarker.core.model.TemplateModelException; +import org.apache.freemarker.core.util.FTLUtil; import org.apache.freemarker.core.util._ClassUtil; /** @@ -218,7 +219,7 @@ final class OverloadedMethods { private _DelayedConversionToString getTMActualParameterTypes(List arguments) { final String[] argumentTypeDescs = new String[arguments.size()]; for (int i = 0; i < arguments.size(); i++) { - argumentTypeDescs[i] = _ClassUtil.getFTLTypeDescription((TemplateModel) arguments.get(i)); + argumentTypeDescs[i] = FTLUtil.getFTLTypeDescription((TemplateModel) arguments.get(i)); } return new DelayedCallSignatureToString(argumentTypeDescs) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/model/impl/beans/SimpleMethodModel.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/model/impl/beans/SimpleMethodModel.java b/src/main/java/org/apache/freemarker/core/model/impl/beans/SimpleMethodModel.java index 0105412..c23d410 100644 --- a/src/main/java/org/apache/freemarker/core/model/impl/beans/SimpleMethodModel.java +++ b/src/main/java/org/apache/freemarker/core/model/impl/beans/SimpleMethodModel.java @@ -30,7 +30,7 @@ import org.apache.freemarker.core.model.TemplateModel; import org.apache.freemarker.core.model.TemplateModelException; import org.apache.freemarker.core.model.TemplateSequenceModel; import org.apache.freemarker.core.model.impl.SimpleNumber; -import org.apache.freemarker.core.util._ClassUtil; +import org.apache.freemarker.core.util.FTLUtil; /** * A class that will wrap a reflected method call into a @@ -86,7 +86,7 @@ public final class SimpleMethodModel extends SimpleMethod public int size() throws TemplateModelException { throw new TemplateModelException( "Getting the number of items or enumerating the items is not supported on this " - + _ClassUtil.getFTLTypeDescription(this) + " value.\n" + + FTLUtil.getFTLTypeDescription(this) + " value.\n" + "(" + "Hint 1: Maybe you wanted to call this method first and then do something with its return value. " + "Hint 2: Getting items by intex possibly works, hence it's a \"+sequence\"." http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/util/FTLUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/util/FTLUtil.java b/src/main/java/org/apache/freemarker/core/util/FTLUtil.java new file mode 100644 index 0000000..6f69590 --- /dev/null +++ b/src/main/java/org/apache/freemarker/core/util/FTLUtil.java @@ -0,0 +1,802 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.freemarker.core.util; + +import org.apache.freemarker.core.ast.Environment; +import org.apache.freemarker.core.ast.Macro; +import org.apache.freemarker.core.ast.ParseException; +import org.apache.freemarker.core.ast.TemplateMarkupOutputModel; +import org.apache.freemarker.core.model.*; +import org.apache.freemarker.core.model.impl.beans.*; + +import java.util.HashSet; +import java.util.Set; + +/** + * Static utility methods that perform tasks specific to the FreeMarker Template Language (FTL). + * This is meant to be used from outside FreeMarker (i.e., it's an official, published API), not just from inside it. + * + * @since 3.0.0 + */ +public final class FTLUtil { + + private static final char[] ESCAPES = createEscapes(); + + private FTLUtil() { + // Not meant to be instantiated + } + + private static char[] createEscapes() { + char[] escapes = new char['\\' + 1]; + for (int i = 0; i < 32; ++i) { + escapes[i] = 1; + } + escapes['\\'] = '\\'; + escapes['\''] = '\''; + escapes['"'] = '"'; + escapes['<'] = 'l'; + escapes['>'] = 'g'; + escapes['&'] = 'a'; + escapes['\b'] = 'b'; + escapes['\t'] = 't'; + escapes['\n'] = 'n'; + escapes['\f'] = 'f'; + escapes['\r'] = 'r'; + return escapes; + } + + /** + * Escapes a string according the FTL string literal escaping rules, assuming the literal is quoted with + * {@code quotation}; it doesn't add the quotation marks themselves. + * + * @param quotation Either {@code '"'} or {@code '\''}. It's assumed that the string literal whose part we calculate is + * enclosed within this kind of quotation mark. Thus, the other kind of quotation character will not be + * escaped in the result. + * @since 2.3.22 + */ + public static String escapeStringLiteralPart(String s, char quotation) { + return escapeStringLiteralPart(s, quotation, false); + } + + /** + * Escapes a string according the FTL string literal escaping rules; it doesn't add the quotation marks themselves. + * As this method doesn't know if the string literal is quoted with regular quotation marks or apostrophe quote, it + * will escape both. + * + * @see #escapeStringLiteralPart(String, char) + */ + public static String escapeStringLiteralPart(String s) { + return escapeStringLiteralPart(s, (char) 0, false); + } + + private static String escapeStringLiteralPart(String s, char quotation, boolean addQuotation) { + final int ln = s.length(); + + final char otherQuotation; + if (quotation == 0) { + otherQuotation = 0; + } else if (quotation == '"') { + otherQuotation = '\''; + } else if (quotation == '\'') { + otherQuotation = '"'; + } else { + throw new IllegalArgumentException("Unsupported quotation character: " + quotation); + } + + final int escLn = ESCAPES.length; + StringBuilder buf = null; + for (int i = 0; i < ln; i++) { + char c = s.charAt(i); + char escape = + c < escLn ? ESCAPES[c] : + c == '{' && i > 0 && isInterpolationStart(s.charAt(i - 1)) ? '{' : + 0; + if (escape == 0 || escape == otherQuotation) { + if (buf != null) { + buf.append(c); + } + } else { + if (buf == null) { + buf = new StringBuilder(s.length() + 4 + (addQuotation ? 2 : 0)); + if (addQuotation) { + buf.append(quotation); + } + buf.append(s.substring(0, i)); + } + if (escape == 1) { + // hex encoding for characters below 0x20 + // that have no other escape representation + buf.append("\\x00"); + int c2 = (c >> 4) & 0x0F; + c = (char) (c & 0x0F); + buf.append((char) (c2 < 10 ? c2 + '0' : c2 - 10 + 'A')); + buf.append((char) (c < 10 ? c + '0' : c - 10 + 'A')); + } else { + buf.append('\\'); + buf.append(escape); + } + } + } + + if (buf == null) { + return addQuotation ? quotation + s + quotation : s; + } else { + if (addQuotation) { + buf.append(quotation); + } + return buf.toString(); + } + } + + private static boolean isInterpolationStart(char c) { + return c == '$' || c == '#'; + } + + /** + * Unescapes a string that was escaped to be part of an FTL string literal. The string to unescape most not include + * the two quotation marks or two apostrophe-quotes that delimit the literal. + * <p> + * \\, \", \', \n, \t, \r, \b and \f will be replaced according to + * Java rules. In additional, it knows \g, \l, \a and \{ which are + * replaced with <, >, & and { respectively. + * \x works as hexadecimal character code escape. The character + * codes are interpreted according to UCS basic plane (Unicode). + * "f\x006Fo", "f\x06Fo" and "f\x6Fo" will be "foo". + * "f\x006F123" will be "foo123" as the maximum number of digits is 4. + * <p> + * All other \X (where X is any character not mentioned above or End-of-string) + * will cause a ParseException. + * + * @param s String literal <em>without</em> the surrounding quotation marks + * @return String with all escape sequences resolved + * @throws ParseException if there string contains illegal escapes + */ + public static String unescapeStringLiteralPart(String s) throws ParseException { + + int idx = s.indexOf('\\'); + if (idx == -1) { + return s; + } + + int lidx = s.length() - 1; + int bidx = 0; + StringBuilder buf = new StringBuilder(lidx); + do { + buf.append(s.substring(bidx, idx)); + if (idx >= lidx) { + throw new ParseException("The last character of string literal is backslash", 0, 0); + } + char c = s.charAt(idx + 1); + switch (c) { + case '"': + buf.append('"'); + bidx = idx + 2; + break; + case '\'': + buf.append('\''); + bidx = idx + 2; + break; + case '\\': + buf.append('\\'); + bidx = idx + 2; + break; + case 'n': + buf.append('\n'); + bidx = idx + 2; + break; + case 'r': + buf.append('\r'); + bidx = idx + 2; + break; + case 't': + buf.append('\t'); + bidx = idx + 2; + break; + case 'f': + buf.append('\f'); + bidx = idx + 2; + break; + case 'b': + buf.append('\b'); + bidx = idx + 2; + break; + case 'g': + buf.append('>'); + bidx = idx + 2; + break; + case 'l': + buf.append('<'); + bidx = idx + 2; + break; + case 'a': + buf.append('&'); + bidx = idx + 2; + break; + case '{': + buf.append('{'); + bidx = idx + 2; + break; + case 'x': { + idx += 2; + int x = idx; + int y = 0; + int z = lidx > idx + 3 ? idx + 3 : lidx; + while (idx <= z) { + char b = s.charAt(idx); + if (b >= '0' && b <= '9') { + y <<= 4; + y += b - '0'; + } else if (b >= 'a' && b <= 'f') { + y <<= 4; + y += b - 'a' + 10; + } else if (b >= 'A' && b <= 'F') { + y <<= 4; + y += b - 'A' + 10; + } else { + break; + } + idx++; + } + if (x < idx) { + buf.append((char) y); + } else { + throw new ParseException("Invalid \\x escape in a string literal", 0, 0); + } + bidx = idx; + break; + } + default: + throw new ParseException("Invalid escape sequence (\\" + c + ") in a string literal", 0, 0); + } + idx = s.indexOf('\\', bidx); + } while (idx != -1); + buf.append(s.substring(bidx)); + + return buf.toString(); + } + + /** + * Creates a <em>quoted</em> FTL string literal from a string, using escaping where necessary. The result either + * uses regular quotation marks (UCS 0x22) or apostrophe-quotes (UCS 0x27), depending on the string content. + * (Currently, apostrophe-quotes will be chosen exactly when the string contains regular quotation character and + * doesn't contain apostrophe-quote character.) + * + * @param s The value that should be converted to an FTL string literal whose evaluated value equals to {@code s} + * @since 2.3.22 + */ + public static String toStringLiteral(String s) { + char quotation; + if (s.indexOf('"') != -1 && s.indexOf('\'') == -1) { + quotation = '\''; + } else { + quotation = '\"'; + } + return escapeStringLiteralPart(s, quotation, true); + } + + /** + * Tells if a character can occur on the beginning of an FTL identifier expression (without escaping). + * + * @since 2.3.22 + */ + public static boolean isNonEscapedIdentifierStart(final char c) { + // This code was generated on JDK 1.8.0_20 Win64 with src/main/misc/identifierChars/IdentifierCharGenerator.java + if (c < 0xAA) { // This branch was edited for speed. + if (c >= 'a' && c <= 'z' || c >= '@' && c <= 'Z') { + return true; + } else { + return c == '$' || c == '_'; + } + } else { // c >= 0xAA + if (c < 0xA7F8) { + if (c < 0x2D6F) { + if (c < 0x2128) { + if (c < 0x2090) { + if (c < 0xD8) { + if (c < 0xBA) { + return c == 0xAA || c == 0xB5; + } else { // c >= 0xBA + return c == 0xBA || c >= 0xC0 && c <= 0xD6; + } + } else { // c >= 0xD8 + if (c < 0x2071) { + return c >= 0xD8 && c <= 0xF6 || c >= 0xF8 && c <= 0x1FFF; + } else { // c >= 0x2071 + return c == 0x2071 || c == 0x207F; + } + } + } else { // c >= 0x2090 + if (c < 0x2115) { + if (c < 0x2107) { + return c >= 0x2090 && c <= 0x209C || c == 0x2102; + } else { // c >= 0x2107 + return c == 0x2107 || c >= 0x210A && c <= 0x2113; + } + } else { // c >= 0x2115 + if (c < 0x2124) { + return c == 0x2115 || c >= 0x2119 && c <= 0x211D; + } else { // c >= 0x2124 + return c == 0x2124 || c == 0x2126; + } + } + } + } else { // c >= 0x2128 + if (c < 0x2C30) { + if (c < 0x2145) { + if (c < 0x212F) { + return c == 0x2128 || c >= 0x212A && c <= 0x212D; + } else { // c >= 0x212F + return c >= 0x212F && c <= 0x2139 || c >= 0x213C && c <= 0x213F; + } + } else { // c >= 0x2145 + if (c < 0x2183) { + return c >= 0x2145 && c <= 0x2149 || c == 0x214E; + } else { // c >= 0x2183 + return c >= 0x2183 && c <= 0x2184 || c >= 0x2C00 && c <= 0x2C2E; + } + } + } else { // c >= 0x2C30 + if (c < 0x2D00) { + if (c < 0x2CEB) { + return c >= 0x2C30 && c <= 0x2C5E || c >= 0x2C60 && c <= 0x2CE4; + } else { // c >= 0x2CEB + return c >= 0x2CEB && c <= 0x2CEE || c >= 0x2CF2 && c <= 0x2CF3; + } + } else { // c >= 0x2D00 + if (c < 0x2D2D) { + return c >= 0x2D00 && c <= 0x2D25 || c == 0x2D27; + } else { // c >= 0x2D2D + return c == 0x2D2D || c >= 0x2D30 && c <= 0x2D67; + } + } + } + } + } else { // c >= 0x2D6F + if (c < 0x31F0) { + if (c < 0x2DD0) { + if (c < 0x2DB0) { + if (c < 0x2DA0) { + return c == 0x2D6F || c >= 0x2D80 && c <= 0x2D96; + } else { // c >= 0x2DA0 + return c >= 0x2DA0 && c <= 0x2DA6 || c >= 0x2DA8 && c <= 0x2DAE; + } + } else { // c >= 0x2DB0 + if (c < 0x2DC0) { + return c >= 0x2DB0 && c <= 0x2DB6 || c >= 0x2DB8 && c <= 0x2DBE; + } else { // c >= 0x2DC0 + return c >= 0x2DC0 && c <= 0x2DC6 || c >= 0x2DC8 && c <= 0x2DCE; + } + } + } else { // c >= 0x2DD0 + if (c < 0x3031) { + if (c < 0x2E2F) { + return c >= 0x2DD0 && c <= 0x2DD6 || c >= 0x2DD8 && c <= 0x2DDE; + } else { // c >= 0x2E2F + return c == 0x2E2F || c >= 0x3005 && c <= 0x3006; + } + } else { // c >= 0x3031 + if (c < 0x3040) { + return c >= 0x3031 && c <= 0x3035 || c >= 0x303B && c <= 0x303C; + } else { // c >= 0x3040 + return c >= 0x3040 && c <= 0x318F || c >= 0x31A0 && c <= 0x31BA; + } + } + } + } else { // c >= 0x31F0 + if (c < 0xA67F) { + if (c < 0xA4D0) { + if (c < 0x3400) { + return c >= 0x31F0 && c <= 0x31FF || c >= 0x3300 && c <= 0x337F; + } else { // c >= 0x3400 + return c >= 0x3400 && c <= 0x4DB5 || c >= 0x4E00 && c <= 0xA48C; + } + } else { // c >= 0xA4D0 + if (c < 0xA610) { + return c >= 0xA4D0 && c <= 0xA4FD || c >= 0xA500 && c <= 0xA60C; + } else { // c >= 0xA610 + return c >= 0xA610 && c <= 0xA62B || c >= 0xA640 && c <= 0xA66E; + } + } + } else { // c >= 0xA67F + if (c < 0xA78B) { + if (c < 0xA717) { + return c >= 0xA67F && c <= 0xA697 || c >= 0xA6A0 && c <= 0xA6E5; + } else { // c >= 0xA717 + return c >= 0xA717 && c <= 0xA71F || c >= 0xA722 && c <= 0xA788; + } + } else { // c >= 0xA78B + if (c < 0xA7A0) { + return c >= 0xA78B && c <= 0xA78E || c >= 0xA790 && c <= 0xA793; + } else { // c >= 0xA7A0 + return c >= 0xA7A0 && c <= 0xA7AA; + } + } + } + } + } + } else { // c >= 0xA7F8 + if (c < 0xAB20) { + if (c < 0xAA44) { + if (c < 0xA8FB) { + if (c < 0xA840) { + if (c < 0xA807) { + return c >= 0xA7F8 && c <= 0xA801 || c >= 0xA803 && c <= 0xA805; + } else { // c >= 0xA807 + return c >= 0xA807 && c <= 0xA80A || c >= 0xA80C && c <= 0xA822; + } + } else { // c >= 0xA840 + if (c < 0xA8D0) { + return c >= 0xA840 && c <= 0xA873 || c >= 0xA882 && c <= 0xA8B3; + } else { // c >= 0xA8D0 + return c >= 0xA8D0 && c <= 0xA8D9 || c >= 0xA8F2 && c <= 0xA8F7; + } + } + } else { // c >= 0xA8FB + if (c < 0xA984) { + if (c < 0xA930) { + return c == 0xA8FB || c >= 0xA900 && c <= 0xA925; + } else { // c >= 0xA930 + return c >= 0xA930 && c <= 0xA946 || c >= 0xA960 && c <= 0xA97C; + } + } else { // c >= 0xA984 + if (c < 0xAA00) { + return c >= 0xA984 && c <= 0xA9B2 || c >= 0xA9CF && c <= 0xA9D9; + } else { // c >= 0xAA00 + return c >= 0xAA00 && c <= 0xAA28 || c >= 0xAA40 && c <= 0xAA42; + } + } + } + } else { // c >= 0xAA44 + if (c < 0xAAC0) { + if (c < 0xAA80) { + if (c < 0xAA60) { + return c >= 0xAA44 && c <= 0xAA4B || c >= 0xAA50 && c <= 0xAA59; + } else { // c >= 0xAA60 + return c >= 0xAA60 && c <= 0xAA76 || c == 0xAA7A; + } + } else { // c >= 0xAA80 + if (c < 0xAAB5) { + return c >= 0xAA80 && c <= 0xAAAF || c == 0xAAB1; + } else { // c >= 0xAAB5 + return c >= 0xAAB5 && c <= 0xAAB6 || c >= 0xAAB9 && c <= 0xAABD; + } + } + } else { // c >= 0xAAC0 + if (c < 0xAAF2) { + if (c < 0xAADB) { + return c == 0xAAC0 || c == 0xAAC2; + } else { // c >= 0xAADB + return c >= 0xAADB && c <= 0xAADD || c >= 0xAAE0 && c <= 0xAAEA; + } + } else { // c >= 0xAAF2 + if (c < 0xAB09) { + return c >= 0xAAF2 && c <= 0xAAF4 || c >= 0xAB01 && c <= 0xAB06; + } else { // c >= 0xAB09 + return c >= 0xAB09 && c <= 0xAB0E || c >= 0xAB11 && c <= 0xAB16; + } + } + } + } + } else { // c >= 0xAB20 + if (c < 0xFB46) { + if (c < 0xFB13) { + if (c < 0xAC00) { + if (c < 0xABC0) { + return c >= 0xAB20 && c <= 0xAB26 || c >= 0xAB28 && c <= 0xAB2E; + } else { // c >= 0xABC0 + return c >= 0xABC0 && c <= 0xABE2 || c >= 0xABF0 && c <= 0xABF9; + } + } else { // c >= 0xAC00 + if (c < 0xD7CB) { + return c >= 0xAC00 && c <= 0xD7A3 || c >= 0xD7B0 && c <= 0xD7C6; + } else { // c >= 0xD7CB + return c >= 0xD7CB && c <= 0xD7FB || c >= 0xF900 && c <= 0xFB06; + } + } + } else { // c >= 0xFB13 + if (c < 0xFB38) { + if (c < 0xFB1F) { + return c >= 0xFB13 && c <= 0xFB17 || c == 0xFB1D; + } else { // c >= 0xFB1F + return c >= 0xFB1F && c <= 0xFB28 || c >= 0xFB2A && c <= 0xFB36; + } + } else { // c >= 0xFB38 + if (c < 0xFB40) { + return c >= 0xFB38 && c <= 0xFB3C || c == 0xFB3E; + } else { // c >= 0xFB40 + return c >= 0xFB40 && c <= 0xFB41 || c >= 0xFB43 && c <= 0xFB44; + } + } + } + } else { // c >= 0xFB46 + if (c < 0xFF21) { + if (c < 0xFDF0) { + if (c < 0xFD50) { + return c >= 0xFB46 && c <= 0xFBB1 || c >= 0xFBD3 && c <= 0xFD3D; + } else { // c >= 0xFD50 + return c >= 0xFD50 && c <= 0xFD8F || c >= 0xFD92 && c <= 0xFDC7; + } + } else { // c >= 0xFDF0 + if (c < 0xFE76) { + return c >= 0xFDF0 && c <= 0xFDFB || c >= 0xFE70 && c <= 0xFE74; + } else { // c >= 0xFE76 + return c >= 0xFE76 && c <= 0xFEFC || c >= 0xFF10 && c <= 0xFF19; + } + } + } else { // c >= 0xFF21 + if (c < 0xFFCA) { + if (c < 0xFF66) { + return c >= 0xFF21 && c <= 0xFF3A || c >= 0xFF41 && c <= 0xFF5A; + } else { // c >= 0xFF66 + return c >= 0xFF66 && c <= 0xFFBE || c >= 0xFFC2 && c <= 0xFFC7; + } + } else { // c >= 0xFFCA + if (c < 0xFFDA) { + return c >= 0xFFCA && c <= 0xFFCF || c >= 0xFFD2 && c <= 0xFFD7; + } else { // c >= 0xFFDA + return c >= 0xFFDA && c <= 0xFFDC; + } + } + } + } + } + } + } + } + + /** + * Tells if a character can occur in an FTL identifier expression (without escaping) as other than the first + * character. + */ + public static boolean isNonEscapedIdentifierPart(final char c) { + return isNonEscapedIdentifierStart(c) || (c >= '0' && c <= '9'); + } + + /** + * Tells if a given character, for which {@link #isNonEscapedIdentifierStart(char)} and + * {@link #isNonEscapedIdentifierPart(char)} is {@code false}, can occur in an identifier if it's preceded by a + * backslash. Currently it return {@code true} for these: {@code '-'}, {@code '.'} and {@code ':'}. + */ + public static boolean isEscapedIdentifierCharacter(final char c) { + return c == '-' || c == '.' || c == ':'; + } + + /** + * Escapes characters in the string that can only occur in FTL identifiers (variable names) escaped. + * This means adding a backslash before any character for which {@link #isEscapedIdentifierCharacter(char)} + * is {@code true}. Other characters will be left unescaped, even if they aren't valid in FTL identifiers. + * + * @param s The identifier to escape. If {@code null}, {@code null} is returned. + */ + public static String escapeIdentifier(String s) { + if (s == null) { + return null; + } + + int ln = s.length(); + + // First we find out if we need to escape, and if so, what the length of the output will be: + int firstEscIdx = -1; + int lastEscIdx = 0; + int plusOutLn = 0; + for (int i = 0; i < ln; i++) { + char c = s.charAt(i); + if (isEscapedIdentifierCharacter(c)) { + if (firstEscIdx == -1) { + firstEscIdx = i; + } + lastEscIdx = i; + plusOutLn++; + } + } + + if (firstEscIdx == -1) { + return s; // Nothing to escape + } else { + char[] esced = new char[ln + plusOutLn]; + if (firstEscIdx != 0) { + s.getChars(0, firstEscIdx, esced, 0); + } + int dst = firstEscIdx; + for (int i = firstEscIdx; i <= lastEscIdx; i++) { + char c = s.charAt(i); + if (isEscapedIdentifierCharacter(c)) { + esced[dst++] = '\\'; + } + esced[dst++] = c; + } + if (lastEscIdx != ln - 1) { + s.getChars(lastEscIdx + 1, ln, esced, dst); + } + + return String.valueOf(esced); + } + } + + /** + * Returns the type description of a value with FTL terms (not plain class name), as it should be used in + * type-related error messages and for debugging purposes. The exact format is not specified and might change over + * time, but currently it's something like {@code "string (wrapper: f.t.SimpleScalar)"} or + * {@code "sequence+hash+string (ArrayList wrapped into f.e.b.CollectionModel)"}. + * + * @param tm The value whose type we will describe. If {@code null}, then {@code "Null"} is returned (without the + * quotation marks). + * + * @since 2.3.20 + */ + public static String getFTLTypeDescription(TemplateModel tm) { + if (tm == null) { + return "Null"; + } else { + Set typeNamesAppended = new HashSet(); + + StringBuilder sb = new StringBuilder(); + + Class primaryInterface = getPrimaryTemplateModelInterface(tm); + if (primaryInterface != null) { + appendTemplateModelTypeName(sb, typeNamesAppended, primaryInterface); + } + + if (tm instanceof Macro) { + appendTypeName(sb, typeNamesAppended, ((Macro) tm).isFunction() ? "function" : "macro"); + } + + appendTemplateModelTypeName(sb, typeNamesAppended, tm.getClass()); + + String javaClassName; + Class unwrappedClass = getUnwrappedClass(tm); + if (unwrappedClass != null) { + javaClassName = _ClassUtil.getShortClassName(unwrappedClass, true); + } else { + javaClassName = null; + } + + sb.append(" ("); + String modelClassName = _ClassUtil.getShortClassName(tm.getClass(), true); + if (javaClassName == null) { + sb.append("wrapper: "); + sb.append(modelClassName); + } else { + sb.append(javaClassName); + sb.append(" wrapped into "); + sb.append(modelClassName); + } + sb.append(")"); + + return sb.toString(); + } + } + + /** + * Returns the {@link TemplateModel} interface that is the most characteristic of the object, or {@code null}. + */ + private static Class getPrimaryTemplateModelInterface(TemplateModel tm) { + if (tm instanceof BeanModel) { + if (tm instanceof CollectionModel) { + return TemplateSequenceModel.class; + } else if (tm instanceof IteratorModel || tm instanceof EnumerationModel) { + return TemplateCollectionModel.class; + } else if (tm instanceof MapModel) { + return TemplateHashModelEx.class; + } else if (tm instanceof NumberModel) { + return TemplateNumberModel.class; + } else if (tm instanceof BooleanModel) { + return TemplateBooleanModel.class; + } else if (tm instanceof DateModel) { + return TemplateDateModel.class; + } else if (tm instanceof StringModel) { + Object wrapped = ((BeanModel) tm).getWrappedObject(); + return wrapped instanceof String + ? TemplateScalarModel.class + : (tm instanceof TemplateHashModelEx ? TemplateHashModelEx.class : null); + } else { + return null; + } + } else if (tm instanceof SimpleMethodModel || tm instanceof OverloadedMethodsModel) { + return TemplateMethodModelEx.class; + } else { + return null; + } + } + + private static void appendTemplateModelTypeName(StringBuilder sb, Set typeNamesAppended, Class cl) { + int initalLength = sb.length(); + + if (TemplateNodeModelEx.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "extended node"); + } else if (TemplateNodeModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "node"); + } + + if (TemplateDirectiveModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "directive"); + } else if (TemplateTransformModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "transform"); + } + + if (TemplateSequenceModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "sequence"); + } else if (TemplateCollectionModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, + TemplateCollectionModelEx.class.isAssignableFrom(cl) ? "extended_collection" : "collection"); + } else if (TemplateModelIterator.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "iterator"); + } + + if (TemplateMethodModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "method"); + } + + if (Environment.Namespace.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "namespace"); + } else if (TemplateHashModelEx.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "extended_hash"); + } else if (TemplateHashModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "hash"); + } + + if (TemplateNumberModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "number"); + } + + if (TemplateDateModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "date_or_time_or_datetime"); + } + + if (TemplateBooleanModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "boolean"); + } + + if (TemplateScalarModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "string"); + } + + if (TemplateMarkupOutputModel.class.isAssignableFrom(cl)) { + appendTypeName(sb, typeNamesAppended, "markup_output"); + } + + if (sb.length() == initalLength) { + appendTypeName(sb, typeNamesAppended, "misc_template_model"); + } + } + + private static Class getUnwrappedClass(TemplateModel tm) { + Object unwrapped; + try { + if (tm instanceof WrapperTemplateModel) { + unwrapped = ((WrapperTemplateModel) tm).getWrappedObject(); + } else if (tm instanceof AdapterTemplateModel) { + unwrapped = ((AdapterTemplateModel) tm).getAdaptedObject(Object.class); + } else { + unwrapped = null; + } + } catch (Throwable e) { + unwrapped = null; + } + return unwrapped != null ? unwrapped.getClass() : null; + } + + private static void appendTypeName(StringBuilder sb, Set typeNamesAppended, String name) { + if (!typeNamesAppended.contains(name)) { + if (sb.length() != 0) sb.append("+"); + sb.append(name); + typeNamesAppended.add(name); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/1c4a73cf/src/main/java/org/apache/freemarker/core/util/_ClassUtil.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/freemarker/core/util/_ClassUtil.java b/src/main/java/org/apache/freemarker/core/util/_ClassUtil.java index 48ae23f..79d2688 100644 --- a/src/main/java/org/apache/freemarker/core/util/_ClassUtil.java +++ b/src/main/java/org/apache/freemarker/core/util/_ClassUtil.java @@ -160,174 +160,6 @@ public class _ClassUtil { } /** - * Returns the {@link TemplateModel} interface that is the most characteristic of the object, or {@code null}. - */ - private static Class getPrimaryTemplateModelInterface(TemplateModel tm) { - if (tm instanceof BeanModel) { - if (tm instanceof CollectionModel) { - return TemplateSequenceModel.class; - } else if (tm instanceof IteratorModel || tm instanceof EnumerationModel) { - return TemplateCollectionModel.class; - } else if (tm instanceof MapModel) { - return TemplateHashModelEx.class; - } else if (tm instanceof NumberModel) { - return TemplateNumberModel.class; - } else if (tm instanceof BooleanModel) { - return TemplateBooleanModel.class; - } else if (tm instanceof DateModel) { - return TemplateDateModel.class; - } else if (tm instanceof StringModel) { - Object wrapped = ((BeanModel) tm).getWrappedObject(); - return wrapped instanceof String - ? TemplateScalarModel.class - : (tm instanceof TemplateHashModelEx ? TemplateHashModelEx.class : null); - } else { - return null; - } - } else if (tm instanceof SimpleMethodModel || tm instanceof OverloadedMethodsModel) { - return TemplateMethodModelEx.class; - } else { - return null; - } - } - - private static void appendTemplateModelTypeName(StringBuilder sb, Set typeNamesAppended, Class cl) { - int initalLength = sb.length(); - - if (TemplateNodeModelEx.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "extended node"); - } else if (TemplateNodeModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "node"); - } - - if (TemplateDirectiveModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "directive"); - } else if (TemplateTransformModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "transform"); - } - - if (TemplateSequenceModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "sequence"); - } else if (TemplateCollectionModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, - TemplateCollectionModelEx.class.isAssignableFrom(cl) ? "extended_collection" : "collection"); - } else if (TemplateModelIterator.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "iterator"); - } - - if (TemplateMethodModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "method"); - } - - if (Environment.Namespace.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "namespace"); - } else if (TemplateHashModelEx.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "extended_hash"); - } else if (TemplateHashModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "hash"); - } - - if (TemplateNumberModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "number"); - } - - if (TemplateDateModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "date_or_time_or_datetime"); - } - - if (TemplateBooleanModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "boolean"); - } - - if (TemplateScalarModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "string"); - } - - if (TemplateMarkupOutputModel.class.isAssignableFrom(cl)) { - appendTypeName(sb, typeNamesAppended, "markup_output"); - } - - if (sb.length() == initalLength) { - appendTypeName(sb, typeNamesAppended, "misc_template_model"); - } - } - - private static Class getUnwrappedClass(TemplateModel tm) { - Object unwrapped; - try { - if (tm instanceof WrapperTemplateModel) { - unwrapped = ((WrapperTemplateModel) tm).getWrappedObject(); - } else if (tm instanceof AdapterTemplateModel) { - unwrapped = ((AdapterTemplateModel) tm).getAdaptedObject(Object.class); - } else { - unwrapped = null; - } - } catch (Throwable e) { - unwrapped = null; - } - return unwrapped != null ? unwrapped.getClass() : null; - } - - private static void appendTypeName(StringBuilder sb, Set typeNamesAppended, String name) { - if (!typeNamesAppended.contains(name)) { - if (sb.length() != 0) sb.append("+"); - sb.append(name); - typeNamesAppended.add(name); - } - } - - /** - * Returns the type description of a value with FTL terms (not plain class name), as it should be used in - * type-related error messages and for debugging purposes. The exact format is not specified and might change over - * time, but currently it's something like {@code "string (wrapper: f.t.SimpleScalar)"} or - * {@code "sequence+hash+string (ArrayList wrapped into f.e.b.CollectionModel)"}. - * - * @since 2.3.20 - */ - public static String getFTLTypeDescription(TemplateModel tm) { - if (tm == null) { - return "Null"; - } else { - Set typeNamesAppended = new HashSet(); - - StringBuilder sb = new StringBuilder(); - - Class primaryInterface = getPrimaryTemplateModelInterface(tm); - if (primaryInterface != null) { - appendTemplateModelTypeName(sb, typeNamesAppended, primaryInterface); - } - - if (tm instanceof Macro) { - appendTypeName(sb, typeNamesAppended, ((Macro) tm).isFunction() ? "function" : "macro"); - } - - appendTemplateModelTypeName(sb, typeNamesAppended, tm.getClass()); - - String javaClassName; - Class unwrappedClass = getUnwrappedClass(tm); - if (unwrappedClass != null) { - javaClassName = getShortClassName(unwrappedClass, true); - } else { - javaClassName = null; - } - - sb.append(" ("); - String modelClassName = getShortClassName(tm.getClass(), true); - if (javaClassName == null) { - sb.append("wrapper: "); - sb.append(modelClassName); - } else { - sb.append(javaClassName); - sb.append(" wrapped into "); - sb.append(modelClassName); - } - sb.append(")"); - - return sb.toString(); - } - } - - /** * Gets the wrapper class for a primitive class, like {@link Integer} for {@code int}, also returns {@link Void} * for {@code void}. *
