KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > filters > DefaultObjectFilter


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

15 package org.josql.filters;
16
17 import java.io.FileFilter JavaDoc;
18 import java.io.File JavaDoc;
19
20 import com.gentlyweb.utils.StringUtils;
21
22 import org.josql.Query;
23 import org.josql.QueryParseException;
24
25 /**
26  * A general purpose object filter that uses a JoSQL statement to provide the filtering.
27  * The value returned by the {@link #accept(Object)} method is determined by executing the
28  * JoSQL WHERE clause passed in. A "wrapper" is created around the WHERE clause to make it
29  * a fully-formed JoSQL statement.
30  * <p>
31  * Last Modified By: $Author: barrygently $<br />
32  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
33  * Current Revision: $Revision: 1.1 $<br />
34  */

35 public class DefaultObjectFilter extends AbstractJoSQLFilter
36 {
37
38     private static String JavaDoc qWrapper = "SELECT * FROM [[CLASS]] WHERE [[WHERE]]";
39     private static String JavaDoc CLASS_TAG = "[[CLASS]]";
40     private static String JavaDoc WHERE_TAG = "[[WHERE]]";
41
42     private Class JavaDoc c = null;
43
44     /**
45      * Init this filter with the where clause, note the class specified will be used in the
46      * FROM clause.
47      *
48      * @param w The where clause.
49      * @param c The class of the objects to filter.
50      * @throws QueryParseException If there is an issue with the parsing of the query.
51      */

52     public DefaultObjectFilter (String JavaDoc w,
53                 Class JavaDoc c)
54                             throws QueryParseException
55     {
56
57     if (c == null)
58     {
59
60         throw new QueryParseException ("Class must be specified");
61
62     }
63
64     this.c = c;
65
66     String JavaDoc q = StringUtils.replaceString (qWrapper,
67                           DefaultObjectFilter.CLASS_TAG,
68                           c.getName ());
69
70     w = w.trim ();
71
72     if (w.toLowerCase ().startsWith ("where"))
73     {
74
75         w = w.substring (5);
76
77     }
78
79     q = StringUtils.replaceString (q,
80                        DefaultObjectFilter.WHERE_TAG,
81                        w);
82
83     this.setQuery (q);
84
85     }
86
87     /**
88      * Init this file filter with the query already built and parsed.
89      *
90      * @param q The query.
91      * @throws IllegalStateException If the Query object has not been parsed.
92      * @throws QueryParseException If the FROM class is not {@link File}.
93      */

94     public DefaultObjectFilter (Query q)
95                             throws IllegalStateException JavaDoc,
96                                    QueryParseException
97     {
98
99     this.c = q.getFromObjectClass ();
100
101     this.setQuery (q);
102
103     }
104
105     /**
106      * Apply the WHERE clause of the statement to the object passed in.
107      * If an exception is thrown by the execution of the WHERE clause the Query
108      * is marked as "dirty" and the where clause is no longer executed on the passed in
109      * objects (since it is likely that the WHERE clause will fail for all objects).
110      * You can get access to exception by using: {@link #getException()}.
111      *
112      * @param o The object to evaluate the WHERE on.
113      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code>.
114      */

115     public boolean accept (Object JavaDoc o)
116     {
117     
118     if (this.badQuery)
119     {
120
121         return false;
122
123     }
124
125     try
126     {
127
128         return this.q.getWhereClause ().isTrue (o,
129                             this.q);
130
131     } catch (Exception JavaDoc e) {
132
133         this.badQuery = true;
134
135         this.exp = e;
136
137     }
138
139     return false;
140
141     }
142
143     /**
144      * Get the class the Query expects to operate on.
145      *
146      * @return The class.
147      */

148     public Class JavaDoc getExpectedClass ()
149     {
150
151     return this.c;
152
153     }
154
155 }
156
Popular Tags