View Javadoc
1   package org.apache.maven.shared.model.fileset.mappers;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  /**
23   * Implementation of FileNameMapper that does simple wildcard pattern
24   * replacements.
25   *
26   * <p>This does simple translations like *.foo -> *.bar where the
27   * prefix to .foo will be left unchanged. It only handles a single *
28   * character, use regular expressions for more complicated
29   * situations.</p>
30   *
31   * <p>This is one of the more useful Mappers, it is used by <code>javac</code> for
32   * example.</p>
33   */
34  public class GlobPatternMapper
35      implements FileNameMapper
36  {
37      /**
38       * Part of &quot;from&quot; pattern before the *.
39       */
40      protected String fromPrefix = null;
41  
42      /**
43       * Part of &quot;from&quot; pattern after the *.
44       */
45      protected String fromPostfix = null;
46  
47      /**
48       * Length of the prefix (&quot;from&quot; pattern).
49       */
50      protected int prefixLength;
51  
52      /**
53       * Length of the postfix (&quot;from&quot; pattern).
54       */
55      protected int postfixLength;
56  
57      /**
58       * Part of &quot;to&quot; pattern before the *.
59       */
60      protected String toPrefix = null;
61  
62      /**
63       * Part of &quot;to&quot; pattern after the *.
64       */
65      protected String toPostfix = null;
66  
67      private boolean handleDirSep = false;
68  
69      private boolean caseSensitive = true;
70  
71      /**
72       * Attribute specifing whether to ignore the difference
73       * between / and \ (the two common directory characters).
74       * @param handleDirSep a boolean, default is false.
75       */
76      public void setHandleDirSep( boolean handleDirSep )
77      {
78          this.handleDirSep = handleDirSep;
79      }
80  
81      /**
82       * Attribute specifing whether to ignore the case difference
83       * in the names.
84       *
85       * @param caseSensitive a boolean, default is false.
86       */
87      public void setCaseSensitive( boolean caseSensitive )
88      {
89          this.caseSensitive = caseSensitive;
90      }
91  
92      @Override
93      public void setFrom( String from )
94      {
95          int index = from.lastIndexOf( "*" );
96          if ( index == -1 )
97          {
98              fromPrefix = from;
99              fromPostfix = "";
100         }
101         else
102         {
103             fromPrefix = from.substring( 0, index );
104             fromPostfix = from.substring( index + 1 );
105         }
106         prefixLength = fromPrefix.length();
107         postfixLength = fromPostfix.length();
108     }
109 
110     @Override
111     public void setTo( String to )
112     {
113         int index = to.lastIndexOf( "*" );
114         if ( index == -1 )
115         {
116             toPrefix = to;
117             toPostfix = "";
118         }
119         else
120         {
121             toPrefix = to.substring( 0, index );
122             toPostfix = to.substring( index + 1 );
123         }
124     }
125 
126     @Override
127     public String mapFileName( String sourceFileName )
128     {
129         if ( fromPrefix == null || !modifyName( sourceFileName ).startsWith( modifyName( fromPrefix ) )
130             || !modifyName( sourceFileName ).endsWith( modifyName( fromPostfix ) ) )
131         {
132             return null;
133         }
134         return toPrefix + extractVariablePart( sourceFileName ) + toPostfix;
135     }
136 
137     /**
138      * Returns the part of the given string that matches the * in the
139      * &quot;from&quot; pattern.
140      * @param name the source file name
141      * @return the variable part of the name
142      */
143     protected String extractVariablePart( String name )
144     {
145         return name.substring( prefixLength, name.length() - postfixLength );
146     }
147 
148     /**
149      * modify string based on dir char mapping and case sensitivity
150      * @param name the name to convert
151      * @return the converted name
152      */
153     private String modifyName( String name )
154     {
155         if ( !caseSensitive )
156         {
157             name = name.toLowerCase();
158         }
159         if ( handleDirSep )
160         {
161             if ( name.indexOf( '\\' ) != -1 )
162             {
163                 name = name.replace( '\\', '/' );
164             }
165         }
166         return name;
167     }
168 }