git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@998962 13f79535-47bb-0310-9956-ffa450edef68master
@@ -203,6 +203,9 @@ Other changes: | |||||
and singleline attributes. | and singleline attributes. | ||||
Bugzilla Report 49764. | Bugzilla Report 49764. | ||||
* A new <cutdirsmapper> can be used like wget's --cut-dirs option to | |||||
strip leading directories from file names. | |||||
Changes from Ant 1.8.0 TO Ant 1.8.1 | Changes from Ant 1.8.0 TO Ant 1.8.1 | ||||
=================================== | =================================== | ||||
@@ -919,7 +919,44 @@ 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="cutdirs-mapper">cutdirsmapper (since Ant 1.8.2)</a></h4> | |||||
<p>This mapper strips a configured number of leading directories from | |||||
the source file name.</p> | |||||
<b>Examples:</b> | |||||
<blockquote><pre> | |||||
<cutdirsmapper dirs="1"/> | |||||
</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>bar/A.txt</code></td> | |||||
</tr> | |||||
</table> | |||||
<p>The cutdirsmapper has no | |||||
corresponding <code><mapper <b>type</b>></code> attribute.</p> | |||||
<table border="1" cellpadding="2" cellspacing="0"> | |||||
<tr> | |||||
<td valign="top"><b>Attribute</b></td> | |||||
<td valign="top"><b>Description</b></td> | |||||
<td align="center" valign="top"><b>Required</b></td> | |||||
</tr> | |||||
<tr> | |||||
<td valign="top">dirs</td> | |||||
<td valign="top"> | |||||
Number of directories to strip (must be a positive number). | |||||
</td> | |||||
<td align="center" valign="top">Yes</td> | |||||
</tr> | |||||
</table> | |||||
</body> | </body> | ||||
</html> | </html> |
@@ -46,6 +46,7 @@ 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 | firstmatchmapper=org.apache.tools.ant.util.FirstMatchMapper | ||||
cutdirsmapper=org.apache.tools.ant.types.mappers.CutDirsMapper | |||||
#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,74 @@ | |||||
/* | |||||
* 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.types.mappers; | |||||
import java.io.File; | |||||
import org.apache.tools.ant.BuildException; | |||||
import org.apache.tools.ant.util.FileNameMapper; | |||||
/** | |||||
* A mapper that strips of the a configurable number of leading | |||||
* directories from a file name. | |||||
* | |||||
* <p>This mapper was inspired by a user-list thread that mentioned | |||||
* wget's --cut-dirs option.</p> | |||||
* | |||||
* @see http://mail-archives.apache.org/mod_mbox/ant-user/201009.mbox/%3C51772743BEA5D44A9EA5BF52AADDD6FB010E96F6@hammai008.delphi.local%3E | |||||
*/ | |||||
public class CutDirsMapper implements FileNameMapper { | |||||
private int dirs = 0; | |||||
/** | |||||
* The number of leading directories to cut. | |||||
*/ | |||||
public void setDirs(int dirs) { | |||||
this.dirs = dirs; | |||||
} | |||||
/** | |||||
* Empty implementation. | |||||
* @param ignore ignored. | |||||
*/ | |||||
public void setFrom(String ignore) { | |||||
} | |||||
/** | |||||
* Empty implementation. | |||||
* @param ignore ignored. | |||||
*/ | |||||
public void setTo(String ignore) { | |||||
} | |||||
/** {@inheritDoc}. */ | |||||
public String[] mapFileName(final String sourceFileName) { | |||||
if (dirs <= 0) { | |||||
throw new BuildException("dirs must be set to a positive number"); | |||||
} | |||||
char fileSep = File.separatorChar; | |||||
String fileSepCorrected = | |||||
sourceFileName.replace('/', fileSep).replace('\\', fileSep); | |||||
int nthMatch = fileSepCorrected.indexOf(fileSep); | |||||
for (int n = 1; nthMatch > -1 && n < dirs; n++) { | |||||
nthMatch = fileSepCorrected.indexOf(fileSep, nthMatch + 1); | |||||
} | |||||
if (nthMatch == -1) { | |||||
return null; | |||||
} | |||||
return new String[] { sourceFileName.substring(nthMatch + 1) }; | |||||
} | |||||
} |
@@ -0,0 +1,56 @@ | |||||
<?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="setUp"> | |||||
<mkdir dir="${input}"/> | |||||
<mkdir dir="${output}"/> | |||||
</target> | |||||
<target name="testCut" depends="setUp"> | |||||
<mkdir dir="${input}/a/b/c"/> | |||||
<touch file="${input}/a/b/D"/> | |||||
<touch file="${input}/a/b/c/E"/> | |||||
<copy todir="${output}"> | |||||
<fileset dir="${input}"/> | |||||
<cutdirsmapper dirs="2"/> | |||||
</copy> | |||||
<au:assertFileExists file="${output}/D"/> | |||||
<au:assertFileExists file="${output}/c/E"/> | |||||
</target> | |||||
<target name="testCutTooManyLevels" depends="setUp"> | |||||
<mkdir dir="${input}/a/b/c"/> | |||||
<touch file="${input}/a/b/D"/> | |||||
<touch file="${input}/a/b/c/E"/> | |||||
<copy todir="${output}"> | |||||
<fileset dir="${input}"/> | |||||
<cutdirsmapper dirs="5"/> | |||||
</copy> | |||||
<au:assertTrue> | |||||
<resourcecount count="0"> | |||||
<fileset dir="${output}"/> | |||||
</resourcecount> | |||||
</au:assertTrue> | |||||
</target> | |||||
</project> |