📄 environment.java
字号:
return currentVisitorNode;
}
/**
* sets TemplateNodeModel as the current visitor node. <tt>.current_node</tt>
*/
public void setCurrentVisitorNode(TemplateNodeModel node) {
currentVisitorNode = node;
}
TemplateModel getNodeProcessor(TemplateNodeModel node) throws TemplateException {
String nodeName = node.getNodeName();
if (nodeName == null) {
throw new TemplateException("Node name is null.", this);
}
TemplateModel result = getNodeProcessor(nodeName, node.getNodeNamespace(), 0);
if (result == null) {
String type = node.getNodeType();
/* DD: Original version: */
if (type == null) {
type = "default";
}
result = getNodeProcessor("@" + type, null, 0);
/* DD: Jonathan's non-BC version and IMHO otherwise wrong version:
if (type != null) {
result = getNodeProcessor("@" + type, null, 0);
}
if (result == null) {
result = getNodeProcessor("@default", null, 0);
}
*/
}
return result;
}
private TemplateModel getNodeProcessor(final String nodeName, final String nsURI, int startIndex)
throws TemplateException
{
TemplateModel result = null;
int i;
for (i = startIndex; i<nodeNamespaces.size(); i++) {
Namespace ns = null;
try {
ns = (Namespace) nodeNamespaces.get(i);
} catch (ClassCastException cce) {
throw new InvalidReferenceException("A using clause should contain a sequence of namespaces or strings that indicate the location of importable macro libraries.", this);
}
result = getNodeProcessor(ns, nodeName, nsURI);
if (result != null)
break;
}
if (result != null) {
this.nodeNamespaceIndex = i+1;
this.currentNodeName = nodeName;
this.currentNodeNS = nsURI;
}
return result;
}
private TemplateModel getNodeProcessor(Namespace ns, String localName, String nsURI) throws TemplateException {
TemplateModel result = null;
if (nsURI == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
Template template = ns.getTemplate();
String prefix = template.getPrefixForNamespace(nsURI);
if (prefix == null) {
// The other template cannot handle this node
// since it has no prefix registered for the namespace
return null;
}
if (prefix.length() >0) {
result = ns.get(prefix + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
} else {
if (nsURI.length() == 0) {
result = ns.get(Template.NO_NS_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (nsURI.equals(template.getDefaultNS())) {
result = ns.get(Template.DEFAULT_NAMESPACE_PREFIX + ":" + localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
if (result == null) {
result = ns.get(localName);
if (!(result instanceof Macro) && !(result instanceof TemplateTransformModel)) {
result = null;
}
}
}
}
return result;
}
/**
* Emulates <code>include</code> directive, except that <code>name</code> must be tempate
* root relative.
*
* <p>It's the same as <code>include(getTemplateForInclusion(name, encoding, parse))</code>.
* But, you may want to separately call these two methods, so you can determine the source of
* exceptions more precisely, and thus achieve more intelligent error handling.
*
* @see #getTemplateForInclusion(String name, String encoding, boolean parse)
* @see #include(Template includedTemplate)
*/
public void include(String name, String encoding, boolean parse)
throws IOException, TemplateException
{
include(getTemplateForInclusion(name, encoding, parse));
}
/**
* Gets a template for inclusion; used with {@link #include(Template includedTemplate)}.
* The advantage over simply using <code>config.getTemplate(...)</code> is that it chooses
* the default encoding as the <code>include</code> directive does.
*
* @param name the name of the template, relatively to the template root directory
* (not the to the directory of the currently executing template file!).
* (Note that you can use {@link freemarker.cache.TemplateCache#getFullTemplatePath}
* to convert paths to template root relative paths.)
* @param encoding the encoding of the obtained template. If null,
* the encoding of the Template that is currently being processed in this
* Environment is used.
* @param parse whether to process a parsed template or just include the
* unparsed template source.
*/
public Template getTemplateForInclusion(String name, String encoding, boolean parse)
throws IOException
{
if (encoding == null) {
encoding = getTemplate().getEncoding();
}
if (encoding == null) {
encoding = getConfiguration().getEncoding(this.getLocale());
}
return getConfiguration().getTemplate(name, getLocale(), encoding, parse);
}
/**
* Processes a Template in the context of this <code>Environment</code>, including its
* output in the <code>Environment</code>'s Writer.
*
* @param includedTemplate the template to process. Note that it does <em>not</em> need
* to be a template returned by
* {@link #getTemplateForInclusion(String name, String encoding, boolean parse)}.
*/
public void include(Template includedTemplate)
throws TemplateException, IOException
{
Template prevTemplate = getTemplate();
setParent(includedTemplate);
importMacros(includedTemplate);
try {
visit(includedTemplate.getRootTreeNode());
}
finally {
setParent(prevTemplate);
}
}
/**
* Emulates <code>import</code> directive, except that <code>name</code> must be tempate
* root relative.
*
* <p>It's the same as <code>importLib(getTemplateForImporting(name), namespace)</code>.
* But, you may want to separately call these two methods, so you can determine the source of
* exceptions more precisely, and thus achieve more intelligent error handling.
*
* @see #getTemplateForImporting(String name)
* @see #importLib(Template includedTemplate, String namespace)
*/
public Namespace importLib(String name, String namespace)
throws IOException, TemplateException
{
return importLib(getTemplateForImporting(name), namespace);
}
/**
* Gets a template for importing; used with
* {@link #importLib(Template importedTemplate, String namespace)}. The advantage
* over simply using <code>config.getTemplate(...)</code> is that it chooses the encoding
* as the <code>import</code> directive does.
*
* @param name the name of the template, relatively to the template root directory
* (not the to the directory of the currently executing template file!).
* (Note that you can use {@link freemarker.cache.TemplateCache#getFullTemplatePath}
* to convert paths to template root relative paths.)
*/
public Template getTemplateForImporting(String name) throws IOException {
return getTemplateForInclusion(name, null, true);
}
/**
* Emulates <code>import</code> directive.
*
* @param loadedTemplate the template to import. Note that it does <em>not</em> need
* to be a template returned by {@link #getTemplateForImporting(String name)}.
*/
public Namespace importLib(Template loadedTemplate, String namespace)
throws IOException, TemplateException
{
if (loadedLibs == null) {
loadedLibs = new HashMap();
}
String templateName = loadedTemplate.getName();
Namespace existingNamespace = (Namespace) loadedLibs.get(templateName);
if (existingNamespace != null) {
if (namespace != null) {
setVariable(namespace, existingNamespace);
}
}
else {
Namespace newNamespace = new Namespace(loadedTemplate);
if (namespace != null) {
currentNamespace.put(namespace, newNamespace);
if (currentNamespace == mainNamespace) {
globalNamespace.put(namespace, newNamespace);
}
}
Namespace prevNamespace = this.currentNamespace;
this.currentNamespace = newNamespace;
loadedLibs.put(templateName, currentNamespace);
Writer prevOut = out;
this.out = NULL_WRITER;
try {
include(loadedTemplate);
} finally {
this.out = prevOut;
this.currentNamespace = prevNamespace;
}
}
return (Namespace) loadedLibs.get(templateName);
}
String renderElementToString(TemplateElement te) throws IOException, TemplateException {
Writer prevOut = out;
try {
StringWriter sw = new StringWriter();
this.out = sw;
visit(te);
return sw.toString();
}
finally {
this.out = prevOut;
}
}
void importMacros(Template template) {
for (Iterator it = template.getMacros().values().iterator(); it.hasNext();) {
visitMacroDef((Macro) it.next());
}
}
/**
* @return the namespace URI registered for this prefix, or null.
* This is based on the mappings registered in the current namespace.
*/
public String getNamespaceForPrefix(String prefix) {
return currentNamespace.getTemplate().getNamespaceForPrefix(prefix);
}
public String getPrefixForNamespace(String nsURI) {
return currentNamespace.getTemplate().getPrefixForNamespace(nsURI);
}
/**
* @return the default node namespace for the current FTL namespace
*/
public String getDefaultNS() {
return currentNamespace.getTemplate().getDefaultNS();
}
/**
* A hook that Jython uses.
*/
public Object __getitem__(String key) throws TemplateModelException {
return BeansWrapper.getDefaultInstance().unwrap(getVariable(key));
}
/**
* A hook that Jython uses.
*/
public void __setitem__(String key, Object o) throws TemplateException {
setGlobalVariable(key, getObjectWrapper().wrap(o));
}
private static final class NumberFormatKey
{
private final String pattern;
private final Locale locale;
NumberFormatKey(String pattern, Locale locale)
{
this.pattern = pattern;
this.locale = locale;
}
public boolean equals(Object o)
{
if(o instanceof NumberFormatKey)
{
NumberFormatKey fk = (NumberFormatKey)o;
return fk.pattern.equals(pattern) && fk.locale.equals(locale);
}
return false;
}
public int hashCode()
{
return pattern.hashCode() ^ locale.hashCode();
}
}
private static final class DateFormatKey
{
private final int dateType;
private final String pattern;
private final Locale locale;
private final TimeZone timeZone;
DateFormatKey(int dateType, String pattern, Locale locale, TimeZone timeZone)
{
this.dateType = dateType;
this.pattern = pattern;
this.locale = locale;
this.timeZone = timeZone;
}
public boolean equals(Object o)
{
if(o instanceof DateFormatKey)
{
DateFormatKey fk = (DateFormatKey)o;
return dateType == fk.dateType && fk.pattern.equals(pattern) && fk.locale.equals(locale) && fk.timeZone.equals(timeZone);
}
return false;
}
public int hashCode()
{
return dateType ^ pattern.hashCode() ^ locale.hashCode() ^ timeZone.hashCode();
}
}
public class Namespace extends SimpleHash {
private Template template;
Namespace() {
this.template = Environment.this.getTemplate();
}
Namespace(Template template) {
this.template = template;
}
/**
* @return the Template object with which this Namespace is associated.
*/
public Template getTemplate() {
return template == null ? Environment.this.getTemplate() : template;
}
}
static final Writer NULL_WRITER = new Writer() {
public void write(char cbuf[], int off, int len) {}
public void flush() {}
public void close() {}
};
private static final Writer EMPTY_BODY_WRITER = new Writer() {
public void write(char[] cbuf, int off, int len) throws IOException {
if (len > 0) {
throw new IOException(
"This transform does not allow nested content.");
}
}
public void flush() {
}
public void close() {
}
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -