Skip to content

[MASSEMBLY-617] : Add new FileMapper for giving a suffix to filename … #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plexus/components.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@
<instantiation-strategy>per-lookup</instantiation-strategy>
<configuration/>
</component>
<component>
<role>org.codehaus.plexus.components.io.filemappers.FileMapper</role>
<role-hint>suffix</role-hint>
<implementation>org.codehaus.plexus.components.io.filemappers.SuffixFileMapper</implementation>
<instantiation-strategy>per-lookup</instantiation-strategy>
<configuration/>
</component>
<component>
<role>org.codehaus.plexus.components.io.filemappers.FileMapper</role>
<role-hint>regexp</role-hint>
Expand Down
27 changes: 27 additions & 0 deletions src/site/apt/filemappers.apt
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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:

-----------------------------------------------------------------------------
<fileMapper implementation="org.codehaus.plexus.components.io.filemappers.SuffixFileMapper">
<suffix>NiceSuffix</suffix>
</fileMapper>
-----------------------------------------------------------------------------

The suffix file mapper uses the role hint "suffix".
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;

import org.codehaus.plexus.PlexusTestCase;

Expand Down Expand Up @@ -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 )
{
Expand Down