1 package org.apache.maven.plugins.war.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
24 import java.io.File;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.LinkedHashSet;
29 import java.util.Set;
30
31
32
33
34
35
36
37
38
39
40
41 public class PathSet
42 implements Iterable<String>
43 {
44 private static final String SEPARATOR = "/";
45 private static final char SEPARATOR_CHAR = SEPARATOR.charAt( 0 );
46
47
48
49 private Set<String> pathsSet = new LinkedHashSet<>();
50
51 static String normalizeSubPath( String path )
52 {
53 if ( path.isEmpty() )
54 {
55 return path;
56 }
57 String cleanPath = path.replaceAll( "[\\\\]+", SEPARATOR )
58 .replaceAll( "[/]+" , SEPARATOR );
59 cleanPath = cleanPath.charAt( 0 ) == SEPARATOR_CHAR ? cleanPath.substring( 1 ) : cleanPath;
60 if ( cleanPath.isEmpty() )
61 {
62 return cleanPath;
63 }
64 if ( cleanPath.charAt( cleanPath.length() - 1 ) == SEPARATOR_CHAR )
65 {
66 return cleanPath.substring( 0, cleanPath.length() - 1 );
67 }
68 return cleanPath;
69 }
70
71
72
73
74
75
76 public PathSet()
77 {
78
79 }
80
81
82
83
84
85
86 public PathSet( Collection<String> paths )
87 {
88 addAll( paths );
89 }
90
91
92
93
94
95
96 public PathSet( String[] paths )
97 {
98 addAll( paths );
99 }
100
101
102
103
104
105
106 public void add( String path )
107 {
108 pathsSet.add( normalizeSubPath( path ) );
109 }
110
111
112
113
114
115
116
117 public void addAll( Collection<String> paths, String prefix )
118 {
119 for ( String val : paths )
120 {
121 add( prefix + SEPARATOR + val );
122 }
123 }
124
125
126
127
128
129
130
131 public void addAll( String[] paths, String prefix )
132 {
133 for ( String val : paths )
134 {
135 add( prefix + SEPARATOR + val );
136 }
137 }
138
139
140
141
142
143
144
145 public void addAll( PathSet paths, String prefix )
146 {
147 for ( String path : paths )
148 {
149 add( prefix + SEPARATOR + path );
150 }
151 }
152
153
154
155
156
157
158 public void addAll( Collection<String> paths )
159 {
160 addAll( paths, "" );
161 }
162
163
164
165
166
167
168 public void addAll( String[] paths )
169 {
170 addAll( paths, "" );
171 }
172
173
174
175
176
177
178 public void addAll( PathSet paths )
179 {
180 addAll( paths, "" );
181 }
182
183
184
185
186
187
188
189 public boolean contains( String path )
190 {
191 return pathsSet.contains( normalizeSubPath( path ) );
192 }
193
194
195
196
197
198
199
200 boolean remove( String path )
201 {
202 return pathsSet.remove( normalizeSubPath( path ) );
203 }
204
205
206
207
208
209
210 public Iterator<String> iterator()
211 {
212 return pathsSet.iterator();
213 }
214
215
216
217
218 public Collection<String> paths()
219 {
220 return pathsSet;
221 }
222
223
224
225
226
227
228
229
230 public void addPrefix( String prefix )
231 {
232 final Set<String> newSet = new HashSet<>();
233 for ( String path : pathsSet )
234 {
235 newSet.add( normalizeSubPath( prefix + path ) );
236 }
237 pathsSet = newSet;
238 }
239
240
241
242
243
244
245 public int size()
246 {
247 return pathsSet.size();
248 }
249
250
251
252
253
254
255
256 public void addAllFilesInDirectory( File directory, String prefix )
257 {
258 DirectoryScanner scanner = new DirectoryScanner();
259 scanner.setBasedir( directory );
260 scanner.scan();
261 addAll( scanner.getIncludedFiles(), prefix );
262 }
263
264 }