diff --git a/WHATSNEW b/WHATSNEW index abf634bd4..c760f2f4f 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -976,6 +976,10 @@ Other changes: * can now calculate relative paths. + * The selector supports a new handleDirSep attribute that + makes it ignore differences between / and \ separators. + Bugzilla Report 47858. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index a4417450d..789835f5d 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -545,6 +545,16 @@ platforms. Whether name comparisons are case-sensitive No, default true + + handledirsep + + If this is specified, the mapper will treat a \ character in a + resource name or name attribute as a / for the purposes of + matching. This attribute can be true or false, the default is + false. + Since Ant 1.8.0. + No +

exists

diff --git a/src/main/org/apache/tools/ant/types/resources/selectors/Name.java b/src/main/org/apache/tools/ant/types/resources/selectors/Name.java index 12e87ebf2..c575a0b13 100644 --- a/src/main/org/apache/tools/ant/types/resources/selectors/Name.java +++ b/src/main/org/apache/tools/ant/types/resources/selectors/Name.java @@ -31,6 +31,7 @@ public class Name implements ResourceSelector { private String regex = null; private String pattern; private boolean cs = true; + private boolean handleDirSep = false; // caches for performance reasons private RegularExpression reg; @@ -61,6 +62,7 @@ public class Name implements ResourceSelector { /** * Set the regular expression to compare names against. * @param r the regex to set. + * @since Ant 1.8.0 */ public void setRegex(String r) { regex = r; @@ -70,6 +72,7 @@ public class Name implements ResourceSelector { /** * Get the regular expression used by this Name ResourceSelector. * @return the String selection pattern. + * @since Ant 1.8.0 */ public String getRegex() { return regex; @@ -91,6 +94,26 @@ public class Name implements ResourceSelector { return cs; } + /** + * Attribute specifing whether to ignore the difference + * between / and \ (the two common directory characters). + * @param handleDirSep a boolean, default is false. + * @since Ant 1.8.0 + */ + public void setHandleDirSep(boolean handleDirSep) { + this.handleDirSep = handleDirSep; + } + + /** + * Whether the difference between / and \ (the two common + * directory characters) is ignored. + * + * @since Ant 1.8.0 + */ + public boolean doesHandledirSep() { + return handleDirSep; + } + /** * Return true if this Resource is selected. * @param r the Resource to check. @@ -107,7 +130,7 @@ public class Name implements ResourceSelector { private boolean matches(String name) { if (pattern != null) { - return SelectorUtils.match(pattern, name, cs); + return SelectorUtils.match(modify(pattern), modify(name), cs); } else { if (reg == null) { reg = new RegularExpression(); @@ -118,7 +141,14 @@ public class Name implements ResourceSelector { if (!cs) { options |= Regexp.MATCH_CASE_INSENSITIVE; } - return expression.matches(name, options); + return expression.matches(modify(name), options); + } + } + + private String modify(String s) { + if (s == null || !handleDirSep || s.indexOf("\\") == -1) { + return s; } + return s.replace('\\', '/'); } } diff --git a/src/tests/antunit/types/resources/selectors/name-test.xml b/src/tests/antunit/types/resources/selectors/name-test.xml index f85458f0e..84cbedb4a 100644 --- a/src/tests/antunit/types/resources/selectors/name-test.xml +++ b/src/tests/antunit/types/resources/selectors/name-test.xml @@ -15,8 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. --> - + @@ -32,7 +31,7 @@ - + @@ -40,7 +39,7 @@ - + @@ -51,9 +50,41 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +