Browse Source

Added a check on Property names using the "if" and "unless" attributes


			
			master
		
Bruce Atherton 23 years ago
parent
commit
71c6478591
3 changed files with 149 additions and 16 deletions
  1. +1
    -1
      docs/manual/CoreTypes/selectors-program.html
  2. +70
    -11
      docs/manual/CoreTypes/selectors.html
  3. +78
    -4
      src/main/org/apache/tools/ant/types/selectors/SelectSelector.java

+ 1
- 1
docs/manual/CoreTypes/selectors-program.html View File

@@ -76,7 +76,7 @@ to
<code>isSelected()</code> method call <code>validate()</code> and
a BuildException will be thrown with the contents of your error
message. The <code>validate()</code> 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 <code>verifySettings()</code>. Override this method and
call <code>setError()</code> within it if you detect any
problems in how your selector is set up.</p>


+ 70
- 11
docs/manual/CoreTypes/selectors.html View File

@@ -23,21 +23,21 @@
contain other selectors, and these are called
<a href="#selectcontainers"><code>Selector Containers</code></a>.
There is also a category of selectors that allow
user-defined extensions, called
user-defined extensions, called
<a href="#customselect"><code>Custom Selectors</code></a>.
The ones built in to Ant are called
<a href="#coreselect"><code>Core Selectors</code></a>.
</p>
<a name="coreselect"></a>
<h3>Core Selectors</h3>

<p>Core selectors are the ones that come standard
with Ant. They can be used within a fileset and can be contained
within Selector Containers.</p>
<p>The core selectors are:</p>
<ul>
<li><a href="#containsselect">&lt;contains&gt;</a> - Select
files that contain a particular text string
@@ -161,7 +161,7 @@
<h4>Depend Selector</h4>

<p>The <code>&lt;depend&gt;</code> tag selects files
whose last modified date is later than another, equivalent file in
whose last modified date is later than another, equivalent file in
another location.</p>
<p>The <code>&lt;depend&gt;</code> tag supports the use of a
@@ -443,9 +443,13 @@
<li><a href="#orselect">&lt;or&gt;</a> - selects a file if any one
of the contained selectors selects it.
<li><a href="#selectorselect">&lt;selector&gt;</a> - contains only one
selector and forwards all requests to it without alteration. This
selector and forwards all requests to it without alteration, provided
that any <code>&quot;if&quot;</code> or
<code>&quot;unless&quot;</code> conditions are met. This
is the selector to use if you want to define a reference. It is
usable as an element of <code>&lt;project&gt;</code>.
usable as an element of <code>&lt;project&gt;</code>. It is also
the one to use if you want selection of files to be dependent on
Ant property settings.
</ul>

<p>All selector containers can contain any other selector, including
@@ -618,12 +622,43 @@
<h4>Selector Reference</h4>

<p>The <code>&lt;selector&gt;</code> 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 <code>&lt;project&gt;</code> tag. It
can contain only one other selector, but of course that selector can
be a container.
</p>

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

<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">if</td>
<td valign="top">Allow files to be selected only if the named
property is set.
</td>
<td valign="top" align="center">No</td>
</tr>
<tr>
<td valign="top">unless</td>
<td valign="top">Allow files to be selected only if the named
property is <b>not</b> set.
</td>
<td valign="top" align="center">No</td>
</tr>
</table>

<p>Here is an example of how to use the Selector Reference:</p>

<blockquote><pre>
@@ -647,7 +682,7 @@
&lt;/fileset&gt;
&lt;/zip&gt;
&lt;/target&gt;
&lt;/project&gt;
</pre></blockquote>

@@ -655,18 +690,42 @@
class file and javadoc file associated with them.
</p>

<p>And an example of selecting files conditionally, based on whether
properties are set:</p>

<blockquote><pre>
&lt;fileset dir=&quot;${working.copy}&quot;&gt;
&lt;or&gt;
&lt;selector if=&quot;include.tests&quot;&gt;
&lt;filename name=&quot;**/*Test.class&quot;&gt;
&lt;/selector&gt;
&lt;selector if=&quot;include.source&quot;&gt;
&lt;and&gt;
&lt;filename name=&quot;**/*.java&quot;&gt;
&lt;not&gt;
&lt;selector unless=&quot;include.tests&quot;&gt;
&lt;filename name=&quot;**/*Test.java&quot;&gt;
&lt;/selector&gt;
&lt;/not&gt;
&lt;/and&gt;
&lt;/selector&gt;
&lt;/or&gt;
&lt;/fileset&gt;
</pre></blockquote>

<p>A fileset that conditionally contains Java source files and Test
source and class files.</p>

<a name="customselect"></a>
<h3>Custom Selectors</h3>

<p>You can write your own selectors and use them within the selector
<p>You can write your own selectors and use them within the selector
containers by specifying them within the &lt;custom&gt; tag.</p>

<p>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 <code>org.apache.tools.ant.types.selectors.FileSelector</code>
interface, which contains a single method. See
interface, which contains a single method. See
<a href="selectors-program.html">Programming Selectors in Ant</a> for
more information.</p>



+ 78
- 4
src/main/org/apache/tools/ant/types/selectors/SelectSelector.java View File

@@ -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 <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
* @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 " +
"<select> tag");
}
}

/**
* Ensures that the selector passes the conditions placed
* on it with <code>if</code> and <code>unless</code>.
*/
public boolean passesConditions() {
if (ifProperty != null &&
getProject().getProperty(ifProperty) == null) {
return false;
} else if (unlessProperty != null &&
getProject().getProperty(unlessProperty) != null) {
return false;
}
return true;
}

/**
* Sets the if attribute to a property which must exist for the
* selector to select any files.
*/
public void setIf(String ifProperty) {
this.ifProperty = ifProperty;
}

/**
* Sets the unless attribute to a property which cannot exist for the
* selector to select any files.
*/
public void setUnless(String unlessProperty) {
this.unlessProperty = unlessProperty;
}

/**
* Returns true (the file is selected) only if the if property (if any)
* exists, the unless property (if any) doesn't exist, and the
* contained selector (if any) selects the file. If there is no contained
* selector, return true (because we assume that the point was to test
* the if and unless conditions).
*
* @param basedir the base directory the scan is being done from
* @param filename the name of the file to check
* @param file a java.io.File object for the filename that the selector
* can use
* @return whether the file should be selected or not
*/
public boolean isSelected(File basedir, String filename, File file) {
validate();

// Deal with if and unless properties first
if (!(passesConditions())) {
return false;
}

Enumeration e = selectorElements();
if (!(e.hasMoreElements())) {
return true;
}
FileSelector f = (FileSelector)e.nextElement();
return f.isSelected(basedir,filename,file);
}
}


Loading…
Cancel
Save