1 package org.apache.velocity.context; 2 3 18 19 import java.util.HashMap ; 20 21 import org.apache.velocity.runtime.RuntimeServices; 22 import org.apache.velocity.runtime.RuntimeConstants; 23 import org.apache.velocity.runtime.directive.VMProxyArg; 24 import org.apache.velocity.util.introspection.IntrospectionCacheData; 25 import org.apache.velocity.runtime.resource.Resource; 26 import org.apache.velocity.app.event.EventCartridge; 27 28 42 public class VMContext implements InternalContextAdapter 43 { 44 45 HashMap vmproxyhash = new HashMap (); 46 47 48 HashMap localcontext = new HashMap (); 49 50 51 InternalContextAdapter innerContext = null; 52 53 54 InternalContextAdapter wrappedContext = null; 55 56 57 private boolean localcontextscope = false; 58 59 62 public VMContext( InternalContextAdapter inner, RuntimeServices rsvc ) 63 { 64 localcontextscope = rsvc.getBoolean( RuntimeConstants.VM_CONTEXT_LOCALSCOPE, false ); 65 66 wrappedContext = inner; 67 innerContext = inner.getBaseContext(); 68 } 69 70 73 public Context getInternalUserContext() 74 { 75 return innerContext.getInternalUserContext(); 76 } 77 78 public InternalContextAdapter getBaseContext() 79 { 80 return innerContext.getBaseContext(); 81 } 82 83 92 public void addVMProxyArg( VMProxyArg vmpa ) 93 { 94 98 99 String key = vmpa.getContextReference(); 100 101 if ( vmpa.isConstant() ) 102 { 103 localcontext.put( key, vmpa.getObject( wrappedContext ) ); 104 } 105 else 106 { 107 vmproxyhash.put( key, vmpa ); 108 } 109 } 110 111 118 public Object put(String key, Object value) 119 { 120 123 124 VMProxyArg vmpa = (VMProxyArg) vmproxyhash.get( key ); 125 126 if( vmpa != null) 127 { 128 return vmpa.setObject( wrappedContext, value ); 129 } 130 else 131 { 132 if(localcontextscope) 133 { 134 138 139 return localcontext.put( key, value ); 140 } 141 else 142 { 143 146 147 if (localcontext.containsKey( key )) 148 { 149 return localcontext.put( key, value); 150 } 151 else 152 { 153 156 157 return innerContext.put( key, value ); 158 } 159 } 160 } 161 } 162 163 169 public Object get( String key ) 170 { 171 174 175 Object o = null; 176 177 VMProxyArg vmpa = (VMProxyArg) vmproxyhash.get( key ); 178 179 if( vmpa != null ) 180 { 181 o = vmpa.getObject( wrappedContext ); 182 } 183 else 184 { 185 if(localcontextscope) 186 { 187 191 192 o = localcontext.get( key ); 193 } 194 else 195 { 196 199 200 o = localcontext.get( key ); 201 202 if ( o == null) 203 { 204 207 208 o = innerContext.get( key ); 209 } 210 } 211 } 212 213 return o; 214 } 215 216 219 public boolean containsKey(Object key) 220 { 221 return false; 222 } 223 224 227 public Object [] getKeys() 228 { 229 return vmproxyhash.keySet().toArray(); 230 } 231 232 235 public Object remove(Object key) 236 { 237 return vmproxyhash.remove( key ); 238 } 239 240 public void pushCurrentTemplateName( String s ) 241 { 242 innerContext.pushCurrentTemplateName( s ); 243 } 244 245 public void popCurrentTemplateName() 246 { 247 innerContext.popCurrentTemplateName(); 248 } 249 250 public String getCurrentTemplateName() 251 { 252 return innerContext.getCurrentTemplateName(); 253 } 254 255 public Object [] getTemplateNameStack() 256 { 257 return innerContext.getTemplateNameStack(); 258 } 259 260 public IntrospectionCacheData icacheGet( Object key ) 261 { 262 return innerContext.icacheGet( key ); 263 } 264 265 public void icachePut( Object key, IntrospectionCacheData o ) 266 { 267 innerContext.icachePut( key, o ); 268 } 269 270 public EventCartridge attachEventCartridge( EventCartridge ec ) 271 { 272 return innerContext.attachEventCartridge( ec ); 273 } 274 275 public EventCartridge getEventCartridge() 276 { 277 return innerContext.getEventCartridge(); 278 } 279 280 281 public void setCurrentResource( Resource r ) 282 { 283 innerContext.setCurrentResource( r ); 284 } 285 286 public Resource getCurrentResource() 287 { 288 return innerContext.getCurrentResource(); 289 } 290 } 291 292 293 294 | Popular Tags |