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 public class PathSet
41 implements Iterable<String>
42 {
43 private static final String SEPARATOR = "/";
44 private static final char SEPARATOR_CHAR = SEPARATOR.charAt( 0 );
45
46
47
48 private Set<String> pathsSet = new LinkedHashSet<>();
49
50 static String normalizeSubPath( String path )
51 {
52 if ( path.isEmpty() )
53 {
54 return path;
55 }
56 String cleanPath = path.replaceAll( "[\\\\]+", SEPARATOR )
57 .replaceAll( "[/]+" , SEPARATOR );
58 cleanPath = cleanPath.charAt( 0 ) == SEPARATOR_CHAR ? cleanPath.substring( 1 ) : cleanPath;
59 if ( cleanPath.isEmpty() )
60 {
61 return cleanPath;
62 }
63 if ( cleanPath.charAt( cleanPath.length() - 1 ) == SEPARATOR_CHAR )
64 {
65 return cleanPath.substring( 0, cleanPath.length() - 1 );
66 }
67 return cleanPath;
68 }
69
70
71
72
73
74
75 public PathSet()
76 {
77
78 }
79
80
81
82
83
84
85 public PathSet( Collection<String> paths )
86 {
87 addAll( paths );
88 }
89
90
91
92
93
94
95 public PathSet( String[] paths )
96 {
97 addAll( paths );
98 }
99
100
101
102
103
104
105 public void add( String path )
106 {
107 pathsSet.add( normalizeSubPath( path ) );
108 }
109
110
111
112
113
114
115
116 public void addAll( Collection<String> paths, String prefix )
117 {
118 for ( String val : paths )
119 {
120 add( prefix + SEPARATOR + val );
121 }
122 }
123
124
125
126
127
128
129
130 public void addAll( String[] paths, String prefix )
131 {
132 for ( String val : paths )
133 {
134 add( prefix + SEPARATOR + val );
135 }
136 }
137
138
139
140
141
142
143
144 public void addAll( PathSet paths, String prefix )
145 {
146 for ( String path : paths )
147 {
148 add( prefix + SEPARATOR + path );
149 }
150 }
151
152
153
154
155
156
157 public void addAll( Collection<String> paths )
158 {
159 addAll( paths, "" );
160 }
161
162
163
164
165
166
167 public void addAll( String[] paths )
168 {
169 addAll( paths, "" );
170 }
171
172
173
174
175
176
177 public void addAll( PathSet paths )
178 {
179 addAll( paths, "" );
180 }
181
182
183
184
185
186
187
188 public boolean contains( String path )
189 {
190 return pathsSet.contains( normalizeSubPath( path ) );
191 }
192
193
194
195
196
197
198
199 boolean remove( String path )
200 {
201 return pathsSet.remove( normalizeSubPath( path ) );
202 }
203
204
205
206
207
208
209 @Override
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 }