001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import java.util.Enumeration;
022: import java.io.File;
023:
024: import org.apache.tools.ant.Project;
025:
026: /**
027: * This selector just holds one other selector and forwards all
028: * requests to it. It exists so that there is a single selector
029: * type that can exist outside of any targets, as an element of
030: * project. It overrides all of the reference stuff so that it
031: * works as expected. Note that this is the only selector you
032: * can reference.
033: *
034: * @since 1.5
035: */
036: public class SelectSelector extends BaseSelectorContainer {
037:
038: private String ifProperty;
039: private String unlessProperty;
040:
041: /**
042: * Default constructor.
043: */
044: public SelectSelector() {
045: }
046:
047: /**
048: * @return a string describing this object
049: */
050: public String toString() {
051: StringBuffer buf = new StringBuffer();
052: if (hasSelectors()) {
053: buf.append("{select");
054: if (ifProperty != null) {
055: buf.append(" if: ");
056: buf.append(ifProperty);
057: }
058: if (unlessProperty != null) {
059: buf.append(" unless: ");
060: buf.append(unlessProperty);
061: }
062: buf.append(" ");
063: buf.append(super .toString());
064: buf.append("}");
065: }
066: return buf.toString();
067: }
068:
069: /**
070: * Performs the check for circular references and returns the
071: * referenced Selector.
072: */
073: private SelectSelector getRef() {
074: Object o = getCheckedRef(this .getClass(), "SelectSelector");
075: return (SelectSelector) o;
076: }
077:
078: /**
079: * Indicates whether there are any selectors here.
080: * @return whether any selectors are in this container
081: */
082: public boolean hasSelectors() {
083: if (isReference()) {
084: return getRef().hasSelectors();
085: }
086: return super .hasSelectors();
087: }
088:
089: /**
090: * Gives the count of the number of selectors in this container
091: * @return the number of selectors in this container
092: */
093: public int selectorCount() {
094: if (isReference()) {
095: return getRef().selectorCount();
096: }
097: return super .selectorCount();
098: }
099:
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 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: * 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: }
|