| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package net.sf.antcontrib.logic; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.util.Vector; |
| 20 |
|
| 21 |
import org.apache.tools.ant.BuildException; |
| 22 |
import org.apache.tools.ant.Task; |
| 23 |
import org.apache.tools.ant.types.Path; |
| 24 |
import org.apache.tools.ant.types.Reference; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public class TimestampSelector extends Task |
| 69 |
{ |
| 70 |
private static final String AGE_ELDEST = "eldest"; |
| 71 |
private static final String AGE_YOUNGEST = "youngest"; |
| 72 |
|
| 73 |
private String property; |
| 74 |
private Path path; |
| 75 |
private String outputSetId; |
| 76 |
private int count = 1; |
| 77 |
private char pathSep = ','; |
| 78 |
private String age = AGE_YOUNGEST; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public TimestampSelector() |
| 85 |
{ |
| 86 |
super(); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public void doFileSetExecute(String paths[]) |
| 91 |
throws BuildException |
| 92 |
{ |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
public void sort(Vector array) |
| 98 |
{ |
| 99 |
sort(array, 0, array.size() - 1); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
protected void sort(Vector array, int start, int end) |
| 104 |
{ |
| 105 |
int p; |
| 106 |
if (end > start) |
| 107 |
{ |
| 108 |
p = partition(array, start, end); |
| 109 |
sort(array, start, p-1); |
| 110 |
sort(array, p+1, end); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
protected int compare(File a, File b) |
| 115 |
{ |
| 116 |
if (age.equalsIgnoreCase(AGE_ELDEST)) |
| 117 |
return new Long(a.lastModified()).compareTo(new Long(b.lastModified())); |
| 118 |
else |
| 119 |
return new Long(b.lastModified()).compareTo(new Long(a.lastModified())); |
| 120 |
} |
| 121 |
|
| 122 |
protected int partition(Vector array, int start, int end) |
| 123 |
{ |
| 124 |
int left, right; |
| 125 |
File partitionElement; |
| 126 |
|
| 127 |
partitionElement = (File)array.elementAt(end); |
| 128 |
|
| 129 |
left = start - 1; |
| 130 |
right = end; |
| 131 |
for (;;) |
| 132 |
{ |
| 133 |
while (compare(partitionElement, (File)array.elementAt(++left)) == 1) |
| 134 |
{ |
| 135 |
if (left == end) break; |
| 136 |
} |
| 137 |
while (compare(partitionElement, (File)array.elementAt(--right)) == -1) |
| 138 |
{ |
| 139 |
if (right == start) break; |
| 140 |
} |
| 141 |
if (left >= right) break; |
| 142 |
swap(array, left, right); |
| 143 |
} |
| 144 |
swap(array, left, end); |
| 145 |
|
| 146 |
return left; |
| 147 |
} |
| 148 |
|
| 149 |
protected void swap(Vector array, int i, int j) |
| 150 |
{ |
| 151 |
Object temp; |
| 152 |
|
| 153 |
temp = array.elementAt(i); |
| 154 |
array.setElementAt(array.elementAt(j), i); |
| 155 |
array.setElementAt(temp, j); |
| 156 |
} |
| 157 |
|
| 158 |
public void execute() |
| 159 |
throws BuildException |
| 160 |
{ |
| 161 |
if (property == null && outputSetId == null) |
| 162 |
throw new BuildException("Property or OutputSetId must be specified."); |
| 163 |
if (path == null) |
| 164 |
throw new BuildException("A path element or pathref attribute must be specified."); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
String s[] = path.list(); |
| 169 |
Vector v = new Vector(); |
| 170 |
for (int i=0;i<s.length;i++) |
| 171 |
{ |
| 172 |
File f = new File(s[i]); |
| 173 |
if (f.exists()) |
| 174 |
v.addElement(f); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
sort(v); |
| 179 |
|
| 180 |
|
| 181 |
Vector v2 = new Vector(); |
| 182 |
int sz = v.size(); |
| 183 |
for (int i=0;i<sz && i<count;i++) |
| 184 |
v2.add(v.elementAt(i)); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
Path path = new Path(getProject()); |
| 191 |
sz = v2.size(); |
| 192 |
for (int i=0;i<sz;i++) |
| 193 |
{ |
| 194 |
File f = (File)(v.elementAt(i)); |
| 195 |
Path p = new Path(getProject(), f.getAbsolutePath()); |
| 196 |
path.addExisting(p); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
if (outputSetId != null) |
| 201 |
{ |
| 202 |
|
| 203 |
getProject().addReference(outputSetId, path); |
| 204 |
} |
| 205 |
else |
| 206 |
{ |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
String paths[] = path.list(); |
| 211 |
StringBuffer sb = new StringBuffer(); |
| 212 |
for (int i=0;i<paths.length;i++) |
| 213 |
{ |
| 214 |
if (i != 0) sb.append(pathSep); |
| 215 |
sb.append(paths[i]); |
| 216 |
} |
| 217 |
|
| 218 |
if (paths.length != 0) |
| 219 |
getProject().setProperty(property, sb.toString()); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
public void setProperty(String property) |
| 225 |
{ |
| 226 |
if (outputSetId != null) |
| 227 |
throw new BuildException("Cannot set both Property and OutputSetId."); |
| 228 |
|
| 229 |
this.property = property; |
| 230 |
} |
| 231 |
|
| 232 |
public void setCount(int count) |
| 233 |
{ |
| 234 |
this.count = count; |
| 235 |
} |
| 236 |
|
| 237 |
public void setAge(String age) |
| 238 |
{ |
| 239 |
if (age.equalsIgnoreCase(AGE_ELDEST) || |
| 240 |
age.equalsIgnoreCase(AGE_YOUNGEST)) |
| 241 |
this.age = age; |
| 242 |
else |
| 243 |
throw new BuildException("Invalid age: " + age); |
| 244 |
} |
| 245 |
|
| 246 |
public void setPathSep(char pathSep) |
| 247 |
{ |
| 248 |
this.pathSep = pathSep; |
| 249 |
} |
| 250 |
|
| 251 |
public void setOutputSetId(String outputSetId) |
| 252 |
{ |
| 253 |
if (property != null) |
| 254 |
throw new BuildException("Cannot set both Property and OutputSetId."); |
| 255 |
this.outputSetId = outputSetId; |
| 256 |
} |
| 257 |
|
| 258 |
public void setPathRef(Reference ref) |
| 259 |
throws BuildException |
| 260 |
{ |
| 261 |
if (path == null) |
| 262 |
{ |
| 263 |
path = new Path(getProject()); |
| 264 |
path.setRefid(ref); |
| 265 |
} |
| 266 |
else |
| 267 |
{ |
| 268 |
throw new BuildException("Path element already specified."); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
public Path createPath() |
| 274 |
throws BuildException |
| 275 |
{ |
| 276 |
if (path == null) |
| 277 |
path = new Path(getProject()); |
| 278 |
else |
| 279 |
throw new BuildException("Path element already specified."); |
| 280 |
return path; |
| 281 |
} |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|