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.task;
8   
9   import java.util.Map;
10  
11  import ognl.ObjectPropertyAccessor;
12  import ognl.OgnlException;
13  
14  import org.apache.velocity.context.Context;
15  
16  /***
17   * @author takeshi
18   */
19  public class VelocityVariableAccessor extends ObjectPropertyAccessor {
20  
21      public VelocityVariableAccessor(Context context) {
22          super();
23          this.context = context;
24      }
25  
26      private final Context context;
27  
28      public Object getProperty(Map context, Object target, Object name) throws OgnlException {
29          final String key = toKey(name);
30          if (key != null)
31              return this.context.get(key);
32          return super.getProperty(context, target, name);
33      }
34  
35      public void setProperty(Map context, Object target, Object name, Object value) throws OgnlException {
36          final String key = toKey(name);
37          if (key != null)
38              this.context.put(key, value);
39          super.setProperty(context, target, name, value);
40      }
41  
42      private static final String VELOCITY_MARK = "$";
43  
44      private static final String VELOCITY_BRACKET_START = "{";
45  
46      private static final String VELOCITY_BRACKET_END = "}";
47  
48      private static final String toKey(Object name) {
49          System.out.println("VelocityVariableAccessor: name=" + name);
50          if (name == null)
51              return null;
52          String key = name.toString();
53          if (!name.toString().startsWith(VELOCITY_MARK))
54              return null;
55          key = key.substring(1);
56          if (key.startsWith(VELOCITY_BRACKET_START) && key.endsWith(VELOCITY_BRACKET_END))
57              key = key.substring(1, key.length() - 2);
58          System.out.println("VelocityVariableAccessor: key=" + key);
59          return key;
60      }
61  
62  }