KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > core > BuiltinVariable


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.core;
54
55 import freemarker.template.*;
56
57 /**
58  * A reference to a built-in identifier, such as .root
59  */

60 final class BuiltinVariable extends Expression {
61
62     static final String JavaDoc NAMESPACE = "namespace";
63     static final String JavaDoc MAIN = "main";
64     static final String JavaDoc GLOBALS = "globals";
65     static final String JavaDoc LOCALS = "locals";
66     static final String JavaDoc DATA_MODEL = "data_model";
67     static final String JavaDoc LANG = "lang";
68     static final String JavaDoc LOCALE = "locale";
69     static final String JavaDoc CURRENT_NODE = "current_node";
70     static final String JavaDoc NODE = "node";
71     static final String JavaDoc PASS = "pass";
72     static final String JavaDoc VARS = "vars";
73     static final String JavaDoc VERSION = "version";
74     static final String JavaDoc ERROR = "error";
75     static final String JavaDoc OUTPUT_ENCODING = "output_encoding";
76     static final String JavaDoc URL_ESCAPING_CHARSET = "url_escaping_charset";
77
78     private final String JavaDoc name;
79
80     BuiltinVariable(String JavaDoc name) throws ParseException {
81         name = name.intern();
82         this.name = name;
83         if (name != NAMESPACE
84             && name != MAIN
85             && name != GLOBALS
86             && name != LOCALS
87             && name != LANG
88             && name != LOCALE
89             && name != DATA_MODEL
90             && name != CURRENT_NODE
91             && name != NODE
92             && name != PASS
93             && name != VARS
94         && name != VERSION
95         && name != OUTPUT_ENCODING
96         && name != URL_ESCAPING_CHARSET
97             && name != ERROR)
98         {
99             throw new ParseException("Unknown built-in variable: " + name, this);
100         }
101     }
102
103     TemplateModel _getAsTemplateModel(Environment env) throws TemplateException {
104         if (name == NAMESPACE) {
105             return env.getCurrentNamespace();
106         }
107         if (name == MAIN) {
108             return env.getMainNamespace();
109         }
110         if (name == GLOBALS) {
111             return env.getGlobalVariables();
112         }
113         if (name == LOCALS) {
114             return env.getCurrentMacroContext().getLocals();
115         }
116         if (name == DATA_MODEL) {
117             return env.getDataModel();
118         }
119         if (name == VARS) {
120             return new VarsHash(env);
121         }
122         if (name == LOCALE) {
123             return new SimpleScalar(env.getLocale().toString());
124         }
125         if (name == LANG) {
126             return new SimpleScalar(env.getLocale().getLanguage());
127         }
128         if (name == CURRENT_NODE || name == NODE) {
129             return env.getCurrentVisitorNode();
130         }
131         if (name == PASS) {
132             return Macro.DO_NOTHING_MACRO;
133         }
134         if (name == VERSION) {
135             return new SimpleScalar(Configuration.getVersionNumber());
136         }
137         if (name == OUTPUT_ENCODING) {
138             String JavaDoc s = env.getOutputEncoding();
139             return s != null ? new SimpleScalar(s) : null;
140         }
141         if (name == URL_ESCAPING_CHARSET) {
142             String JavaDoc s = env.getURLEscapingCharset();
143             return s != null ? new SimpleScalar(s) : null;
144         }
145         if (name == ERROR) {
146             return new SimpleScalar(env.getCurrentRecoveredErrorMesssage());
147         }
148         throw new TemplateException("Invalid built-in variable: " + this, env);
149     }
150
151     public String JavaDoc toString() {
152         return "." + name;
153     }
154
155     public String JavaDoc getCanonicalForm() {
156         return "." + name;
157     }
158
159     boolean isLiteral() {
160         return false;
161     }
162
163     Expression _deepClone(String JavaDoc name, Expression subst) {
164         return this;
165     }
166
167     class VarsHash implements TemplateHashModel {
168         
169         Environment env;
170         
171         VarsHash(Environment env) {
172             this.env = env;
173         }
174         
175         public TemplateModel get(String JavaDoc key) throws TemplateModelException {
176             return env.getVariable(key);
177         }
178         
179         public boolean isEmpty() {
180             return false;
181         }
182     }
183 }
184
Popular Tags