Source Code Cross Referenced for ExpressionEvaluator.java in  » Development » JoSQL » org » josql » utils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » JoSQL » org.josql.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.