📄 configuration.java
字号:
* Sets the servlet context from which to load templates
* @param sctxt the ServletContext object. Note that the type is <code>Object</code>
* to prevent class loading errors when user who uses FreeMarker not for
* servlets has no javax.servlet in the CLASSPATH.
* @param path the path relative to the ServletContext.
* If this path is absolute, it is taken to be relative
* to the server's URL, i.e. http://myserver.com/
* and if the path is relative, it is taken to be
* relative to the web app context, i.e.
* http://myserver.context.com/mywebappcontext/
*/
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 a class relative to which we do the
* Class.getResource() call to load templates.
*/
public void setClassForTemplateLoading(Class clazz, String pathPrefix) {
setTemplateLoader(new ClassTemplateLoader(clazz, pathPrefix));
}
/**
* 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 setTemplateUpdateDelay(int delay) {
cache.setDelay(1000L * delay);
}
/**
* 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 setStrictSyntaxMode(boolean b) {
strictSyntax = b;
}
/**
* Tells whether directives such as if, else, etcetera
* must be written as #if, #else, etcetera.
*
* @see #setStrictSyntaxMode
*/
public boolean getStrictSyntaxMode() {
return strictSyntax;
}
/**
* Sets whether the FTL parser will try to remove
* superfluous white-space around certain FTL tags.
*/
public void setWhitespaceStripping(boolean b) {
whitespaceStripping = b;
}
/**
* Gets whether the FTL parser will try to remove
* superfluous white-space around certain FTL tags.
*
* @see #setWhitespaceStripping
*/
public boolean getWhitespaceStripping() {
return whitespaceStripping;
}
/**
* Determines the syntax of the template files (angle bracket VS square bracket)
* that has no <markup>ftl</markup> directive in it. The <code>tagSyntax</code>
* parameter must be one of:
* <ul>
* <li>{@link Configuration#AUTO_DETECT_TAG_SYNTAX}:
* use the syntax of the first FreeMarker tag (can be anything, like <tt>list</tt>,
* <tt>include</tt>, user defined, ...etc)
* <li>{@link Configuration#ANGLE_BRACKET_TAG_SYNTAX}:
* use the angle bracket syntax (the normal syntax)
* <li>{@link Configuration#SQUARE_BRACKET_TAG_SYNTAX}:
* use the square bracket syntax
* </ul>
*
* <p>In FreeMarker 2.3.x {@link Configuration#ANGLE_BRACKET_TAG_SYNTAX} is the
* default for better backward compatibility. Starting from 2.4.x {@link
* Configuration#AUTO_DETECT_TAG_SYNTAX} is the default, so it is recommended to use
* that even for 2.3.x.
*
* <p>This setting is ignored for the templates that have <tt>ftl</tt> directive in
* it. For those templates the syntax used for the <tt>ftl</tt> directive determines
* the syntax.
*/
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;
}
/**
* @return whether the alternative square bracket
* syntax is set as the default
*/
public int getTagSyntax() {
return tagSyntax;
}
/**
* Equivalent to <tt>getTemplate(name, thisCfg.getLocale(), thisCfg.getEncoding(thisCfg.getLocale()), true)</tt>.
*/
public Template getTemplate(String name) throws IOException {
Locale loc = getLocale();
return getTemplate(name, loc, getEncoding(loc), true);
}
/**
* Equivalent to <tt>getTemplate(name, locale, thisCfg.getEncoding(locale), true)</tt>.
*/
public Template getTemplate(String name, Locale locale) throws IOException {
return getTemplate(name, locale, getEncoding(locale), true);
}
/**
* Equivalent to <tt>getTemplate(name, thisCfg.getLocale(), encoding, true)</tt>.
*/
public Template getTemplate(String name, String encoding) throws IOException {
return getTemplate(name, getLocale(), encoding, true);
}
/**
* Equivalent to <tt>getTemplate(name, locale, encoding, true)</tt>.
*/
public Template getTemplate(String name, Locale locale, String encoding) throws IOException {
return getTemplate(name, locale, encoding, true);
}
/**
* Retrieves a template specified by a name and locale, interpreted using
* the specified character encoding, either parsed or unparsed. For the
* exact semantics of parameters, see
* {@link TemplateCache#getTemplate(String, Locale, String, boolean)}.
* @return the requested template.
* @throws FileNotFoundException if the template could not be found.
* @throws IOException if there was a problem loading the template.
* @throws ParseException (extends <code>IOException</code>) if the template is syntactically bad.
*/
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;
}
/**
* 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 setDefaultEncoding(String encoding) {
defaultEncoding = encoding;
}
/**
* 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 getDefaultEncoding() {
return defaultEncoding;
}
/**
* Gets the preferred character encoding for the given locale, or the
* default encoding if no encoding is set explicitly for the specified
* locale. You can associate encodings with locales using
* {@link #setEncoding(Locale, String)} or {@link #loadBuiltInEncodingMap()}.
* @param loc the locale
* @return the preferred character encoding for the locale.
*/
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;
}
/**
* 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 {@link #setDefaultEncoding}.
*
* @see #clearEncodingMap
* @see #loadBuiltInEncodingMap
*/
public void setEncoding(Locale locale, String encoding) {
encodingMap.put(locale.toString(), encoding);
}
/**
* 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.
*
* <p>Never use <tt>TemplateModel</tt> 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.
*
* @param name the name used to access the data object from your template.
* If a shared variable with this name already exists, it will replace
* that.
* @see #setSharedVariable(String,Object)
* @see #setAllSharedVariables
*/
public void setSharedVariable(String name, TemplateModel tm) {
variables.put(name, tm);
}
/**
* 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 Set getSharedVariableNames() {
return new HashSet(variables.keySet());
}
/**
* Adds shared variable to the configuration.
* It uses {@link Configurable#getObjectWrapper()} to wrap the
* <code>obj</code>.
* @see #setSharedVariable(String,TemplateModel)
* @see #setAllSharedVariables
*/
public void setSharedVariable(String name, Object obj) throws TemplateModelException {
setSharedVariable(name, getObjectWrapper().wrap(obj));
}
/**
* Adds all object in the hash as shared variable to the configuration.
*
* <p>Never use <tt>TemplateModel</tt> 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.
*
* @param hash a hash model whose objects will be copied to the
* configuration with same names as they are given in the hash.
* If a shared variable with these names already exist, it will be replaced
* with those from the map.
*
* @see #setSharedVariable(String,Object)
* @see #setSharedVariable(String,TemplateModel)
*/
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());
}
}
/**
* 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.
*
* @see #setSharedVariable(String,Object)
* @see #setSharedVariable(String,TemplateModel)
* @see #setAllSharedVariables
*/
public TemplateModel getSharedVariable(String name) {
return (TemplateModel) variables.get(name);
}
/**
* Removes all shared variables, except the predefined ones (compress, html_escape, etc.).
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -