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 org.apache.velocity.app.event.EventCartridge;
23 import org.apache.velocity.runtime.resource.Resource;
24 import org.apache.velocity.util.introspection.IntrospectionCacheData;
25
26 import java.util.List;
27
28 /**
29 * This adapter class is the container for all context types for internal
30 * use. The AST now uses this class rather than the app-level Context
31 * interface to allow flexibility in the future.
32 *
33 * Currently, we have two context interfaces which must be supported :
34 * <ul>
35 * <li> Context : used for application/template data access
36 * <li> InternalHousekeepingContext : used for internal housekeeping and caching
37 * <li> InternalWrapperContext : used for getting root cache context and other
38 * such.
39 * <li> InternalEventContext : for event handling.
40 * </ul>
41 *
42 * This class implements the two interfaces to ensure that all methods are
43 * supported. When adding to the interfaces, or adding more context
44 * functionality, the interface is the primary definition, so alter that first
45 * and then all classes as necessary. As of this writing, this would be
46 * the only class affected by changes to InternalContext
47 *
48 * This class ensures that an InternalContextBase is available for internal
49 * use. If an application constructs their own Context-implementing
50 * object w/o subclassing AbstractContext, it may be that support for
51 * InternalContext is not available. Therefore, InternalContextAdapter will
52 * create an InternalContextBase if necessary for this support. Note that
53 * if this is necessary, internal information such as node-cache data will be
54 * lost from use to use of the context. This may or may not be important,
55 * depending upon application.
56 *
57 *
58 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
59 * @version $Id: InternalContextAdapterImpl.java 752769 2009-03-12 04:32:20Z nbubna $
60 */
61 public final class InternalContextAdapterImpl implements InternalContextAdapter
62 {
63 /**
64 * the user data Context that we are wrapping
65 */
66 Context context = null;
67
68 /**
69 * the ICB we are wrapping. We may need to make one
70 * if the user data context implementation doesn't
71 * support one. The default AbstractContext-derived
72 * VelocityContext does, and it's recommended that
73 * people derive new contexts from AbstractContext
74 * rather than piecing things together
75 */
76 InternalHousekeepingContext icb = null;
77
78 /**
79 * The InternalEventContext that we are wrapping. If
80 * the context passed to us doesn't support it, no
81 * biggie. We don't make it for them - since its a
82 * user context thing, nothing gained by making one
83 * for them now
84 */
85 InternalEventContext iec = null;
86
87 /**
88 * CTOR takes a Context and wraps it, delegating all 'data' calls
89 * to it.
90 *
91 * For support of internal contexts, it will create an InternalContextBase
92 * if need be.
93 * @param c
94 */
95 public InternalContextAdapterImpl( Context c )
96 {
97 context = c;
98
99 if ( !( c instanceof InternalHousekeepingContext ))
100 {
101 icb = new InternalContextBase();
102 }
103 else
104 {
105 icb = (InternalHousekeepingContext) context;
106 }
107
108 if ( c instanceof InternalEventContext)
109 {
110 iec = ( InternalEventContext) context;
111 }
112 }
113
114 /* --- InternalHousekeepingContext interface methods --- */
115
116 /**
117 * @see org.apache.velocity.context.InternalHousekeepingContext#pushCurrentTemplateName(java.lang.String)
118 */
119 public void pushCurrentTemplateName( String s )
120 {
121 icb.pushCurrentTemplateName( s );
122 }
123
124 /**
125 * @see org.apache.velocity.context.InternalHousekeepingContext#popCurrentTemplateName()
126 */
127 public void popCurrentTemplateName()
128 {
129 icb.popCurrentTemplateName();
130 }
131
132 /**
133 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentTemplateName()
134 */
135 public String getCurrentTemplateName()
136 {
137 return icb.getCurrentTemplateName();
138 }
139
140 /**
141 * @see org.apache.velocity.context.InternalHousekeepingContext#getTemplateNameStack()
142 */
143 public Object[] getTemplateNameStack()
144 {
145 return icb.getTemplateNameStack();
146 }
147
148 /**
149 * @see org.apache.velocity.context.InternalHousekeepingContext#pushCurrentMacroName(java.lang.String)
150 * @since 1.6
151 */
152 public void pushCurrentMacroName( String s )
153 {
154 icb.pushCurrentMacroName( s );
155 }
156
157 /**
158 * @see org.apache.velocity.context.InternalHousekeepingContext#popCurrentMacroName()
159 * @since 1.6
160 */
161 public void popCurrentMacroName()
162 {
163 icb.popCurrentMacroName();
164 }
165
166 /**
167 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentMacroName()
168 * @since 1.6
169 */
170 public String getCurrentMacroName()
171 {
172 return icb.getCurrentMacroName();
173 }
174
175 /**
176 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentMacroCallDepth()
177 * @since 1.6
178 */
179 public int getCurrentMacroCallDepth()
180 {
181 return icb.getCurrentMacroCallDepth();
182 }
183
184 /**
185 * @see org.apache.velocity.context.InternalHousekeepingContext#getMacroNameStack()
186 * @since 1.6
187 */
188 public Object[] getMacroNameStack()
189 {
190 return icb.getMacroNameStack();
191 }
192
193 /**
194 * @see org.apache.velocity.context.InternalHousekeepingContext#icacheGet(java.lang.Object)
195 */
196 public IntrospectionCacheData icacheGet( Object key )
197 {
198 return icb.icacheGet( key );
199 }
200
201 /**
202 * @see org.apache.velocity.context.InternalHousekeepingContext#icachePut(java.lang.Object, org.apache.velocity.util.introspection.IntrospectionCacheData)
203 */
204 public void icachePut( Object key, IntrospectionCacheData o )
205 {
206 icb.icachePut( 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 icb.setCurrentResource(r);
215 }
216
217 /**
218 * @see org.apache.velocity.context.InternalHousekeepingContext#getCurrentResource()
219 */
220 public Resource getCurrentResource()
221 {
222 return icb.getCurrentResource();
223 }
224
225 /**
226 * @see org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List)
227 * @since 1.6
228 */
229 public void setMacroLibraries(List macroLibraries)
230 {
231 icb.setMacroLibraries(macroLibraries);
232 }
233
234 /**
235 * @see org.apache.velocity.context.InternalHousekeepingContext#getMacroLibraries()
236 * @since 1.6
237 */
238 public List getMacroLibraries()
239 {
240 return icb.getMacroLibraries();
241 }
242
243 /* --- Context interface methods --- */
244
245 /**
246 * @see org.apache.velocity.context.Context#put(java.lang.String, java.lang.Object)
247 */
248 public Object put(String key, Object value)
249 {
250 return context.put( key , value );
251 }
252
253 /**
254 * @see org.apache.velocity.context.Context#get(java.lang.String)
255 */
256 public Object get(String key)
257 {
258 return context.get( key );
259 }
260
261 /**
262 * @see org.apache.velocity.context.Context#containsKey(java.lang.Object)
263 */
264 public boolean containsKey(Object key)
265 {
266 return context.containsKey( key );
267 }
268
269 /**
270 * @see org.apache.velocity.context.Context#getKeys()
271 */
272 public Object[] getKeys()
273 {
274 return context.getKeys();
275 }
276
277 /**
278 * @see org.apache.velocity.context.Context#remove(java.lang.Object)
279 */
280 public Object remove(Object key)
281 {
282 return context.remove( key );
283 }
284
285
286 /* ---- InternalWrapperContext --- */
287
288 /**
289 * returns the user data context that
290 * we are wrapping
291 * @return The internal user data context.
292 */
293 public Context getInternalUserContext()
294 {
295 return context;
296 }
297
298 /**
299 * Returns the base context that we are
300 * wrapping. Here, its this, but for other thing
301 * like VM related context contortions, it can
302 * be something else
303 * @return The base context.
304 */
305 public InternalContextAdapter getBaseContext()
306 {
307 return this;
308 }
309
310 /* ----- InternalEventContext ---- */
311
312 /**
313 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge)
314 */
315 public EventCartridge attachEventCartridge( EventCartridge ec )
316 {
317 if (iec != null)
318 {
319 return iec.attachEventCartridge( ec );
320 }
321
322 return null;
323 }
324
325 /**
326 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge()
327 */
328 public EventCartridge getEventCartridge()
329 {
330 if ( iec != null)
331 {
332 return iec.getEventCartridge( );
333 }
334
335 return null;
336 }
337
338 }
339
340
341