KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.josql.Query;
21 import org.josql.QueryParseException;
22
23 /**
24  * A {@link FileFilter} that uses a JoSQL statement to provide the filtering.
25  * The value returned by the {@link #accept(File)} method is determined by executing the
26  * WHERE clause of a JoSQL statement on each File passed in.
27  * <p>
28  * Since this uses a sub-set of the JoSQL functionality certain restrictions apply:
29  * <ul>
30  * <li>The SELECT clause is ignored.</li>
31  * <li>The object defined in the FROM clause <b>must</b> be java.io.File</li>
32  * <li>EXECUTE ON functions are not supported here since there is no way to get the entire set
33  * of objects to work on.</li>
34  * <li>ORDER BY, GROUP BY, HAVING and LIMIT clauses are ignored.</li>
35  * </ul>
36  * Examples:
37  * <p>
38  * <pre>
39  * SELECT *
40  * FROM {@link File java.io.File}
41  * WHERE name $LIKE '%.html'
42  * AND lastModified > {@link org.josql.functions.ConversionFunctions#toDateMillis(String,String) toDateMillis}('12-04-2004')
43  * AND file
44  * </pre>
45  * <p>
46  * Last Modified By: $Author: barrygently $<br />
47  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
48  * Current Revision: $Revision: 1.1 $<br />
49  */

50 public class JoSQLFileFilter extends AbstractJoSQLFilter implements FileFilter JavaDoc
51 {
52
53     /**
54      * Init this file filter with the query.
55      *
56      * @param q The query.
57      * @throws QueryParseException If there is an issue with the parsing of the query,
58      * or if the FROM class is not {@link File}.
59      */

60     public JoSQLFileFilter (String JavaDoc q)
61                         throws QueryParseException
62     {
63
64     super (q);
65
66     }
67
68     /**
69      * Init this file filter with the query already built and parsed.
70      *
71      * @param q The query.
72      * @throws IllegalStateException If the Query object has not been parsed.
73      * @throws QueryParseException If the FROM class is not {@link File}.
74      */

75     public JoSQLFileFilter (Query q)
76                         throws IllegalStateException JavaDoc,
77                                QueryParseException
78     {
79
80     super (q);
81
82     }
83
84     public boolean accept (Object JavaDoc o)
85     {
86
87     if (!(o instanceof File JavaDoc))
88     {
89
90         throw new IllegalArgumentException JavaDoc ("Expected object to be of type: " +
91                         File JavaDoc.class.getName () +
92                         ", got: " +
93                         o.getClass ().getName ());
94
95     }
96
97     return this.accept ((File JavaDoc) o);
98
99     }
100
101     /**
102      * Apply the WHERE clause of the statement to the {@link File} passed in.
103      * If an exception is thrown by the execution of the WHERE clause the Query
104      * is marked as "dirty" and the where clause is no longer executed on the passed in
105      * files (since it is likely that the WHERE clause will fail for all File objects).
106      * You can get access to exception by using: {@link #getException()}.
107      *
108      * @param f The file to evaluate the WHERE on.
109      * @return <code>true</code> if the WHERE clause evaluates to <code>true</code>.
110      */

111     public boolean accept (File JavaDoc f)
112     {
113     
114     if (this.badQuery)
115     {
116
117         return false;
118
119     }
120
121     try
122     {
123
124         return this.q.getWhereClause ().isTrue (f,
125                             this.q);
126
127     } catch (Exception JavaDoc e) {
128
129         this.badQuery = true;
130
131         this.exp = e;
132
133     }
134
135     return false;
136
137     }
138
139     /**
140      * Always returns {@link File}.
141      *
142      * @return The file class.
143      */

144     public Class JavaDoc getExpectedClass ()
145     {
146
147     return File JavaDoc.class;
148
149     }
150
151 }
152
Popular Tags