Author: bodewig
Date: Mon Sep 20 15:51:18 2010
New Revision: 998962
URL: http://svn.apache.org/viewvc?rev=998962&view=rev
Log:
Add a mapper similar to wget --cut-dirs, based on user-list discussion
initiated by Michael Ludwig
Added:
ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java
(with props)
ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml (with props)
Modified:
ant/core/trunk/WHATSNEW
ant/core/trunk/docs/manual/Types/mapper.html
ant/core/trunk/src/main/org/apache/tools/ant/types/defaults.properties
Modified: ant/core/trunk/WHATSNEW
URL:
http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?rev=998962&r1=998961&r2=998962&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Mon Sep 20 15:51:18 2010
@@ -203,6 +203,9 @@ Other changes:
and singleline attributes.
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
===================================
Modified: ant/core/trunk/docs/manual/Types/mapper.html
URL:
http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/Types/mapper.html?rev=998962&r1=998961&r2=998962&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/Types/mapper.html (original)
+++ ant/core/trunk/docs/manual/Types/mapper.html Mon Sep 20 15:51:18 2010
@@ -919,7 +919,44 @@ list of mapped names reset after every i
<code><mapper <b>type</b>></code> attribute.
</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>
</html>
Modified: ant/core/trunk/src/main/org/apache/tools/ant/types/defaults.properties
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/defaults.properties?rev=998962&r1=998961&r2=998962&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/types/defaults.properties
(original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/types/defaults.properties Mon
Sep 20 15:51:18 2010
@@ -46,6 +46,7 @@ compositemapper=org.apache.tools.ant.uti
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
Added:
ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java?rev=998962&view=auto
==============================================================================
---
ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java
(added)
+++
ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java
Mon Sep 20 15:51:18 2010
@@ -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/%3c51772743bea5d44a9ea5bf52aaddd6fb010e9...@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) };
+ }
+}
Propchange:
ant/core/trunk/src/main/org/apache/tools/ant/types/mappers/CutDirsMapper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml
URL:
http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml?rev=998962&view=auto
==============================================================================
--- ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml (added)
+++ ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml Mon Sep 20 15:51:18
2010
@@ -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>
\ No newline at end of file
Propchange: ant/core/trunk/src/tests/antunit/types/cutdirs-test.xml
------------------------------------------------------------------------------
svn:eol-style = native