Debian Codegraph @

src/main/java/net/sf/antcontrib/logic/TimestampSelector.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Task definition for the foreach task. The foreach task iterates
29 * over a list, a list of filesets, or both.
30 *
31 * <pre>
32 *
33 * Usage:
34 *
35 * Task declaration in the project:
36 * <code>
37 * &lt;taskdef name="latesttimestamp" classname="net.sf.antcontrib.logic.TimestampSelector" /&gt;
38 * </code>
39 *
40 * Call Syntax:
41 * <code>
42 * &lt;timestampselector
43 * [property="prop" | outputsetref="id"]
44 * [count="num"]
45 * [age="eldest|youngest"]
46 * [pathSep=","]
47 * [pathref="ref"] &gt;
48 * &lt;path&gt;
49 * ...
50 * &lt;/path&gt;
51 * &lt;/latesttimestamp&gt;
52 * </code>
53 *
54 * Attributes:
55 * outputsetref --> The reference of the output Path set which will contain the
56 * files with the latest timestamps.
57 * property --> The name of the property to set with file having the latest
58 * timestamp. If you specify the "count" attribute, you will get
59 * the lastest N files. These will be the absolute pathnames
60 * count --> How many of the latest files do you wish to find
61 * pathSep --> What to use as the path separator when using the "property"
62 * attribute, in conjunction with the "count" attribute
63 * pathref --> The reference of the path which is the input set of files.
64 *
65 * </pre>
66 * @author <a href="mailto:[email protected]">Matthew Inger</a>
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 * Default Constructor
83 */
84 public TimestampSelector()
85 {
86 super();
87 }
88
89
90 public void doFileSetExecute(String paths[])
91 throws BuildException
92 {
93
94 }
95
96 // Sorts entire array
97 public void sort(Vector array)
98 {
99 sort(array, 0, array.size() - 1);
100 }
101
102 // Sorts partial array
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 // Figure out the list of existing file elements
167 // from the designated path
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 // Sort the vector, need to make java 1.1 compliant
178 sort(v);
179
180 // Pull off the first N items
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 // Build the resulting Path object
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 // Add the reference to the project
203 getProject().addReference(outputSetId, path);
204 }
205 else
206 {
207 // Concat the paths, and put them in a property
208 // which is separated list of the files, using the
209 // "pathSep" attribute as the separator
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