1 package org.apache.maven.plugin.war.util;
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 import org.codehaus.plexus.util.DirectoryScanner;
23 import org.codehaus.plexus.util.StringUtils;
24
25 import java.io.File;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.LinkedHashSet;
30 import java.util.Set;
31
32 /**
33 * Set of file's paths.
34 * <p/>
35 * The class extends functionality of a "normal" set of strings by a process of the paths normalization. All paths are
36 * converted to unix form (slashes) and they don't start with starting /.
37 *
38 * @author Piotr Tabor
39 * @version $Id: PathSet.html 935522 2015-01-08 20:15:45Z khmarbaise $
40 */
41
42 public class PathSet
43 implements Iterable<String>
44 {
45
46 /**
47 * Set of normalized paths
48 */
49 private Set<String> pathsSet = new LinkedHashSet<String>();
50
51 /**
52 * The method normalizes the path.
53 * <p/>
54 * <ul>
55 * <li>changes directory separator to unix's separator(/)</li>
56 * <li>deletes all trailing slashes</li>
57 * </ul>
58 *
59 * @param path to normalization
60 * @return normalized path
61 */
62 protected String normalizeFilePath( String path )
63 {
64 return normalizeFilePathStatic( path );
65 }
66
67 /*-------------------- Business interface ------------------------------*/
68
69 /**
70 * Creates an empty paths set
71 */
72 public PathSet()
73 {
74 /* Empty default constructor */
75 }
76
77 /**
78 * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
79 *
80 * @param paths to be added
81 */
82 public PathSet( Collection<String> paths )
83 {
84 addAll( paths );
85 }
86
87 /**
88 * Creates paths set and normalizate and adds all 'paths'. The source 'paths' will not be changed
89 *
90 * @param paths to be added
91 */
92 public PathSet( String[] paths )
93 {
94 addAll( paths );
95 }
96
97 /**
98 * Normalizes and adds given path to the set.
99 *
100 * @param path to be added
101 */
102 public void add( String path )
103 {
104 pathsSet.add( normalizeFilePath( path ) );
105 }
106
107 /**
108 * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
109 *
110 * @param paths - collection of strings to be added
111 * @param prefix added to all given paths
112 */
113 public void addAll( Collection<String> paths, String prefix )
114 {
115 for ( String val : paths )
116 {
117 add( prefix + val );
118 }
119 }
120
121 /**
122 * Normalizes and adds given paths to the set. The source collection will not be changed
123 *
124 * @param paths to be added
125 * @param prefix added to all given paths
126 */
127 public void addAll( String[] paths, String prefix )
128 {
129 for ( String val : paths )
130 {
131 add( prefix + val );
132 }
133 }
134
135 /**
136 * Adds given paths to the set. The source collection will not be changed
137 *
138 * @param paths to be added
139 * @param prefix added to all given paths
140 */
141 public void addAll( PathSet paths, String prefix )
142 {
143 for ( String path : paths )
144 {
145 add( prefix + path );
146 }
147 }
148
149 /**
150 * Normalizes and adds given paths (collection of strings) to the set. The source collection will not be changed
151 *
152 * @param paths - collection of strings to be added
153 */
154 public void addAll( Collection<String> paths )
155 {
156 addAll( paths, "" );
157 }
158
159 /**
160 * Normalizes and adds given paths to the set. The source collection will not be changed
161 *
162 * @param paths to be added
163 */
164 public void addAll( String[] paths )
165 {
166 addAll( paths, "" );
167 }
168
169 /**
170 * Adds given paths to the set. The source collection will not be changed
171 *
172 * @param paths to be added
173 */
174 public void addAll( PathSet paths )
175 {
176 addAll( paths, "" );
177 }
178
179 /**
180 * Checks if the set constains given path. The path is normalized before check.
181 *
182 * @param path we are looking for in the set.
183 * @return information if the set constains the path.
184 */
185 public boolean contains( String path )
186 {
187 return pathsSet.contains( normalizeFilePath( path ) );
188 }
189
190 /**
191 * Removes the specified path if it exists.
192 *
193 * @param path the path to remove
194 * @return true if the path was removed, false if it did not existed
195 */
196 boolean remove( String path )
197 {
198 final String normalizedPath = normalizeFilePath( path );
199 return pathsSet.remove( normalizedPath );
200
201 }
202
203 /**
204 * Returns iterator of normalized paths (strings)
205 *
206 * @return iterator of normalized paths (strings)
207 */
208 public Iterator<String> iterator()
209 {
210 return pathsSet.iterator();
211 }
212
213 /**
214 * @return {@link #pathsSet}
215 */
216 public Collection<String> paths()
217 {
218 return pathsSet;
219 }
220
221 /**
222 * Adds given prefix to all paths in the set.
223 * <p/>
224 * The prefix should be ended by '/'. The generated paths are normalized.
225 *
226 * @param prefix to be added to all items
227 */
228 public void addPrefix( String prefix )
229 {
230 final Set<String> newSet = new HashSet<String>();
231 for ( String path : pathsSet )
232 {
233 newSet.add( normalizeFilePath( prefix + path ) );
234 }
235 pathsSet = newSet;
236 }
237
238 /**
239 * Returns count of the paths in the set
240 *
241 * @return count of the paths in the set
242 */
243 public int size()
244 {
245 return pathsSet.size();
246 }
247
248 /**
249 * Adds to the set all files in the given directory
250 *
251 * @param directory that will be searched for file's paths to add
252 * @param prefix to be added to all found files
253 */
254 public void addAllFilesInDirectory( File directory, String prefix )
255 {
256 DirectoryScanner scanner = new DirectoryScanner();
257 scanner.setBasedir( directory );
258 scanner.scan();
259 addAll( scanner.getIncludedFiles(), prefix );
260 }
261
262 /*-------------------- Universal static mathods ------------------------*/
263 /**
264 * The method normalizes the path.
265 * <p/>
266 * <ul>
267 * <li>changes directory separator to unix's separator(/)</li>
268 * <li>deletes all trailing slashes</li>
269 * </ul>
270 *
271 * @param path to normalization
272 * @return normalized path
273 */
274 public static String normalizeFilePathStatic( String path )
275 {
276 return trimTrailingSlashes( StringUtils.replace( path, '\\', '/' ) );
277 }
278
279 /**
280 * The method deletes all trailing slashes from the given string
281 *
282 * @param str a string
283 * @return trimed string
284 */
285 public static String trimTrailingSlashes( String str )
286 {
287 int i;
288 for ( i = 0; i < str.length() && str.charAt( i ) == '/'; i++ )
289 {
290 // just calculate i
291 }
292 return str.substring( i );
293 }
294
295 }