Browse Source

Add a firstmatchmapper. PR 44873.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@699288 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 16 years ago
parent
commit
78ba5f0753
5 changed files with 128 additions and 0 deletions
  1. +5
    -0
      WHATSNEW
  2. +33
    -0
      docs/manual/CoreTypes/mapper.html
  3. +1
    -0
      src/main/org/apache/tools/ant/types/defaults.properties
  4. +48
    -0
      src/main/org/apache/tools/ant/util/FirstMatchMapper.java
  5. +41
    -0
      src/tests/antunit/types/firstmatch-test.xml

+ 5
- 0
WHATSNEW View File

@@ -385,6 +385,11 @@ Other changes:
* <compositemapper>'s order of results is now predictable.
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
=============================================



+ 33
- 0
docs/manual/CoreTypes/mapper.html View File

@@ -887,6 +887,39 @@ list of mapped names reset after every invocation.
<code>&lt;mapper <b>type</b>&gt;</code> attribute.
</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>
&lt;firstmatchmapper&gt;
&lt;globmapper from="*.txt" to="*.bak"/&gt;
&lt;globmapper from="*A.*" to="*B.*"/&gt;
&lt;/firstmatchmapper&gt;
</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>&lt;mapper <b>type</b>&gt;</code> attribute.
</p>



</body>


+ 1
- 0
src/main/org/apache/tools/ant/types/defaults.properties View File

@@ -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


+ 48
- 0
src/main/org/apache/tools/ant/util/FirstMatchMapper.java View File

@@ -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;
}

}


+ 41
- 0
src/tests/antunit/types/firstmatch-test.xml View File

@@ -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>

Loading…
Cancel
Save