Skip to content
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

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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,81 @@
package org.codehaus.plexus.components.io.filemappers;

/*
* Copyright 2007 The Codehaus Foundation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just noticed that this is the old license header. Judging by the other new files in the codehaus-plexus project you can just delete this line and keep the rest (the Apache license).

*
* 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;

import org.codehaus.plexus.util.StringUtils;

/**
* A file mapper, which maps by adding a suffix.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice if make it clear that the suffix is added before the dot. It is mentioned in the site docs but it would be nice if you state it here as well (so it is visible in the IDEs).

*/
public class SuffixFileMapper extends AbstractFileMapper
{
/**
* The suffix mappers role-hint: "suffix".
*/
public static final String ROLE_HINT = "suffix";

private String suffix;

@Nonnull public String getMappedFileName( @Nonnull String name )
{
final String s = super.getMappedFileName( name ); // Check for null, etc.
return getMappedFileName( suffix, s );
}

/**
* Returns the suffix to add.
*/
public String getSuffix()
{
return suffix;
}

/**
* Sets the suffix to add.
*/
public void setSuffix( String suffix )
{
this.suffix = suffix;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As null is not valid value you can add check if suffix is null:

if ( suffix == null )
{
    throw new IllegalArgumentException( "The suffix is null." );
}

Not all marchers all have that (the regex one does not have) but I think as most have it is better this way.

}

/**
* Performs the mapping of a file name by adding a suffix.
*/
public static String getMappedFileName( String suffix, String name )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you followed the PrefixFileMapper code, but the rest does not have public static String getMappedFileName and I think it would be best to be consistent with them as PrefixFileMapper is a kind of exception. If you don't have other concerns I think it's best to move it to public String getMappedFileName( @Nonnull String name ).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I have no problem with that
Indeed like you said I'm basically inspired of PrefixFileMapper because I didn't know plexus-io :(

{
String nameWithSuffix = name;
if ( StringUtils.isNotBlank( suffix ) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of the mappers throw IllegalStateException if any of the params is not initialized. Would be nice if this mapper is consistent with the others (PrefixFileMapper is exception but I think it would be better if it stays the only exception).

{
final int dirSep = Math.max( name.lastIndexOf( '/' ), name.lastIndexOf( '\\' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to split the file name into file name and directory name. See FileExtensionMapper for example. But feel free to leave it like that if you think it is more readable this way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes actually I try to do the same thing as FileExtensionMapper but I want to handle the case of multiple "." in extension (like tar.gz). So I have added a new test case to check that is correctly handle by the SuffixMapper. So I it ok for you I give my code who works with this case (FileExtensionMapper does not handle that case)

String filename = dirSep > 0 ? name.substring( dirSep +1 ) : name;
String dirname = dirSep > 0 ? name.substring( 0, dirSep +1 ) : "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope I don't get annoying with that but I think there is missing white space after the plus sign.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem code style must be respected

if ( filename.contains( "." ) )
{
String beforeExtension = filename.substring( 0, filename.indexOf( '.' ) );
String afterExtension = filename.substring( filename.indexOf( '.' ) + 1 ) ;
nameWithSuffix = dirname + beforeExtension + suffix + "." + afterExtension;
}
else
{
nameWithSuffix += suffix;
}
}
return nameWithSuffix;
}
}
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
23 changes: 23 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,22 @@ 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}} add the given suffix to the filename. The suffix will be added before the file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct is adds the given

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops you see my bad English :(

extension. Examples :
theFile.txt => theFileNiceSuffix.txt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The examples will be displayed on one line as the new lines will be stripped. If you wrap them as code that would solve the problem and they would stand out as well. And would be great if you add example with dot in the file name as it would make clear how the mapper behaves in such cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have still the following example : dir/archive.tar.gz => dir/archiveNiceSuffix.tar.gz
this is not enough ? If not please tell me an example

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is ok. My bad, didn't read the docs carefully enough. Sorry about that.

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 @@ -164,6 +164,23 @@ public void testPrefixMapper() throws Exception
mapper.setPrefix( prefix );
testFileMapper( mapper, SAMPLES, results );
}

public void testSuffixMapper() throws Exception
{
final String suffix = "suffix";
String[] results = getIdentityResults();
testFileMapper( new SuffixFileMapper(), SAMPLES, results );
testFileMapper( (SuffixFileMapper) lookup( FileMapper.ROLE, SuffixFileMapper.ROLE_HINT ), SAMPLES, results );

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" };
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