KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Length


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.taskdefs.condition.Condition;
30 import org.apache.tools.ant.types.FileSet;
31 import org.apache.tools.ant.types.Resource;
32 import org.apache.tools.ant.types.Comparison;
33 import org.apache.tools.ant.types.ResourceCollection;
34 import org.apache.tools.ant.types.EnumeratedAttribute;
35 import org.apache.tools.ant.types.resources.Resources;
36 import org.apache.tools.ant.types.resources.FileResource;
37 import org.apache.tools.ant.util.PropertyOutputStream;
38
39 /**
40  * Gets lengths: of files/resources, byte size; of strings, length (optionally trimmed).
41  * The task is overloaded in this way for semantic reasons, much like Available.
42  * @since Ant 1.6.3
43  */

44 public class Length extends Task implements Condition {
45
46     private static final String JavaDoc ALL = "all";
47     private static final String JavaDoc EACH = "each";
48     private static final String JavaDoc STRING = "string";
49
50     private static final String JavaDoc LENGTH_REQUIRED
51         = "Use of the Length condition requires that the length attribute be set.";
52
53     private String JavaDoc property;
54     private String JavaDoc string;
55     private Boolean JavaDoc trim;
56     private String JavaDoc mode = ALL;
57     private Comparison when = Comparison.EQUAL;
58     private Long JavaDoc length;
59     private Resources resources;
60
61     /**
62      * The property in which the length will be stored.
63      * @param property the <code>String</code> property key.
64      */

65     public synchronized void setProperty(String JavaDoc property) {
66         this.property = property;
67     }
68
69     /**
70      * Set the single file for this task.
71      * @param file the <code>File</code> whose length to retrieve.
72      */

73     public synchronized void setFile(File JavaDoc file) {
74         add(new FileResource(file));
75     }
76
77     /**
78      * Add a FileSet.
79      * @param fs the <code>FileSet</code> to add.
80      */

81     public synchronized void add(FileSet fs) {
82         add((ResourceCollection) fs);
83     }
84
85     /**
86      * Add a ResourceCollection.
87      * @param c the <code>ResourceCollection</code> to add.
88      * @since Ant 1.7
89      */

90     public synchronized void add(ResourceCollection c) {
91         if (c == null) {
92             return;
93         }
94         resources = (resources == null) ? new Resources() : resources;
95         resources.add(c);
96     }
97
98     /**
99      * Set the target count number for use as a Condition.
100      * @param ell the long length to compare with.
101      */

102     public synchronized void setLength(long ell) {
103         length = new Long JavaDoc(ell);
104     }
105
106     /**
107      * Set the comparison for use as a Condition.
108      * @param w EnumeratedAttribute When.
109      * @see org.apache.tools.ant.types.Comparison
110      */

111     public synchronized void setWhen(When w) {
112         setWhen((Comparison) w);
113     }
114
115     /**
116      * Set the comparison for use as a Condition.
117      * @param c Comparison.
118      * @see org.apache.tools.ant.types.Comparison
119      * @since Ant 1.7
120      */

121     public synchronized void setWhen(Comparison c) {
122         when = c;
123     }
124
125     /**
126      * Set the execution mode for working with files.
127      * @param m the <code>FileMode</code> to use.
128      */

129     public synchronized void setMode(FileMode m) {
130         this.mode = m.getValue();
131     }
132
133     /**
134      * Set the string whose length to get.
135      * @param string <code>String</code>.
136      */

137     public synchronized void setString(String JavaDoc string) {
138         this.string = string;
139         this.mode = STRING;
140     }
141
142     /**
143      * Set whether to trim in string mode.
144      * @param trim <code>boolean</code>.
145      */

146     public synchronized void setTrim(boolean trim) {
147         this.trim = trim ? Boolean.TRUE : Boolean.FALSE;
148     }
149
150     /**
151      * Learn whether strings will be trimmed.
152      * @return boolean trim setting.
153      */

154     public boolean getTrim() {
155         return trim != null && trim.booleanValue();
156     }
157
158     /**
159      * Execute the length task.
160      */

161     public void execute() {
162         validate();
163         PrintStream JavaDoc ps = new PrintStream JavaDoc((property != null)
164             ? (OutputStream JavaDoc) new PropertyOutputStream(getProject(), property)
165             : (OutputStream JavaDoc) new LogOutputStream(this, Project.MSG_INFO));
166
167         if (STRING.equals(mode)) {
168             ps.print(getLength(string, getTrim()));
169             ps.close();
170         } else if (EACH.equals(mode)) {
171             handleResources(new EachHandler(ps));
172         } else if (ALL.equals(mode)) {
173             handleResources(new AllHandler(ps));
174         }
175     }
176
177     /**
178      * Fulfill the condition contract.
179      * @return true if the condition is true.
180      * @throws BuildException if an error occurs.
181      */

182     public boolean eval() {
183         validate();
184         if (length == null) {
185             throw new BuildException(LENGTH_REQUIRED);
186         }
187         Long JavaDoc ell = null;
188         if (STRING.equals(mode)) {
189             ell = new Long JavaDoc(getLength(string, getTrim()));
190         } else {
191             ConditionHandler h = new ConditionHandler();
192             handleResources(h);
193             ell = new Long JavaDoc(h.getLength());
194         }
195         return when.evaluate(ell.compareTo(length));
196     }
197
198     private void validate() {
199         if (string != null) {
200             if (resources != null) {
201                 throw new BuildException("the string length function"
202                     + " is incompatible with the file/resource length function");
203             }
204             if (!(STRING.equals(mode))) {
205                 throw new BuildException("the mode attribute is for use"
206                     + " with the file/resource length function");
207             }
208         } else if (resources != null) {
209             if (!(EACH.equals(mode) || ALL.equals(mode))) {
210                 throw new BuildException("invalid mode setting for"
211                     + " file/resource length function: \"" + mode + "\"");
212             } else if (trim != null) {
213                 throw new BuildException("the trim attribute is"
214                     + " for use with the string length function only");
215             }
216         } else {
217             throw new BuildException("you must set either the string attribute"
218                 + " or specify one or more files using the file attribute or"
219                 + " nested resource collections");
220         }
221     }
222
223     private void handleResources(Handler h) {
224         for (Iterator JavaDoc i = resources.iterator(); i.hasNext();) {
225             Resource r = (Resource) i.next();
226             if (!r.isExists()) {
227                 log(r + " does not exist", Project.MSG_ERR);
228             } else if (r.isDirectory()) {
229                 log(r + " is a directory; length unspecified",
230                     Project.MSG_ERR);
231             } else {
232                 h.handle(r);
233             }
234         }
235         h.complete();
236     }
237
238     private static long getLength(String JavaDoc s, boolean t) {
239         return (t ? s.trim() : s).length();
240     }
241
242     /** EnumeratedAttribute operation mode */
243     public static class FileMode extends EnumeratedAttribute {
244         static final String JavaDoc[] MODES = new String JavaDoc[] {EACH, ALL};
245
246         /**
247          * Return the possible values for FileMode.
248          * @return <code>String[]</code>.
249          */

250         public String JavaDoc[] getValues() {
251             return MODES;
252         }
253
254     }
255
256     /**
257      * EnumeratedAttribute for the when attribute.
258      */

259     public static class When extends Comparison {
260         //extend Comparison; retain for BC only
261
}
262
263     private abstract class Handler {
264         private PrintStream JavaDoc ps;
265         Handler(PrintStream JavaDoc ps) {
266             this.ps = ps;
267         }
268
269         protected PrintStream JavaDoc getPs() {
270             return ps;
271         }
272
273         protected abstract void handle(Resource r);
274
275         void complete() {
276             ps.close();
277         }
278     }
279
280     private class EachHandler extends Handler {
281         EachHandler(PrintStream JavaDoc ps) {
282             super(ps);
283         }
284         protected void handle(Resource r) {
285             getPs().print(r.toString());
286             getPs().print(" : ");
287             //when writing to the log, we'll see what's happening:
288
long size = r.getSize();
289             if (size == Resource.UNKNOWN_SIZE) {
290                 getPs().println("unknown");
291             } else {
292                 getPs().println(size);
293             }
294        }
295     }
296
297     private class AllHandler extends Handler {
298         private long accum = 0L;
299         AllHandler(PrintStream JavaDoc ps) {
300             super(ps);
301         }
302         protected long getAccum() {
303             return accum;
304         }
305         protected synchronized void handle(Resource r) {
306             long size = r.getSize();
307             if (size == Resource.UNKNOWN_SIZE) {
308                 log("Size unknown for " + r.toString(), Project.MSG_WARN);
309             } else {
310                 accum += size;
311             }
312         }
313         void complete() {
314             getPs().print(accum);
315             super.complete();
316         }
317     }
318
319     private class ConditionHandler extends AllHandler {
320         ConditionHandler() {
321             super(null);
322         }
323         void complete() {
324         }
325         long getLength() {
326             return getAccum();
327         }
328     }
329 }
330
Popular Tags