From 78ba5f07530c5aaf81dfd8f7643180af7359effd Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 26 Sep 2008 12:00:51 +0000 Subject: [PATCH] Add a firstmatchmapper. PR 44873. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@699288 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 ++ docs/manual/CoreTypes/mapper.html | 33 +++++++++++++ .../tools/ant/types/defaults.properties | 1 + .../tools/ant/util/FirstMatchMapper.java | 48 +++++++++++++++++++ src/tests/antunit/types/firstmatch-test.xml | 41 ++++++++++++++++ 5 files changed, 128 insertions(+) create mode 100644 src/main/org/apache/tools/ant/util/FirstMatchMapper.java create mode 100644 src/tests/antunit/types/firstmatch-test.xml diff --git a/WHATSNEW b/WHATSNEW index 6c737aaf7..edbb93284 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -385,6 +385,11 @@ Other changes: * 's order of results is now predictable. Bugzilla Report 44873 + * a new has been added, which works similar to + but only returns the results of the first nested + mapper that matches. + Bugzilla Report 44873 + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTypes/mapper.html b/docs/manual/CoreTypes/mapper.html index e55652e60..a7cf83773 100644 --- a/docs/manual/CoreTypes/mapper.html +++ b/docs/manual/CoreTypes/mapper.html @@ -887,6 +887,39 @@ list of mapped names reset after every invocation. <mapper type> attribute.

+

firstmatchmapper (since Ant 1.8.0)

+

+ This mapper supports an arbitrary number of nested mappers and + returns the results of the first mapper that matches. This is + different from composite mapper + which collects the results of all matching children.

+Examples: +
+<firstmatchmapper>
+  <globmapper from="*.txt" to="*.bak"/>
+  <globmapper from="*A.*" to="*B.*"/>
+</firstmatchmapper>
+
+ + + + + + + + + + + + + + +
Source file nameTarget file names
foo/bar/A.txtfoo/bar/A.bak
foo/bar/A.javafoo/bar/B.java
+ +

The firstmatchmapper has no corresponding + <mapper type> attribute. +

+ diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index dc4c696c1..61ac3e28d 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -45,6 +45,7 @@ unpackagemapper=org.apache.tools.ant.util.UnPackageNameMapper compositemapper=org.apache.tools.ant.util.CompositeMapper chainedmapper=org.apache.tools.ant.util.ChainedMapper filtermapper=org.apache.tools.ant.types.mappers.FilterMapper +firstmatchmapper=org.apache.tools.ant.util.FirstMatchMapper #this condition is in here because it is the sole #condition defined in Ant1.6 diff --git a/src/main/org/apache/tools/ant/util/FirstMatchMapper.java b/src/main/org/apache/tools/ant/util/FirstMatchMapper.java new file mode 100644 index 000000000..cd85003ae --- /dev/null +++ b/src/main/org/apache/tools/ant/util/FirstMatchMapper.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.util; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; + +/** + * A ContainerMapper that returns the results of its + * first constituent FileNameMappers that matches. + * + * @since Ant 1.8.0 + */ +public class FirstMatchMapper extends ContainerMapper { + + /** {@inheritDoc}. */ + public String[] mapFileName(String sourceFileName) { + for (Iterator iter = getMappers().iterator(); iter.hasNext();) { + FileNameMapper mapper = (FileNameMapper) iter.next(); + if (mapper != null) { + String[] mapped = mapper.mapFileName(sourceFileName); + if (mapped != null) { + return mapped; + } + } + } + return null; + } + +} + diff --git a/src/tests/antunit/types/firstmatch-test.xml b/src/tests/antunit/types/firstmatch-test.xml new file mode 100644 index 000000000..68c332b23 --- /dev/null +++ b/src/tests/antunit/types/firstmatch-test.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + + + + + +