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:54:12
    6    */
    7   package org.asyrinx.joey.gen.task;
    8
    9   import java.util.HashMap;
    10  import java.util.Map;
    11
    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  import org.asyrinx.brownie.core.lang.StringUtils;
    18
    19  /***
    20   * @author takeshi
    21   */
    22  public class VelocityOgnlHelper extends OgnlHelper {
    23
    24      public VelocityOgnlHelper(Context context) {
    25          super(toMap(context));
    26          this.context = context;
    27      }
    28
    29      private static Map toMap(Context context) {
    30          final Map result = new HashMap();
    31          final Object[] keys = context.getKeys();
    32          for (int i = 0; i < keys.length; i++)
    33              result.put(keys[i], context.get(keys[i].toString()));
    34          return result;
    35      }
    36
    37      final Context context;
    38
    39      static final String VELOCITY_VARIABLE_HEADER = "velocity_";
    40
    41      final Log log = LogFactory.getLog(this.getClass());
    42
    43      public Object getValue(String expression) throws OgnlException {
    44          final Object[] keys = this.context.getKeys();
    45          for (int i = 0; i < keys.length; i++) {
    46              final String key = keys[i].toString();
    47              expression = StringUtils.replace(expression, "$" + key, VELOCITY_VARIABLE_HEADER + key);
    48              expression = StringUtils.replace(expression, "${" + key + "}", VELOCITY_VARIABLE_HEADER + key);
    49          }
    50          if (expression.indexOf("${") > -1) {
    51              for (int i = 0; i < keys.length; i++) {
    52                  final String key = keys[i].toString();
    53                  log.debug("velocity context: " + key + "=" + this.context.get(key));
    54              }
    55          }
    56          return super.getValue(expression);
    57      }
    58
    59  }