1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
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 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import java.util.Enumeration;
|
---|
21 | import java.io.File;
|
---|
22 |
|
---|
23 | import org.apache.tools.ant.Project;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This selector just holds one other selector and forwards all
|
---|
27 | * requests to it. It exists so that there is a single selector
|
---|
28 | * type that can exist outside of any targets, as an element of
|
---|
29 | * project. It overrides all of the reference stuff so that it
|
---|
30 | * works as expected. Note that this is the only selector you
|
---|
31 | * can reference.
|
---|
32 | *
|
---|
33 | * @since 1.5
|
---|
34 | */
|
---|
35 | public class SelectSelector extends BaseSelectorContainer {
|
---|
36 |
|
---|
37 | private String ifProperty;
|
---|
38 | private String unlessProperty;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Default constructor.
|
---|
42 | */
|
---|
43 | public SelectSelector() {
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * @return a string describing this object
|
---|
48 | */
|
---|
49 | public String toString() {
|
---|
50 | StringBuffer buf = new StringBuffer();
|
---|
51 | if (hasSelectors()) {
|
---|
52 | buf.append("{select");
|
---|
53 | if (ifProperty != null) {
|
---|
54 | buf.append(" if: ");
|
---|
55 | buf.append(ifProperty);
|
---|
56 | }
|
---|
57 | if (unlessProperty != null) {
|
---|
58 | buf.append(" unless: ");
|
---|
59 | buf.append(unlessProperty);
|
---|
60 | }
|
---|
61 | buf.append(" ");
|
---|
62 | buf.append(super.toString());
|
---|
63 | buf.append("}");
|
---|
64 | }
|
---|
65 | return buf.toString();
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Performs the check for circular references and returns the
|
---|
70 | * referenced Selector.
|
---|
71 | */
|
---|
72 | private SelectSelector getRef() {
|
---|
73 | Object o = getCheckedRef(this.getClass(), "SelectSelector");
|
---|
74 | return (SelectSelector) o;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Indicates whether there are any selectors here.
|
---|
79 | * @return whether any selectors are in this container
|
---|
80 | */
|
---|
81 | public boolean hasSelectors() {
|
---|
82 | if (isReference()) {
|
---|
83 | return getRef().hasSelectors();
|
---|
84 | }
|
---|
85 | return super.hasSelectors();
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Gives the count of the number of selectors in this container
|
---|
90 | * @return the number of selectors in this container
|
---|
91 | */
|
---|
92 | public int selectorCount() {
|
---|
93 | if (isReference()) {
|
---|
94 | return getRef().selectorCount();
|
---|
95 | }
|
---|
96 | return super.selectorCount();
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Returns the set of selectors as an array.
|
---|
101 | * @param p the current project
|
---|
102 | * @return an array of selectors in this container
|
---|
103 | */
|
---|
104 | public FileSelector[] getSelectors(Project p) {
|
---|
105 | if (isReference()) {
|
---|
106 | return getRef().getSelectors(p);
|
---|
107 | }
|
---|
108 | return super.getSelectors(p);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Returns an enumerator for accessing the set of selectors.
|
---|
113 | * @return an enumerator that goes through each of the selectors
|
---|
114 | */
|
---|
115 | public Enumeration selectorElements() {
|
---|
116 | if (isReference()) {
|
---|
117 | return getRef().selectorElements();
|
---|
118 | }
|
---|
119 | return super.selectorElements();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Add a new selector into this container.
|
---|
124 | *
|
---|
125 | * @param selector the new selector to add
|
---|
126 | */
|
---|
127 | public void appendSelector(FileSelector selector) {
|
---|
128 | if (isReference()) {
|
---|
129 | throw noChildrenAllowed();
|
---|
130 | }
|
---|
131 | super.appendSelector(selector);
|
---|
132 | }
|
---|
133 |
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Makes sure that there is only one entry, sets an error message if
|
---|
137 | * not.
|
---|
138 | */
|
---|
139 | public void verifySettings() {
|
---|
140 | int cnt = selectorCount();
|
---|
141 | if (cnt < 0 || cnt > 1) {
|
---|
142 | setError("Only one selector is allowed within the "
|
---|
143 | + "<selector> tag");
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Ensures that the selector passes the conditions placed
|
---|
149 | * on it with <code>if</code> and <code>unless</code>.
|
---|
150 | * @return true if conditions are passed
|
---|
151 | */
|
---|
152 | public boolean passesConditions() {
|
---|
153 | if (ifProperty != null
|
---|
154 | && getProject().getProperty(ifProperty) == null) {
|
---|
155 | return false;
|
---|
156 | } else if (unlessProperty != null
|
---|
157 | && getProject().getProperty(unlessProperty) != null) {
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 | return true;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Sets the if attribute to a property which must exist for the
|
---|
165 | * selector to select any files.
|
---|
166 | * @param ifProperty the property to check
|
---|
167 | */
|
---|
168 | public void setIf(String ifProperty) {
|
---|
169 | this.ifProperty = ifProperty;
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Sets the unless attribute to a property which cannot exist for the
|
---|
174 | * selector to select any files.
|
---|
175 | * @param unlessProperty the property to check
|
---|
176 | */
|
---|
177 | public void setUnless(String unlessProperty) {
|
---|
178 | this.unlessProperty = unlessProperty;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Returns true (the file is selected) only if the if property (if any)
|
---|
183 | * exists, the unless property (if any) doesn't exist, and the
|
---|
184 | * contained selector (if any) selects the file. If there is no contained
|
---|
185 | * selector, return true (because we assume that the point was to test
|
---|
186 | * the if and unless conditions).
|
---|
187 | *
|
---|
188 | * @param basedir the base directory the scan is being done from
|
---|
189 | * @param filename the name of the file to check
|
---|
190 | * @param file a java.io.File object for the filename that the selector
|
---|
191 | * can use
|
---|
192 | * @return whether the file should be selected or not
|
---|
193 | */
|
---|
194 | public boolean isSelected(File basedir, String filename, File file) {
|
---|
195 | validate();
|
---|
196 |
|
---|
197 | // Deal with if and unless properties first
|
---|
198 | if (!(passesConditions())) {
|
---|
199 | return false;
|
---|
200 | }
|
---|
201 |
|
---|
202 | Enumeration e = selectorElements();
|
---|
203 | if (!(e.hasMoreElements())) {
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 | FileSelector f = (FileSelector) e.nextElement();
|
---|
207 | return f.isSelected(basedir, filename, file);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|