diff --git a/WHATSNEW b/WHATSNEW index 058658723..d5a79ae95 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -203,6 +203,9 @@ Other changes: and singleline attributes. Bugzilla Report 49764. + * A new 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 =================================== diff --git a/docs/manual/Types/mapper.html b/docs/manual/Types/mapper.html index 862708a2a..f28d46a3e 100644 --- a/docs/manual/Types/mapper.html +++ b/docs/manual/Types/mapper.html @@ -919,7 +919,44 @@ list of mapped names reset after every invocation. <mapper type> attribute.

+

cutdirsmapper (since Ant 1.8.2)

+

This mapper strips a configured number of leading directories from + the source file name.

+ +Examples: +
+<cutdirsmapper dirs="1"/>
+
+ + + + + + + + + + +
Source file nameTarget file names
foo/bar/A.txtbar/A.txt
+ +

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

+ + + + + + + + + + + + +
AttributeDescriptionRequired
dirs + Number of directories to strip (must be a positive number). + Yes
diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index acd064615..e85109cb3 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -46,6 +46,7 @@ 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 +cutdirsmapper=org.apache.tools.ant.types.mappers.CutDirsMapper #this condition is in here because it is the sole #condition defined in Ant1.6 diff --git a/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java b/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java new file mode 100644 index 000000000..fef495601 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java @@ -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. + * + *

This mapper was inspired by a user-list thread that mentioned + * wget's --cut-dirs option.

+ * + * @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) }; + } +} diff --git a/src/tests/antunit/types/cutdirs-test.xml b/src/tests/antunit/types/cutdirs-test.xml new file mode 100644 index 000000000..64f0652bb --- /dev/null +++ b/src/tests/antunit/types/cutdirs-test.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file