📄 configuration.java
字号:
public void clearSharedVariables() {
variables.clear();
loadBuiltInSharedVariables();
}
/**
* Removes all entries from the template cache, thus forcing reloading of templates
* on subsequent <code>getTemplate</code> calls.
* This method is thread-safe and can be called while the engine works.
*/
public void clearTemplateCache() {
cache.clear();
}
/**
* Returns if localized template lookup is enabled or not.
* This method is thread-safe and can be called while the engine works.
*/
public boolean getLocalizedLookup() {
return cache.getLocalizedLookup();
}
/**
* Enables/disables localized template lookup. Enabled by default.
* This method is thread-safe and can be called while the engine works.
*/
public void setLocalizedLookup(boolean localizedLookup) {
this.localizedLookup = localizedLookup;
cache.setLocalizedLookup(localizedLookup);
}
/**
* Sets a setting by name and string value.
*
* In additional to the settings understood by
* {@link Configurable#setSetting the super method}, it understands these:
* <ul>
* <li><code>"auto_import"</code>: Sets the list of auto-imports. Example of valid value:
* <br><code>/lib/form.ftl as f, /lib/widget as w, "/lib/evil name.ftl" as odd</code>
* See: {@link #setAutoImports}
* <li><code>"auto_include"</code>: Sets the list of auto-includes. Example of valid value:
* <br><code>/include/common.ftl, "/include/evil name.ftl"</code>
* See: {@link #setAutoIncludes}
* <li><code>"default_encoding"</code>: The name of the charset, such as <code>"UTF-8"</code>.
* See: {@link #setDefaultEncoding}
* <li><code>"localized_lookup"</code>:
* <code>"true"</code>, <code>"false"</code>, <code>"yes"</code>, <code>"no"</code>,
* <code>"t"</code>, <code>"f"</code>, <code>"y"</code>, <code>"n"</code>.
* Case insensitive.
* See: {@link #setLocalizedLookup}
* <li><code>"strict_syntax"</code>: <code>"true"</code>, <code>"false"</code>, etc.
* See: {@link #setStrictSyntaxMode}
* <li><code>"whitespace_stripping"</code>: <code>"true"</code>, <code>"false"</code>, etc.
* See: {@link #setWhitespaceStripping}
* <li><code>"cache_storage"</code>: 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 {@link freemarker.cache.MruCacheStorage} will be used with the
* maximum strong and soft sizes specified with the setting value. Examples
* of valid setting values:
* <table border=1 cellpadding=4>
* <tr><th>Setting value<th>max. strong size<th>max. soft size
* <tr><td><code>"strong:50, soft:500"</code><td>50<td>500
* <tr><td><code>"strong:100, soft"</code><td>100<td><code>Integer.MAX_VALUE</code>
* <tr><td><code>"strong:100"</code><td>100<td>0
* <tr><td><code>"soft:100"</code><td>0<td>100
* <tr><td><code>"strong"</code><td><code>Integer.MAX_VALUE</code><td>0
* <tr><td><code>"soft"</code><td>0<td><code>Integer.MAX_VALUE</code>
* </table>
* The value is not case sensitive. The order of <tt>soft</tt> and <tt>strong</tt>
* entries is not significant.
* See also: {@link #setCacheStorage}
* <li><code>"template_update_delay"</code>: Valid positive integer, the
* update delay measured in seconds.
* See: {@link #setTemplateUpdateDelay}
* <li><code>"tag_syntax"</code>: Must be one of:
* <code>"auto_detect"</code>, <code>"angle_bracket"</code>,
* <code>"square_bracket"</code>.
* </ul>
*
* @param key the name of the setting.
* @param value the string that describes the new value of the setting.
*
* @throws UnknownSettingException if the key is wrong.
* @throws TemplateException if the new value of the setting can't be set
* for any other reasons.
*/
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());
}
}
/**
* Add an auto-imported template.
* The importing will happen at the top of any template that
* is vended by this Configuration object.
* @param namespace the name of the namespace into which the template is imported
* @param template the name of the template
*/
public synchronized void addAutoImport(String namespace, String template) {
autoImports.remove(namespace);
autoImports.add(namespace);
autoImportMap.put(namespace, template);
}
/**
* Remove an auto-imported template
* @param namespace the name of the namespace into which the template was imported
*/
public synchronized void removeAutoImport(String namespace) {
autoImports.remove(namespace);
autoImportMap.remove(namespace);
}
/**
* 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 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);
}
}
void doAutoImports(Environment env) throws TemplateException, IOException {
for (int i=0; i<autoImports.size(); i++) {
String namespace = (String) autoImports.get(i);
String templateName = (String) autoImportMap.get(namespace);
env.importLib(templateName, namespace);
}
}
/**
* add a template to be automatically included at the top of any template that
* is vended by this Configuration object.
* @param templateName the lookup name of the template.
*/
public synchronized void addAutoInclude(String templateName) {
autoIncludes.remove(templateName);
autoIncludes.add(templateName);
}
/**
* set the list of automatically included templates.
* Note that all previous auto-includes 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);
}
}
/**
* remove a template from the auto-include list.
* @param templateName the lookup name of the template in question.
*/
public synchronized void removeAutoInclude(String templateName) {
autoIncludes.remove(templateName);
}
/**
* Returns FreeMarker version number string.
* Examples of possible return values:
* <code>"2.2.5"</code>, <code>"2.3pre13"</code>,
* <code>"2.3pre13mod"</code>, <code>"2.3rc1"</code>, <code>"2.3"</code>,
* <code>"3.0"</code>.
*
* <p>Notes on FreeMarker version numbering rules:
* <ul>
* <li>"pre" and "rc" (lowercase!) means "preview" and "release
* candidate" respectively. It is must be followed with a
* number (as "1" for the first release candidate).
* <li>The "mod" after the version number indicates that it's an
* unreleased modified version of the released version.
* After releases, the nighly builds are such releases. E.g.
* the nightly build after releasing "2.2.1" but before releasing
* "2.2.2" is "2.2.1mod".
* <li>The 2nd version number must be present, and maybe 0,
* as in "3.0".
* <li>The 3rd version number is never 0. E.g. the version
* number string for the first release of the 2.2 series
* is "2.2", and NOT "2.2.0".
* <li>When only the 3rd version number increases
* (2.2 -> 2.2.1, 2.2.1 -> 2.2.2 etc.), 100% backward compatiblity
* with the previous version MUST be kept.
* This means that <tt>freemarker.jar</tt> can be replaced in an
* application without risk (as far as the application doesn't depend
* on the presence of a FreeMarker bug).
* Note that backward compatibility restrictions do not apply for
* preview releases.
* </ul>
*/
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);
}
}
void doAutoIncludes(Environment env) throws TemplateException, IOException {
for (int i = 0; i < autoIncludes.size(); i++) {
String templateName = (String) autoIncludes.get(i);
Template template = getTemplate(templateName, env.getLocale());
env.include(template);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -