KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > SelectSelector


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.types.selectors;
20
21 import java.util.Enumeration JavaDoc;
22 import java.io.File JavaDoc;
23
24 import org.apache.tools.ant.Project;
25
26 /**
27  * This selector just holds one other selector and forwards all
28  * requests to it. It exists so that there is a single selector
29  * type that can exist outside of any targets, as an element of
30  * project. It overrides all of the reference stuff so that it
31  * works as expected. Note that this is the only selector you
32  * can reference.
33  *
34  * @since 1.5
35  */

36 public class SelectSelector extends BaseSelectorContainer {
37
38     private String JavaDoc ifProperty;
39     private String JavaDoc unlessProperty;
40
41     /**
42      * Default constructor.
43      */

44     public SelectSelector() {
45     }
46
47     /**
48      * @return a string describing this object
49      */

50     public String JavaDoc toString() {
51         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
52         if (hasSelectors()) {
53             buf.append("{select");
54             if (ifProperty != null) {
55                 buf.append(" if: ");
56                 buf.append(ifProperty);
57             }
58             if (unlessProperty != null) {
59                 buf.append(" unless: ");
60                 buf.append(unlessProperty);
61             }
62             buf.append(" ");
63             buf.append(super.toString());
64             buf.append("}");
65         }
66         return buf.toString();
67     }
68
69     /**
70      * Performs the check for circular references and returns the
71      * referenced Selector.
72      */

73     private SelectSelector getRef() {
74         Object JavaDoc o = getCheckedRef(this.getClass(), "SelectSelector");
75         return (SelectSelector) o;
76     }
77
78     /**
79      * Indicates whether there are any selectors here.
80      * @return whether any selectors are in this container
81      */

82     public boolean hasSelectors() {
83         if (isReference()) {
84             return getRef().hasSelectors();
85         }
86         return super.hasSelectors();
87     }
88
89     /**
90      * Gives the count of the number of selectors in this container
91      * @return the number of selectors in this container
92      */

93     public int selectorCount() {
94         if (isReference()) {
95             return getRef().selectorCount();
96         }
97         return super.selectorCount();
98     }
99
100     /**
101      * Returns the set of selectors as an array.
102      * @param p the current project
103      * @return an array of selectors in this container
104      */

105     public FileSelector[] getSelectors(Project p) {
106         if (isReference()) {
107             return getRef().getSelectors(p);
108         }
109         return super.getSelectors(p);
110     }
111
112     /**
113      * Returns an enumerator for accessing the set of selectors.
114      * @return an enumerator that goes through each of the selectors
115      */

116     public Enumeration JavaDoc selectorElements() {
117         if (isReference()) {
118             return getRef().selectorElements();
119         }
120         return super.selectorElements();
121     }
122
123     /**
124      * Add a new selector into this container.
125      *
126      * @param selector the new selector to add
127      */

128     public void appendSelector(FileSelector selector) {
129         if (isReference()) {
130             throw noChildrenAllowed();
131         }
132         super.appendSelector(selector);
133     }
134
135
136     /**
137      * Makes sure that there is only one entry, sets an error message if
138      * not.
139      */

140     public void verifySettings() {
141         int cnt = selectorCount();
142         if (cnt < 0 || cnt > 1) {
143             setError("Only one selector is allowed within the "
144                 + "<selector> tag");
145         }
146     }
147
148     /**
149      * Ensures that the selector passes the conditions placed
150      * on it with <code>if</code> and <code>unless</code>.
151      * @return true if conditions are passed
152      */

153     public boolean passesConditions() {
154         if (ifProperty != null
155             && getProject().getProperty(ifProperty) == null) {
156             return false;
157         } else if (unlessProperty != null
158             && getProject().getProperty(unlessProperty) != null) {
159             return false;
160         }
161         return true;
162     }
163
164     /**
165      * Sets the if attribute to a property which must exist for the
166      * selector to select any files.
167      * @param ifProperty the property to check
168      */

169     public void setIf(String JavaDoc ifProperty) {
170         this.ifProperty = ifProperty;
171     }
172
173     /**
174      * Sets the unless attribute to a property which cannot exist for the
175      * selector to select any files.
176      * @param unlessProperty the property to check
177      */

178     public void setUnless(String JavaDoc unlessProperty) {
179         this.unlessProperty = unlessProperty;
180     }
181
182     /**
183      * Returns true (the file is selected) only if the if property (if any)
184      * exists, the unless property (if any) doesn't exist, and the
185      * contained selector (if any) selects the file. If there is no contained
186      * selector, return true (because we assume that the point was to test
187      * the if and unless conditions).
188      *
189      * @param basedir the base directory the scan is being done from
190      * @param filename the name of the file to check
191      * @param file a java.io.File object for the filename that the selector
192      * can use
193      * @return whether the file should be selected or not
194      */

195     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
196         validate();
197
198         // Deal with if and unless properties first
199
if (!(passesConditions())) {
200             return false;
201         }
202
203         Enumeration JavaDoc e = selectorElements();
204         if (!(e.hasMoreElements())) {
205             return true;
206         }
207         FileSelector f = (FileSelector) e.nextElement();
208         return f.isSelected(basedir, filename, file);
209     }
210 }
211
212
Popular Tags