From 71c6478591a532996f8be85048231d23541200e2 Mon Sep 17 00:00:00 2001 From: Bruce Atherton Date: Thu, 8 Aug 2002 05:47:27 +0000 Subject: [PATCH] Added a check on Property names using the "if" and "unless" attributes for the selector tag, so that selectors that it contains are made conditional. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@273196 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTypes/selectors-program.html | 2 +- docs/manual/CoreTypes/selectors.html | 81 +++++++++++++++--- .../ant/types/selectors/SelectSelector.java | 82 ++++++++++++++++++- 3 files changed, 149 insertions(+), 16 deletions(-) diff --git a/docs/manual/CoreTypes/selectors-program.html b/docs/manual/CoreTypes/selectors-program.html index c4a12745e..b61e8f60a 100755 --- a/docs/manual/CoreTypes/selectors-program.html +++ b/docs/manual/CoreTypes/selectors-program.html @@ -76,7 +76,7 @@ to isSelected() method call validate() and a BuildException will be thrown with the contents of your error message. The validate() method also gives you a - last change to check your settings for consistency because it + last chance to check your settings for consistency because it calls verifySettings(). Override this method and call setError() within it if you detect any problems in how your selector is set up.

diff --git a/docs/manual/CoreTypes/selectors.html b/docs/manual/CoreTypes/selectors.html index 4424147e3..025a63a15 100755 --- a/docs/manual/CoreTypes/selectors.html +++ b/docs/manual/CoreTypes/selectors.html @@ -23,21 +23,21 @@ contain other selectors, and these are called Selector Containers. There is also a category of selectors that allow - user-defined extensions, called + user-defined extensions, called Custom Selectors. The ones built in to Ant are called Core Selectors.

- +

Core Selectors

Core selectors are the ones that come standard with Ant. They can be used within a fileset and can be contained within Selector Containers.

- +

The core selectors are:

- +

All selector containers can contain any other selector, including @@ -618,12 +622,43 @@

Selector Reference

The <selector> tag is used to create selectors - that can be reused through references. It should be used outside of + that can be reused through references. It is the only selector which can + be used outside of any target, as an element of the <project> tag. It can contain only one other selector, but of course that selector can be a container.

+

The <selector> tag can also be used to select + files conditionally based on whether an Ant property exists or not. + This functionality is realized using the "if" and + "unless" attributes in exactly the same way they + are used on targets or on the <include> and + <exclude> tags within a + <patternset>.

+ + + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
ifAllow files to be selected only if the named + property is set. + No
unlessAllow files to be selected only if the named + property is not set. + No
+

Here is an example of how to use the Selector Reference:

@@ -647,7 +682,7 @@
             </fileset>
         </zip>
     </target>
-    
+
 </project>
 
@@ -655,18 +690,42 @@ class file and javadoc file associated with them.

+

And an example of selecting files conditionally, based on whether + properties are set:

+ +
+<fileset dir="${working.copy}">
+    <or>
+        <selector if="include.tests">
+            <filename name="**/*Test.class">
+        </selector>
+        <selector if="include.source">
+            <and>
+                <filename name="**/*.java">
+                <not>
+                    <selector unless="include.tests">
+                        <filename name="**/*Test.java">
+                    </selector>
+                </not>
+            </and>
+        </selector>
+    </or>
+</fileset>
+
+

A fileset that conditionally contains Java source files and Test + source and class files.

Custom Selectors

-

You can write your own selectors and use them within the selector +

You can write your own selectors and use them within the selector containers by specifying them within the <custom> tag.

First, you have to write your selector class in Java. The only requirement it must meet in order to be a selector is that it implements the org.apache.tools.ant.types.selectors.FileSelector - interface, which contains a single method. See + interface, which contains a single method. See Programming Selectors in Ant for more information.

diff --git a/src/main/org/apache/tools/ant/types/selectors/SelectSelector.java b/src/main/org/apache/tools/ant/types/selectors/SelectSelector.java index eff32d153..c74c0d6fa 100644 --- a/src/main/org/apache/tools/ant/types/selectors/SelectSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/SelectSelector.java @@ -55,6 +55,8 @@ package org.apache.tools.ant.types.selectors; import java.util.Enumeration; +import java.io.File; + import org.apache.tools.ant.Project; /** @@ -68,7 +70,10 @@ import org.apache.tools.ant.Project; * @author Bruce Atherton * @since 1.5 */ -public class SelectSelector extends AndSelector { +public class SelectSelector extends BaseSelectorContainer { + + private String ifProperty; + private String unlessProperty; /** * Default constructor. @@ -79,7 +84,16 @@ public class SelectSelector extends AndSelector { public String toString() { StringBuffer buf = new StringBuffer(); if (hasSelectors()) { - buf.append("{select: "); + buf.append("{select"); + if (ifProperty != null) { + buf.append(" if: "); + buf.append(ifProperty); + } + if (unlessProperty != null) { + buf.append(" unless: "); + buf.append(unlessProperty); + } + buf.append(" "); buf.append(super.toString()); buf.append("}"); } @@ -155,11 +169,71 @@ public class SelectSelector extends AndSelector { * not. */ public void verifySettings() { - if (selectorCount() != 1) { - setError("One and only one selector is allowed within the " + + int cnt = selectorCount(); + if (cnt < 0 || cnt > 1) { + setError("Only one selector is allowed within the " + "