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