KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs;
19
20 import org.apache.tools.ant.Task;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.taskdefs.condition.Condition;
23 import org.apache.tools.ant.types.Reference;
24 import org.apache.tools.ant.types.Comparison;
25 import org.apache.tools.ant.types.ResourceCollection;
26
27 /**
28  * Count resources from a ResourceCollection, storing to a property or
29  * writing to the log. Can also be used as a Condition.
30  * @since Ant 1.7
31  */

32 public class ResourceCount extends Task implements Condition {
33
34     private static final String JavaDoc ONE_NESTED_MESSAGE
35         = "ResourceCount can count resources from exactly one nested ResourceCollection.";
36
37     private static final String JavaDoc COUNT_REQUIRED
38         = "Use of the ResourceCount condition requires that the count attribute be set.";
39
40     private ResourceCollection rc;
41     private Comparison when = Comparison.EQUAL;
42     private Integer JavaDoc count;
43     private String JavaDoc property;
44
45     /**
46      * Add the ResourceCollection to count.
47      * @param r the ResourceCollection to count.
48      * @throws BuildException if already set.
49      */

50     public void add(ResourceCollection r) {
51         if (rc != null) {
52             throw new BuildException(ONE_NESTED_MESSAGE);
53         }
54         rc = r;
55     }
56
57     /**
58      * Set the ResourceCollection reference.
59      * @param r the Reference.
60      */

61     public void setRefid(Reference r) {
62         Object JavaDoc o = r.getReferencedObject();
63         if (!(o instanceof ResourceCollection)) {
64             throw new BuildException(r.getRefId()
65                 + " doesn\'t denote a ResourceCollection");
66         }
67         add((ResourceCollection) o);
68     }
69
70     /**
71      * Execute as a Task.
72      */

73     public void execute() {
74         if (rc == null) {
75             throw new BuildException(ONE_NESTED_MESSAGE);
76         }
77         if (property == null) {
78             log("resource count = " + rc.size());
79         } else {
80             getProject().setNewProperty(property, Integer.toString(rc.size()));
81         }
82     }
83
84     /**
85      * Fulfill the condition contract.
86      * @return true if the specified ResourceCollection satisfies the set criteria.
87      * @throws BuildException if an error occurs.
88      */

89     public boolean eval() {
90         if (rc == null) {
91             throw new BuildException(ONE_NESTED_MESSAGE);
92         }
93         if (count == null) {
94             throw new BuildException(COUNT_REQUIRED);
95         }
96         return when.evaluate(new Integer JavaDoc(rc.size()).compareTo(count));
97     }
98
99     /**
100      * Set the target count number for use as a Condition.
101      * @param c number of Resources as int.
102      */

103     public void setCount(int c) {
104         count = new Integer JavaDoc(c);
105     }
106
107     /**
108      * Set the comparison for use as a Condition.
109      * @param c Comparison (an EnumeratedAttribute) When.
110      * @see org.apache.tools.ant.types.Comparison
111      */

112     public void setWhen(Comparison c) {
113         when = c;
114     }
115
116     /**
117      * Set the name of the property to set in task mode.
118      * @param p the property name to set.
119      */

120     public void setProperty(String JavaDoc p) {
121         property = p;
122     }
123
124 }
125
Popular Tags