| Constructor: |
public Template(String name,
Reader reader) throws IOException {
this(name, reader, 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. |
public Template(String name,
Reader reader,
Configuration cfg) throws IOException {
this(name, reader, cfg, null);
}
This is equivalent to Template(name, reader, cfg, null) |
Template(String name,
TemplateElement root,
Configuration config) {
this(name, config);
this.rootElement = root;
DebuggerService.registerTemplate(this);
}
This constructor is only used internally. |
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 {
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);
}
}
catch(ParseException e) {
e.setTemplateName(name);
throw e;
}
finally {
ltb.close();
}
DebuggerService.registerTemplate(this);
namespaceURIToPrefixLookup = Collections.unmodifiableMap(namespaceURIToPrefixLookup);
prefixToNamespaceURILookup = Collections.unmodifiableMap(prefixToNamespaceURILookup);
}
Constructs a template from a character stream. Parameters:
name - the path of the template file relative to the directory what you use to store
the templates. See #getName for more details.
reader - the character stream to read from. It will always be closed (Reader.close()).
cfg - the Configuration object that this Template is associated with.
If this is null, the "default" Configuration object is used,
which is highly discouraged, because it can easily lead to
erroneous, unpredictable behaviour.
(See more here... )
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.
|
| Method from freemarker.template.Template Detail: |
public void addImport(LibraryLoad ll) {
imports.add(ll);
}
Called by code internally to maintain
a list of imports |
public void addMacro(Macro macro) {
macros.put(macro.getName(), macro);
}
Called by code internally to maintain
a table of macros |
public void addPrefixNSMapping(String prefix,
String nsURI) {
if (nsURI.length() == 0) {
throw new IllegalArgumentException("Cannot map empty string URI");
}
if (prefix.length() == 0) {
throw new IllegalArgumentException("Cannot map empty string prefix");
}
if (prefix.equals(NO_NS_PREFIX)) {
throw new IllegalArgumentException("The prefix: " + prefix + " cannot be registered, it is reserved for special internal use.");
}
if (prefixToNamespaceURILookup.containsKey(prefix)) {
throw new IllegalArgumentException("The prefix: '" + prefix + "' was repeated. This is illegal.");
}
if (namespaceURIToPrefixLookup.containsKey(nsURI)) {
throw new IllegalArgumentException("The namespace URI: " + nsURI + " cannot be mapped to 2 different prefixes.");
}
if (prefix.equals(DEFAULT_NAMESPACE_PREFIX)) {
this.defaultNS = nsURI;
} else {
prefixToNamespaceURILookup.put(prefix, nsURI);
namespaceURIToPrefixLookup.put(nsURI, prefix);
}
}
|
public TreePath containingElements(int column,
int line) {
ArrayList elements = new ArrayList();
TemplateElement element = rootElement;
mainloop:
while (element.contains(column, line)) {
elements.add(element);
for (Enumeration enumeration = element.children(); enumeration.hasMoreElements();) {
TemplateElement elem = (TemplateElement) enumeration.nextElement();
if (elem.contains(column, line)) {
element = elem;
continue mainloop;
}
}
break;
}
if (elements == null || elements.isEmpty()) {
return null;
}
return new TreePath(elements.toArray());
}
|
public Environment createProcessingEnvironment(Object rootMap,
Writer out) throws IOException, TemplateException {
return createProcessingEnvironment(rootMap, out, null);
}
Same as createProcessingEnvironment(rootMap, out, null). |
public Environment createProcessingEnvironment(Object rootMap,
Writer out,
ObjectWrapper wrapper) throws IOException, TemplateException {
TemplateHashModel root = null;
if(rootMap instanceof TemplateHashModel) {
root = (TemplateHashModel)rootMap;
}
else {
if(wrapper == null) {
wrapper = getObjectWrapper();
}
try {
root = rootMap != null
? (TemplateHashModel)wrapper.wrap(rootMap)
: new SimpleHash(wrapper);
if(root == null) {
throw new IllegalArgumentException(wrapper.getClass().getName() + " converted " + rootMap.getClass().getName() + " to null.");
}
}
catch(ClassCastException e) {
throw new IllegalArgumentException(wrapper.getClass().getName() + " could not convert " + rootMap.getClass().getName() + " to a TemplateHashModel.");
}
}
Environment env = new Environment(this, root, out);
getConfiguration().doAutoImports(env);
getConfiguration().doAutoIncludes(env);
return env;
}
Creates a 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.
Example:
This:
Environment env = myTemplate.createProcessingEnvironment(root, out, null);
env.process();
is equivalent with this:
myTemplate.process(root, out);
But with createProcessingEnvironment, you can manipulate the environment
before and after the processing:
Environment env = myTemplate.createProcessingEnvironment(root, out);
env.include("include/common.ftl", null, true); // before processing
env.process();
TemplateModel x = env.getVariable("x"); // after processing
|
public void dump(PrintStream ps) {
ps.print(rootElement.getCanonicalForm());
}
Dump the raw template in canonical form. |
public void dump(Writer out) throws IOException {
out.write(rootElement.getCanonicalForm());
}
Dump the raw template in canonical form. |
public Configuration getConfiguration() {
return (Configuration) getParent();
}
Returns the Configuration object associated with this template. |
public String getDefaultNS() {
return this.defaultNS;
}
|
public String getEncoding() {
return this.encoding;
}
Returns the character encoding used for reading included files. |
public List getImports() {
return imports;
}
|
public Map getMacros() {
return macros;
}
|
public String getName() {
return name;
}
The path of the template file relative to the directory what you use to store the templates.
For example, if the real path of template is "/www/templates/community/forum.fm",
and you use ""/www/templates" as
"directoryForTemplateLoading" ,
then name should be "community/forum.fm". The name is used for example when you
use <include ...> and you give a path that is relative to the current
template, or in error messages when FreeMarker logs an error while it processes the template. |
public String getNamespaceForPrefix(String prefix) {
if (prefix.equals("")) {
return defaultNS == null ? "" : defaultNS;
}
return (String) prefixToNamespaceURILookup.get(prefix);
}
|
public static 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;
}
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.) |
public String getPrefixForNamespace(String nsURI) {
if (nsURI == null) {
return null;
}
if (nsURI.length() == 0) {
return defaultNS == null ? "" : NO_NS_PREFIX;
}
if (nsURI.equals(defaultNS)) {
return "";
}
return (String) namespaceURIToPrefixLookup.get(nsURI);
}
|
public String getPrefixedName(String localName,
String nsURI) {
if (nsURI == null || nsURI.length() == 0) {
if (defaultNS != null) {
return NO_NS_PREFIX + ":" + localName;
} else {
return localName;
}
}
if (nsURI.equals(defaultNS)) {
return localName;
}
String prefix = getPrefixForNamespace(nsURI);
if (prefix == null) {
return null;
}
return prefix + ":" + localName;
}
|
public TemplateElement getRootTreeNode() {
return rootElement;
}
|
public String getSource(int beginColumn,
int beginLine,
int endColumn,
int endLine) {
// Our container is zero-based.
--beginLine;
--beginColumn;
--endColumn;
--endLine;
StringBuffer buf = new StringBuffer();
for (int i = beginLine ; i< =endLine; i++) {
if (i < lines.size()) {
buf.append(lines.get(i));
}
}
int lastLineLength = lines.get(endLine).toString().length();
int trailingCharsToDelete = lastLineLength - endColumn -1;
buf.delete(0, beginColumn);
buf.delete(buf.length() - trailingCharsToDelete, buf.length());
return buf.toString();
}
Returns the template source at the location
specified by the coordinates given. |
public void process(Object rootMap,
Writer out) throws IOException, TemplateException {
createProcessingEnvironment(rootMap, out, null).process();
}
Processes the template, using data from the map, and outputs
the resulting text to the supplied Writer The elements of the
map are converted to template models using the default object wrapper
returned by the getObjectWrapper()
method of the Configuration. |
public void process(Object rootMap,
Writer out,
ObjectWrapper wrapper) throws IOException, TemplateException {
process(rootMap, out, wrapper, null);
}
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. |
public void process(Object rootMap,
Writer out,
ObjectWrapper wrapper,
TemplateNodeModel rootNode) throws IOException, TemplateException {
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. |
public void setEncoding(String encoding) {
this.encoding = encoding;
}
Sets the character encoding to use for
included files. Usually you don't set this value manually,
instead it is assigned to the template upon loading. |
public String toString() {
StringWriter sw = new StringWriter();
try {
dump(sw);
} catch (IOException ioe) {
throw new RuntimeException(ioe.getMessage());
}
return sw.toString();
}
Returns a string representing the raw template
text in canonical form. |