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 javac for
32 * example.</p>
33 *
34 * @version $Id: GlobPatternMapper.html 886882 2013-11-16 21:55:43Z hboutemy $
35 */
36 public class GlobPatternMapper
37 implements FileNameMapper
38 {
39 /**
40 * Part of "from" pattern before the *.
41 */
42 protected String fromPrefix = null;
43
44 /**
45 * Part of "from" pattern after the *.
46 */
47 protected String fromPostfix = null;
48
49 /**
50 * Length of the prefix ("from" pattern).
51 */
52 protected int prefixLength;
53
54 /**
55 * Length of the postfix ("from" pattern).
56 */
57 protected int postfixLength;
58
59 /**
60 * Part of "to" pattern before the *.
61 */
62 protected String toPrefix = null;
63
64 /**
65 * Part of "to" pattern after the *.
66 */
67 protected String toPostfix = null;
68
69 private boolean handleDirSep = false;
70
71 private boolean caseSensitive = true;
72
73 /**
74 * Attribute specifing whether to ignore the difference
75 * between / and \ (the two common directory characters).
76 * @param handleDirSep a boolean, default is false.
77 */
78 public void setHandleDirSep( boolean handleDirSep )
79 {
80 this.handleDirSep = handleDirSep;
81 }
82
83 /**
84 * Attribute specifing whether to ignore the case difference
85 * in the names.
86 *
87 * @param caseSensitive a boolean, default is false.
88 */
89 public void setCaseSensitive( boolean caseSensitive )
90 {
91 this.caseSensitive = caseSensitive;
92 }
93
94 /** {@inheritDoc} */
95 public void setFrom( String from )
96 {
97 int index = from.lastIndexOf( "*" );
98 if ( index == -1 )
99 {
100 fromPrefix = from;
101 fromPostfix = "";
102 }
103 else
104 {
105 fromPrefix = from.substring( 0, index );
106 fromPostfix = from.substring( index + 1 );
107 }
108 prefixLength = fromPrefix.length();
109 postfixLength = fromPostfix.length();
110 }
111
112 /** {@inheritDoc} */
113 public void setTo( String to )
114 {
115 int index = to.lastIndexOf( "*" );
116 if ( index == -1 )
117 {
118 toPrefix = to;
119 toPostfix = "";
120 }
121 else
122 {
123 toPrefix = to.substring( 0, index );
124 toPostfix = to.substring( index + 1 );
125 }
126 }
127
128 /** {@inheritDoc} */
129 public String mapFileName( String sourceFileName )
130 {
131 if ( fromPrefix == null || !modifyName( sourceFileName ).startsWith( modifyName( fromPrefix ) )
132 || !modifyName( sourceFileName ).endsWith( modifyName( fromPostfix ) ) )
133 {
134 return null;
135 }
136 return new String( toPrefix + extractVariablePart( sourceFileName ) + toPostfix );
137 }
138
139 /**
140 * Returns the part of the given string that matches the * in the
141 * "from" pattern.
142 * @param name the source file name
143 * @return the variable part of the name
144 */
145 protected String extractVariablePart( String name )
146 {
147 return name.substring( prefixLength, name.length() - postfixLength );
148 }
149
150 /**
151 * modify string based on dir char mapping and case sensitivity
152 * @param name the name to convert
153 * @return the converted name
154 */
155 private String modifyName( String name )
156 {
157 if ( !caseSensitive )
158 {
159 name = name.toLowerCase();
160 }
161 if ( handleDirSep )
162 {
163 if ( name.indexOf( '\\' ) != -1 )
164 {
165 name = name.replace( '\\', '/' );
166 }
167 }
168 return name;
169 }
170 }