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 685724 2008-08-13 23:12:12Z 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 /** 227 * @see org.apache.velocity.context.InternalHousekeepingContext#getAllowRendering() 228 * @since 1.5 229 */ 230 public boolean getAllowRendering() 231 { 232 return icb.getAllowRendering(); 233 } 234 235 /** 236 * @see org.apache.velocity.context.InternalHousekeepingContext#setAllowRendering(boolean) 237 * @since 1.5 238 */ 239 public void setAllowRendering(boolean v) 240 { 241 icb.setAllowRendering(v); 242 } 243 244 /** 245 * @see org.apache.velocity.context.InternalHousekeepingContext#setMacroLibraries(List) 246 * @since 1.6 247 */ 248 public void setMacroLibraries(List macroLibraries) 249 { 250 icb.setMacroLibraries(macroLibraries); 251 } 252 253 /** 254 * @see org.apache.velocity.context.InternalHousekeepingContext#getMacroLibraries() 255 * @since 1.6 256 */ 257 public List getMacroLibraries() 258 { 259 return icb.getMacroLibraries(); 260 } 261 262 /* --- Context interface methods --- */ 263 264 /** 265 * @see org.apache.velocity.context.Context#put(java.lang.String, java.lang.Object) 266 */ 267 public Object put(String key, Object value) 268 { 269 return context.put( key , value ); 270 } 271 272 /** 273 * @see InternalWrapperContext#localPut(String, Object) 274 * @since 1.5 275 */ 276 public Object localPut(final String key, final Object value) 277 { 278 return put(key, value); 279 } 280 281 /** 282 * @see org.apache.velocity.context.Context#get(java.lang.String) 283 */ 284 public Object get(String key) 285 { 286 return context.get( key ); 287 } 288 289 /** 290 * @see org.apache.velocity.context.Context#containsKey(java.lang.Object) 291 */ 292 public boolean containsKey(Object key) 293 { 294 return context.containsKey( key ); 295 } 296 297 /** 298 * @see org.apache.velocity.context.Context#getKeys() 299 */ 300 public Object[] getKeys() 301 { 302 return context.getKeys(); 303 } 304 305 /** 306 * @see org.apache.velocity.context.Context#remove(java.lang.Object) 307 */ 308 public Object remove(Object key) 309 { 310 return context.remove( key ); 311 } 312 313 314 /* ---- InternalWrapperContext --- */ 315 316 /** 317 * returns the user data context that 318 * we are wrapping 319 * @return The internal user data context. 320 */ 321 public Context getInternalUserContext() 322 { 323 return context; 324 } 325 326 /** 327 * Returns the base context that we are 328 * wrapping. Here, its this, but for other thing 329 * like VM related context contortions, it can 330 * be something else 331 * @return The base context. 332 */ 333 public InternalContextAdapter getBaseContext() 334 { 335 return this; 336 } 337 338 /* ----- InternalEventContext ---- */ 339 340 /** 341 * @see org.apache.velocity.context.InternalEventContext#attachEventCartridge(org.apache.velocity.app.event.EventCartridge) 342 */ 343 public EventCartridge attachEventCartridge( EventCartridge ec ) 344 { 345 if (iec != null) 346 { 347 return iec.attachEventCartridge( ec ); 348 } 349 350 return null; 351 } 352 353 /** 354 * @see org.apache.velocity.context.InternalEventContext#getEventCartridge() 355 */ 356 public EventCartridge getEventCartridge() 357 { 358 if ( iec != null) 359 { 360 return iec.getEventCartridge( ); 361 } 362 363 return null; 364 } 365 } 366 367 368