diff --git a/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java new file mode 100644 index 00000000..3d8e4258 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -0,0 +1,72 @@ +package org.codehaus.plexus.components.io.filemappers; + +/* + * Licensed 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. + */ + +import javax.annotation.Nonnull; + +/** + * A file mapper, which maps by adding a suffix to the filename. + * If the filename contains dot, the suffix will be added before. + * Example : directory/archive.tar.gz => directory/archivesuffix.tar.gz + */ +public class SuffixFileMapper extends AbstractFileMapper +{ + /** + * The suffix mappers role-hint: "suffix". + */ + public static final String ROLE_HINT = "suffix"; + + private String suffix; + + /** + * Returns the suffix to add. + */ + public String getSuffix() + { + return suffix; + } + + /** + * Sets the suffix to add. + */ + public void setSuffix( String suffix ) + { + if ( suffix == null ) + { + throw new IllegalArgumentException( "The suffix is null." ); + } + this.suffix = suffix; + } + + @Nonnull + public String getMappedFileName( @Nonnull String pName) + { + final String name = super.getMappedFileName( pName ); + if ( suffix == null ) + { + throw new IllegalStateException( "The suffix has not been set." ); + } + final int dirSep = Math.max( name.lastIndexOf( '/' ), name.lastIndexOf( '\\' ) ); + String filename = dirSep > 0 ? name.substring( dirSep +1 ) : name; + String dirname = dirSep > 0 ? name.substring( 0, dirSep +1 ) : ""; + if ( filename.contains( "." ) ) + { + String beforeExtension = filename.substring( 0, filename.indexOf( '.' ) ); + String afterExtension = filename.substring( filename.indexOf( '.' ) + 1 ) ; + return dirname + beforeExtension + suffix + "." + afterExtension; + } + return name + suffix; + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/plexus/components.xml b/src/main/resources/META-INF/plexus/components.xml index e5e28754..544289a7 100644 --- a/src/main/resources/META-INF/plexus/components.xml +++ b/src/main/resources/META-INF/plexus/components.xml @@ -42,6 +42,13 @@ per-lookup + + org.codehaus.plexus.components.io.filemappers.FileMapper + suffix + org.codehaus.plexus.components.io.filemappers.SuffixFileMapper + per-lookup + + org.codehaus.plexus.components.io.filemappers.FileMapper regexp diff --git a/src/site/apt/filemappers.apt b/src/site/apt/filemappers.apt index f716d2a9..391378a8 100644 --- a/src/site/apt/filemappers.apt +++ b/src/site/apt/filemappers.apt @@ -30,6 +30,10 @@ File Mappers * The {{{#Merging File Mapper}Merging File Mapper}}; its role hint is "merge". + + * The {{{#Suffix File Mapper}Suffix File Mapper}}; its role hint is + "suffix". + * {Identity Mapper} @@ -94,3 +98,26 @@ File Mappers ----------------------------------------------------------------------------- The merging file mapper uses the role hint "merge". + +* {Suffix File Mapper} + + The {{{./apidocs/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.html}suffix + file mapper}} adds the given suffix to the filename. The suffix will be added before the file + extension. Examples : + +----------------------------------------------------------------------------- + theFile.txt => theFileNiceSuffix.txt + dir/file.java => dir/fileNiceSuffix.java + fileWithoutExtension => fileWithoutExtensionNiceSuffix + dir/archive.tar.gz => dir/archiveNiceSuffix.tar.gz +----------------------------------------------------------------------------- + + It would be configured as follows: + +----------------------------------------------------------------------------- + + NiceSuffix + +----------------------------------------------------------------------------- + + The suffix file mapper uses the role hint "suffix". diff --git a/src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java b/src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java index 4830d7e7..8c953acb 100644 --- a/src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java +++ b/src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java @@ -17,6 +17,7 @@ */ import java.lang.reflect.UndeclaredThrowableException; +import java.util.Arrays; import org.codehaus.plexus.PlexusTestCase; @@ -164,6 +165,22 @@ public void testPrefixMapper() throws Exception mapper.setPrefix( prefix ); testFileMapper( mapper, SAMPLES, results ); } + + public void testSuffixMapper() throws Exception + { + final String suffix = "suffix"; + String[] samples = Arrays.copyOf( SAMPLES, SAMPLES.length + 2 ); + samples[samples.length - 2] = "archive.tar.gz"; + samples[samples.length - 1] = "directory/archive.tar.gz"; + String[] results = new String[] { null, null, "asuffix", "xyzsuffix.gif", "b/asuffix", "b/xyzsuffix.gif", "b\\asuffix", + "b\\xyzsuffix.gif", "c.c/asuffix", "c.c/xyzsuffix.gif", "c.c\\asuffix", "c.c\\xyzsuffix.gif", "archivesuffix.tar.gz", "directory/archivesuffix.tar.gz" }; + SuffixFileMapper mapper = new SuffixFileMapper(); + mapper.setSuffix( suffix ); + testFileMapper( mapper, samples, results ); + mapper = (SuffixFileMapper) lookup( FileMapper.ROLE, SuffixFileMapper.ROLE_HINT ); + mapper.setSuffix( suffix ); + testFileMapper( mapper, samples, results ); + } private RegExpFileMapper configure( RegExpFileMapper pMapper, String pPattern, String pReplacement ) {