diff --git a/WHATSNEW b/WHATSNEW index 056ab2ad9..36788ad67 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -95,6 +95,8 @@ Other changes: * Added preserveLastModified attribute to signjar task. Bugzilla report 30987. +* Added isfileselected condition. + Changes from Ant 1.6.2 to current Ant 1.6 CVS version ===================================================== diff --git a/docs/manual/CoreTasks/conditions.html b/docs/manual/CoreTasks/conditions.html index aed1ede9a..2e5081510 100644 --- a/docs/manual/CoreTasks/conditions.html +++ b/docs/manual/CoreTasks/conditions.html @@ -356,6 +356,42 @@ that is "true","yes", or "on"

No +

isfileselected

+

+ Test whether a file passes an embedded selector. +

+

+ This condition has been added in Apache Ant 1.7. +

+ + + + + + + + + + + + + + + + +
AttributeDescriptionRequired
file + The file to check if is passes the embedded selector. + Yes
basedirThe base directory to use for name based selectors. It this is not set, + the project's basedirectory will be used.No
+

+ Example usage: +

+
+
+    <isfileselected file="a.xml">
+      <date datetime="06/28/2000 2:02 pm" when="equal"/>
+    </isfileselected>
+  

typefound

Test whether a given type is defined, and that diff --git a/src/etc/testcases/taskdefs/conditions/isfileselected.xml b/src/etc/testcases/taskdefs/conditions/isfileselected.xml new file mode 100644 index 000000000..7b13aecf6 --- /dev/null +++ b/src/etc/testcases/taskdefs/conditions/isfileselected.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/condition/IsFileSelected.java b/src/main/org/apache/tools/ant/taskdefs/condition/IsFileSelected.java new file mode 100644 index 000000000..35787a742 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/condition/IsFileSelected.java @@ -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); + } +} diff --git a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java new file mode 100644 index 000000000..4265e407e --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java @@ -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); + } + + /** + *

+ * This validates each contained selector + * provided that the selector implements the validate interface. + *

+ *

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:

+ * + */ + 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); + } + +} diff --git a/src/testcases/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java b/src/testcases/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java new file mode 100644 index 000000000..9eef95e05 --- /dev/null +++ b/src/testcases/org/apache/tools/ant/taskdefs/condition/IsFileSelectedTest.java @@ -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"); + } +}