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