001: /*
002: * Copyright 2004-2007 Gary Bentley
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may
005: * not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015: package org.josql.utils;
016:
017: import java.util.List;
018: import java.util.ArrayList;
019:
020: import org.josql.Query;
021: import org.josql.QueryParseException;
022: import org.josql.QueryExecutionException;
023:
024: import org.josql.expressions.Expression;
025:
026: /**
027: * This class can be used as a convenient way of evaluating an expression
028: * without having to use the Query object itself.
029: * <p>
030: * In this way you can easily evaluate JoSQL expressions against objects.
031: * <p>
032: * Usage:
033: * <p>
034: * Use the static methods to evaluate the expression in one call, for instance to
035: * find out details about a file:
036: * <p>
037: * String exp = "path + ', size: ' + formatNumber(length) + ', last modified: ' + formatDate(lastModified)<br />
038: * String details = ExpressionEvaluator.getValue (exp, new File ('/home/me/myfile.txt'));
039: */
040: public class ExpressionEvaluator {
041:
042: private Expression where = null;
043: private Query q = null;
044:
045: /**
046: * Create a new expression evaluator.
047: *
048: * @param exp The expression to be evaluated.
049: * @param cl The class of the object(s) that the expression will be
050: * evaluated against.
051: * @throws QueryParseException If the expression cannot be parsed.
052: */
053: public ExpressionEvaluator(String exp, Class cl)
054: throws QueryParseException {
055:
056: this (exp, cl, null);
057:
058: }
059:
060: /**
061: * Create a new expression evaluator.
062: *
063: * @param exp The expression to be evaluated.
064: * @param cl The class of the object(s) that the expression will be
065: * evaluated against.
066: * @param fhs A list of function handlers that contain functions that will
067: * be used by the expression, can be null.
068: * @throws QueryParseException If the expression cannot be parsed.
069: */
070: public ExpressionEvaluator(String exp, Class cl, List fhs)
071: throws QueryParseException {
072:
073: Query q = new Query();
074:
075: if (fhs != null) {
076:
077: for (int i = 0; i < fhs.size(); i++) {
078:
079: q.addFunctionHandler(fhs.get(i));
080:
081: }
082:
083: }
084:
085: q.parse("SELECT * FROM " + cl.getName() + " WHERE " + exp);
086:
087: this .q = q;
088:
089: this .where = q.getWhereClause();
090:
091: }
092:
093: /**
094: * Get the query associated with the expression, use this to setup
095: * bind variables, function handlers and so on, which of course must
096: * be setup prior to evaluating the expression.
097: *
098: * @return The Query object.
099: */
100: public Query getQuery() {
101:
102: return this .q;
103:
104: }
105:
106: /**
107: * Evaluate the expression against the object passed in.
108: *
109: * @param o The object to evaluate the expression against.
110: * @return The value of calling Expression.isTrue (Query, Object).
111: * @throws QueryExecutionException If the expression cannot be executed.
112: */
113: public boolean isTrue(Object o) throws QueryExecutionException {
114:
115: if (o == null) {
116:
117: throw new NullPointerException("Object passed in is null.");
118:
119: }
120:
121: return this .where.isTrue(o, this .q);
122:
123: }
124:
125: /**
126: * Evaluate the expression against the list of objects passed in and
127: * return the value.
128: *
129: * @param l The list of objects to evaluate the expression against.
130: * @return The values gained when evaluating the expression against all
131: * the objects in the list.
132: * @throws QueryExecutionException If the expression cannot be executed.
133: */
134: public List getValues(List l) throws QueryExecutionException {
135:
136: if (l == null) {
137:
138: throw new NullPointerException("List is null");
139:
140: }
141:
142: int s = l.size();
143:
144: List ret = new ArrayList(s);
145:
146: for (int i = s - 1; i > -1; i--) {
147:
148: ret.set(i, this .getValue(l.get(i)));
149:
150: }
151:
152: return ret;
153:
154: }
155:
156: /**
157: * Evaluate the expression against the object passed in and return the
158: * value.
159: *
160: * @param o The object to evaluate the expression against.
161: * @return The value gained when evaluating the expression against
162: * the object.
163: * @throws QueryExecutionException If the expression cannot be executed.
164: */
165: public Object getValue(Object o) throws QueryExecutionException {
166:
167: return this .where.getValue(o, this .q);
168:
169: }
170:
171: /**
172: * Evaluate the expression against the object passed in.
173: *
174: * @param exp A string representation of the expression to evaluate.
175: * @param o The object to evaluate the expression against.
176: * @return The value of calling Expression.isTrue (Query, Object).
177: * @throws QueryParseException If the expression cannot be parsed.
178: * @throws QueryExecutionException If the expression cannot be executed.
179: */
180: public static boolean isTrue(String exp, Object o)
181: throws QueryParseException, QueryExecutionException {
182:
183: if (o == null) {
184:
185: throw new NullPointerException("Object passed in is null.");
186:
187: }
188:
189: ExpressionEvaluator ee = new ExpressionEvaluator(exp, o
190: .getClass());
191:
192: return ee.isTrue(o);
193:
194: }
195:
196: /**
197: * Evaluate the expression against the list of objects passed in and
198: * return the value.
199: *
200: * @param exp A string representation of the expression to evaluate.
201: * @param l The list of objects to evaluate the expression against.
202: * @return The values gained when evaluating the expression against all
203: * the objects in the list.
204: * @throws QueryParseException If the expression cannot be parsed.
205: * @throws QueryExecutionException If the expression cannot be executed.
206: */
207: public static List getValues(String exp, List l)
208: throws QueryParseException, QueryExecutionException {
209:
210: if (l == null) {
211:
212: throw new NullPointerException("List is null");
213:
214: }
215:
216: if (l.size() == 0) {
217:
218: return new ArrayList();
219:
220: }
221:
222: Class c = null;
223:
224: for (int i = 0; i < l.size(); i++) {
225:
226: Object o = l.get(i);
227:
228: if (o != null) {
229:
230: c = o.getClass();
231:
232: if (c != null) {
233:
234: break;
235:
236: }
237:
238: }
239:
240: }
241:
242: if (c == null) {
243:
244: throw new NullPointerException(
245: "All objects in the list are null");
246:
247: }
248:
249: ExpressionEvaluator ee = new ExpressionEvaluator(exp, c);
250:
251: return ee.getValues(l);
252:
253: }
254:
255: /**
256: * Evaluate the expression against the object passed in and return the
257: * value.
258: *
259: * @param exp A string representation of the expression to evaluate.
260: * @param o The object to evaluate the expression against.
261: * @return The value gained when evaluating the expression against
262: * the object.
263: * @throws QueryParseException If the expression cannot be parsed.
264: * @throws QueryExecutionException If the expression cannot be executed.
265: */
266: public static Object getValue(String exp, Object o)
267: throws QueryParseException, QueryExecutionException {
268:
269: if (o == null) {
270:
271: throw new NullPointerException("Object passed in is null.");
272:
273: }
274:
275: ExpressionEvaluator ee = new ExpressionEvaluator(exp, o
276: .getClass());
277:
278: return ee.getValue(o);
279:
280: }
281:
282: }
|