| Method from freemarker.template.Configuration Detail: |
public synchronized void addAutoImport(String namespace,
String template) {
autoImports.remove(namespace);
autoImports.add(namespace);
autoImportMap.put(namespace, template);
}
Add an auto-imported template.
The importing will happen at the top of any template that
is vended by this Configuration object. |
public synchronized void addAutoInclude(String templateName) {
autoIncludes.remove(templateName);
autoIncludes.add(templateName);
}
add a template to be automatically included at the top of any template that
is vended by this Configuration object. |
public void clearEncodingMap() {
encodingMap.clear();
}
Clears language-to-encoding map. |
public void clearSharedVariables() {
variables.clear();
loadBuiltInSharedVariables();
}
Removes all shared variables, except the predefined ones (compress, html_escape, etc.). |
public void clearTemplateCache() {
cache.clear();
}
Removes all entries from the template cache, thus forcing reloading of templates
on subsequent getTemplate calls.
This method is thread-safe and can be called while the engine works. |
public Object clone() {
try {
Configuration copy = (Configuration)super.clone();
copy.variables = new HashMap(variables);
copy.encodingMap = new HashMap(encodingMap);
copy.createTemplateCache(cache.getTemplateLoader(), cache.getCacheStorage());
return copy;
} catch (CloneNotSupportedException e) {
throw new RuntimeException("Clone is not supported, but it should be: " + e.getMessage());
}
}
|
void doAutoImports(Environment env) throws IOException, TemplateException {
for (int i=0; i< autoImports.size(); i++) {
String namespace = (String) autoImports.get(i);
String templateName = (String) autoImportMap.get(namespace);
env.importLib(templateName, namespace);
}
}
|
void doAutoIncludes(Environment env) throws IOException, TemplateException {
for (int i = 0; i < autoIncludes.size(); i++) {
String templateName = (String) autoIncludes.get(i);
Template template = getTemplate(templateName, env.getLocale());
env.include(template);
}
}
|
public static Configuration getDefaultConfiguration() {
return defaultConfig;
} Deprecated! The - usage of the static singleton (the "default")
Configuration instance can easily cause erroneous, unpredictable
behavior. This is because multiple independent software components may use
FreeMarker internally inside the same application, so they will interfere
because of the common Configuration instance. Each such component
should use its own private Configuration object instead, that it
typically creates with new Configuration() when the component
is initialized.
Returns the default (singleton) Configuration object. Note that you can
create as many separate configurations as you wish; this global instance
is provided for convenience, or when you have no reason to use a separate
instance. |
public String getDefaultEncoding() {
return defaultEncoding;
}
Gets the default encoding for converting bytes to characters when
reading template files in a locale for which no explicit encoding
was specified. Defaults to default system encoding. |
public String getEncoding(Locale loc) {
// Try for a full name match (may include country and variant)
String charset = (String) encodingMap.get(loc.toString());
if (charset == null) {
if (loc.getVariant().length() > 0) {
Locale l = new Locale(loc.getLanguage(), loc.getCountry());
charset = (String) encodingMap.get(l.toString());
if (charset != null) {
encodingMap.put(loc.toString(), charset);
}
}
charset = (String) encodingMap.get(loc.getLanguage());
if (charset != null) {
encodingMap.put(loc.toString(), charset);
}
}
return charset != null ? charset : defaultEncoding;
}
|
public boolean getLocalizedLookup() {
return cache.getLocalizedLookup();
}
Returns if localized template lookup is enabled or not.
This method is thread-safe and can be called while the engine works. |
public TemplateModel getSharedVariable(String name) {
return (TemplateModel) variables.get(name);
}
Gets a shared variable. Shared variables are variables that are
available to all templates. When a template is processed, and an identifier
is undefined in the data model, a shared variable object with the same identifier
is then looked up in the configuration. There are several predefined variables
that are always available through this method, see the FreeMarker manual
for a comprehensive list of them. |
public Set getSharedVariableNames() {
return new HashSet(variables.keySet());
}
Returns the set containing the names of all defined shared variables.
The method returns a new Set object on each call that is completely
disconnected from the Configuration. That is, modifying the set will have
no effect on the Configuration object. |
public boolean getStrictSyntaxMode() {
return strictSyntax;
}
Tells whether directives such as if, else, etcetera
must be written as #if, #else, etcetera. |
public int getTagSyntax() {
return tagSyntax;
}
|
public Template getTemplate(String name) throws IOException {
Locale loc = getLocale();
return getTemplate(name, loc, getEncoding(loc), true);
}
Equivalent to getTemplate(name, thisCfg.getLocale(), thisCfg.getEncoding(thisCfg.getLocale()), true). |
public Template getTemplate(String name,
Locale locale) throws IOException {
return getTemplate(name, locale, getEncoding(locale), true);
}
Equivalent to getTemplate(name, locale, thisCfg.getEncoding(locale), true). |
public Template getTemplate(String name,
String encoding) throws IOException {
return getTemplate(name, getLocale(), encoding, true);
}
Equivalent to getTemplate(name, thisCfg.getLocale(), encoding, true). |
public Template getTemplate(String name,
Locale locale,
String encoding) throws IOException {
return getTemplate(name, locale, encoding, true);
}
Equivalent to getTemplate(name, locale, encoding, true). |
public Template getTemplate(String name,
Locale locale,
String encoding,
boolean parse) throws IOException {
Template result = cache.getTemplate(name, locale, encoding, parse);
if (result == null) {
throw new FileNotFoundException("Template " + name + " not found.");
}
return result;
}
|
public TemplateLoader getTemplateLoader() {
return cache.getTemplateLoader();
}
|
public static String getVersionNumber() {
if (cachedVersion != null) {
return cachedVersion;
}
try {
Properties vp = new Properties();
InputStream ins = Configuration.class.getClassLoader()
.getResourceAsStream("freemarker/version.properties");
if (ins == null) {
throw new RuntimeException("Version file is missing.");
} else {
try {
vp.load(ins);
} finally {
ins.close();
}
String v = vp.getProperty("version");
if (v == null) {
throw new RuntimeException("Version file is corrupt: version key is missing.");
}
cachedVersion = v;
}
return cachedVersion;
} catch (IOException e) {
throw new RuntimeException("Failed to load version file: " + e);
}
}
|
public boolean getWhitespaceStripping() {
return whitespaceStripping;
}
Gets whether the FTL parser will try to remove
superfluous white-space around certain FTL tags. |
public void loadBuiltInEncodingMap() {
encodingMap.clear();
encodingMap.put("ar", "ISO-8859-6");
encodingMap.put("be", "ISO-8859-5");
encodingMap.put("bg", "ISO-8859-5");
encodingMap.put("ca", "ISO-8859-1");
encodingMap.put("cs", "ISO-8859-2");
encodingMap.put("da", "ISO-8859-1");
encodingMap.put("de", "ISO-8859-1");
encodingMap.put("el", "ISO-8859-7");
encodingMap.put("en", "ISO-8859-1");
encodingMap.put("es", "ISO-8859-1");
encodingMap.put("et", "ISO-8859-1");
encodingMap.put("fi", "ISO-8859-1");
encodingMap.put("fr", "ISO-8859-1");
encodingMap.put("hr", "ISO-8859-2");
encodingMap.put("hu", "ISO-8859-2");
encodingMap.put("is", "ISO-8859-1");
encodingMap.put("it", "ISO-8859-1");
encodingMap.put("iw", "ISO-8859-8");
encodingMap.put("ja", "Shift_JIS");
encodingMap.put("ko", "EUC-KR");
encodingMap.put("lt", "ISO-8859-2");
encodingMap.put("lv", "ISO-8859-2");
encodingMap.put("mk", "ISO-8859-5");
encodingMap.put("nl", "ISO-8859-1");
encodingMap.put("no", "ISO-8859-1");
encodingMap.put("pl", "ISO-8859-2");
encodingMap.put("pt", "ISO-8859-1");
encodingMap.put("ro", "ISO-8859-2");
encodingMap.put("ru", "ISO-8859-5");
encodingMap.put("sh", "ISO-8859-5");
encodingMap.put("sk", "ISO-8859-2");
encodingMap.put("sl", "ISO-8859-2");
encodingMap.put("sq", "ISO-8859-2");
encodingMap.put("sr", "ISO-8859-5");
encodingMap.put("sv", "ISO-8859-1");
encodingMap.put("tr", "ISO-8859-9");
encodingMap.put("uk", "ISO-8859-5");
encodingMap.put("zh", "GB2312");
encodingMap.put("zh_TW", "Big5");
}
Loads a preset language-to-encoding map. It assumes the usual character
encodings for most languages.
The previous content of the encoding map will be lost.
This default map currently contains the following mappings:
| ar | ISO-8859-6 |
| be | ISO-8859-5 |
| bg | ISO-8859-5 |
| ca | ISO-8859-1 |
| cs | ISO-8859-2 |
| da | ISO-8859-1 |
| de | ISO-8859-1 |
| el | ISO-8859-7 |
| en | ISO-8859-1 |
| es | ISO-8859-1 |
| et | ISO-8859-1 |
| fi | ISO-8859-1 |
| fr | ISO-8859-1 |
| hr | ISO-8859-2 |
| hu | ISO-8859-2 |
| is | ISO-8859-1 |
| it | ISO-8859-1 |
| iw | ISO-8859-8 |
| ja | Shift_JIS |
| ko | EUC-KR |
| lt | ISO-8859-2 |
| lv | ISO-8859-2 |
| mk | ISO-8859-5 |
| nl | ISO-8859-1 |
| no | ISO-8859-1 |
| pl | ISO-8859-2 |
| pt | ISO-8859-1 |
| ro | ISO-8859-2 |
| ru | ISO-8859-5 |
| sh | ISO-8859-5 |
| sk | ISO-8859-2 |
| sl | ISO-8859-2 |
| sq | ISO-8859-2 |
| sr | ISO-8859-5 |
| sv | ISO-8859-1 |
| tr | ISO-8859-9 |
| uk | ISO-8859-5 |
| zh | GB2312 |
| zh_TW | Big5 |
|
public synchronized void removeAutoImport(String namespace) {
autoImports.remove(namespace);
autoImportMap.remove(namespace);
}
Remove an auto-imported template |
public synchronized void removeAutoInclude(String templateName) {
autoIncludes.remove(templateName);
}
remove a template from the auto-include list. |
public void setAllSharedVariables(TemplateHashModelEx hash) throws TemplateModelException {
TemplateModelIterator keys = hash.keys().iterator();
TemplateModelIterator values = hash.values().iterator();
while(keys.hasNext())
{
setSharedVariable(((TemplateScalarModel)keys.next()).getAsString(), values.next());
}
}
Adds all object in the hash as shared variable to the configuration.
Never use TemplateModel implementation that is not thread-safe for shared variables,
if the configuration is used by multiple threads! It is the typical situation for Servlet based Web sites. |
public synchronized void setAutoImports(Map map) {
autoImports = new ArrayList(map.keySet());
if (map instanceof HashMap) {
autoImportMap = (Map) ((HashMap) map).clone();
}
else if (map instanceof SortedMap) {
autoImportMap = new TreeMap(map);
}
else {
autoImportMap = new HashMap(map);
}
}
set a map of namespace names to templates for auto-importing
a set of templates. Note that all previous auto-imports are removed. |
public synchronized void setAutoIncludes(List templateNames) {
autoIncludes.clear();
Iterator it = templateNames.iterator();
while (it.hasNext()) {
Object o = it.next();
if (!(o instanceof String)) {
throw new IllegalArgumentException("List items must be String-s.");
}
autoIncludes.add(o);
}
}
set the list of automatically included templates.
Note that all previous auto-includes are removed. |
public synchronized void setCacheStorage(CacheStorage storage) {
createTemplateCache(cache.getTemplateLoader(), storage);
}
|
public void setClassForTemplateLoading(Class clazz,
String pathPrefix) {
setTemplateLoader(new ClassTemplateLoader(clazz, pathPrefix));
}
Sets a class relative to which we do the
Class.getResource() call to load templates. |
public static void setDefaultConfiguration(Configuration config) {
defaultConfig = config;
} Deprecated! Using - the "default" Configuration instance can
easily lead to erroneous, unpredictable behaviour.
See more here... .
|
public void setDefaultEncoding(String encoding) {
defaultEncoding = encoding;
}
Sets the default encoding for converting bytes to characters when
reading template files in a locale for which no explicit encoding
was specified. Defaults to default system encoding. |
public void setDirectoryForTemplateLoading(File dir) throws IOException {
TemplateLoader tl = getTemplateLoader();
if (tl instanceof FileTemplateLoader) {
String path = ((FileTemplateLoader) tl).baseDir.getCanonicalPath();
if (path.equals(dir.getCanonicalPath()))
return;
}
setTemplateLoader(new FileTemplateLoader(dir));
}
Set the explicit directory from which to load templates. |
public void setEncoding(Locale locale,
String encoding) {
encodingMap.put(locale.toString(), encoding);
}
Sets the character set encoding to use for templates of
a given locale. If there is no explicit encoding set for some
locale, then the default encoding will be used, what you can
set with #setDefaultEncoding . |
public void setLocalizedLookup(boolean localizedLookup) {
this.localizedLookup = localizedLookup;
cache.setLocalizedLookup(localizedLookup);
}
Enables/disables localized template lookup. Enabled by default.
This method is thread-safe and can be called while the engine works. |
public void setServletContextForTemplateLoading(Object sctxt,
String path) {
try {
if (path == null) {
setTemplateLoader( (TemplateLoader)
ClassUtil.forName("freemarker.cache.WebappTemplateLoader")
.getConstructor(new Class[]{ClassUtil.forName("javax.servlet.ServletContext")})
.newInstance(new Object[]{sctxt}) );
}
else {
setTemplateLoader( (TemplateLoader)
ClassUtil.forName("freemarker.cache.WebappTemplateLoader")
.getConstructor(new Class[]{ClassUtil.forName("javax.servlet.ServletContext"), String.class})
.newInstance(new Object[]{sctxt, path}) );
}
} catch (Exception exc) {
throw new RuntimeException("Internal FreeMarker error: " + exc);
}
}
Sets the servlet context from which to load templates |
public void setSetting(String key,
String value) throws TemplateException {
if ("TemplateUpdateInterval".equalsIgnoreCase(key)) {
key = TEMPLATE_UPDATE_DELAY_KEY;
} else if ("DefaultEncoding".equalsIgnoreCase(key)) {
key = DEFAULT_ENCODING_KEY;
}
try {
if (DEFAULT_ENCODING_KEY.equals(key)) {
setDefaultEncoding(value);
} else if (LOCALIZED_LOOKUP_KEY.equals(key)) {
setLocalizedLookup(StringUtil.getYesNo(value));
} else if (STRICT_SYNTAX_KEY.equals(key)) {
setStrictSyntaxMode(StringUtil.getYesNo(value));
} else if (WHITESPACE_STRIPPING_KEY.equals(key)) {
setWhitespaceStripping(StringUtil.getYesNo(value));
} else if (CACHE_STORAGE_KEY.equals(key)) {
if (value.indexOf('.") == -1) {
int strongSize = 0;
int softSize = 0;
Map map = StringUtil.parseNameValuePairList(
value, String.valueOf(Integer.MAX_VALUE));
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry ent = (Map.Entry) it.next();
String pname = (String) ent.getKey();
int pvalue;
try {
pvalue = Integer.parseInt((String) ent.getValue());
} catch (NumberFormatException e) {
throw invalidSettingValueException(key, value);
}
if ("soft".equalsIgnoreCase(pname)) {
softSize = pvalue;
} else if ("strong".equalsIgnoreCase(pname)) {
strongSize = pvalue;
} else {
throw invalidSettingValueException(key, value);
}
}
if (softSize == 0 && strongSize == 0) {
throw invalidSettingValueException(key, value);
}
setCacheStorage(new MruCacheStorage(strongSize, softSize));
} else {
setCacheStorage((CacheStorage) ClassUtil.forName(value)
.newInstance());
}
} else if (TEMPLATE_UPDATE_DELAY_KEY.equals(key)) {
setTemplateUpdateDelay(Integer.parseInt(value));
} else if (AUTO_INCLUDE_KEY.equals(key)) {
setAutoIncludes(new SettingStringParser(value).parseAsList());
} else if (AUTO_IMPORT_KEY.equals(key)) {
setAutoImports(new SettingStringParser(value).parseAsImportList());
} else if (TAG_SYNTAX_KEY.equals(key)) {
if ("auto_detect".equals(value)) {
setTagSyntax(AUTO_DETECT_TAG_SYNTAX);
} else if ("angle_bracket".equals(value)) {
setTagSyntax(ANGLE_BRACKET_TAG_SYNTAX);
} else if ("square_bracket".equals(value)) {
setTagSyntax(SQUARE_BRACKET_TAG_SYNTAX);
} else {
throw invalidSettingValueException(key, value);
}
} else {
super.setSetting(key, value);
}
} catch(Exception e) {
throw new TemplateException(
"Failed to set setting " + key + " to value " + value,
e, getEnvironment());
}
}
Sets a setting by name and string value.
In additional to the settings understood by
the super method , it understands these:
"auto_import": Sets the list of auto-imports. Example of valid value:
/lib/form.ftl as f, /lib/widget as w, "/lib/evil name.ftl" as odd
See: #setAutoImports
"auto_include": Sets the list of auto-includes. Example of valid value:
/include/common.ftl, "/include/evil name.ftl"
See: #setAutoIncludes
"default_encoding": The name of the charset, such as "UTF-8".
See: #setDefaultEncoding
"localized_lookup":
"true", "false", "yes", "no",
"t", "f", "y", "n".
Case insensitive.
See: #setLocalizedLookup
"strict_syntax": "true", "false", etc.
See: #setStrictSyntaxMode
"whitespace_stripping": "true", "false", etc.
See: #setWhitespaceStripping
"cache_storage": If the value contains dot, then it is
interpreted as class name, and the object will be created with
its parameterless constructor. If the value does not contain dot,
then a 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
|
|---|
"strong:50, soft:500" | 50 | 500
| "strong:100, soft" | 100 | Integer.MAX_VALUE
| "strong:100" | 100 | 0
| "soft:100" | 0 | 100
| "strong" | Integer.MAX_VALUE | 0
| "soft" | 0 | Integer.MAX_VALUE
|
The value is not case sensitive. The order of soft and strong
entries is not significant.
See also: #setCacheStorage
"template_update_delay": Valid positive integer, the
update delay measured in seconds.
See: #setTemplateUpdateDelay
"tag_syntax": Must be one of:
"auto_detect", "angle_bracket",
"square_bracket".
|
public void setSharedVariable(String name,
TemplateModel tm) {
variables.put(name, tm);
}
Adds a shared variable to the configuration.
Shared variables are variables that are visible
as top-level variables for all templates which use this
configuration, if the data model does not contain a
variable with the same name.
Never use TemplateModel implementation that is not thread-safe for shared variables,
if the configuration is used by multiple threads! It is the typical situation for Servlet based Web sites. |
public void setSharedVariable(String name,
Object obj) throws TemplateModelException {
setSharedVariable(name, getObjectWrapper().wrap(obj));
}
|
public void setStrictSyntaxMode(boolean b) {
strictSyntax = b;
}
Sets whether directives such as if, else, etcetera
must be written as #if, #else, etcetera.
Any tag not starting with <# or </# is considered as plain text
and will go to the output as is. Tag starting with <# or </# must
be valid FTL tag, or else the template is invalid (i.e. <#noSuchDirective>
is an error). |
public void setTagSyntax(int tagSyntax) {
if (tagSyntax != AUTO_DETECT_TAG_SYNTAX
&& tagSyntax != SQUARE_BRACKET_TAG_SYNTAX
&& tagSyntax != ANGLE_BRACKET_TAG_SYNTAX)
{
throw new IllegalArgumentException("This can only be set to one of three settings: Configuration.AUTO_DETECT_TAG_SYNTAX, Configuration.ANGLE_BRACKET_SYNTAX, or Configuration.SQAUARE_BRACKET_SYNTAX");
}
this.tagSyntax = tagSyntax;
}
Determines the syntax of the template files (angle bracket VS square bracket)
that has no ftl directive in it. The tagSyntax
parameter must be one of:
In FreeMarker 2.3.x Configuration#ANGLE_BRACKET_TAG_SYNTAX is the
default for better backward compatibility. Starting from 2.4.x Configuration#AUTO_DETECT_TAG_SYNTAX is the default, so it is recommended to use
that even for 2.3.x.
This setting is ignored for the templates that have ftl directive in
it. For those templates the syntax used for the ftl directive determines
the syntax. |
public synchronized void setTemplateLoader(TemplateLoader loader) {
createTemplateCache(loader, cache.getCacheStorage());
}
|
public void setTemplateUpdateDelay(int delay) {
cache.setDelay(1000L * delay);
}
Set the time in seconds that must elapse before checking whether there is a newer
version of a template file.
This method is thread-safe and can be called while the engine works. |
public void setWhitespaceStripping(boolean b) {
whitespaceStripping = b;
}
Sets whether the FTL parser will try to remove
superfluous white-space around certain FTL tags. |