1 package org.apache.velocity.context;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.util.HashMap;
23 import java.util.Stack;
24 import java.util.List;
25
26 import org.apache.velocity.app.event.EventCartridge;
27 import org.apache.velocity.runtime.resource.Resource;
28 import org.apache.velocity.util.introspection.IntrospectionCacheData;
29
30 /**
31 * class to encapsulate the 'stuff' for internal operation of velocity.
32 * We use the context as a thread-safe storage : we take advantage of the
33 * fact that it's a visitor of sorts to all nodes (that matter) of the
34 * AST during init() and render().
35 * Currently, it carries the template name for namespace
36 * support, as well as node-local context data introspection caching.
37 *
38 * Note that this is not a public class. It is for package access only to
39 * keep application code from accessing the internals, as AbstractContext
40 * is derived from this.
41 *
42 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
43 * @version $Id: InternalContextBase.java 679861 2008-07-25 17:17:50Z nbubna $
44 */
45 class InternalContextBase implements InternalHousekeepingContext, InternalEventContext
46 {
47 /**
48 * Version Id for serializable
49 */
50 private static final long serialVersionUID = -245905472770843470L;
51
52 /**
53 * cache for node/context specific introspection information
54 */
55 private HashMap introspectionCache = new HashMap(33);
56
57 /**
58 * Template name stack. The stack top contains the current template name.
59 */
60 private Stack templateNameStack = new Stack();
61
62 /**
63 * Velocimacro name stack. The stack top contains the current macro name.
64 */
65 private Stack macroNameStack = new Stack();
66
67 /**
68 * EventCartridge we are to carry. Set by application
69 */
70 private EventCartridge eventCartridge = null;
71
72 /**
73 * Current resource - used for carrying encoding and other
74 * information down into the rendering process
75 */
76 private Resource currentResource = null;
77
78 /**
79 * Is rendering allowed? Defaults to true, can be changed by #stop directive.
80 */
81 private boolean allowRendering = true;
82
83 /**
84 * List for holding the macro libraries. Contains the macro library
85 * template name as strings.
86 */
87 private List macroLibraries = null;
88
89 /**
90 * set the current template name on top of stack
91 *
92 * @param s current template name
93 */
94 public void pushCurrentTemplateName( String s )
95 {
96 templateNameStack.push(s);
97 }
98
99 /**
100 * remove the current template name from stack
101 */
102 public void popCurrentTemplateName()
103 {
104 templateNameStack.pop();
105 }
106
107 /**
108 * get the current template name
109 *
110 * @return String current template name
111 */
112 public String getCurrentTemplateName()
113 {
114 if ( templateNameStack.empty() )
115 return "<undef>";
116 else
117 return (String) templateNameStack.peek();
118 }
119
120 /**
121 * get the current template name stack
122 *
123 * @return Object[] with the template name stack contents.
124 */
125 public Object[] getTemplateNameStack()
126 {
127 return templateNameStack.toArray();
128 }
129
130 /**
131 * set the current macro name on top of stack
132 *
133 * @param s current macro name
134 */
135 public void pushCurrentMacroName( String s )
136 {
137 macroNameStack.push(s);
138 }
139
140 /**
141 * remove the current macro name from stack
142 */
143 public void popCurrentMacroName()
144 {
145 macroNameStack.pop();
146 }
147
148 /**
149 * get the current macro name
150 *
151 * @return String current macro name
152 */
153 public String getCurrentMacroName()
154 {
155 if (macroNameStack.empty())
156 {
157 return "<undef>";
158 }
159 else
160 {
161 return (String) macroNameStack.peek();
162 }
163 }
164
165 /**
166 * get the current macro call depth
167 *
168 * @return int current macro call depth
169 */
170 public int getCurrentMacroCallDepth()
171 {
172 return macroNameStack.size();
173 }
174
175 /**
176 * get the current macro name stack
177 *
178 * @return Object[] with the macro name stack contents.
179 */
180 public Object[] getMacroNameStack()
181 {
182 return macroNameStack.toArray();
183 }
184
185 /**
186 * returns an IntrospectionCache Data (@see IntrospectionCacheData)
187 * object if exists for the key
188 *
189 * @param key key to find in cache
190 * @return cache object
191 */
192 public IntrospectionCacheData icacheGet( Object key )
193 {
194 return ( IntrospectionCacheData ) introspectionCache.get( key );
195 }
196
197 /**
198 * places an IntrospectionCache Data (@see IntrospectionCacheData)
199 * element in the cache for specified key
200 *
201 * @param key key
202 * @param o IntrospectionCacheData object to place in cache
203 */
204 public void icachePut( Object key, IntrospectionCacheData o )
205 {
206 introspectionCache.put( key, o );
207 }
208
209 /**
210 * @see org.apache.velocity.context.InternalHousekeepingContext#setCurrentResource(org.apache.velocity.runtime.resource.Resource)
211 */
212 public void setCurrentResource( Resource r )
213 {
214 currentResource = r;
215 }
216
217 /**
218 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
219 */
220 public Resource getCurrentResource()
221 {
222 return currentResource;
223 }
224
225
226 /**
227 * @see org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering()
228 */
229 public boolean getAllowRendering()
230 {
231 return allowRendering;
232 }
233
234 /**
235 * @see org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean)
236 */
237 public void setAllowRendering(boolean v)
238 {
239 allowRendering = v;
240 }
241
242 /**
243 * @see org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
244 */
245 public void setMacroLibraries(List macroLibraries)
246 {
247 this.macroLibraries = macroLibraries;
248 }
249
250 /**
251 * @see org.apache.velocity.context.InternalHousekeepingContext#getMacroLibraries()
252 */
253 public List getMacroLibraries()
254 {
255 return macroLibraries;
256 }
257
258
259 /**
260 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
261 */
262 public EventCartridge attachEventCartridge( EventCartridge ec )
263 {
264 EventCartridge temp = eventCartridge;
265
266 eventCartridge = ec;
267
268 return temp;
269 }
270
271 /**
272 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
273 */
274 public EventCartridge getEventCartridge()
275 {
276 return eventCartridge;
277 }
278 }
279
280