git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@699288 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -385,6 +385,11 @@ Other changes: | |||||
| * <compositemapper>'s order of results is now predictable. | * <compositemapper>'s order of results is now predictable. | ||||
| Bugzilla Report 44873 | Bugzilla Report 44873 | ||||
| * a new <firstmatchmapper> has been added, which works similar to | |||||
| <compositemapper> 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 | Changes from Ant 1.7.0 TO Ant 1.7.1 | ||||
| ============================================= | ============================================= | ||||
| @@ -887,6 +887,39 @@ list of mapped names reset after every invocation. | |||||
| <code><mapper <b>type</b>></code> attribute. | <code><mapper <b>type</b>></code> attribute. | ||||
| </p> | </p> | ||||
| <h4><a name="firstmatch-mapper">firstmatchmapper (since Ant 1.8.0)</a></h4> | |||||
| <p> | |||||
| This mapper supports an arbitrary number of nested mappers and | |||||
| returns the results of the first mapper that matches. This is | |||||
| different from <a href="#composite-mapper">composite mapper</a> | |||||
| which collects the results of all matching children.</p> | |||||
| <b>Examples:</b> | |||||
| <blockquote><pre> | |||||
| <firstmatchmapper> | |||||
| <globmapper from="*.txt" to="*.bak"/> | |||||
| <globmapper from="*A.*" to="*B.*"/> | |||||
| </firstmatchmapper> | |||||
| </pre></blockquote> | |||||
| <table border="1" cellpadding="2" cellspacing="0"> | |||||
| <tr> | |||||
| <td valign="top"><b>Source file name</b></td> | |||||
| <td valign="top"><b>Target file names</b></td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="center"><code>foo/bar/A.txt</code></td> | |||||
| <td valign="top"><code>foo/bar/A.bak</code></td> | |||||
| </tr> | |||||
| <tr> | |||||
| <td valign="center"><code>foo/bar/A.java</code></td> | |||||
| <td valign="top"><code>foo/bar/B.java</code></td> | |||||
| </tr> | |||||
| </table> | |||||
| <p>The firstmatchmapper has no corresponding | |||||
| <code><mapper <b>type</b>></code> attribute. | |||||
| </p> | |||||
| </body> | </body> | ||||
| @@ -45,6 +45,7 @@ unpackagemapper=org.apache.tools.ant.util.UnPackageNameMapper | |||||
| compositemapper=org.apache.tools.ant.util.CompositeMapper | compositemapper=org.apache.tools.ant.util.CompositeMapper | ||||
| chainedmapper=org.apache.tools.ant.util.ChainedMapper | chainedmapper=org.apache.tools.ant.util.ChainedMapper | ||||
| filtermapper=org.apache.tools.ant.types.mappers.FilterMapper | 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 | #this condition is in here because it is the sole | ||||
| #condition defined in Ant1.6 | #condition defined in Ant1.6 | ||||
| @@ -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 <CODE>ContainerMapper</CODE> that returns the results of its | |||||
| * first constituent <CODE>FileNameMapper</CODE>s 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; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,41 @@ | |||||
| <?xml version="1.0"?> | |||||
| <!-- | |||||
| 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. | |||||
| --> | |||||
| <project xmlns:au="antlib:org.apache.ant.antunit" | |||||
| name="firstmatch-test" | |||||
| default="antunit"> | |||||
| <import file="../antunit-base.xml" /> | |||||
| <target name="testMatch"> | |||||
| <mkdir dir="${input}"/> | |||||
| <touch file="${input}/A"/> | |||||
| <touch file="${input}/B"/> | |||||
| <mkdir dir="${output}"/> | |||||
| <copy todir="${output}" enablemultiplemappings="true"> | |||||
| <fileset dir="${input}"/> | |||||
| <firstmatchmapper> | |||||
| <globmapper from="A" to="A.txt"/> | |||||
| <identitymapper/> | |||||
| </firstmatchmapper> | |||||
| </copy> | |||||
| <au:assertFileExists file="${output}/A.txt"/> | |||||
| <au:assertFileExists file="${output}/B"/> | |||||
| <au:assertFileDoesntExist file="${output}/A"/> | |||||
| <au:assertFileDoesntExist file="${output}/B.txt"/> | |||||
| </target> | |||||
| </project> | |||||