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