git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@277088 13f79535-47bb-0310-9956-ffa450edef68master
@@ -95,6 +95,8 @@ Other changes: | |||||
* Added preserveLastModified attribute to signjar task. | * Added preserveLastModified attribute to signjar task. | ||||
Bugzilla report 30987. | Bugzilla report 30987. | ||||
* Added isfileselected condition. | |||||
Changes from Ant 1.6.2 to current Ant 1.6 CVS version | Changes from Ant 1.6.2 to current Ant 1.6 CVS version | ||||
===================================================== | ===================================================== | ||||
@@ -356,6 +356,42 @@ that is "true","yes", or "on"</p> | |||||
<td valign="top" align="center">No</td> | <td valign="top" align="center">No</td> | ||||
</tr> | </tr> | ||||
</table> | </table> | ||||
<h4>isfileselected</h4> | |||||
<p> | |||||
Test whether a file passes an embedded selector. | |||||
</p> | |||||
<p> | |||||
This condition has been added in Apache Ant 1.7. | |||||
</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">file</td> | |||||
<td valign="top"> | |||||
The file to check if is passes the embedded selector. | |||||
</td> | |||||
<td valign="top" align="center">Yes</td> | |||||
</tr> | |||||
<tr> | |||||
<td valign="top">basedir</td> | |||||
<td valign="top">The base directory to use for name based selectors. It this is not set, | |||||
the project's basedirectory will be used.</td> | |||||
<td valign="top" align="center">No</td> | |||||
</tr> | |||||
</table> | |||||
<p> | |||||
Example usage: | |||||
</p> | |||||
<blockquote> | |||||
<pre> | |||||
<isfileselected file="a.xml"> | |||||
<date datetime="06/28/2000 2:02 pm" when="equal"/> | |||||
</isfileselected> | |||||
</pre></blockquote> | |||||
<h4>typefound</h4> | <h4>typefound</h4> | ||||
<p>Test whether a given type is defined, and that | <p>Test whether a given type is defined, and that | ||||
@@ -0,0 +1,53 @@ | |||||
<project> | |||||
<macrodef name="pass"> | |||||
<element name="conditions" implicit="yes"/> | |||||
<attribute name="failmessage"/> | |||||
<sequential> | |||||
<fail message="@{failmessage}"> | |||||
<condition> | |||||
<not> | |||||
<conditions/> | |||||
</not> | |||||
</condition> | |||||
</fail> | |||||
</sequential> | |||||
</macrodef> | |||||
<target name="simple"> | |||||
<pass failmessage="a simple test"> | |||||
<isfileselected file="jars/pass.jar"> | |||||
<signedselector/> | |||||
</isfileselected> | |||||
</pass> | |||||
</target> | |||||
<target name="name"> | |||||
<pass failmessage="name did not match"> | |||||
<isfileselected file="jars/nosign.jar"> | |||||
<filename name="jars/nosign.jar"/> | |||||
</isfileselected> | |||||
</pass> | |||||
</target> | |||||
<target name="basedir"> | |||||
<pass failmessage="name did not match with a basedir change"> | |||||
<isfileselected file="jars/nosign.jar" basedir="jars"> | |||||
<filename name="nosign.jar"/> | |||||
</isfileselected> | |||||
</pass> | |||||
</target> | |||||
<target name="type"> | |||||
<pass failmessage="type selector did not work"> | |||||
<isfileselected file="isfileselected.xml"> | |||||
<type type="file"/> | |||||
</isfileselected> | |||||
</pass> | |||||
</target> | |||||
<target name="not.selector"> | |||||
<fileset dir="."> | |||||
<isfileselected file="nosigned.jar"/> | |||||
</fileset> | |||||
</target> | |||||
</project> |
@@ -0,0 +1,77 @@ | |||||
/* | |||||
* Copyright 2004 The Apache Software Foundation | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
* | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.condition; | |||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.util.FileUtils; | |||||
import java.io.File; | |||||
import org.apache.tools.ant.types.selectors.FileSelector; | |||||
import org.apache.tools.ant.types.selectors.AbstractSelectorContainer; | |||||
/** | |||||
* This is a condition that checks to see if a file passes an embedded selector. | |||||
*/ | |||||
public class IsFileSelected extends AbstractSelectorContainer implements Condition { | |||||
private static final FileUtils FILE_UTILS = FileUtils.newFileUtils(); | |||||
private File file; | |||||
private File baseDir; | |||||
/** | |||||
* The file to check. | |||||
* @param file the file to check if if passes the embedded selector. | |||||
*/ | |||||
public void setFile(File file) { | |||||
this.file = file; | |||||
} | |||||
/** | |||||
* The base directory to use. | |||||
* @param baseDir the base directory to use, if null use the project's | |||||
* basedir. | |||||
*/ | |||||
public void setBaseDir(File baseDir) { | |||||
this.baseDir = baseDir; | |||||
} | |||||
/** | |||||
* validate the parameters. | |||||
*/ | |||||
public void validate() { | |||||
if (selectorCount() != 1) { | |||||
throw new BuildException("Only one selector allowed"); | |||||
} | |||||
super.validate(); | |||||
} | |||||
/** | |||||
* Evaluate the selector with the file. | |||||
* @return true if the file is selected by the embedded selector. | |||||
*/ | |||||
public boolean eval() { | |||||
if (file == null) { | |||||
throw new BuildException("file attribute not set"); | |||||
} | |||||
validate(); | |||||
File myBaseDir = baseDir; | |||||
if (myBaseDir == null) { | |||||
myBaseDir = getProject().getBaseDir(); | |||||
} | |||||
FileSelector f = getSelectors(getProject())[0]; | |||||
return f.isSelected( | |||||
myBaseDir, FILE_UTILS.removeLeadingPath(myBaseDir, file), file); | |||||
} | |||||
} |
@@ -0,0 +1,289 @@ | |||||
/* | |||||
* Copyright 2002-2004 The Apache Software Foundation | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
* | |||||
*/ | |||||
package org.apache.tools.ant.types.selectors; | |||||
import java.io.File; | |||||
import java.util.Enumeration; | |||||
import java.util.Vector; | |||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.Project; | |||||
import org.apache.tools.ant.types.DataType; | |||||
import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector; | |||||
/** | |||||
* This is the a base class a container of selectors - it does | |||||
* not need do be a selector itself. | |||||
* | |||||
* @since 1.7 | |||||
*/ | |||||
public abstract class AbstractSelectorContainer extends DataType | |||||
implements SelectorContainer { | |||||
private Vector selectorsList = new Vector(); | |||||
/** | |||||
* Indicates whether there are any selectors here. | |||||
* @return true if there are selectors | |||||
*/ | |||||
public boolean hasSelectors() { | |||||
return !(selectorsList.isEmpty()); | |||||
} | |||||
/** | |||||
* Gives the count of the number of selectors in this container | |||||
* @return the number of selectors | |||||
*/ | |||||
public int selectorCount() { | |||||
return selectorsList.size(); | |||||
} | |||||
/** | |||||
* Returns the set of selectors as an array. | |||||
* @param p the current project | |||||
* @return an array of selectors | |||||
*/ | |||||
public FileSelector[] getSelectors(Project p) { | |||||
FileSelector[] result = new FileSelector[selectorsList.size()]; | |||||
selectorsList.copyInto(result); | |||||
return result; | |||||
} | |||||
/** | |||||
* Returns an enumerator for accessing the set of selectors. | |||||
* @return an enumerator for the selectors | |||||
*/ | |||||
public Enumeration selectorElements() { | |||||
return selectorsList.elements(); | |||||
} | |||||
/** | |||||
* Convert the Selectors within this container to a string. This will | |||||
* just be a helper class for the subclasses that put their own name | |||||
* around the contents listed here. | |||||
* | |||||
* @return comma separated list of Selectors contained in this one | |||||
*/ | |||||
public String toString() { | |||||
StringBuffer buf = new StringBuffer(); | |||||
Enumeration e = selectorElements(); | |||||
if (e.hasMoreElements()) { | |||||
while (e.hasMoreElements()) { | |||||
buf.append(e.nextElement().toString()); | |||||
if (e.hasMoreElements()) { | |||||
buf.append(", "); | |||||
} | |||||
} | |||||
} | |||||
return buf.toString(); | |||||
} | |||||
/** | |||||
* Add a new selector into this container. | |||||
* | |||||
* @param selector the new selector to add | |||||
*/ | |||||
public void appendSelector(FileSelector selector) { | |||||
selectorsList.addElement(selector); | |||||
} | |||||
/** | |||||
* <p> | |||||
* This validates each contained selector | |||||
* provided that the selector implements the validate interface. | |||||
* </p> | |||||
* <p>Ordinarily, this will validate all the elements of a selector | |||||
* container even if the isSelected() method of some elements is | |||||
* never called. This has two effects:</p> | |||||
* <ul> | |||||
* <li>Validation will often occur twice. | |||||
* <li>Since it is not required that selectors derive from | |||||
* BaseSelector, there could be selectors in the container whose | |||||
* error conditions are not detected if their isSelected() call | |||||
* is never made. | |||||
* </ul> | |||||
*/ | |||||
public void validate() { | |||||
Enumeration e = selectorElements(); | |||||
while (e.hasMoreElements()) { | |||||
Object o = e.nextElement(); | |||||
if (o instanceof BaseSelector) { | |||||
((BaseSelector) o).validate(); | |||||
} | |||||
} | |||||
} | |||||
/* Methods below all add specific selectors */ | |||||
/** | |||||
* add a "Select" selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addSelector(SelectSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add an "And" selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addAnd(AndSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add an "Or" selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addOr(OrSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a "Not" selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addNot(NotSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a "None" selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addNone(NoneSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a majority selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addMajority(MajoritySelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a selector date entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addDate(DateSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a selector size entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addSize(SizeSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a selector filename entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addFilename(FilenameSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add an extended selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addCustom(ExtendSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a contains selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addContains(ContainsSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a present selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addPresent(PresentSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a depth selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addDepth(DepthSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a depends selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addDepend(DependSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* adds a different selector to the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addDifferent(DifferentSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* adds a type selector to the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addType(TypeSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add a regular expression selector entry on the selector list | |||||
* @param selector the selector to add | |||||
*/ | |||||
public void addContainsRegexp(ContainsRegexpSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add the modified selector | |||||
* @param selector the selector to add | |||||
* @since ant 1.6 | |||||
*/ | |||||
public void addModified(ModifiedSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
/** | |||||
* add an arbitary selector | |||||
* @param selector the selector to add | |||||
* @since Ant 1.6 | |||||
*/ | |||||
public void add(FileSelector selector) { | |||||
appendSelector(selector); | |||||
} | |||||
} |
@@ -0,0 +1,52 @@ | |||||
/* | |||||
* Copyright 2004 The Apache Software Foundation | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
* | |||||
*/ | |||||
package org.apache.tools.ant.taskdefs.condition; | |||||
import org.apache.tools.ant.BuildFileTest; | |||||
/** | |||||
* Testcase for the <isfileselected> condition. | |||||
* | |||||
*/ | |||||
public class IsFileSelectedTest extends BuildFileTest { | |||||
public IsFileSelectedTest(String name) { | |||||
super(name); | |||||
} | |||||
public void setUp() { | |||||
configureProject("src/etc/testcases/taskdefs/conditions/isfileselected.xml"); | |||||
} | |||||
public void testSimple() { | |||||
executeTarget("simple"); | |||||
} | |||||
public void testName() { | |||||
executeTarget("name"); | |||||
} | |||||
public void testBaseDir() { | |||||
executeTarget("basedir"); | |||||
} | |||||
public void testType() { | |||||
executeTarget("type"); | |||||
} | |||||
public void testNotSelector() { | |||||
expectBuildExceptionContaining( | |||||
"not.selector", "checking for use as a selector (not allowed)", | |||||
"fileset doesn't support the nested \"isfile"); | |||||
} | |||||
} |