freemarker.core.Configurable Maven / Gradle / Ivy
Show all versions of freemarker-gae Show documentation
/*
* Copyright 2014 Attila Szegedi, Daniel Dekany, Jonathan Revusky
*
* Licensed 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 freemarker.core;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import freemarker.cache.TemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.DefaultObjectWrapperBuilder;
import freemarker.template.ObjectWrapper;
import freemarker.template.SimpleObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.TemplateModel;
import freemarker.template.Version;
import freemarker.template._TemplateAPI;
import freemarker.template.utility.NullArgumentException;
import freemarker.template.utility.StringUtil;
/**
* This is a common superclass of {@link freemarker.template.Configuration},
* {@link freemarker.template.Template}, and {@link Environment} classes.
* It provides settings that are common to each of them. FreeMarker
* uses a three-level setting hierarchy - the return value of every setting
* getter method on Configurable
objects inherits its value from its parent
* Configurable
object, unless explicitly overridden by a call to a
* corresponding setter method on the object itself. The parent of an
* Environment
object is a Template
object, the
* parent of a Template
object is a Configuration
* object.
*/
public class Configurable
{
static final String C_TRUE_FALSE = "true,false";
private static final String DEFAULT = "default";
private static final String JVM_DEFAULT = "JVM default";
public static final String LOCALE_KEY = "locale";
public static final String NUMBER_FORMAT_KEY = "number_format";
public static final String TIME_FORMAT_KEY = "time_format";
public static final String DATE_FORMAT_KEY = "date_format";
public static final String DATETIME_FORMAT_KEY = "datetime_format";
public static final String TIME_ZONE_KEY = "time_zone";
public static final String SQL_DATE_AND_TIME_TIME_ZONE_KEY
= "sql_date_and_time_time_zone";
public static final String CLASSIC_COMPATIBLE_KEY = "classic_compatible";
public static final String TEMPLATE_EXCEPTION_HANDLER_KEY = "template_exception_handler";
public static final String ARITHMETIC_ENGINE_KEY = "arithmetic_engine";
public static final String OBJECT_WRAPPER_KEY = "object_wrapper";
public static final String BOOLEAN_FORMAT_KEY = "boolean_format";
public static final String OUTPUT_ENCODING_KEY = "output_encoding";
public static final String URL_ESCAPING_CHARSET_KEY = "url_escaping_charset";
public static final String STRICT_BEAN_MODELS = "strict_bean_models";
/** @since 2.3.17 */
public static final String AUTO_FLUSH_KEY = "auto_flush";
/** @since 2.3.17 */
public static final String NEW_BUILTIN_CLASS_RESOLVER_KEY = "new_builtin_class_resolver";
/** @since 2.3.21 */
public static final String SHOW_ERROR_TIPS_KEY = "show_error_tips";
private Configurable parent;
private Properties properties;
private HashMap customAttributes;
private Locale locale;
private String numberFormat;
private String timeFormat;
private String dateFormat;
private String dateTimeFormat;
private TimeZone timeZone;
private TimeZone sqlDataAndTimeTimeZone;
private boolean sqlDataAndTimeTimeZoneSet;
private String booleanFormat;
private String trueStringValue; // deduced from booleanFormat
private String falseStringValue; // deduced from booleanFormat
private Integer classicCompatible;
private TemplateExceptionHandler templateExceptionHandler;
private ArithmeticEngine arithmeticEngine;
private ObjectWrapper objectWrapper;
private String outputEncoding;
private boolean outputEncodingSet;
private String urlEscapingCharset;
private boolean urlEscapingCharsetSet;
private Boolean autoFlush;
private TemplateClassResolver newBuiltinClassResolver;
private Boolean showErrorTips;
/**
* Creates a top-level configurable, one that doesn't inherit from a parent, and thus stores the default values.
*
* @deprecated This shouldn't even be public; don't use it.
*/
public Configurable() {
this(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
}
/**
* Intended to be called from inside FreeMarker only.
* Creates a top-level configurable, one that doesn't inherit from a parent, and thus stores the default values.
* Called by the {@link Configuration} constructor.
*/
protected Configurable(Version incompatibleImprovements) {
_TemplateAPI.checkVersionNotNullAndSupported(incompatibleImprovements);
parent = null;
locale = Locale.getDefault();
timeZone = TimeZone.getDefault();
sqlDataAndTimeTimeZone = null;
numberFormat = "number";
timeFormat = "";
dateFormat = "";
dateTimeFormat = "";
classicCompatible = new Integer(0);
templateExceptionHandler = TemplateExceptionHandler.DEBUG_HANDLER;
arithmeticEngine = ArithmeticEngine.BIGDECIMAL_ENGINE;
objectWrapper = Configuration.getDefaultObjectWrapper(incompatibleImprovements);
autoFlush = Boolean.TRUE;
newBuiltinClassResolver = TemplateClassResolver.UNRESTRICTED_RESOLVER;
showErrorTips = Boolean.TRUE;
// outputEncoding and urlEscapingCharset defaults to null,
// which means "not specified"
properties = new Properties();
properties.setProperty(LOCALE_KEY, locale.toString());
properties.setProperty(TIME_FORMAT_KEY, timeFormat);
properties.setProperty(DATE_FORMAT_KEY, dateFormat);
properties.setProperty(DATETIME_FORMAT_KEY, dateTimeFormat);
properties.setProperty(TIME_ZONE_KEY, timeZone.getID());
properties.setProperty(SQL_DATE_AND_TIME_TIME_ZONE_KEY, String.valueOf(sqlDataAndTimeTimeZone));
properties.setProperty(NUMBER_FORMAT_KEY, numberFormat);
properties.setProperty(CLASSIC_COMPATIBLE_KEY, classicCompatible.toString());
properties.setProperty(TEMPLATE_EXCEPTION_HANDLER_KEY, templateExceptionHandler.getClass().getName());
properties.setProperty(ARITHMETIC_ENGINE_KEY, arithmeticEngine.getClass().getName());
properties.setProperty(AUTO_FLUSH_KEY, autoFlush.toString());
properties.setProperty(NEW_BUILTIN_CLASS_RESOLVER_KEY, newBuiltinClassResolver.getClass().getName());
properties.setProperty(SHOW_ERROR_TIPS_KEY, showErrorTips.toString());
// as outputEncoding and urlEscapingCharset defaults to null,
// they are not set
setBooleanFormat(C_TRUE_FALSE);
customAttributes = new HashMap();
}
/**
* Creates a new instance. Normally you do not need to use this constructor,
* as you don't use Configurable
directly, but its subclasses.
*/
public Configurable(Configurable parent) {
this.parent = parent;
locale = null;
numberFormat = null;
classicCompatible = null;
templateExceptionHandler = null;
properties = new Properties(parent.properties);
customAttributes = new HashMap();
}
protected Object clone() throws CloneNotSupportedException {
Configurable copy = (Configurable)super.clone();
copy.properties = new Properties(properties);
copy.customAttributes = (HashMap)customAttributes.clone();
return copy;
}
/**
* Returns the parent Configurable object of this object.
* The parent stores the default values for this configurable. For example,
* the parent of the {@link freemarker.template.Template} object is the
* {@link freemarker.template.Configuration} object, so setting values not
* specified on template level are specified by the confuration object.
*
* @return the parent Configurable object, or null, if this is
* the root Configurable object.
*/
public final Configurable getParent() {
return parent;
}
/**
* Reparenting support. This is used by Environment when it includes a
* template - the included template becomes the parent configurable during
* its evaluation.
*/
final void setParent(Configurable parent) {
this.parent = parent;
}
/**
* Toggles the "Classic Compatible" mode. For a comprehensive description
* of this mode, see {@link #isClassicCompatible()}.
*/
public void setClassicCompatible(boolean classicCompatibility) {
this.classicCompatible = new Integer(classicCompatibility ? 1 : 0);
properties.setProperty(CLASSIC_COMPATIBLE_KEY, classicCompatibilityIntToString(classicCompatible));
}
/**
* Same as {@link #setClassicCompatible(boolean)}, but allows some extra values.
*
* @param classicCompatibility {@code 0} means {@code false}, {@code 1} means {@code true},
* {@code 2} means {@code true} but with emulating bugs in early 2.x classic-compatibility mode. Currently
* {@code 2} affects how booleans are converted to string; with {@code 1} it's always {@code "true"}/{@code ""},
* but with {@code 2} it's {@code "true"}/{@code "false"} for values wrapped by {@link BeansWrapper} as then
* {@link Boolean#toString()} prevails. Note that {@code someBoolean?string} will always consistently format the
* boolean according the {@code boolean_format} setting, just like in FreeMarker 2.3 and later.
*/
public void setClassicCompatibleAsInt(int classicCompatibility) {
if (classicCompatibility < 0 || classicCompatibility > 2) {
throw new IllegalArgumentException("Unsupported \"classicCompatibility\": " + classicCompatibility);
}
this.classicCompatible = new Integer(classicCompatibility);
}
private String classicCompatibilityIntToString(Integer i) {
if (i == null) return null;
else if (i.intValue() == 0) return MiscUtil.C_FALSE;
else if (i.intValue() == 1) return MiscUtil.C_TRUE;
else return i.toString();
}
/**
* Returns whether the engine runs in the "Classic Compatibile" mode.
* When this mode is active, the engine behavior is altered in following
* way: (these resemble the behavior of the 1.7.x line of FreeMarker engine,
* now named "FreeMarker Classic", hence the name).
*
* - handle undefined expressions gracefully. Namely when an expression
* "expr" evaluates to null:
*
* -
* in <assign varname=expr> directive,
* or in ${expr} directive,
* or in otherexpr == expr,
* or in otherexpr != expr,
* or in hash[expr],
* or in expr[keyOrIndex] (since 2.3.20),
* or in expr.key (since 2.3.20),
* then it's treated as empty string.
*
* - as argument of <list expr as item> or
* <foreach item in expr>, the loop body is not executed
* (as if it were a 0-length list)
*
* - as argument of <if> directive, or on other places where a
* boolean expression is expected, it's treated as false
*
*
*
* - Non-boolean models are accepted in <if> directive,
* or as operands of logical operators. "Empty" models (zero-length string,
* empty sequence or hash) are evaluated as false, all others are evaluated as
* true.
* - When boolean value is treated as a string (i.e. output in
* ${...} directive, or concatenated with other string), true
* values are converted to string "true", false values are converted to
* empty string. Except, if the value of the setting is 2, it will be
* formatted according the boolean_format setting, just like in
* 2.3.20 and later.
*
* - Scalar models supplied to <list> and
* <foreach> are treated as a one-element list consisting
* of the passed model.
*
* - Paths parameter of <include> will be interpreted as
* absolute path.
*
*
* In all other aspects, the engine is a 2.1 engine even in compatibility
* mode - you don't lose any of the new functionality by enabling it.
*/
public boolean isClassicCompatible() {
return classicCompatible != null ? classicCompatible.intValue() != 0 : parent.isClassicCompatible();
}
public int getClassicCompatibleAsInt() {
return classicCompatible != null ? classicCompatible.intValue() : parent.getClassicCompatibleAsInt();
}
/**
* Sets the default locale used for number and date formatting (among others), also the locale used for searching
* localized template variations when no locale was explicitly requested.
*
* @see Configuration#getTemplate(String, Locale)
*/
public void setLocale(Locale locale) {
NullArgumentException.check("locale", locale);
this.locale = locale;
properties.setProperty(LOCALE_KEY, locale.toString());
}
/**
* The getter pair of {@link #setTimeZone(TimeZone)}.
*/
public TimeZone getTimeZone() {
return timeZone != null ? timeZone : parent.getTimeZone();
}
/**
* Sets the time zone to use when formatting date/time values.
* Defaults to the system time zone ({@link TimeZone#getDefault()}), regardless of the "locale" FreeMarker setting,
* so in a server application you probably want to set it explicitly in the {@link Environment} to match the
* preferred time zone of target audience (like the Web page visitor).
*
* If you or the templates set the time zone, you should probably also set
* {@link #setSQLDateAndTimeTimeZone(TimeZone)}!
*
* @see #setSQLDateAndTimeTimeZone(TimeZone)
*/
public void setTimeZone(TimeZone timeZone) {
NullArgumentException.check("timeZone", timeZone);
this.timeZone = timeZone;
properties.setProperty(TIME_ZONE_KEY, timeZone.getID());
}
/**
* Sets the time zone used when dealing with {@link java.sql.Date java.sql.Date} and
* {@link java.sql.Time java.sql.Time} values. It defaults to {@code null} for backward compatibility, but in most
* application this should be set to the JVM default time zone (server default time zone), because that's what
* most JDBC drivers will use when constructing the {@link java.sql.Date java.sql.Date} and
* {@link java.sql.Time java.sql.Time} values. If this setting is {@code null}, FreeMarker will use the value of
* ({@link #getTimeZone()}) for {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time} values,
* which often gives bad results.
*
*
This setting doesn't influence the formatting of other kind of values (like of
* {@link java.sql.Timestamp java.sql.Timestamp} or plain {@link java.util.Date java.util.Date} values).
*
*
To decide what value you need, a few things has to be understood:
*
* - Date-only and time-only values in SQL-oriented databases are usually store calendar and clock field
* values directly (year, month, day, or hour, minute, seconds (with decimals)), as opposed to a set of points
* on the physical time line. Thus, unlike SQL timestamps, these values usually aren't meant to be shown
* differently depending on the time zone of the audience.
*
*
- When a JDBC query has to return a date-only or time-only value, it has to convert it to a point on the
* physical time line, because that's what {@link java.util.Date} and its subclasses store (milliseconds since
* the epoch). Obviously, this is impossible to do. So JDBC just chooses a physical time which, when rendered
* with the JVM default time zone, will give the same field values as those stored
* in the database. (Actually, you can give JDBC a calendar, and so it can use other time zones too, but most
* application won't care using those overloads.) For example, assume that the system time zone is GMT+02:00.
* Then, 2014-07-12 in the database will be translated to physical time 2014-07-11 22:00:00 UTC, because that
* rendered in GMT+02:00 gives 2014-07-12 00:00:00. Similarly, 11:57:00 in the database will be translated to
* physical time 1970-01-01 09:57:00 UTC. Thus, the physical time stored in the returned value depends on the
* default system time zone of the JDBC client, not just on the content in the database. (This used to be the
* default behavior of ORM-s, like Hibernate, too.)
*
*
- The value of the {@code time_zone} FreeMarker configuration setting sets the time zone used for the
* template output. For example, when a web page visitor has a preferred time zone, the web application framework
* may calls {@link Environment#setTimeZone(TimeZone)} with that time zone. Thus, the visitor will
* see {@link java.sql.Timestamp java.sql.Timestamp} and plain {@link java.util.Date java.util.Date} values as
* they look in his own time zone. While
* this is desirable for those types, as they meant to represent physical points on the time line, this is not
* necessarily desirable for date-only and time-only values. When {@code sql_date_and_time_time_zone} is
* {@code null}, {@code time_zone} is used for rendering all kind of date/time/dateTime values, including
* {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time}, and then if, for example,
* {@code time_zone} is GMT+00:00, the
* values from the earlier examples will be shown as 2014-07-11 (one day off) and 09:57:00 (2 hours off). While
* those are the time zone correct renderings, those values probably was meant to shown "as is".
*
*
- You may wonder why this setting isn't simply "SQL time zone", since the time zone related behavior of JDBC
* applies to {@link java.sql.Timestamp java.sql.Timestamp} too. FreeMarker assumes that you have set up your
* application so that time stamps coming from the database go through the necessary conversion to store the
* correct distance from the epoch (1970-01-01 00:00:00 UTC), as requested by {@link java.util.Date}. In that case
* the time stamp can be safely rendered in different time zones, and thus it needs no special treatment.
*
*
* @param tz Maybe {@code null}, in which case {@link java.sql.Date java.sql.Date} and
* {@link java.sql.Time java.sql.Time} values will be formatted in the time zone returned by
* {@link #getTimeZone()}.
* (Note that since {@code null} is an allowed value for this setting, it will not cause
* {@link #getSQLDateAndTimeTimeZone()} to fall back to the parent configuration.)
*
* @see #setTimeZone(TimeZone)
*
* @since 2.3.21
*/
public void setSQLDateAndTimeTimeZone(TimeZone tz) {
sqlDataAndTimeTimeZone = tz;
sqlDataAndTimeTimeZoneSet = true;
properties.setProperty(SQL_DATE_AND_TIME_TIME_ZONE_KEY, tz != null ? tz.getID() : "null");
}
/**
* The getter pair of {@link #setSQLDateAndTimeTimeZone(TimeZone)}.
*
* @return {@code null} if the value of {@link #getTimeZone()} should be used for formatting
* {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time} values, otherwise the time zone
* that should be used to format the values of those two types.
*
* @since 2.3.21
*/
public TimeZone getSQLDateAndTimeTimeZone() {
return sqlDataAndTimeTimeZoneSet
? sqlDataAndTimeTimeZone
: (parent != null ? parent.getSQLDateAndTimeTimeZone() : null);
}
/**
* Returns the assumed locale when searching for template files with no
* explicit requested locale. Defaults to system locale.
*/
public Locale getLocale() {
return locale != null ? locale : parent.getLocale();
}
/**
* Sets the number format used to convert numbers to strings.
*/
public void setNumberFormat(String numberFormat) {
NullArgumentException.check("numberFormat", numberFormat);
this.numberFormat = numberFormat;
properties.setProperty(NUMBER_FORMAT_KEY, numberFormat);
}
/**
* Returns the default number format used to convert numbers to strings.
* Defaults to "number"
*/
public String getNumberFormat() {
return numberFormat != null ? numberFormat : parent.getNumberFormat();
}
/**
* The string value for the boolean {@code true} and {@code false} values, intended for human audience (not for a
* computer language), separated with comma. For example, {@code "yes,no"}. Note that white-space is significant,
* so {@code "yes, no"} is WRONG (unless you want that leading space before "no").
*
* For backward compatibility the default is {@code "true,false"}, but using that value is denied for automatic
* boolean-to-string conversion (like ${myBoolean}
will fail with it), only {@code myBool?string} will
* allow it, which is deprecated since FreeMarker 2.3.20.
*
*
Note that automatic boolean-to-string conversion only exists since FreeMarker 2.3.20. Earlier this setting
* only influenced the result of {@code myBool?string}.
*/
public void setBooleanFormat(String booleanFormat) {
NullArgumentException.check("booleanFormat", booleanFormat);
int commaIdx = booleanFormat.indexOf(',');
if(commaIdx == -1) {
throw new IllegalArgumentException(
"Setting value must be string that contains two comma-separated values for true and false, " +
"respectively.");
}
this.booleanFormat = booleanFormat;
properties.setProperty(BOOLEAN_FORMAT_KEY, booleanFormat);
if (booleanFormat.equals(C_TRUE_FALSE)) {
// C_TRUE_FALSE is the default for BC, but it's not a good default for human audience formatting, so we
// pretend that it wasn't set.
trueStringValue = null;
falseStringValue = null;
} else {
trueStringValue = booleanFormat.substring(0, commaIdx);
falseStringValue = booleanFormat.substring(commaIdx + 1);
}
}
/**
* The getter pair of {@link #setBooleanFormat(String)}.
*/
public String getBooleanFormat() {
return booleanFormat != null ? booleanFormat : parent.getBooleanFormat();
}
String formatBoolean(boolean value, boolean fallbackToTrueFalse) throws TemplateException {
if (value) {
String s = getTrueStringValue();
if (s == null) {
if (fallbackToTrueFalse) {
return MiscUtil.C_TRUE;
} else {
throw new _MiscTemplateException(getNullBooleanFormatErrorDescription());
}
} else {
return s;
}
} else {
String s = getFalseStringValue();
if (s == null) {
if (fallbackToTrueFalse) {
return MiscUtil.C_FALSE;
} else {
throw new _MiscTemplateException(getNullBooleanFormatErrorDescription());
}
} else {
return s;
}
}
}
private _ErrorDescriptionBuilder getNullBooleanFormatErrorDescription() {
return new _ErrorDescriptionBuilder(new Object[] {
"Can't convert boolean to string automatically, because the \"", BOOLEAN_FORMAT_KEY ,"\" setting was ",
new _DelayedJQuote(getBooleanFormat()),
(getBooleanFormat().equals(C_TRUE_FALSE)
? ", which is the legacy default computer-language format, and hence isn't accepted."
: ".") }).tips(new Object[] {
"If you just want \"true\"/\"false\" result as you are generting computer-language output, "
+ "use \"?c\", like ${myBool?c}.",
"You can write myBool?string('yes', 'no') and like to specify boolean formatting in place.",
new Object[] {
"If you need the same two values on most places, the programmers should set the \"",
BOOLEAN_FORMAT_KEY ,"\" setting to something like \"yes,no\"." }
});
}
/**
* Returns the string to which {@code true} is converted to for human audience, or {@code null} if automatic
* coercion to string is not allowed. The default value is {@code null}.
*
*
This value is deduced from the {@code "boolean_format"} setting.
* Confusingly, for backward compatibility (at least until 2.4) that defaults to {@code "true,false"}, yet this
* defaults to {@code null}. That's so because {@code "true,false"} is treated exceptionally, as that default is a
* historical mistake in FreeMarker, since it targets computer language output, not human writing. Thus it's
* ignored.
*
* @since 2.3.20
*/
String getTrueStringValue() {
// The first step deliberately tests booleanFormat instead of trueStringValue!
return booleanFormat != null ? trueStringValue : (parent != null ? parent.getTrueStringValue() : null);
}
/**
* Same as {@link #getTrueStringValue()} but with {@code false}.
* @since 2.3.20
*/
String getFalseStringValue() {
// The first step deliberately tests booleanFormat instead of falseStringValue!
return booleanFormat != null ? falseStringValue : (parent != null ? parent.getFalseStringValue() : null);
}
/**
* Sets the format used to convert {@link java.util.Date}-s to string-s that are time (no date part) values,
* also the format that {@code someString?time} will use to parse strings.
*
*
For the possible values see {@link #setDateTimeFormat(String)}.
*
*
Defaults to {@code ""}, which means "use the FreeMarker default", which is currently {@code "medium"}.
*/
public void setTimeFormat(String timeFormat) {
NullArgumentException.check("timeFormat", timeFormat);
this.timeFormat = timeFormat;
properties.setProperty(TIME_FORMAT_KEY, timeFormat);
}
/**
* The getter pair of {@link #setTimeFormat(String)}.
*/
public String getTimeFormat() {
return timeFormat != null ? timeFormat : parent.getTimeFormat();
}
/**
* Sets the format used to convert {@link java.util.Date}-s to string-s that are date (no time part) values,
* also the format that {@code someString?date} will use to parse strings.
*
*
For the possible values see {@link #setDateTimeFormat(String)}.
*
*
Defaults to {@code ""}, which means "use the FreeMarker default", which is currently {@code "code"}.
*/
public void setDateFormat(String dateFormat) {
NullArgumentException.check("dateFormat", dateFormat);
this.dateFormat = dateFormat;
properties.setProperty(DATE_FORMAT_KEY, dateFormat);
}
/**
* The getter pair of {@link #setDateFormat(String)}.
*/
public String getDateFormat() {
return dateFormat != null ? dateFormat : parent.getDateFormat();
}
/**
* Sets the format used to convert {@link java.util.Date}-s to string-s that are date-time (timestamp) values,
* also the format that {@code someString?datetime} will use to parse strings.
*
*
The possible setting values are (the quotation marks aren't part of the value itself):
*
*
* Patterns accepted by Java's {@link SimpleDateFormat}, for example {@code "dd.MM.yyyy HH:mm:ss"} (where
* {@code HH} means 24 hours format) or {@code "MM/dd/yyyy hh:mm:ss a"} (where {@code a} prints AM or PM, if
* the current language is English).
*
*
{@code "xs"} for XML Schema format, or {@code "iso"} for ISO 8601:2004 format.
* These formats allow various additional options, separated with space, like in
* {@code "iso m nz"} (or with {@code _}, like in {@code "iso_m_nz"}; this is useful in a case like
* {@code lastModified?string.iso_m_nz}). The options and their meanings are:
*
*
* Accuracy options:
* {@code ms} = Milliseconds, always shown with all 3 digits, even if it's all 0-s.
* Example: {@code 13:45:05.800}
* {@code s} = Seconds (fraction seconds are dropped even if non-0), like {@code 13:45:05}
* {@code m} = Minutes, like {@code 13:45}. This isn't allowed for "xs".
* {@code h} = Hours, like {@code 13}. This isn't allowed for "xs".
* Neither = Up to millisecond accuracy, but trailing millisecond 0-s are removed, also the whole
* milliseconds part if it would be 0 otherwise. Example: {@code 13:45:05.8}
*
*
Time zone offset visibility options:
* {@code fz} = "Force Zone", always show time zone offset (even for for
* {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time} values).
* But, because ISO 8601 doesn't allow for dates (means date without time of the day) to
* show the zone offset, this option will have no effect in the case of {@code "iso"} with
* dates.
* {@code nz} = "No Zone", never show time zone offset
* Neither = always show time zone offset, except for {@link java.sql.Date java.sql.Date}
* and {@link java.sql.Time java.sql.Time}, and for {@code "iso"} date values.
*
*
Time zone options:
* {@code u} = Use UTC instead of what the {@code time_zone} setting suggests. However,
* {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time} aren't affected
* by this (see {@link #setSQLDateAndTimeTimeZone(TimeZone)} to understand why)
* {@code fu} = "Force UTC", that is, use UTC instead of what the {@code time_zone} or the
* {@code sql_date_and_time_time_zone} setting suggests. This also effects
* {@link java.sql.Date java.sql.Date} and {@link java.sql.Time java.sql.Time} values
* Neither = Use the time zone suggested by the {@code time_zone} or the
* {@code sql_date_and_time_time_zone} configuration setting ({@link #setTimeZone(TimeZone)} and
* {@link #setSQLDateAndTimeTimeZone(TimeZone)}).
*
*
* The options can be specified in any order.
*
* Options from the same category are mutually exclusive, like using {@code m} and {@code s}
* together is an error.
*
*
The accuracy and time zone offset visibility options don't influence parsing, only formatting.
* For example, even if you use "iso m nz", "2012-01-01T15:30:05.125+01" will be parsed successfully and with
* milliseconds accuracy.
* The time zone options (like "u") influence what time zone is chosen only when parsing a string that doesn't
* contain time zone offset.
*
*
Parsing with {@code "iso"} understands both extend format and basic format, like
* {@code 20141225T235018}. It doesn't, however, support the parsing of all kind of ISO 8601 strings: if
* there's a date part, it must use year, month and day of the month values (not week of the year), and the
* day can't be omitted.
*
*
The output of {@code "iso"} is deliberately so that it's also a good representation of the value with
* XML Schema format, except for 0 and negative years, where it's impossible. Also note that the time zone
* offset is omitted for date values in the {@code "iso"} format, while it's preserved for the {@code "xs"}
* format.
*
*
{@code "short"}, {@code "medium"}, {@code "long"}, or {@code "full"}, which that has locale-dependent
* meaning defined by the Java platform (see in the documentation of {@link java.text.DateFormat}).
* For date-time values, you can specify the length of the date and time part independently, be separating
* them with {@code _}, like {@code "short_medium"}. ({@code "medium"} means
* {@code "medium_medium"} for date-time values.)
*
*
* Defaults to {@code ""}, which means "use the FreeMarker default", which is currently {@code "code"}.
*/
public void setDateTimeFormat(String dateTimeFormat) {
NullArgumentException.check("dateTimeFormat", dateTimeFormat);
this.dateTimeFormat = dateTimeFormat;
properties.setProperty(DATETIME_FORMAT_KEY, dateTimeFormat);
}
/**
* The getter pair of {@link #setDateTimeFormat(String)}.
*/
public String getDateTimeFormat() {
return dateTimeFormat != null ? dateTimeFormat : parent.getDateTimeFormat();
}
/**
* Sets the exception handler used to handle exceptions occurring inside templates.
* The default is {@link TemplateExceptionHandler#DEBUG_HANDLER}. The recommended values are:
*
*
* - In production systems: {@link TemplateExceptionHandler#RETHROW_HANDLER}
*
- During development of HTML templates: {@link TemplateExceptionHandler#HTML_DEBUG_HANDLER}
*
- During development of non-HTML templates: {@link TemplateExceptionHandler#DEBUG_HANDLER}
*
*
* All of these will let the exception propagate further, so that you can catch it around
* {@link Template#process(Object, Writer)} for example. The difference is in what they print on the output before
* they do that.
*
*
Note that the {@link TemplateExceptionHandler} is not meant to be used for generating HTTP error pages.
* Neither is it meant to be used to roll back the printed output. These should be solved outside template
* processing when the exception raises from {@link Template#process(Object, Writer) Template.process}.
* {@link TemplateExceptionHandler} meant to be used if you want to include special content in the template
* output, or if you want to suppress certain exceptions.
*/
public void setTemplateExceptionHandler(TemplateExceptionHandler templateExceptionHandler) {
NullArgumentException.check("templateExceptionHandler", templateExceptionHandler);
this.templateExceptionHandler = templateExceptionHandler;
properties.setProperty(TEMPLATE_EXCEPTION_HANDLER_KEY, templateExceptionHandler.getClass().getName());
}
/**
* The getter pair of {@link #setTemplateExceptionHandler(TemplateExceptionHandler)}.
*/
public TemplateExceptionHandler getTemplateExceptionHandler() {
return templateExceptionHandler != null
? templateExceptionHandler : parent.getTemplateExceptionHandler();
}
/**
* Sets the arithmetic engine used to perform arithmetic operations.
* The default is {@link ArithmeticEngine#BIGDECIMAL_ENGINE}.
*/
public void setArithmeticEngine(ArithmeticEngine arithmeticEngine) {
NullArgumentException.check("arithmeticEngine", arithmeticEngine);
this.arithmeticEngine = arithmeticEngine;
properties.setProperty(ARITHMETIC_ENGINE_KEY, arithmeticEngine.getClass().getName());
}
/**
* The getter pair of {@link #setArithmeticEngine(ArithmeticEngine)}.
*/
public ArithmeticEngine getArithmeticEngine() {
return arithmeticEngine != null
? arithmeticEngine : parent.getArithmeticEngine();
}
/**
* Sets the object wrapper used to wrap objects to {@link TemplateModel}-s.
* The default is {@link ObjectWrapper#DEFAULT_WRAPPER}.
*/
public void setObjectWrapper(ObjectWrapper objectWrapper) {
NullArgumentException.check("objectWrapper", objectWrapper);
this.objectWrapper = objectWrapper;
properties.setProperty(OBJECT_WRAPPER_KEY, objectWrapper.getClass().getName());
}
/**
* The getter pair of {@link #setObjectWrapper(ObjectWrapper)}.
*/
public ObjectWrapper getObjectWrapper() {
return objectWrapper != null
? objectWrapper : parent.getObjectWrapper();
}
/**
* Informs FreeMarker about the charset used for the output. As FreeMarker outputs character stream (not
* byte stream), it's not aware of the output charset unless the software that encloses it tells it
* with this setting. Some templates may use FreeMarker features that require this information.
* Setting this to {@code null} means that the output encoding is not known.
*
*
Defaults to {@code null} (unknown).
*/
public void setOutputEncoding(String outputEncoding) {
this.outputEncoding = outputEncoding;
// java.util.Properties doesn't allow null value!
if (outputEncoding != null) {
properties.setProperty(OUTPUT_ENCODING_KEY, outputEncoding);
} else {
properties.remove(OUTPUT_ENCODING_KEY);
}
outputEncodingSet = true;
}
public String getOutputEncoding() {
return outputEncodingSet
? outputEncoding
: (parent != null ? parent.getOutputEncoding() : null);
}
/**
* Sets the URL escaping charset. If not set ({@code null}), the output encoding
* ({@link #setOutputEncoding(String)}) will be used for URL escaping.
*
* Defaults to {@code null}.
*/
public void setURLEscapingCharset(String urlEscapingCharset) {
this.urlEscapingCharset = urlEscapingCharset;
// java.util.Properties doesn't allow null value!
if (urlEscapingCharset != null) {
properties.setProperty(URL_ESCAPING_CHARSET_KEY, urlEscapingCharset);
} else {
properties.remove(URL_ESCAPING_CHARSET_KEY);
}
urlEscapingCharsetSet = true;
}
public String getURLEscapingCharset() {
return urlEscapingCharsetSet
? urlEscapingCharset
: (parent != null ? parent.getURLEscapingCharset() : null);
}
/**
* Sets the {@link TemplateClassResolver} that is used when the
* new
built-in is called in a template. That is, when
* a template contains the "com.example.SomeClassName"?new
* expression, this object will be called to resolve the
* "com.example.SomeClassName"
string to a class. The default
* value is {@link TemplateClassResolver#UNRESTRICTED_RESOLVER} in
* FreeMarker 2.3.x, and {@link TemplateClassResolver#SAFER_RESOLVER}
* starting from FreeMarker 2.4.0. If you allow users to upload templates,
* it's important to use a custom restrictive {@link TemplateClassResolver}.
*
* @since 2.3.17
*/
public void setNewBuiltinClassResolver(TemplateClassResolver newBuiltinClassResolver) {
NullArgumentException.check("newBuiltinClassResolver", newBuiltinClassResolver);
this.newBuiltinClassResolver = newBuiltinClassResolver;
properties.setProperty(NEW_BUILTIN_CLASS_RESOLVER_KEY,
newBuiltinClassResolver.getClass().getName());
}
/**
* Retrieves the {@link TemplateClassResolver} used
* to resolve classes when "SomeClassName"?new is called in a template.
*
* @since 2.3.17
*/
public TemplateClassResolver getNewBuiltinClassResolver() {
return newBuiltinClassResolver != null
? newBuiltinClassResolver : parent.getNewBuiltinClassResolver();
}
/**
* Sets whether the output {@link Writer} is automatically flushed at
* the end of {@link Template#process(Object, Writer)} (and its
* overloads). The default is {@code true}.
*
*
Using {@code false} is needed for example when a Web page is composed
* from several boxes (like portlets, GUI panels, etc.) that aren't inserted
* with #include (or with similar directives) into a master
* FreeMarker template, rather they are all processed with a separate
* {@link Template#process(Object, Writer)} call. In a such scenario the
* automatic flushes would commit the HTTP response after each box, hence
* interfering with full-page buffering, and also possibly decreasing
* performance with too frequent and too early response buffer flushes.
*
* @since 2.3.17
*/
public void setAutoFlush(boolean autoFlush) {
this.autoFlush = Boolean.valueOf(autoFlush);
properties.setProperty(AUTO_FLUSH_KEY, String.valueOf(autoFlush));
}
/**
* See {@link #setAutoFlush(boolean)}
*
* @since 2.3.17
*/
public boolean getAutoFlush() {
return autoFlush != null
? autoFlush.booleanValue()
: (parent != null ? parent.getAutoFlush() : true);
}
/**
* Sets if tips should be shown in error messages of errors arising during template processing.
* The default is {@code true}.
*
* @since 2.3.21
*/
public void setShowErrorTips(boolean showTips) {
this.showErrorTips = Boolean.valueOf(showTips);
properties.setProperty(SHOW_ERROR_TIPS_KEY, String.valueOf(showTips));
}
/**
* See {@link #setShowErrorTips(boolean)}
*
* @since 2.3.21
*/
public boolean getShowErrorTips() {
return showErrorTips != null
? showErrorTips.booleanValue()
: (parent != null ? parent.getShowErrorTips() : true);
}
private static final String ALLOWED_CLASSES = "allowed_classes";
private static final String TRUSTED_TEMPLATES = "trusted_templates";
/**
* Sets a FreeMarker setting by a name and string value. If you can configure FreeMarker directly with Java (or
* other programming language), you should use the dedicated setter methods instead (like
* {@link #setObjectWrapper(ObjectWrapper)}. This meant to be used if you get the settings from somewhere
* as text. Regardless, below you will find an overview of the settings available no matter how you set them.
*
*
The list of settings commonly supported in all {@link Configurable} subclasses:
*
* {@code "locale"}:
* See {@link #setLocale(Locale)}.
*
String value: local codes with the usual format in Java, such as {@code "en_US"}.
*
*
{@code "classic_compatible"}:
* See {@link #setClassicCompatible(boolean)} and {@link Configurable#setClassicCompatibleAsInt(int)}.
*
String value: {@code "true"}, {@code "false"}, also since 2.3.20 {@code 0} or {@code 1} or {@code 2}.
* (Also accepts {@code "yes"}, {@code "no"}, {@code "t"}, {@code "f"}, {@code "y"}, {@code "n"}.)
* Case insensitive.
*
*
{@code "template_exception_handler"}:
* See {@link #setTemplateExceptionHandler(TemplateExceptionHandler)}.
*
String value: If the value contains dot, then it's interpreted as an object builder
* expression.
* If the value does not contain dot, then it must be one of these predefined values (case insensitive):
* {@code "rethrow"} (means {@link TemplateExceptionHandler#RETHROW_HANDLER}),
* {@code "debug"} (means {@link TemplateExceptionHandler#DEBUG_HANDLER}),
* {@code "html_debug"} (means {@link TemplateExceptionHandler#HTML_DEBUG_HANDLER}),
* {@code "ignore"} (means {@link TemplateExceptionHandler#IGNORE_HANDLER}).
*
*
{@code "arithmetic_engine"}:
* See {@link #setArithmeticEngine(ArithmeticEngine)}.
*
String value: If the value contains dot, then it's interpreted as an object builder
* expression.
* If the value does not contain dot,
* then it must be one of these special values (case insensitive):
* {@code "bigdecimal"}, {@code "conservative"}.
*
*
{@code "object_wrapper"}:
* See {@link #setObjectWrapper(ObjectWrapper)}.
*
String value: If the value contains dot, then it's interpreted as an object builder
* expression, with the addition that {@link BeansWrapper}, {@link DefaultObjectWrapper} and
* {@link SimpleObjectWrapper} can be referred without package name. For example, these strings are valid
* values: {@code "DefaultObjectWrapper(2.3.21)"},
* {@code "BeansWrapper(2.3.21, simpleMapWrapper=true)"}.
*
If the value does not contain dot, then it must be one of these special values (case insensitive):
* {@code "default"} (means {@link ObjectWrapper#DEFAULT_WRAPPER}
* or {@link DefaultObjectWrapperBuilder#build()}),
* {@code "simple"} (means {@link ObjectWrapper#SIMPLE_WRAPPER}),
* {@code "beans"} (means {@link BeansWrapper#BEANS_WRAPPER}
* or {@link BeansWrapperBuilder#build()}),
* {@code "jython"} (means {@link freemarker.ext.jython.JythonWrapper#DEFAULT_WRAPPER})
*
*
{@code "number_format"}: See {@link #setNumberFormat(String)}.
*
*
{@code "boolean_format"}: See {@link #setBooleanFormat(String)} .
*
*
{@code "date_format", "time_format", "datetime_format"}:
* See {@link #setDateFormat(String)}, {@link #setTimeFormat(String)}, {@link #setDateTimeFormat(String)}.
*
*
{@code "time_zone"}:
* See {@link #setTimeZone(TimeZone)}.
*
String value: With the format as {@link TimeZone#getTimeZone} defines it. Also, since 2.3.21
* {@code "JVM default"} can be used that will be replaced with the actual JVM default time zone when
* {@link #setSetting(String, String)} is called.
* For example {@code "GMT-8:00"} or {@code "America/Los_Angeles"}
*
If you set this setting, consider setting {@code sql_date_and_time_time_zone}
* too (see below)!
*
*
{@code sql_date_and_time_time_zone}:
* See {@link #setSQLDateAndTimeTimeZone(TimeZone)}.
* Since 2.3.21.
*
String value: With the format as {@link TimeZone#getTimeZone} defines it. Also, {@code "JVM default"}
* can be used that will be replaced with the actual JVM default time zone when
* {@link #setSetting(String, String)} is called. Also {@code "null"} can be used, which has the same effect
* as {@link #setSQLDateAndTimeTimeZone(TimeZone) setSQLDateAndTimeTimeZone(null)}.
*
*
{@code "output_encoding"}:
* See {@link #setOutputEncoding(String)}.
*
*
{@code "url_escaping_charset"}:
* See {@link #setURLEscapingCharset(String)}.
*
*
{@code "auto_flush"}:
* See {@link #setAutoFlush(boolean)}.
* Since 2.3.17.
*
String value: {@code "true"}, {@code "false"}, {@code "y"}, etc.
*
*
{@code "new_builtin_class_resolver"}:
* See {@link #setNewBuiltinClassResolver(TemplateClassResolver)}.
* Since 2.3.17.
*
String value: If the value contains dot, then it's interpreted as an object builder
* expression. Otherwise the value must be one of these (ignore the quotation marks):
*
* {@code "unrestricted"}:
* Use {@link TemplateClassResolver#UNRESTRICTED_RESOLVER}
*
{@code "safer"}:
* Use {@link TemplateClassResolver#SAFER_RESOLVER}
*
{@code "allows_nothing"}:
* Use {@link TemplateClassResolver#ALLOWS_NOTHING_RESOLVER}
*
Something that contains colon will use
* {@link OptInTemplateClassResolver} and is expected to
* store comma separated values (possibly quoted) segmented
* with {@code "allowed_classes:"} and/or
* {@code "trusted_templates:"}. Examples of valid values:
*
*
*
* Setting value
* Meaning
*
*
* {@code allowed_classes: com.example.C1, com.example.C2,
* trusted_templates: lib/*, safe.ftl}
*
* Only allow instantiating the {@code com.example.C1} and
* {@code com.example.C2} classes. But, allow templates
* within the {@code lib/} directory (like
* {@code lib/foo/bar.ftl}) and template {@code safe.ftl}
* (that does not match {@code foo/safe.ftl}, only
* exactly {@code safe.ftl}) to instantiate anything
* that {@link TemplateClassResolver#SAFER_RESOLVER} allows.
*
*
* {@code allowed_classes: com.example.C1, com.example.C2}
* Only allow instantiating the {@code com.example.C1} and
* {@code com.example.C2} classes. There are no
* trusted templates.
*
*
{@code trusted_templates: lib/*, safe.ftl}
*
* Do not allow instantiating any classes, except in
* templates inside {@code lib/} or in template
* {@code safe.ftl}.
*
*
* For more details see {@link OptInTemplateClassResolver}.
*
*
Otherwise if the value contains dot, it's interpreted as
* a full-qualified class name, and the object will be created
* with its parameterless constructor.
*
*
* {@code "show_error_tips"}:
* See {@link #setShowErrorTips(boolean)}.
* Since 2.3.21.
*
String value: {@code "true"}, {@code "false"}, {@code "y"}, etc.
*
*
*
* {@link Configuration} (a subclass of {@link Configurable}) also understands these:
*
* {@code "auto_import"}:
* See {@link Configuration#setAutoImports(Map)}
*
String value is something like:
*
{@code /lib/form.ftl as f, /lib/widget as w, "/lib/odd name.ftl" as odd}
*
*
{@code "auto_include"}: Sets the list of auto-includes.
* See {@link Configuration#setAutoIncludes(List)}
*
String value is something like:
*
{@code /include/common.ftl, "/include/evil name.ftl"}
*
*
{@code "default_encoding"}:
* See {@link Configuration#setDefaultEncoding(String)}.
*
As the default value is the system default, which can change
* from one server to another, you should always set this!
*
*
{@code "localized_lookup"}:
* See {@link Configuration#setLocalizedLookup}.
*
String value: {@code "true"}, {@code "false"} (also the equivalents: {@code "yes"}, {@code "no"},
* {@code "t"}, {@code "f"}, {@code "y"}, {@code "n"}).
* Case insensitive.
*
*
{@code "strict_syntax"}:
* See {@link Configuration#setStrictSyntaxMode}. Deprecated.
*
String value: {@code "true"}, {@code "false"}, {@code yes}, etc.
*
*
{@code "whitespace_stripping"}:
* See {@link Configuration#setWhitespaceStripping}.
*
String value: {@code "true"}, {@code "false"}, {@code yes}, etc.
*
*
{@code "cache_storage"}:
* See {@link Configuration#setCacheStorage}.
*
String value: If the value contains dot, then it's interpreted as an object builder
* expression.
* If the value does not contain dot,
* then a {@link freemarker.cache.MruCacheStorage} will be used with the
* maximum strong and soft sizes specified with the setting value. Examples
* of valid setting values:
*
*
* Setting value max. strong size max. soft size
* {@code "strong:50, soft:500"} 50 500
* {@code "strong:100, soft"} 100 {@code Integer.MAX_VALUE}
* {@code "strong:100"} 100 0
* {@code "soft:100"} 0 100
* {@code "strong"} {@code Integer.MAX_VALUE} 0
* {@code "soft"} 0 {@code Integer.MAX_VALUE}
*
*
* The value is not case sensitive. The order of soft and strong
* entries is not significant.
*
*
{@code "template_update_delay"}:
* See {@link Configuration#setTemplateUpdateDelay}.
*
String value: Valid positive integer, the update delay measured in seconds.
*
*
{@code "tag_syntax"}:
* See {@link Configuration#setTagSyntax(int)}.
*
String value: Must be one of
* {@code "auto_detect"}, {@code "angle_bracket"}, and {@code "square_bracket"}.
*
*
{@code "incompatible_improvements"}:
* See {@link Configuration#setIncompatibleImprovements(Version)}.
*
String value: version number like {@code 2.3.20}.
*
*
{@code "incompatible_enhancements"}:
* See: {@link Configuration#setIncompatibleEnhancements(String)}.
* This setting name is deprecated, use {@code "incompatible_improvements"} instead.
*
*
{@code "template_loader"}:
* See: {@link Configuration#setTemplateLoader(TemplateLoader)}.
*
String value: Interpreted as an object builder expression.
*
*
* Regarding object builder expressions (used by the setting values where it was
* indicated):
*
* Before FreeMarker 2.3.21 it had to be a fully qualified class name, and nothing else.
* Since 2.3.21, the generic syntax is:
* className(constrArg1, constrArg2, ... constrArgN,
* propName1=propValue1, propName2=propValue2, ...
* propNameN=propValueN),
* where
* className is the fully qualified class name of the instance to create (except if we have
* builder class or INSTANCE field around, but see that later),
* constrArg-s are the values of constructor arguments,
* and propName=propValue-s set JavaBean properties (like x=1 means
* setX(1)) on the created instance. You can have any number of constructor arguments and property
* setters, including 0. Constructor arguments must precede any property setters.
*
* -
* Example: com.example.MyObjectWrapper(1, 2, exposeFields=true, cacheSize=5000) is nearly
* equivalent with this Java code:
* obj = new com.example.MyObjectWrapper(1, 2); obj.setExposeFields(true); obj.setCacheSize(5000);
*
* -
*
If you have no constructor arguments and property setters, and the className class has
* a public static INSTANCE field, the value of that filed will be the value of the expression, and
* the constructor won't be called.
*
* -
*
If there exists a class named classNameBuilder, then that class will be instantiated
* instead with the given constructor arguments, and the JavaBean properties of that builder instance will be
* set. After that, the public build() method of the instance will be called, whose return value
* will be the value of the whole expression. (The builder class and the build() method is simply
* found by name, there's no special interface to implement.) Note that if you use the backward compatible
* syntax, where these's no parenthesis after the class name, then it will not look for builder class.
*
* -
*
Currently, the values of arguments and properties can only be one of these:
*
* - A numerical literal, like {@code 123} or {@code -1.5}. Like in FTL, there are no numerical types,
* the value will be automatically converted to the type of the target.
* - A boolean literal: {@code true} or {@code false}
*
- The null literal: {@code null}
*
- A string literal with FTL syntax, except that it can't contain ${...}-s and
* #{...}-s. Examples: {@code "Line 1\nLine 2"} or {@code r"C:\temp"}.
*
- An object builder expression. That is, object builder expressions can be nested into each other.
*
*
* -
*
The top-level object builder expressions may omit {@code ()}. In that case, for backward compatibility,
* the {@code INSTANCE} field and the builder class is not searched, so the instance will be always
* created with its parameterless constructor. (This behavior will possibly change in 2.4.) The {@code ()}
* can't be omitted for nested expressions.
*
* -
*
The classes and methods that the expression meant to access must be all public.
*
*
*
* @param name the name of the setting.
* @param value the string that describes the new value of the setting.
*
* @throws UnknownSettingException if the name is wrong.
* @throws TemplateException if the new value of the setting can't be set for any other reasons.
*/
public void setSetting(String name, String value) throws TemplateException {
boolean unknown = false;
try {
if (LOCALE_KEY.equals(name)) {
setLocale(StringUtil.deduceLocale(value));
} else if (NUMBER_FORMAT_KEY.equals(name)) {
setNumberFormat(value);
} else if (TIME_FORMAT_KEY.equals(name)) {
setTimeFormat(value);
} else if (DATE_FORMAT_KEY.equals(name)) {
setDateFormat(value);
} else if (DATETIME_FORMAT_KEY.equals(name)) {
setDateTimeFormat(value);
} else if (TIME_ZONE_KEY.equals(name)) {
setTimeZone(parseTimeZoneSettingValue(value));
} else if (SQL_DATE_AND_TIME_TIME_ZONE_KEY.equals(name)) {
setSQLDateAndTimeTimeZone(value.equals("null") ? null : parseTimeZoneSettingValue(value));
} else if (CLASSIC_COMPATIBLE_KEY.equals(name)) {
char firstChar;
if (value != null && value.length() > 0) {
firstChar = value.charAt(0);
} else {
firstChar = 0;
}
if (Character.isDigit(firstChar) || firstChar == '+' || firstChar == '-') {
setClassicCompatibleAsInt(Integer.parseInt(value));
} else {
setClassicCompatible(value != null ? StringUtil.getYesNo(value) : false);
}
} else if (TEMPLATE_EXCEPTION_HANDLER_KEY.equals(name)) {
if (value.indexOf('.') == -1) {
if ("debug".equalsIgnoreCase(value)) {
setTemplateExceptionHandler(
TemplateExceptionHandler.DEBUG_HANDLER);
} else if ("html_debug".equalsIgnoreCase(value)) {
setTemplateExceptionHandler(
TemplateExceptionHandler.HTML_DEBUG_HANDLER);
} else if ("ignore".equalsIgnoreCase(value)) {
setTemplateExceptionHandler(
TemplateExceptionHandler.IGNORE_HANDLER);
} else if ("rethrow".equalsIgnoreCase(value)) {
setTemplateExceptionHandler(
TemplateExceptionHandler.RETHROW_HANDLER);
} else {
throw invalidSettingValueException(name, value);
}
} else {
setTemplateExceptionHandler((TemplateExceptionHandler) _ObjectBuilderSettingEvaluator.eval(
value, TemplateExceptionHandler.class, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (ARITHMETIC_ENGINE_KEY.equals(name)) {
if (value.indexOf('.') == -1) {
if ("bigdecimal".equalsIgnoreCase(value)) {
setArithmeticEngine(ArithmeticEngine.BIGDECIMAL_ENGINE);
} else if ("conservative".equalsIgnoreCase(value)) {
setArithmeticEngine(ArithmeticEngine.CONSERVATIVE_ENGINE);
} else {
throw invalidSettingValueException(name, value);
}
} else {
setArithmeticEngine((ArithmeticEngine) _ObjectBuilderSettingEvaluator.eval(
value, ArithmeticEngine.class, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (OBJECT_WRAPPER_KEY.equals(name)) {
if (DEFAULT.equalsIgnoreCase(value)) {
setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
} else if ("simple".equalsIgnoreCase(value)) {
setObjectWrapper(ObjectWrapper.SIMPLE_WRAPPER);
} else if ("beans".equalsIgnoreCase(value)) {
setObjectWrapper(ObjectWrapper.BEANS_WRAPPER);
} else if ("jython".equalsIgnoreCase(value)) {
Class clazz = Class.forName(
"freemarker.ext.jython.JythonWrapper");
setObjectWrapper(
(ObjectWrapper) clazz.getField("INSTANCE").get(null));
} else {
setObjectWrapper((ObjectWrapper) _ObjectBuilderSettingEvaluator.eval(
value, ObjectWrapper.class, _SettingEvaluationEnvironment.getCurrent()));
}
} else if (BOOLEAN_FORMAT_KEY.equals(name)) {
setBooleanFormat(value);
} else if (OUTPUT_ENCODING_KEY.equals(name)) {
setOutputEncoding(value);
} else if (URL_ESCAPING_CHARSET_KEY.equals(name)) {
setURLEscapingCharset(value);
} else if (STRICT_BEAN_MODELS.equals(name)) {
setStrictBeanModels(StringUtil.getYesNo(value));
} else if (AUTO_FLUSH_KEY.equals(name)) {
setAutoFlush(StringUtil.getYesNo(value));
} else if (SHOW_ERROR_TIPS_KEY.equals(name)) {
setShowErrorTips(StringUtil.getYesNo(value));
} else if (NEW_BUILTIN_CLASS_RESOLVER_KEY.equals(name)) {
if ("unrestricted".equals(value)) {
setNewBuiltinClassResolver(TemplateClassResolver.UNRESTRICTED_RESOLVER);
} else if ("safer".equals(value)) {
setNewBuiltinClassResolver(TemplateClassResolver.SAFER_RESOLVER);
} else if ("allows_nothing".equals(value)) {
setNewBuiltinClassResolver(TemplateClassResolver.ALLOWS_NOTHING_RESOLVER);
} else if (value.indexOf(":") != -1) {
List segments = parseAsSegmentedList(value);
Set allowedClasses = null;
List trustedTemplates = null;
for (int i = 0; i < segments.size(); i++) {
KeyValuePair kv = (KeyValuePair) segments.get(i);
String segmentKey = (String) kv.getKey();
List segmentValue = (List) kv.getValue();
if (segmentKey.equals(ALLOWED_CLASSES)) {
allowedClasses = new HashSet(segmentValue);
} else if (segmentKey.equals(TRUSTED_TEMPLATES)) {
trustedTemplates = segmentValue;
} else {
throw new ParseException(
"Unrecognized list segment key: " + StringUtil.jQuote(segmentKey) +
". Supported keys are: \"" + ALLOWED_CLASSES + "\", \"" +
TRUSTED_TEMPLATES + "\"", 0, 0);
}
}
setNewBuiltinClassResolver(
new OptInTemplateClassResolver(allowedClasses, trustedTemplates));
} else if (value.indexOf('.') != -1) {
setNewBuiltinClassResolver((TemplateClassResolver) _ObjectBuilderSettingEvaluator.eval(
value, TemplateClassResolver.class, _SettingEvaluationEnvironment.getCurrent()));
} else {
throw invalidSettingValueException(name, value);
}
} else {
unknown = true;
}
} catch(Exception e) {
throw settingValueAssignmentException(name, value, e);
}
if (unknown) {
throw unknownSettingException(name);
}
}
private TimeZone parseTimeZoneSettingValue(String value) {
TimeZone tz;
if (JVM_DEFAULT.equalsIgnoreCase(value)) {
tz = TimeZone.getDefault();
} else {
tz = TimeZone.getTimeZone(value);
}
return tz;
}
public void setStrictBeanModels(boolean strict) {
if (!(objectWrapper instanceof BeansWrapper)) {
throw new IllegalStateException("The value of the " + OBJECT_WRAPPER_KEY +
" setting isn't a " + BeansWrapper.class.getName() + ".");
}
((BeansWrapper) objectWrapper).setStrict(strict);
}
/**
* Returns the textual representation of a setting.
* @param key the setting key. Can be any of standard XXX_KEY
* constants, or a custom key.
*
* @deprecated It's not possible in general to convert setting values to string,
* and thus it's impossible to ensure that {@link #setSetting(String, String)} will work with
* the returned value correctly.
*/
public String getSetting(String key) {
return properties.getProperty(key);
}
/**
* This meant to return the String-to-String Map
of the
* settings. So it actually should return a Properties
object,
* but it doesn't by mistake. The returned Map
is read-only,
* but it will reflect the further configuration changes (aliasing effect).
*
* @deprecated This method was always defective, and certainly it always
* will be. Don't use it. (Simply, it's hardly possible in general to
* convert setting values to text in a way that ensures that
* {@link #setSettings(Properties)} will work with them correctly.)
*/
public Map getSettings() {
return Collections.unmodifiableMap(properties);
}
protected Environment getEnvironment() {
return this instanceof Environment
? (Environment) this
: Environment.getCurrentEnvironment();
}
protected TemplateException unknownSettingException(String name) {
return new UnknownSettingException(getEnvironment(), name, getCorrectedNameForUnknownSetting(name));
}
/**
* @param name The wrong name
* @return The corrected name, or {@code null} if there's no known correction
* @since 2.3.21
*/
protected String getCorrectedNameForUnknownSetting(String name) {
return null;
}
/**
* @since 2.3.21
*/
protected TemplateException settingValueAssignmentException(String name, String value, Throwable cause) {
return new SettingValueAssignmentException(getEnvironment(), name, value, cause);
}
protected TemplateException invalidSettingValueException(String name, String value) {
return new _MiscTemplateException(getEnvironment(), new Object[] {
"Invalid value for setting ", new _DelayedJQuote(name), ": ",
new _DelayedJQuote(value) });
}
/**
* The setting name was not recognized.
*/
public static class UnknownSettingException extends _MiscTemplateException {
private UnknownSettingException(Environment env, String name, String correctedName) {
super(env, new Object[] {
"Unknown setting: ", new _DelayedJQuote(name),
correctedName == null
? (Object) "" : new Object[] { ". You may meant: ", new _DelayedJQuote(correctedName) } });
}
}
/**
* The setting name was recognized, but its value couldn't be parsed or the setting couldn't be set for some
* other reason. This exception always has a cause exception.
*
* @since 2.3.21
*/
public static class SettingValueAssignmentException extends _MiscTemplateException {
private SettingValueAssignmentException(Environment env, String name, String value, Throwable cause) {
super(cause, env, new Object[] {
"Failed to set setting ", new _DelayedJQuote(name),
" to value ", new _DelayedJQuote(value), "; see cause exception." });
}
}
/**
* Set the settings stored in a Properties
object.
*
* @throws TemplateException if the Properties
object contains
* invalid keys, or invalid setting values, or any other error occurs
* while changing the settings.
*/
public void setSettings(Properties props) throws TemplateException {
final _SettingEvaluationEnvironment prevEnv = _SettingEvaluationEnvironment.startScope();
try {
for (Iterator it = props.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
setSetting(key, props.getProperty(key).trim());
}
} finally {
_SettingEvaluationEnvironment.endScope(prevEnv);
}
}
/**
* Reads a setting list (key and element pairs) from the input stream.
* The stream has to follow the usual .properties
format.
*
* @throws TemplateException if the stream contains
* invalid keys, or invalid setting values, or any other error occurs
* while changing the settings.
* @throws IOException if an error occurred when reading from the input stream.
*/
public void setSettings(InputStream propsIn) throws TemplateException, IOException {
Properties p = new Properties();
p.load(propsIn);
setSettings(p);
}
/**
* Internal entry point for setting unnamed custom attributes
*/
void setCustomAttribute(Object key, Object value) {
synchronized(customAttributes) {
customAttributes.put(key, value);
}
}
/**
* Internal entry point for getting unnamed custom attributes
*/
Object getCustomAttribute(Object key, CustomAttribute attr) {
synchronized(customAttributes) {
Object o = customAttributes.get(key);
if(o == null && !customAttributes.containsKey(key)) {
o = attr.create();
customAttributes.put(key, o);
}
return o;
}
}
/**
* Sets a named custom attribute for this configurable.
*
* @param name the name of the custom attribute
* @param value the value of the custom attribute. You can set the value to
* null, however note that there is a semantic difference between an
* attribute set to null and an attribute that is not present, see
* {@link #removeCustomAttribute(String)}.
*/
public void setCustomAttribute(String name, Object value) {
synchronized(customAttributes) {
customAttributes.put(name, value);
}
}
/**
* Returns an array with names of all custom attributes defined directly
* on this configurable. (That is, it doesn't contain the names of custom attributes
* defined indirectly on its parent configurables.) The returned array is never null,
* but can be zero-length.
* The order of elements in the returned array is not defined and can change
* between invocations.
*/
public String[] getCustomAttributeNames() {
synchronized(customAttributes) {
Collection names = new LinkedList(customAttributes.keySet());
for (Iterator iter = names.iterator(); iter.hasNext();) {
if(!(iter.next() instanceof String)) {
iter.remove();
}
}
return (String[])names.toArray(new String[names.size()]);
}
}
/**
* Removes a named custom attribute for this configurable. Note that this
* is different than setting the custom attribute value to null. If you
* set the value to null, {@link #getCustomAttribute(String)} will return
* null, while if you remove the attribute, it will return the value of
* the attribute in the parent configurable (if there is a parent
* configurable, that is).
*
* @param name the name of the custom attribute
*/
public void removeCustomAttribute(String name) {
synchronized(customAttributes) {
customAttributes.remove(name);
}
}
/**
* Retrieves a named custom attribute for this configurable. If the
* attribute is not present in the configurable, and the configurable has
* a parent, then the parent is looked up as well.
*
* @param name the name of the custom attribute
*
* @return the value of the custom attribute. Note that if the custom attribute
* was created with <#ftl attributes={...}>, then this value is already
* unwrapped (i.e. it's a String
, or a List
, or a
* Map
, ...etc., not a FreeMarker specific class).
*/
public Object getCustomAttribute(String name) {
Object retval;
synchronized(customAttributes) {
retval = customAttributes.get(name);
if(retval == null && customAttributes.containsKey(name)) {
return null;
}
}
if(retval == null && parent != null) {
return parent.getCustomAttribute(name);
}
return retval;
}
protected void doAutoImportsAndIncludes(Environment env)
throws TemplateException, IOException
{
if(parent != null) parent.doAutoImportsAndIncludes(env);
}
protected ArrayList parseAsList(String text) throws ParseException {
return new SettingStringParser(text).parseAsList();
}
protected ArrayList parseAsSegmentedList(String text)
throws ParseException {
return new SettingStringParser(text).parseAsSegmentedList();
}
protected HashMap parseAsImportList(String text) throws ParseException {
return new SettingStringParser(text).parseAsImportList();
}
private static class KeyValuePair {
private final Object key;
private final Object value;
KeyValuePair(Object key, Object value) {
this.key = key;
this.value = value;
}
Object getKey() {
return key;
}
Object getValue() {
return value;
}
}
/**
* Helper class for parsing setting values given with string.
*/
private static class SettingStringParser {
private String text;
private int p;
private int ln;
private SettingStringParser(String text) {
this.text = text;
this.p = 0;
this.ln = text.length();
}
ArrayList parseAsSegmentedList() throws ParseException {
ArrayList segments = new ArrayList();
ArrayList currentSegment = null;
char c;
while (true) {
c = skipWS();
if (c == ' ') break;
String item = fetchStringValue();
c = skipWS();
if (c == ':') {
currentSegment = new ArrayList();
segments.add(new KeyValuePair(item, currentSegment));
} else {
if (currentSegment == null) {
throw new ParseException(
"The very first list item must be followed by \":\" so " +
"it will be the key for the following sub-list.",
0, 0);
}
currentSegment.add(item);
}
if (c == ' ') break;
if (c != ',' && c != ':') throw new ParseException(
"Expected \",\" or \":\" or the end of text but " +
"found \"" + c + "\"", 0, 0);
p++;
}
return segments;
}
ArrayList parseAsList() throws ParseException {
char c;
ArrayList seq = new ArrayList();
while (true) {
c = skipWS();
if (c == ' ') break;
seq.add(fetchStringValue());
c = skipWS();
if (c == ' ') break;
if (c != ',') throw new ParseException(
"Expected \",\" or the end of text but " +
"found \"" + c + "\"", 0, 0);
p++;
}
return seq;
}
HashMap parseAsImportList() throws ParseException {
char c;
HashMap map = new HashMap();
while (true) {
c = skipWS();
if (c == ' ') break;
String lib = fetchStringValue();
c = skipWS();
if (c == ' ') throw new ParseException(
"Unexpected end of text: expected \"as\"", 0, 0);
String s = fetchKeyword();
if (!s.equalsIgnoreCase("as")) throw new ParseException(
"Expected \"as\", but found " + StringUtil.jQuote(s), 0, 0);
c = skipWS();
if (c == ' ') throw new ParseException(
"Unexpected end of text: expected gate hash name", 0, 0);
String ns = fetchStringValue();
map.put(ns, lib);
c = skipWS();
if (c == ' ') break;
if (c != ',') throw new ParseException(
"Expected \",\" or the end of text but "
+ "found \"" + c + "\"", 0, 0);
p++;
}
return map;
}
String fetchStringValue() throws ParseException {
String w = fetchWord();
if (w.startsWith("'") || w.startsWith("\"")) {
w = w.substring(1, w.length() - 1);
}
return StringUtil.FTLStringLiteralDec(w);
}
String fetchKeyword() throws ParseException {
String w = fetchWord();
if (w.startsWith("'") || w.startsWith("\"")) {
throw new ParseException(
"Keyword expected, but a string value found: " + w, 0, 0);
}
return w;
}
char skipWS() {
char c;
while (p < ln) {
c = text.charAt(p);
if (!Character.isWhitespace(c)) return c;
p++;
}
return ' ';
}
private String fetchWord() throws ParseException {
if (p == ln) throw new ParseException(
"Unexpeced end of text", 0, 0);
char c = text.charAt(p);
int b = p;
if (c == '\'' || c == '"') {
boolean escaped = false;
char q = c;
p++;
while (p < ln) {
c = text.charAt(p);
if (!escaped) {
if (c == '\\') {
escaped = true;
} else if (c == q) {
break;
}
} else {
escaped = false;
}
p++;
}
if (p == ln) {
throw new ParseException("Missing " + q, 0, 0);
}
p++;
return text.substring(b, p);
} else {
do {
c = text.charAt(p);
if (!(Character.isLetterOrDigit(c)
|| c == '/' || c == '\\' || c == '_'
|| c == '.' || c == '-' || c == '!'
|| c == '*' || c == '?')) break;
p++;
} while (p < ln);
if (b == p) {
throw new ParseException("Unexpected character: " + c, 0, 0);
} else {
return text.substring(b, p);
}
}
}
}
}