1 package org.alfresco.maven.plugin.amp.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 */
41
42 public class PathSet
43 {
44
45 /***
46 * Set of normalized paths
47 */
48 private Set
49
50 /***
51 * The method normalizes the path.
52 * <p/>
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
67
68 /***
69 * Creates an empty paths set
70 */
71 public PathSet()
72 {
73
74 }
75
76 /***
77 * Creates paths set and normalizate and adds all 'paths'.
78 * The source 'paths' will not be changed
79 *
80 * @param paths to be added
81 */
82 public PathSet( Collection
83 {
84 addAll( paths );
85 }
86
87 /***
88 * Creates paths set and normalizate and adds all 'paths'.
89 * The source 'paths' will not be changed
90 *
91 * @param paths to be added
92 */
93 public PathSet( String[] paths )
94 {
95 addAll( paths );
96 }
97
98 /***
99 * Normalizes and adds given path to the set.
100 *
101 * @param path to be added
102 */
103 public void add( String path )
104 {
105 pathsSet.add( normalizeFilePath( path ) );
106 }
107
108 /***
109 * Normalizes and adds given paths (collection of strings)
110 * to the set. The source collection will not be changed
111 *
112 * @param paths - collection of strings to be added
113 * @param prefix added to all given paths
114 */
115 public void addAll( Collection
116 {
117 for ( Iterator iter = paths.iterator(); iter.hasNext(); )
118 {
119 add( prefix + iter.next() );
120 }
121 }
122
123 /***
124 * Normalizes and adds given paths to the set.
125 * The source collection will not be changed
126 *
127 * @param paths to be added
128 * @param prefix added to all given paths
129 */
130 public void addAll( String[] paths, String prefix )
131 {
132 for ( int i = 0; i < paths.length; i++ )
133 {
134 add( prefix + paths[i] );
135 }
136 }
137
138 /***
139 * Adds given paths to the set.
140 * The source collection will not be changed
141 *
142 * @param paths to be added
143 * @param prefix added to all given paths
144 */
145 public void addAll( PathSet paths, String prefix )
146 {
147 for ( Iterator iter = paths.iterator(); iter.hasNext(); )
148 {
149 add( prefix + iter.next() );
150 }
151 }
152
153 /***
154 * Normalizes and adds given paths (collection of strings)
155 * to the set. The source collection will not be changed
156 *
157 * @param paths - collection of strings to be added
158 */
159 public void addAll( Collection
160 {
161 addAll( paths, "" );
162 }
163
164 /***
165 * Normalizes and adds given paths to the set.
166 * The source collection will not be changed
167 *
168 * @param paths to be added
169 */
170 public void addAll( String[] paths )
171 {
172 addAll( paths, "" );
173 }
174
175 /***
176 * Adds given paths to the set.
177 * The source collection will not be changed
178 *
179 * @param paths to be added
180 */
181 public void addAll( PathSet paths )
182 {
183 addAll( paths, "" );
184 }
185
186 /***
187 * Checks if the set constains given path. The path is normalized
188 * before check.
189 *
190 * @param path we are looking for in the set.
191 * @return information if the set constains the path.
192 */
193 public boolean contains( String path )
194 {
195 return pathsSet.contains( normalizeFilePath( path ) );
196 }
197
198 /***
199 * Returns iterator of normalized paths (strings)
200 *
201 * @return iterator of normalized paths (strings)
202 */
203 public Iterator iterator()
204 {
205 return pathsSet.iterator();
206 }
207
208 /***
209 * Adds given prefix to all paths in the set.
210 * <p/>
211 * The prefix should be ended by '/'. The generated paths are normalized.
212 *
213 * @param prefix to be added to all items
214 */
215 public void addPrefix( String prefix )
216 {
217 final Set
218 for ( Iterator iter = pathsSet.iterator(); iter.hasNext(); )
219 {
220 String path = (String) iter.next();
221 newSet.add( normalizeFilePath( prefix + path ) );
222 }
223 pathsSet = newSet;
224 }
225
226 /***
227 * Returns count of the paths in the set
228 *
229 * @return count of the paths in the set
230 */
231 public int size()
232 {
233 return pathsSet.size();
234 }
235
236 /***
237 * Adds to the set all files in the given directory
238 *
239 * @param directory that will be searched for file's paths to add
240 * @param prefix to be added to all found files
241 */
242 public void addAllFilesInDirectory( File directory, String prefix )
243 {
244 DirectoryScanner scanner = new DirectoryScanner();
245 scanner.setBasedir( directory );
246 scanner.scan();
247 addAll( scanner.getIncludedFiles(), prefix );
248 }
249
250
251 /***
252 * The method normalizes the path.
253 * <p/>
254 * <ul>
255 * <li>changes directory separator to unix's separator(/)</li>
256 * <li>deletes all trailing slashes</li>
257 * </ul>
258 *
259 * @param path to normalization
260 * @return normalized path
261 */
262 public static String normalizeFilePathStatic( String path )
263 {
264 return trimTrailingSlashes( StringUtils.replace( path, '//', '/' ) );
265 }
266
267 /***
268 * The method deletes all trailing slashes from the given string
269 *
270 * @param str a string
271 * @return trimed string
272 */
273 public static String trimTrailingSlashes( String str )
274 {
275 int i;
276 for ( i = 0; i < str.length() && str.charAt( i ) == '/'; i++ )
277
278 {
279
280 }
281 return str.substring( i );
282 }
283
284 }