View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.geronimo.genesis.util;
21  
22  import java.util.Map;
23  
24  import org.apache.commons.jexl.Expression;
25  import org.apache.commons.jexl.ExpressionFactory;
26  import org.apache.commons.jexl.JexlContext;
27  import org.apache.commons.jexl.JexlHelper;
28  import org.apache.commons.jexl.resolver.FlatResolver;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * Parses expressions using <a href="http://jakarta.apache.org/commons/jexl/">Commons Jexl</a>.
34   *
35   * @version $Rev: 462660 $ $Date: 2006-10-10 18:57:41 -0700 (Tue, 10 Oct 2006) $
36   */
37  public class ExpressionParser
38  {
39      private static final Log log = LogFactory.getLog(ExpressionParser.class);
40  
41      protected JexlContext context;
42  
43      public ExpressionParser(final Map vars) {
44          if (vars == null) {
45              throw new IllegalArgumentException("vars");
46          }
47  
48          context = JexlHelper.createContext();
49          context.setVars(vars);
50  
51          if (log.isTraceEnabled()) {
52              log.trace("Using variables: " + context.getVars());
53          }
54      }
55  
56      public ExpressionParser() {
57          this(System.getProperties());
58      }
59  
60      public Map getVariables() {
61          return context.getVars();
62      }
63  
64      public Object getVariable(final Object name) {
65          if (name == null) {
66              throw new IllegalArgumentException("name");
67          }
68  
69          return getVariables().get(name);
70      }
71  
72      public Object setVariable(final Object name, final Object value) {
73          if (name == null) {
74              throw new IllegalArgumentException("name");
75          }
76  
77          return getVariables().put(name, value);
78      }
79  
80      public Object unsetVariable(final Object name) {
81          if (name == null) {
82              throw new IllegalArgumentException("name");
83          }
84  
85          return getVariables().remove(name);
86      }
87  
88      public void addVariables(final Map map) {
89          if (map == null) {
90              throw new IllegalArgumentException("map");
91          }
92  
93          getVariables().putAll(map);
94      }
95  
96      private FlatResolver resolver = new FlatResolver(true);
97  
98      protected Expression createExpression(final String expression) throws Exception {
99          // assert expression != null;
100 
101         Expression expr = ExpressionFactory.createExpression(expression);
102         expr.addPreResolver(resolver);
103 
104         return expr;
105     }
106 
107     public Object evaluate(final String expression) throws Exception {
108         if (expression == null) {
109             throw new IllegalArgumentException("expression");
110         }
111 
112         boolean trace = log.isTraceEnabled();
113         if (trace) {
114             log.trace("Evaluating expression: " + expression);
115         }
116 
117         Expression expr = createExpression(expression);
118         Object obj = expr.evaluate(context);
119         if (trace) {
120             log.trace("Result: " + obj);
121         }
122 
123         return obj;
124     }
125 
126     public String parse(final String input) {
127         if (input == null) {
128             throw new IllegalArgumentException("input");
129         }
130 
131         boolean trace = log.isTraceEnabled();
132         if (trace) {
133             log.trace("Parsing input: " + input);
134         }
135 
136         StringBuffer buff = new StringBuffer();
137 
138         int cur = 0;
139         int prefixLoc;
140         int suffixLoc;
141 
142         while (cur < input.length()) {
143             prefixLoc = input.indexOf("${", cur);
144 
145             if (prefixLoc < 0) {
146                 break;
147             }
148 
149             suffixLoc = input.indexOf("}", prefixLoc);
150             if (suffixLoc < 0) {
151                 throw new RuntimeException("Missing '}': " + input);
152             }
153 
154             String expr = input.substring(prefixLoc + 2, suffixLoc);
155             buff.append(input.substring(cur, prefixLoc));
156 
157             try {
158                 buff.append(evaluate(expr));
159             }
160             catch (Exception e) {
161                 throw new RuntimeException("Failed to evaluate: " + expr, e);
162             }
163 
164             cur = suffixLoc + 1;
165         }
166 
167         buff.append(input.substring(cur));
168 
169         if (trace) {
170             log.trace("Parsed result: " + buff);
171         }
172 
173         return buff.toString();
174     }
175 
176     public String parse(final String input, final boolean trim) {
177         String output = parse(input);
178         if (trim && output != null) {
179             output = output.trim();
180         }
181 
182         return output;
183     }
184 }