📄 template.java
字号:
/*
* Copyright (c) 2003 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 javax.swing.tree.TreePath;
import freemarker.core.*;
import freemarker.debug.impl.DebuggerService;
/**
* <p>A core FreeMarker API that represents a compiled template.
* Typically, you will use a {@link Configuration} object to instantiate a template.
*
* <PRE>
Configuration cfg = new Configuration();
...
Template myTemplate = cfg.getTemplate("myTemplate.html");
</PRE>
*
* <P>However, you can also construct a template directly by passing in to
* the appropriate constructor a java.io.Reader instance that is set to
* read the raw template text. The compiled template is
* stored in an an efficient data structure for later use.
*
* <p>To render the template, i.e. to merge it with a data model, and
* thus produce "cooked" output, call the <tt>process</tt> method.
*
* <p>Any error messages from exceptions thrown during compilation will be
* included in the output stream and thrown back to the calling code.
* To change this behavior, you can install custom exception handlers using
* {@link Configurable#setTemplateExceptionHandler(TemplateExceptionHandler)} on
* a Configuration object (for all templates belonging to a configuration) or on
* a Template object (for a single template).
*
* <p>It's not legal to modify the values of FreeMarker settings: a) while the
* template is executing; b) if the template object is already accessible from
* multiple threads.
*
* @version $Id: Template.java,v 1.216.2.3 2006/03/10 17:49:02 revusky Exp $
*/
public class Template extends Configurable {
public static final String DEFAULT_NAMESPACE_PREFIX = "D";
public static final String NO_NS_PREFIX = "N";
private Map macros = new HashMap();
private List imports = new Vector();
private TemplateElement rootElement;
private String encoding, defaultNS;
private final String name;
private final ArrayList lines = new ArrayList();
private Map prefixToNamespaceURILookup = new HashMap();
private Map namespaceURIToPrefixLookup = new HashMap();
/**
* A prime constructor to which all other constructors should
* delegate directly or indirectly.
*/
private Template(String name, Configuration cfg)
{
super(cfg != null ? cfg : Configuration.getDefaultConfiguration());
this.name = name;
}
/**
* Constructs a template from a character stream.
*
* @param name the path of the template file relative to the directory what you use to store
* the templates. See {@link #getName} for more details.
* @param reader the character stream to read from. It will always be closed (Reader.close()).
* @param cfg the Configuration object that this Template is associated with.
* If this is null, the "default" {@link Configuration} object is used,
* which is highly discouraged, because it can easily lead to
* erroneous, unpredictable behaviour.
* (See more {@link Configuration#getDefaultConfiguration() here...})
* @param encoding This is the encoding that we are supposed to be using. If this is
* non-null (It's not actually necessary because we are using a Reader) then it is
* checked against the encoding specified in the FTL header -- assuming that is specified,
* and if they don't match a WrongEncodingException is thrown.
*/
public Template(String name, Reader reader, Configuration cfg, String encoding)
throws IOException
{
this(name, cfg);
this.encoding = encoding;
if (!(reader instanceof BufferedReader)) {
reader = new BufferedReader(reader, 0x1000);
}
LineTableBuilder ltb = new LineTableBuilder(reader);
try {
FMParser parser = new FMParser(this,
ltb,
getConfiguration().getStrictSyntaxMode(),
getConfiguration().getWhitespaceStripping(),
getConfiguration().getTagSyntax());
this.rootElement = parser.Root();
}
catch (TokenMgrError exc) {
throw new ParseException("Token manager error: " + exc, 0, 0);
}
finally {
ltb.close();
}
DebuggerService.registerTemplate(this);
namespaceURIToPrefixLookup = Collections.unmodifiableMap(namespaceURIToPrefixLookup);
prefixToNamespaceURILookup = Collections.unmodifiableMap(prefixToNamespaceURILookup);
}
/**
* This is equivalent to Template(name, reader, cfg, null)
*/
public Template(String name, Reader reader, Configuration cfg) throws IOException {
this(name, reader, cfg, null);
}
/**
* Constructs a template from a character stream.
*
* This is the same as the 3 parameter version when you pass null
* as the cfg parameter.
*
* @deprecated This constructor uses the "default" {@link Configuration}
* instance, which can easily lead to erroneous, unpredictable behaviour.
* See more {@link Configuration#getDefaultConfiguration() here...}.
*/
public Template(String name, Reader reader) throws IOException {
this(name, reader, null);
}
/**
* This constructor is only used internally.
*/
Template(String name, TemplateElement root, Configuration config) {
this(name, config);
this.rootElement = root;
DebuggerService.registerTemplate(this);
}
/**
* Returns a trivial template, one that is just a single block of
* plain text, no dynamic content. (Used by the cache module to create
* unparsed templates.)
* @param name the path of the template file relative to the directory what you use to store
* the templates. See {@link #getName} for more details.
* @param content the block of text that this template represents
* @param config the configuration to which this template belongs
*/
static public Template getPlainTextTemplate(String name, String content, Configuration config) {
Template template = new Template(name, config);
TextBlock block = new TextBlock(content);
template.rootElement = block;
DebuggerService.registerTemplate(template);
return template;
}
/**
* Processes the template, using data from the map, and outputs
* the resulting text to the supplied <tt>Writer</tt> The elements of the
* map are converted to template models using the default object wrapper
* returned by the {@link Configuration#getObjectWrapper() getObjectWrapper()}
* method of the <tt>Configuration</tt>.
* @param rootMap the root node of the data model. If null, an
* empty data model is used. Can be any object that the effective object
* wrapper can turn into a <tt>TemplateHashModel</tt>. Basically, simple and
* beans wrapper can turn <tt>java.util.Map</tt> objects into hashes
* and the Jython wrapper can turn both a <tt>PyDictionary</tt> as well as
* any object that implements <tt>__getitem__</tt> into a template hash.
* Naturally, you can pass any object directly implementing
* <tt>TemplateHashModel</tt> as well.
* @param out a <tt>Writer</tt> to output the text to.
* @throws TemplateException if an exception occurs during template processing
* @throws IOException if an I/O exception occurs during writing to the writer.
*/
public void process(Object rootMap, Writer out)
throws TemplateException, IOException
{
createProcessingEnvironment(rootMap, out, null).process();
}
/**
* Processes the template, using data from the root map object, and outputs
* the resulting text to the supplied writer, using the supplied
* object wrapper to convert map elements to template models.
* @param rootMap the root node of the data model. If null, an
* empty data model is used. Can be any object that the effective object
* wrapper can turn into a <tt>TemplateHashModel</tt> Basically, simple and
* beans wrapper can turn <tt>java.util.Map</tt> objects into hashes
* and the Jython wrapper can turn both a <tt>PyDictionary</tt> as well as any
* object that implements <tt>__getitem__</tt> into a template hash.
* Naturally, you can pass any object directly implementing
* <tt>TemplateHashModel</tt> as well.
* @param wrapper The object wrapper to use to wrap objects into
* {@link TemplateModel} instances. If null, the default wrapper retrieved
* by {@link Configurable#getObjectWrapper()} is used.
* @param out the writer to output the text to.
* @param rootNode The root node for recursive processing, this may be null.
*
* @throws TemplateException if an exception occurs during template processing
* @throws IOException if an I/O exception occurs during writing to the writer.
*/
public void process(Object rootMap, Writer out, ObjectWrapper wrapper, TemplateNodeModel rootNode)
throws TemplateException, IOException
{
Environment env = createProcessingEnvironment(rootMap, out, wrapper);
if (rootNode != null) {
env.setCurrentVisitorNode(rootNode);
}
env.process();
}
/**
* Processes the template, using data from the root map object, and outputs
* the resulting text to the supplied writer, using the supplied
* object wrapper to convert map elements to template models.
* @param rootMap the root node of the data model. If null, an
* empty data model is used. Can be any object that the effective object
* wrapper can turn into a <tt>TemplateHashModel</tt> Basically, simple and
* beans wrapper can turn <tt>java.util.Map</tt> objects into hashes
* and the Jython wrapper can turn both a <tt>PyDictionary</tt> as well as any
* object that implements <tt>__getitem__</tt> into a template hash.
* Naturally, you can pass any object directly implementing
* <tt>TemplateHashModel</tt> as well.
* @param wrapper The object wrapper to use to wrap objects into
* {@link TemplateModel} instances. If null, the default wrapper retrieved
* by {@link Configurable#getObjectWrapper()} is used.
* @param out the writer to output the text to.
*
* @throws TemplateException if an exception occurs during template processing
* @throws IOException if an I/O exception occurs during writing to the writer.
*/
public void process(Object rootMap, Writer out, ObjectWrapper wrapper)
throws TemplateException, IOException
{
process(rootMap, out, wrapper, null);
}
/**
* Creates a {@link freemarker.core.Environment Environment} object,
* using this template, the data model provided as the root map object, and
* the supplied object wrapper to convert map elements to template models.
* You can then call Environment.process() on the returned environment
* to set off the actual rendering.
* Use this method if you want to do some special initialization on the environment
* before template processing, or if you want to read the environment after template
* processing.
*
* <p>Example:
*
* <p>This:
* <pre>
* Environment env = myTemplate.createProcessingEnvironment(root, out, null);
* env.process();
* </pre>
* is equivalent with this:
* <pre>
* myTemplate.process(root, out);
* </pre>
* But with <tt>createProcessingEnvironment</tt>, you can manipulate the environment
* before and after the processing:
* <pre>
* Environment env = myTemplate.createProcessingEnvironment(root, out);
* env.include("include/common.ftl", null, true); // before processing
* env.process();
* TemplateModel x = env.getVariable("x"); // after processing
* </pre>
*
* @param rootMap the root node of the data model. If null, an
* empty data model is used. Can be any object that the effective object
* wrapper can turn into a <tt>TemplateHashModel</tt> Basically, simple and
* beans wrapper can turn <tt>java.util.Map</tt> objects into hashes
* and the Jython wrapper can turn both a <tt>PyDictionary</tt> as well as any
* object that implements <tt>__getitem__</tt> into a template hash.
* Naturally, you can pass any object directly implementing
* <tt>TemplateHashModel</tt> as well.
* @param wrapper The object wrapper to use to wrap objects into
* {@link TemplateModel} instances. If null, the default wrapper retrieved
* by {@link Configurable#getObjectWrapper()} is used.
* @param out the writer to output the text to.
* @return the {@link freemarker.core.Environment Environment} object created for processing
* @throws TemplateException if an exception occurs while setting up the Environment object.
* @throws IOException if an exception occurs doing any auto-imports
*/
public Environment createProcessingEnvironment(Object rootMap, Writer out, ObjectWrapper wrapper)
throws TemplateException, IOException
{
TemplateHashModel root = null;
if(rootMap instanceof TemplateHashModel) {
root = (TemplateHashModel)rootMap;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -