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  import java.util.regex.Matcher;
21  import java.util.regex.Pattern;
22  
23  /**
24   * Implementation of a file mapper, which uses regular expressions.
25   */
26  public class RegExpFileMapper
27      extends AbstractFileMapper
28  {
29      /**
30       * The regexp mappers role-hint: "regexp".
31       */
32      public static final String ROLE_HINT = "regexp";
33  
34      private Pattern pattern;
35  
36      private String replacement;
37  
38      private boolean replaceAll;
39  
40      /**
41       * Sets the regular expression pattern.
42       */
43      public void setPattern( String pPattern )
44      {
45          pattern = Pattern.compile( pPattern );
46      }
47  
48      /**
49       * Returns the regular expression pattern.
50       */
51      public String getPattern()
52      {
53          return pattern == null ? null : pattern.pattern();
54      }
55  
56      /**
57       * Sets the replacement string.
58       */
59      public void setReplacement( String pReplacement )
60      {
61          replacement = pReplacement;
62      }
63  
64      /**
65       * Returns the replacement string.
66       */
67      public String getReplacement()
68      {
69          return replacement;
70      }
71  
72      /**
73       * Returns, whether to replace the first occurrency of the pattern
74       * (default), or all.
75       */
76      public boolean getReplaceAll()
77      {
78          return replaceAll;
79      }
80  
81      /**
82       * Sets, whether to replace the first occurrency of the pattern
83       * (default), or all.
84       */
85      public void setReplaceAll( boolean pReplaceAll )
86      {
87          replaceAll = pReplaceAll;
88      }
89  
90      @Nonnull
91      public String getMappedFileName( @Nonnull String pName)
92      {
93          final String name = super.getMappedFileName( pName );
94          if ( pattern == null )
95          {
96              throw new IllegalStateException( "The regular expression pattern has not been set." );
97          }
98          if (replacement == null)
99          {
100             throw new IllegalStateException( "The pattern replacement string has not been set." );
101         }
102         final Matcher matcher = pattern.matcher( name );
103         if ( !matcher.find() )
104         {
105             return name;
106         }
107         if ( !getReplaceAll() )
108         {
109             return matcher.replaceFirst( replacement );
110         }
111         return matcher.replaceAll( replacement );
112     }
113 }