📄 configuration.java
字号:
/*
* Copyright (c) 2003-2006 The Visigoth Software Society. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Visigoth Software Society (http://www.visigoths.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
* project contributors may be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact visigoths@visigoths.org.
*
* 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
* nor may "FreeMarker" or "Visigoth" appear in their names
* without prior written permission of the Visigoth Software Society.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Visigoth Software Society. For more
* information on the Visigoth Software Society, please see
* http://www.visigoths.org/
*/
package freemarker.template;
import java.io.*;
import java.util.*;
import freemarker.cache.*;
import freemarker.core.*;
import freemarker.template.utility.*;
/**
* Main entry point into the FreeMarker API, this class encapsulates the
* various configuration parameters with which FreeMarker is run, as well
* as serves as a central template loading and caching point. Note that
* this class uses a default strategy for loading
* and caching templates. You can plug in a replacement
* template loading mechanism by using the {@link #setTemplateLoader(TemplateLoader)}
* method.
*
* This object is <em>not synchronized</em>. Thus, the settings must not be changed
* after you have started to access the object from multiple threads. If you use multiple
* threads, set everything directly after you have instantiated the <code>Configuration</code>
* object, and don't change the settings anymore.
*
* @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
* @author Attila Szegedi
* @version $Id: Configuration.java,v 1.122.2.5 2006/04/26 21:25:19 ddekany Exp $
*/
public class Configuration extends Configurable implements Cloneable {
public static final String DEFAULT_ENCODING_KEY = "default_encoding";
public static final String LOCALIZED_LOOKUP_KEY = "localized_lookup";
public static final String STRICT_SYNTAX_KEY = "strict_syntax";
public static final String WHITESPACE_STRIPPING_KEY = "whitespace_stripping";
public static final String CACHE_STORAGE_KEY = "cache_storage";
public static final String TEMPLATE_UPDATE_DELAY_KEY = "template_update_delay";
public static final String AUTO_IMPORT_KEY = "auto_import";
public static final String AUTO_INCLUDE_KEY = "auto_include";
public static final String TAG_SYNTAX_KEY = "tag_syntax";
public static final int AUTO_DETECT_TAG_SYNTAX = 0;
public static final int ANGLE_BRACKET_TAG_SYNTAX = 1;
public static final int SQUARE_BRACKET_TAG_SYNTAX = 2;
private static Configuration defaultConfig = new Configuration();
private static String cachedVersion;
private boolean strictSyntax = true, localizedLookup = true, whitespaceStripping = true;
private int tagSyntax = ANGLE_BRACKET_TAG_SYNTAX;
private TemplateCache cache;
private HashMap variables = new HashMap();
private HashMap encodingMap = new HashMap();
private Map autoImportMap = new HashMap();
private ArrayList autoImports = new ArrayList(), autoIncludes = new ArrayList();
private String defaultEncoding = SecurityUtilities.getSystemProperty("file.encoding");
public Configuration() {
cache = new TemplateCache();
cache.setConfiguration(this);
cache.setDelay(5000);
loadBuiltInSharedVariables();
}
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());
}
}
private void loadBuiltInSharedVariables() {
variables.put("capture_output", new CaptureOutput());
variables.put("compress", StandardCompress.INSTANCE);
variables.put("html_escape", new HtmlEscape());
variables.put("normalize_newlines", new NormalizeNewlines());
variables.put("xml_escape", new XmlEscape());
}
/**
* 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:
* <table>
* <tr><td>ar</td><td>ISO-8859-6</td></tr>
* <tr><td>be</td><td>ISO-8859-5</td></tr>
* <tr><td>bg</td><td>ISO-8859-5</td></tr>
* <tr><td>ca</td><td>ISO-8859-1</td></tr>
* <tr><td>cs</td><td>ISO-8859-2</td></tr>
* <tr><td>da</td><td>ISO-8859-1</td></tr>
* <tr><td>de</td><td>ISO-8859-1</td></tr>
* <tr><td>el</td><td>ISO-8859-7</td></tr>
* <tr><td>en</td><td>ISO-8859-1</td></tr>
* <tr><td>es</td><td>ISO-8859-1</td></tr>
* <tr><td>et</td><td>ISO-8859-1</td></tr>
* <tr><td>fi</td><td>ISO-8859-1</td></tr>
* <tr><td>fr</td><td>ISO-8859-1</td></tr>
* <tr><td>hr</td><td>ISO-8859-2</td></tr>
* <tr><td>hu</td><td>ISO-8859-2</td></tr>
* <tr><td>is</td><td>ISO-8859-1</td></tr>
* <tr><td>it</td><td>ISO-8859-1</td></tr>
* <tr><td>iw</td><td>ISO-8859-8</td></tr>
* <tr><td>ja</td><td>Shift_JIS</td></tr>
* <tr><td>ko</td><td>EUC-KR</td></tr>
* <tr><td>lt</td><td>ISO-8859-2</td></tr>
* <tr><td>lv</td><td>ISO-8859-2</td></tr>
* <tr><td>mk</td><td>ISO-8859-5</td></tr>
* <tr><td>nl</td><td>ISO-8859-1</td></tr>
* <tr><td>no</td><td>ISO-8859-1</td></tr>
* <tr><td>pl</td><td>ISO-8859-2</td></tr>
* <tr><td>pt</td><td>ISO-8859-1</td></tr>
* <tr><td>ro</td><td>ISO-8859-2</td></tr>
* <tr><td>ru</td><td>ISO-8859-5</td></tr>
* <tr><td>sh</td><td>ISO-8859-5</td></tr>
* <tr><td>sk</td><td>ISO-8859-2</td></tr>
* <tr><td>sl</td><td>ISO-8859-2</td></tr>
* <tr><td>sq</td><td>ISO-8859-2</td></tr>
* <tr><td>sr</td><td>ISO-8859-5</td></tr>
* <tr><td>sv</td><td>ISO-8859-1</td></tr>
* <tr><td>tr</td><td>ISO-8859-9</td></tr>
* <tr><td>uk</td><td>ISO-8859-5</td></tr>
* <tr><td>zh</td><td>GB2312</td></tr>
* <tr><td>zh_TW</td><td>Big5</td></tr>
* </table>
* @see #clearEncodingMap
* @see #setEncoding
*/
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");
}
/**
* Clears language-to-encoding map.
* @see #loadBuiltInEncodingMap
* @see #setEncoding
*/
public void clearEncodingMap() {
encodingMap.clear();
}
/**
* 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.
*
* @deprecated The usage of the static singleton (the "default")
* {@link 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 {@link Configuration} instance. Each such component
* should use its own private {@link Configuration} object instead, that it
* typically creates with <code>new Configuration()</code> when the component
* is initialized.
*/
static public Configuration getDefaultConfiguration() {
return defaultConfig;
}
/**
* Sets the Configuration object that will be retrieved from future calls
* to {@link #getDefaultConfiguration()}.
*
* @deprecated Using the "default" {@link Configuration} instance can
* easily lead to erroneous, unpredictable behaviour.
* See more {@link Configuration#getDefaultConfiguration() here...}.
*/
static public void setDefaultConfiguration(Configuration config) {
defaultConfig = config;
}
/**
* Sets a template loader that is used to look up and load templates.
* By providing your own template loader, you can customize the way
* templates are loaded. Several convenience methods in this class already
* allow you to install commonly used loaders:
* {@link #setClassForTemplateLoading(Class, String)},
* {@link #setDirectoryForTemplateLoading(File)}, and
* {@link #setServletContextForTemplateLoading(Object, String)}. By default,
* a multi-loader is used that first tries to load a template from the file
* in the current directory, then from a resource on the classpath.
*/
public synchronized void setTemplateLoader(TemplateLoader loader) {
createTemplateCache(loader, cache.getCacheStorage());
}
private void createTemplateCache(TemplateLoader loader, CacheStorage storage)
{
TemplateCache oldCache = cache;
cache = new TemplateCache(loader, storage);
cache.setDelay(oldCache.getDelay());
cache.setConfiguration(this);
cache.setLocalizedLookup(localizedLookup);
}
/**
* @return the template loader that is used to look up and load templates.
* @see #setTemplateLoader
*/
public TemplateLoader getTemplateLoader()
{
return cache.getTemplateLoader();
}
public synchronized void setCacheStorage(CacheStorage storage) {
createTemplateCache(cache.getTemplateLoader(), storage);
}
/**
* Set the explicit directory from which to load templates.
*/
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));
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -