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 731266 2009-01-04 15:11:20Z byron $
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 * List for holding the macro libraries. Contains the macro library
80 * template name as strings.
81 */
82 private List macroLibraries = null;
83
84 /**
85 * set the current template name on top of stack
86 *
87 * @param s current template name
88 */
89 public void pushCurrentTemplateName( String s )
90 {
91 templateNameStack.push(s);
92 }
93
94 /**
95 * remove the current template name from stack
96 */
97 public void popCurrentTemplateName()
98 {
99 templateNameStack.pop();
100 }
101
102 /**
103 * get the current template name
104 *
105 * @return String current template name
106 */
107 public String getCurrentTemplateName()
108 {
109 if ( templateNameStack.empty() )
110 return "<undef>";
111 else
112 return (String) templateNameStack.peek();
113 }
114
115 /**
116 * get the current template name stack
117 *
118 * @return Object[] with the template name stack contents.
119 */
120 public Object[] getTemplateNameStack()
121 {
122 return templateNameStack.toArray();
123 }
124
125 /**
126 * set the current macro name on top of stack
127 *
128 * @param s current macro name
129 */
130 public void pushCurrentMacroName( String s )
131 {
132 macroNameStack.push(s);
133 }
134
135 /**
136 * remove the current macro name from stack
137 */
138 public void popCurrentMacroName()
139 {
140 macroNameStack.pop();
141 }
142
143 /**
144 * get the current macro name
145 *
146 * @return String current macro name
147 */
148 public String getCurrentMacroName()
149 {
150 if (macroNameStack.empty())
151 {
152 return "<undef>";
153 }
154 else
155 {
156 return (String) macroNameStack.peek();
157 }
158 }
159
160 /**
161 * get the current macro call depth
162 *
163 * @return int current macro call depth
164 */
165 public int getCurrentMacroCallDepth()
166 {
167 return macroNameStack.size();
168 }
169
170 /**
171 * get the current macro name stack
172 *
173 * @return Object[] with the macro name stack contents.
174 */
175 public Object[] getMacroNameStack()
176 {
177 return macroNameStack.toArray();
178 }
179
180 /**
181 * returns an IntrospectionCache Data (@see IntrospectionCacheData)
182 * object if exists for the key
183 *
184 * @param key key to find in cache
185 * @return cache object
186 */
187 public IntrospectionCacheData icacheGet( Object key )
188 {
189 return ( IntrospectionCacheData ) introspectionCache.get( key );
190 }
191
192 /**
193 * places an IntrospectionCache Data (@see IntrospectionCacheData)
194 * element in the cache for specified key
195 *
196 * @param key key
197 * @param o IntrospectionCacheData object to place in cache
198 */
199 public void icachePut( Object key, IntrospectionCacheData o )
200 {
201 introspectionCache.put( key, o );
202 }
203
204 /**
205 * @see org.apache.velocity.context.InternalHousekeepingContext#setCurrentResource(org.apache.velocity.runtime.resource.Resource)
206 */
207 public void setCurrentResource( Resource r )
208 {
209 currentResource = r;
210 }
211
212 /**
213 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
214 */
215 public Resource getCurrentResource()
216 {
217 return currentResource;
218 }
219
220 /**
221 * @see org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
222 */
223 public void setMacroLibraries(List macroLibraries)
224 {
225 this.macroLibraries = macroLibraries;
226 }
227
228 /**
229 * @see org.apache.velocity.context.InternalHousekeepingContext#getMacroLibraries()
230 */
231 public List getMacroLibraries()
232 {
233 return macroLibraries;
234 }
235
236
237 /**
238 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
239 */
240 public EventCartridge attachEventCartridge( EventCartridge ec )
241 {
242 EventCartridge temp = eventCartridge;
243
244 eventCartridge = ec;
245
246 return temp;
247 }
248
249 /**
250 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
251 */
252 public EventCartridge getEventCartridge()
253 {
254 return eventCartridge;
255 }
256 }
257
258