View Javadoc
1   package org.codehaus.plexus.components.io.filemappers;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import javax.annotation.Nonnull;
20  
21  /**
22   * An implementation of {@link FileMapper}, which changes the files extension.
23   */
24  public class FileExtensionMapper extends AbstractFileMapper
25  {
26      /**
27       * The file extension mappers role-hint: "fileExtension".
28       */
29      public static final String ROLE_HINT = "fileExtension";
30  
31      private String targetExtension;
32  
33      /**
34       * Sets the target files extension.
35       * 
36       * @param pTargetExtension the target extensions
37       * @throws IllegalArgumentException
38       *             The target extension is null or empty.
39       */
40      public void setTargetExtension( String pTargetExtension )
41      {
42          if ( pTargetExtension == null )
43          {
44              throw new IllegalArgumentException( "The target extension is null." );
45          }
46          if ( pTargetExtension.length() == 0 )
47          {
48              throw new IllegalArgumentException( "The target extension is empty." );
49          }
50          if ( pTargetExtension.charAt( 0 ) == '.' )
51          {
52              targetExtension = pTargetExtension;
53          }
54          else
55          {
56              targetExtension = '.' + pTargetExtension;
57          }
58      }
59  
60      /**
61       * Returns the target files extension.
62       * @return The target extension
63       */
64      public String getTargetExtension()
65      {
66          return targetExtension;
67      }
68  
69      @Nonnull public String getMappedFileName( @Nonnull String pName )
70      {
71          final String ext = getTargetExtension();
72          if ( ext == null )
73          {
74              throw new IllegalStateException( "The target extension has not been set." );
75          }
76          final String name = super.getMappedFileName( pName ); // Check arguments
77          final int dirSep = Math.max( pName.lastIndexOf( '/' ), pName.lastIndexOf( '\\' ) );
78          final int offset = pName.lastIndexOf( '.' );
79          if ( offset <= dirSep )
80          {
81              return name + ext;
82          }
83          else
84          {
85              return name.substring( 0, offset ) + ext;
86          }
87      }
88  }