Skip to content

Commit 6a843bd

Browse files
tcollignonplamentotev
authored andcommitted
[MASSEMBLY-617] : Add new FileMapper for giving a suffix to filename -> SuffixFileMapper
Closes #14
1 parent 523bc52 commit 6a843bd

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</parent>
99

1010
<artifactId>plexus-io</artifactId>
11-
<version>3.0.2-SNAPSHOT</version>
11+
<version>3.1.0-SNAPSHOT</version>
1212

1313
<name>Plexus IO Components</name>
1414

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.codehaus.plexus.components.io.filemappers;
2+
3+
/*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import javax.annotation.Nonnull;
18+
19+
/**
20+
* A file mapper, which maps by adding a suffix to the filename.
21+
* If the filename contains dot, the suffix will be added before.
22+
* Example : directory/archive.tar.gz => directory/archivesuffix.tar.gz
23+
*/
24+
public class SuffixFileMapper extends AbstractFileMapper
25+
{
26+
/**
27+
* The suffix mappers role-hint: "suffix".
28+
*/
29+
public static final String ROLE_HINT = "suffix";
30+
31+
private String suffix;
32+
33+
/**
34+
* Returns the suffix to add.
35+
*/
36+
public String getSuffix()
37+
{
38+
return suffix;
39+
}
40+
41+
/**
42+
* Sets the suffix to add.
43+
*/
44+
public void setSuffix( String suffix )
45+
{
46+
if ( suffix == null )
47+
{
48+
throw new IllegalArgumentException( "The suffix is null." );
49+
}
50+
this.suffix = suffix;
51+
}
52+
53+
@Nonnull
54+
public String getMappedFileName( @Nonnull String pName)
55+
{
56+
final String name = super.getMappedFileName( pName );
57+
if ( suffix == null )
58+
{
59+
throw new IllegalStateException( "The suffix has not been set." );
60+
}
61+
final int dirSep = Math.max( name.lastIndexOf( '/' ), name.lastIndexOf( '\\' ) );
62+
String filename = dirSep > 0 ? name.substring( dirSep + 1 ) : name;
63+
String dirname = dirSep > 0 ? name.substring( 0, dirSep + 1 ) : "";
64+
if ( filename.contains( "." ) )
65+
{
66+
String beforeExtension = filename.substring( 0, filename.indexOf( '.' ) );
67+
String afterExtension = filename.substring( filename.indexOf( '.' ) + 1 ) ;
68+
return dirname + beforeExtension + suffix + "." + afterExtension;
69+
}
70+
return name + suffix;
71+
}
72+
}

src/main/resources/META-INF/plexus/components.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<instantiation-strategy>per-lookup</instantiation-strategy>
4343
<configuration/>
4444
</component>
45+
<component>
46+
<role>org.codehaus.plexus.components.io.filemappers.FileMapper</role>
47+
<role-hint>suffix</role-hint>
48+
<implementation>org.codehaus.plexus.components.io.filemappers.SuffixFileMapper</implementation>
49+
<instantiation-strategy>per-lookup</instantiation-strategy>
50+
<configuration/>
51+
</component>
4552
<component>
4653
<role>org.codehaus.plexus.components.io.filemappers.FileMapper</role>
4754
<role-hint>regexp</role-hint>

src/site/apt/filemappers.apt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ File Mappers
3131
* The {{{#Merging File Mapper}Merging File Mapper}}; its role hint is
3232
"merge".
3333

34+
* The {{{#Suffix File Mapper}Suffix File Mapper}}; its role hint is
35+
"suffix".
36+
3437
* {Identity Mapper}
3538

3639
The {{{./apidocs/org/codehaus/plexus/components/io/filemappers/IdentityMapper.html}identity mapper}}
@@ -94,3 +97,26 @@ File Mappers
9497
-----------------------------------------------------------------------------
9598

9699
The merging file mapper uses the role hint "merge".
100+
101+
* {Suffix File Mapper}
102+
103+
The {{{./apidocs/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.html}suffix
104+
file mapper}} adds the given suffix to the filename. The suffix will be added before the file
105+
extension. Examples :
106+
107+
-----------------------------------------------------------------------------
108+
theFile.txt => theFileNiceSuffix.txt
109+
dir/file.java => dir/fileNiceSuffix.java
110+
fileWithoutExtension => fileWithoutExtensionNiceSuffix
111+
dir/archive.tar.gz => dir/archiveNiceSuffix.tar.gz
112+
-----------------------------------------------------------------------------
113+
114+
It would be configured as follows:
115+
116+
-----------------------------------------------------------------------------
117+
<fileMapper implementation="org.codehaus.plexus.components.io.filemappers.SuffixFileMapper">
118+
<suffix>NiceSuffix</suffix>
119+
</fileMapper>
120+
-----------------------------------------------------------------------------
121+
122+
The suffix file mapper uses the role hint "suffix".

src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
import java.lang.reflect.UndeclaredThrowableException;
20+
import java.util.Arrays;
2021

2122
import org.codehaus.plexus.PlexusTestCase;
2223

@@ -165,6 +166,23 @@ public void testPrefixMapper() throws Exception
165166
testFileMapper( mapper, SAMPLES, results );
166167
}
167168

169+
public void testSuffixMapper() throws Exception
170+
{
171+
final String suffix = "suffix";
172+
String[] samples = Arrays.copyOf( SAMPLES, SAMPLES.length + 2 );
173+
samples[samples.length - 2] = "archive.tar.gz";
174+
samples[samples.length - 1] = "directory/archive.tar.gz";
175+
String[] results = new String[] { null, null, "asuffix", "xyzsuffix.gif", "b/asuffix", "b/xyzsuffix.gif",
176+
"b\\asuffix", "b\\xyzsuffix.gif", "c.c/asuffix", "c.c/xyzsuffix.gif", "c.c\\asuffix", "c.c\\xyzsuffix.gif",
177+
"archivesuffix.tar.gz", "directory/archivesuffix.tar.gz" };
178+
SuffixFileMapper mapper = new SuffixFileMapper();
179+
mapper.setSuffix( suffix );
180+
testFileMapper( mapper, samples, results );
181+
mapper = (SuffixFileMapper) lookup( FileMapper.ROLE, SuffixFileMapper.ROLE_HINT );
182+
mapper.setSuffix( suffix );
183+
testFileMapper( mapper, samples, results );
184+
}
185+
168186
private RegExpFileMapper configure( RegExpFileMapper pMapper, String pPattern, String pReplacement )
169187
{
170188
pMapper.setPattern( pPattern );

0 commit comments

Comments
 (0)