KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > expression > Expression


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.expression;
17
18 import scriptella.core.SystemException;
19 import scriptella.spi.ParametersCallback;
20
21 import java.util.Map JavaDoc;
22 import java.util.WeakHashMap JavaDoc;
23
24
25 /**
26  * Base class for all expressions.
27  *
28  * @author Fyodor Kupolov
29  * @version 1.0
30  */

31 public abstract class Expression {
32     private static final Map JavaDoc<String JavaDoc, Expression> EXPRESSIONS_CACHE = new WeakHashMap JavaDoc<String JavaDoc, Expression>();
33     private String JavaDoc expression;
34
35     protected Expression(String JavaDoc expression) {
36         this.expression = expression;
37     }
38
39     public abstract Object JavaDoc evaluate(final ParametersCallback callback)
40             throws EvaluationException;
41
42     public String JavaDoc getExpression() {
43         return expression;
44     }
45
46     public static Expression compile(final String JavaDoc expression)
47             throws ParseException {
48         Expression ex = EXPRESSIONS_CACHE.get(expression);
49
50         if (ex != null) {
51             return ex;
52         }
53
54         ex = new JexlExpression(expression);
55         EXPRESSIONS_CACHE.put(expression, ex);
56
57         return ex;
58     }
59
60     public static class ParseException extends SystemException {
61         public ParseException() {
62         }
63
64         public ParseException(String JavaDoc message) {
65             super(message);
66         }
67
68         public ParseException(String JavaDoc message, Throwable JavaDoc cause) {
69             super(message, cause);
70         }
71
72         public ParseException(Throwable JavaDoc cause) {
73             super(cause);
74         }
75     }
76
77     public static class EvaluationException extends SystemException {
78         public EvaluationException() {
79         }
80
81         public EvaluationException(String JavaDoc message) {
82             super(message);
83         }
84
85         public EvaluationException(String JavaDoc message, Throwable JavaDoc cause) {
86             super(message, cause);
87         }
88
89         public EvaluationException(Throwable JavaDoc cause) {
90             super(cause);
91         }
92     }
93 }
94
Popular Tags