From 9370b32844db3946c18ebac6cf7aeec4cb760022 Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Sun, 15 Jul 2018 00:56:56 +0200 Subject: [PATCH 1/5] [MASSEMBLY-617] : Add new FileMapper for giving a suffix to filename -> SuffixFileMapper --- .../io/filemappers/SuffixFileMapper.java | 78 +++++++++++++++++++ .../resources/META-INF/plexus/components.xml | 7 ++ .../io/filemappers/FileMapperTest.java | 16 ++++ 3 files changed, 101 insertions(+) create mode 100644 src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java 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..706fbd90 --- /dev/null +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -0,0 +1,78 @@ +package org.codehaus.plexus.components.io.filemappers; + +/* + * Copyright 2007 The Codehaus Foundation. + * + * 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. + */ +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; + } + + /** + * Performs the mapping of a file name by adding a suffix. + */ + public static String getMappedFileName( String suffix, String name ) + { + String nameWithSuffix = name; + if ( StringUtils.isNotBlank( suffix ) ) + { + if ( name.contains( "." ) ) + { + String beforeExtension = name.substring( 0, name.indexOf('.') ); + String afterExtension = name.substring( name.indexOf('.') + 1 ) ; + nameWithSuffix = beforeExtension + suffix + "." + afterExtension; + } + else + { + nameWithSuffix += suffix; + } + } + return nameWithSuffix; + } +} \ 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/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java b/src/test/java/org/codehaus/plexus/components/io/filemappers/FileMapperTest.java index 4830d7e7..293618ee 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 @@ -164,6 +164,22 @@ 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", "csuffix.c/a", "csuffix.c/xyz.gif", "csuffix.c\\a", "csuffix.c\\xyz.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 ) { From 94833d16736e8ce6462ddbc3b9386d809d4c8df3 Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Sat, 21 Jul 2018 23:13:09 +0200 Subject: [PATCH 2/5] Take care of directory, and add documentation --- .../io/filemappers/SuffixFileMapper.java | 21 +++++++++-------- src/site/apt/filemappers.apt | 23 +++++++++++++++++++ .../io/filemappers/FileMapperTest.java | 3 ++- 3 files changed, 37 insertions(+), 10 deletions(-) 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 index 706fbd90..d182ef36 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -62,16 +62,19 @@ public static String getMappedFileName( String suffix, String name ) String nameWithSuffix = name; if ( StringUtils.isNotBlank( suffix ) ) { - if ( name.contains( "." ) ) + 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 = name.substring( 0, name.indexOf('.') ); - String afterExtension = name.substring( name.indexOf('.') + 1 ) ; - nameWithSuffix = beforeExtension + suffix + "." + afterExtension; - } - else - { - nameWithSuffix += suffix; - } + String beforeExtension = filename.substring( 0, filename.indexOf( '.' ) ); + String afterExtension = filename.substring( filename.indexOf( '.' ) + 1 ) ; + nameWithSuffix = dirname + beforeExtension + suffix + "." + afterExtension; + } + else + { + nameWithSuffix += suffix; + } } return nameWithSuffix; } diff --git a/src/site/apt/filemappers.apt b/src/site/apt/filemappers.apt index f716d2a9..fbacc8c2 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,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 + 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 293618ee..16477c79 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 @@ -172,7 +172,8 @@ public void testSuffixMapper() throws Exception 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", "csuffix.c/a", "csuffix.c/xyz.gif", "csuffix.c\\a", "csuffix.c\\xyz.gif"}; + 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 ); From b29e094515577ce188b5d9c96af6c6da8fc3cf72 Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Sat, 21 Jul 2018 23:16:59 +0200 Subject: [PATCH 3/5] fixing code style --- .../plexus/components/io/filemappers/SuffixFileMapper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index d182ef36..032333a6 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -62,9 +62,9 @@ public static String getMappedFileName( String suffix, String name ) String nameWithSuffix = name; if ( StringUtils.isNotBlank( suffix ) ) { - 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 ) : ""; + 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( '.' ) ); From 9ffe46ae69f0f11f585429a4d691aee5c6490eb9 Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Wed, 25 Jul 2018 22:05:53 +0200 Subject: [PATCH 4/5] Add new test case, code style, improve doc, remove static method --- .../io/filemappers/SuffixFileMapper.java | 49 ++++++++----------- src/site/apt/filemappers.apt | 6 ++- .../io/filemappers/FileMapperTest.java | 16 +++--- 3 files changed, 34 insertions(+), 37 deletions(-) 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 index 032333a6..2d5dee4d 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -1,5 +1,7 @@ package org.codehaus.plexus.components.io.filemappers; +import java.util.regex.Matcher; + /* * Copyright 2007 The Codehaus Foundation. * @@ -21,7 +23,9 @@ import org.codehaus.plexus.util.StringUtils; /** - * A file mapper, which maps by adding a suffix. + * 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 { @@ -32,12 +36,6 @@ public class SuffixFileMapper extends AbstractFileMapper 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. */ @@ -53,29 +51,24 @@ public void setSuffix( String suffix ) { this.suffix = suffix; } - - /** - * Performs the mapping of a file name by adding a suffix. - */ - public static String getMappedFileName( String suffix, String name ) + + @Nonnull + public String getMappedFileName( @Nonnull String pName) { - String nameWithSuffix = name; - if ( StringUtils.isNotBlank( suffix ) ) + 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( "." ) ) { - 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 ) ; - nameWithSuffix = dirname + beforeExtension + suffix + "." + afterExtension; - } - else - { - nameWithSuffix += suffix; - } + String beforeExtension = filename.substring( 0, filename.indexOf( '.' ) ); + String afterExtension = filename.substring( filename.indexOf( '.' ) + 1 ) ; + return dirname + beforeExtension + suffix + "." + afterExtension; } - return nameWithSuffix; + return name + suffix; } } \ No newline at end of file diff --git a/src/site/apt/filemappers.apt b/src/site/apt/filemappers.apt index fbacc8c2..391378a8 100644 --- a/src/site/apt/filemappers.apt +++ b/src/site/apt/filemappers.apt @@ -102,12 +102,16 @@ File Mappers * {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 + 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: ----------------------------------------------------------------------------- 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 16477c79..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; @@ -168,18 +169,17 @@ public void testPrefixMapper() throws Exception 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" }; + 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 ); + testFileMapper( mapper, samples, results ); mapper = (SuffixFileMapper) lookup( FileMapper.ROLE, SuffixFileMapper.ROLE_HINT ); mapper.setSuffix( suffix ); - testFileMapper( mapper, SAMPLES, results ); + testFileMapper( mapper, samples, results ); } private RegExpFileMapper configure( RegExpFileMapper pMapper, String pPattern, String pReplacement ) From 8954c8eda41d10d0afa473d3c488022184d88579 Mon Sep 17 00:00:00 2001 From: Thomas COLLIGNON Date: Tue, 31 Jul 2018 06:32:05 +0200 Subject: [PATCH 5/5] Import unused, Licence, add null check in setter --- .../components/io/filemappers/SuffixFileMapper.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 index 2d5dee4d..3d8e4258 100644 --- a/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java +++ b/src/main/java/org/codehaus/plexus/components/io/filemappers/SuffixFileMapper.java @@ -1,10 +1,6 @@ package org.codehaus.plexus.components.io.filemappers; -import java.util.regex.Matcher; - /* - * Copyright 2007 The Codehaus Foundation. - * * 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 @@ -20,8 +16,6 @@ import javax.annotation.Nonnull; -import org.codehaus.plexus.util.StringUtils; - /** * A file mapper, which maps by adding a suffix to the filename. * If the filename contains dot, the suffix will be added before. @@ -49,6 +43,10 @@ public String getSuffix() */ public void setSuffix( String suffix ) { + if ( suffix == null ) + { + throw new IllegalArgumentException( "The suffix is null." ); + } this.suffix = suffix; }