1 15 package org.josql.contrib; 16 17 import java.io.File ; 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 85 public class JoSQLAntFileSelector extends Query implements ExtendFileSelector 86 { 87 88 private static String sqlPrefix = "SELECT * FROM java.io.File WHERE "; 89 90 public static final String WHERE = "where"; 91 public static final String DEBUG = "debug"; 92 93 private Exception parseExp = null; 94 private boolean configured = false; 95 private String 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 buf = new StringBuffer (); 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 e) { 158 159 this.parseExp = e; 160 161 } 162 163 } 164 165 } 166 167 } 168 169 public boolean isSelected (File basedir, 170 String filename, 171 File 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 e) { 227 228 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 |