KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > contrib > JoSQLAntFileSelector


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.contrib;
16
17 import java.io.File JavaDoc;
18
19 import org.apache.tools.ant.types.selectors.ExtendFileSelector;
20
21 import org.apache.tools.ant.types.Parameter;
22
23 import org.apache.tools.ant.BuildException;
24
25 import org.josql.Query;
26 import org.josql.QueryParseException;
27
28 /**
29  * A custom file selector for use with Ant.
30  * <p>
31  * See: <a target="_blank" HREF="http://ant.apache.org/manual/CoreTypes/selectors.html#customselect">Custom Ant Selectors</a> for more details.
32  * <p>
33  * Allows a JoSQL <a HREF="http://josql.sourceforge.net/manual/3-5.html" target="_blank">WHERE clause</a>
34  * to be applied to each file passed to the selector {@link #isSelected(File,String,File)}.
35  * <p>
36  * An obvious question to ask here is "why do I need this when Ant has lots of custom file selectors".
37  * Well, in short, you don't "need" this selector, but I've found that trying to remember all the
38  * custom elements and their attributes can be painful and doesn't give me all the power needed to
39  * select the files I want. This custom selector however does.
40  * <p>
41  * The selector supports the following "param"s.
42  * <ul>
43  * <li><b>where</b> - multiple <b>where</b> params are supported, the value of each param is concatenated
44  * with the others to form the full WHERE clause. It is up to you to decide how you split up
45  * your where clause.</li>
46  * <li><b>debug</b> - when set to <b>on</b> (case-insensitive) debug information about the WHERE clause
47  * actually used and for each file passed to {@link #isSelected(File,String,File)} what the
48  * WHERE clause evaluated to, either <code>true</code> or <code>false</code>.</li>
49  * </ul>
50  * <p>
51  * Usage:
52  * <p>
53  * A typical usage may be:
54  * <p>
55  * <pre>
56  * &lt;fileset dir="myDir"
57  * includes="**">
58  * &lt;custom classpath="[PATH_TO_JOSQL]/JoSQL-1.0.jar:[PATH_TO_JOSQL]/3rd-party-jars/gentlyWEB-utils-1.1.jar"
59  * classname="org.josql.contrib.JoSQLAntFileSelector">
60  * &lt;param name="debug" value="on" />
61  * &lt;param name="where" value="toDate (lastModified) > toDate ('22/Sep/2005')" />
62  * &lt;param name="where" value="AND length > 10000" />
63  * &lt;/custom>
64  * &lt;/fileset>
65  * </pre>
66  * <p>
67  * This will create a file set containing all the files modified after 22/Sep/2005 and have a length greater
68  * than 10000 bytes.
69  * <p>
70  * Compare this to how it would be "normally" be done with standard Ant fileset selectors:
71  * <pre>
72  * &lt;fileset dir="myDir"
73  * includes="**">
74  * &lt;date datetime="22/Sep/2005"
75  * pattern="dd/MMM/yyyy"
76  * when="after" />
77  * &lt;size value="10000"
78  * when="more" />
79  * &lt;/fileset>
80  * </pre>
81  * <p>
82  * Of course it is perfectly possible to mix and match this selector and other custom selectors or
83  * the built-in selectors.
84  */

85 public class JoSQLAntFileSelector extends Query implements ExtendFileSelector
86 {
87
88     private static String JavaDoc sqlPrefix = "SELECT * FROM java.io.File WHERE ";
89     
90     public static final String JavaDoc WHERE = "where";
91     public static final String JavaDoc DEBUG = "debug";
92
93     private Exception JavaDoc parseExp = null;
94     private boolean configured = false;
95     private String JavaDoc where = null;
96     private boolean debug = false;
97     private boolean shownWhere = false;
98     
99     public JoSQLAntFileSelector ()
100     {
101
102     }
103
104     public void setParameters (Parameter[] parms)
105     {
106
107     if (parms != null)
108     {
109
110         StringBuffer JavaDoc buf = new StringBuffer JavaDoc ();
111
112         for (int i = 0; i < parms.length; i++)
113         {
114
115         Parameter p = parms[i];
116
117         if (p.getName ().toLowerCase ().equals (JoSQLAntFileSelector.DEBUG))
118         {
119
120             if (p.getValue ().toLowerCase ().equals ("on"))
121             {
122
123             this.debug = true;
124
125             }
126
127         }
128
129         if (p.getName ().toLowerCase ().equals (JoSQLAntFileSelector.WHERE))
130         {
131
132             if (buf.length () > 0)
133             {
134
135             buf.append (" ");
136
137             }
138
139             buf.append (p.getValue ().trim ());
140
141         }
142
143         }
144
145         if (buf.length () > 0)
146         {
147
148         try
149         {
150
151             this.where = buf.toString ();
152
153             this.parse (JoSQLAntFileSelector.sqlPrefix + buf.toString ());
154
155             this.configured = true;
156
157         } catch (Exception JavaDoc e) {
158
159             this.parseExp = e;
160
161         }
162
163         }
164
165     }
166
167     }
168
169     public boolean isSelected (File JavaDoc basedir,
170                    String JavaDoc filename,
171                    File JavaDoc file)
172                                throws BuildException
173     {
174
175     if (!this.configured)
176     {
177
178         if (this.parseExp != null)
179         {
180
181         throw new BuildException ("Unable to init query with where clause: " +
182                       this.where +
183                       ", reason: " + this.parseExp.getMessage (),
184                       this.parseExp);
185
186         }
187
188         throw new BuildException ("Selector is not configured, expected to find a parameter with name: " +
189                       JoSQLAntFileSelector.WHERE +
190                       " that provides the where clause to evaluate against each file.");
191
192     }
193
194     if (this.debug)
195     {
196
197         if (!this.shownWhere)
198         {
199
200         System.out.println ("Using WHERE clause: " + this.where);
201
202         this.shownWhere = true;
203
204         }
205
206     }
207
208     try
209     {
210
211         boolean v = this.getWhereClause ().isTrue (file,
212                                this);
213
214         if (this.debug)
215         {
216
217         System.out.println ("WHERE = " +
218                     v +
219                     " for file: " +
220                     file);
221
222         }
223
224         return v;
225
226     } catch (Exception JavaDoc e) {
227
228         //e.printStackTrace ();
229

230         throw new BuildException ("Unable to execute where clause: " +
231                       this.getWhereClause () +
232                       " on file: " +
233                       file +
234                       ", reason: " + e.getMessage (),
235                       e);
236
237     }
238
239     }
240
241 }
242
Popular Tags