From 9fd3da80a6a942c873be1479785079901536489b Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Thu, 28 Jan 2010 18:53:10 +0000 Subject: [PATCH] Mapper-aware selectors (depends, different, present) now accept typedef'd FileNameMappers git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@904213 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 +- .../ant/types/selectors/MappingSelector.java | 33 +++++++++---- .../ant/types/selectors/PresentSelector.java | 39 ++++++++++------ .../antunit/types/selectors/depend-test.xml | 45 ++++++++++++++++++ .../types/selectors/different-test.xml | 46 +++++++++++++++++++ .../antunit/types/selectors/present-test.xml | 45 ++++++++++++++++++ 6 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 src/tests/antunit/types/selectors/depend-test.xml create mode 100644 src/tests/antunit/types/selectors/different-test.xml create mode 100644 src/tests/antunit/types/selectors/present-test.xml diff --git a/WHATSNEW b/WHATSNEW index ef7a83def..5b02e58d5 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -31,7 +31,7 @@ Fixed bugs: Other changes: -------------- -Changes from Ant 1.7.1 TO Ant 1.8.0RC1 +Changes from Ant 1.7.1 TO Ant 1.8.0RCx ====================================== Changes that could break older environments: @@ -221,6 +221,9 @@ Changes that could break older environments: file. So Ant is now capable of supporting several ProjectHelper implementations, deciding on which to use depending of the input build file. + * Mapper-aware selectors (depends, different, present) now accept typedef'd + FileNameMappers. + Fixed bugs: ----------- diff --git a/src/main/org/apache/tools/ant/types/selectors/MappingSelector.java b/src/main/org/apache/tools/ant/types/selectors/MappingSelector.java index 5135605cd..3b96f1b65 100644 --- a/src/main/org/apache/tools/ant/types/selectors/MappingSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/MappingSelector.java @@ -66,16 +66,29 @@ public abstract class MappingSelector extends BaseSelector { /** * Defines the FileNameMapper to use (nested mapper element). * @return a mapper to be configured - * @throws BuildException if more that one mapper defined + * @throws BuildException if more than one mapper defined */ public Mapper createMapper() throws BuildException { - if (mapperElement != null) { + if (map != null || mapperElement != null) { throw new BuildException("Cannot define more than one mapper"); } mapperElement = new Mapper(getProject()); return mapperElement; } + /** + * Add a configured FileNameMapper instance. + * @param fileNameMapper the FileNameMapper to add + * @throws BuildException if more than one mapper defined + * @since Ant 1.8.0 + */ + public void addConfigured(FileNameMapper fileNameMapper) { + if (map != null || mapperElement != null) { + throw new BuildException("Cannot define more than one mapper"); + } + this.map = fileNameMapper; + } + /** * Checks to make sure all settings are kosher. In this case, it * means that the dest attribute has been set and we have a mapper. @@ -84,13 +97,15 @@ public abstract class MappingSelector extends BaseSelector { if (targetdir == null) { setError("The targetdir attribute is required."); } - if (mapperElement == null) { - map = new IdentityMapper(); - } else { - map = mapperElement.getImplementation(); - } if (map == null) { - setError("Could not set element."); + if (mapperElement == null) { + map = new IdentityMapper(); + } else { + map = mapperElement.getImplementation(); + if (map == null) { + setError("Could not set element."); + } + } } } @@ -121,7 +136,7 @@ public abstract class MappingSelector extends BaseSelector { + targetdir.getName() + " with filename " + filename); } String destname = destfiles[0]; - File destfile = new File(targetdir, destname); + File destfile = FILE_UTILS.resolveFile(targetdir, destname); boolean selected = selectionTest(file, destfile); return selected; diff --git a/src/main/org/apache/tools/ant/types/selectors/PresentSelector.java b/src/main/org/apache/tools/ant/types/selectors/PresentSelector.java index b32578af3..633b128ae 100644 --- a/src/main/org/apache/tools/ant/types/selectors/PresentSelector.java +++ b/src/main/org/apache/tools/ant/types/selectors/PresentSelector.java @@ -24,6 +24,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.types.EnumeratedAttribute; import org.apache.tools.ant.types.Mapper; import org.apache.tools.ant.util.FileNameMapper; +import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.IdentityMapper; /** @@ -35,7 +36,6 @@ import org.apache.tools.ant.util.IdentityMapper; * @since 1.5 */ public class PresentSelector extends BaseSelector { - private File targetdir = null; private Mapper mapperElement = null; private FileNameMapper map = null; @@ -86,17 +86,29 @@ public class PresentSelector extends BaseSelector { /** * Defines the FileNameMapper to use (nested mapper element). * @return a mapper to be configured - * @throws BuildException if more that one mapper defined + * @throws BuildException if more than one mapper defined */ public Mapper createMapper() throws BuildException { - if (mapperElement != null) { + if (map != null || mapperElement != null) { throw new BuildException("Cannot define more than one mapper"); } mapperElement = new Mapper(getProject()); return mapperElement; } - + /** + * Add a configured FileNameMapper instance. + * @param fileNameMapper the FileNameMapper to add + * @throws BuildException if more than one mapper defined + * @since Ant 1.8.0 + */ + public void addConfigured(FileNameMapper fileNameMapper) { + if (map != null || mapperElement != null) { + throw new BuildException("Cannot define more than one mapper"); + } + this.map = fileNameMapper; + } + /** * This sets whether to select a file if its dest file is present. * It could be a negate boolean, but by doing things @@ -123,13 +135,15 @@ public class PresentSelector extends BaseSelector { if (targetdir == null) { setError("The targetdir attribute is required."); } - if (mapperElement == null) { - map = new IdentityMapper(); - } else { - map = mapperElement.getImplementation(); - } if (map == null) { - setError("Could not set element."); + if (mapperElement == null) { + map = new IdentityMapper(); + } else { + map = mapperElement.getImplementation(); + if (map == null) { + setError("Could not set element."); + } + } } } @@ -160,7 +174,7 @@ public class PresentSelector extends BaseSelector { + targetdir + " with filename " + filename); } String destname = destfiles[0]; - File destfile = new File(targetdir, destname); + File destfile = FileUtils.getFileUtils().resolveFile(targetdir, destname); return destfile.exists() == destmustexist; } @@ -173,9 +187,8 @@ public class PresentSelector extends BaseSelector { * @return the values as an array of strings */ public String[] getValues() { - return new String[]{"srconly", "both"}; + return new String[] { "srconly", "both" }; } } } - diff --git a/src/tests/antunit/types/selectors/depend-test.xml b/src/tests/antunit/types/selectors/depend-test.xml new file mode 100644 index 000000000..e868f6a65 --- /dev/null +++ b/src/tests/antunit/types/selectors/depend-test.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/antunit/types/selectors/different-test.xml b/src/tests/antunit/types/selectors/different-test.xml new file mode 100644 index 000000000..c2542d2f9 --- /dev/null +++ b/src/tests/antunit/types/selectors/different-test.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + foo + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/antunit/types/selectors/present-test.xml b/src/tests/antunit/types/selectors/present-test.xml new file mode 100644 index 000000000..e31c722dd --- /dev/null +++ b/src/tests/antunit/types/selectors/present-test.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +