View Javadoc

1   /*
2    * joey-gen and its relative products are published under the terms
3    * of the Apache Software License.
4    * 
5    * Created on 2004/12/20 8:01:11
6    */
7   package org.asyrinx.joey.gen.core;
8   
9   import java.util.Map;
10  
11  import ognl.MapPropertyAccessor;
12  import ognl.OgnlException;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.velocity.context.Context;
17  
18  /***
19   * @author takeshi
20   */
21  public class VelocityOgnlAccessor extends MapPropertyAccessor {
22  
23      public VelocityOgnlAccessor(Context context) {
24          super();
25          this.context = context;
26      }
27  
28      private final Context context;
29  
30      public Object getProperty(Map context, Object target, Object name) throws OgnlException {
31          final String key = toKey(name);
32          if (key != null) {
33              log.debug("key '" + key + "' found in velocity context.");
34              return this.context.get(key);
35          }
36          return super.getProperty(context, target, name);
37      }
38  
39      public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
40          final String key = toKey(name);
41          if (key != null)
42              this.context.put(key, value);
43          super.setProperty(context, target, name, value);
44      }
45  
46      static final Log log = LogFactory.getLog(VelocityOgnlAccessor.class);
47  
48      private static final String toKey(Object name) {
49          if (name == null)
50              return null;
51          String key = name.toString();
52          if (!key.startsWith(VelocityOgnlHelper.VELOCITY_VARIABLE_HEADER))
53              return null;
54          key = key.substring(VelocityOgnlHelper.VELOCITY_VARIABLE_HEADER.length());
55          return key;
56      }
57  
58  }