|
@@ -10,7 +10,7 @@ |
10 |
10 |
|
/** |
11 |
11 |
|
* Collects filesystem paths using wildcards, preserving the directory structure. Copies, deletes, and zips paths. |
12 |
12 |
|
*/ |
13 |
|
- |
public class Paths implements Iterable<String> |
|
13 |
+ |
public class Paths |
14 |
14 |
|
{ |
15 |
15 |
|
static private final Comparator<FilePath> LONGEST_TO_SHORTEST = new Comparator<FilePath>() |
16 |
16 |
|
{ |
|
@@ -26,7 +26,7 @@ |
26 |
26 |
|
/** |
27 |
27 |
|
* Only the Files will be stored! |
28 |
28 |
|
*/ |
29 |
|
- |
private final HashSet<FilePath> mPaths = new HashSet<FilePath>(); |
|
29 |
+ |
private final RootedPathsCollection mPaths = new RootedPathsCollection(); |
30 |
30 |
|
|
31 |
31 |
|
/** |
32 |
32 |
|
* Creates an empty Paths object. |
|
@@ -39,6 +39,7 @@ |
39 |
39 |
|
* Creates a Paths object and calls {@link #glob(String, String[])} with the specified arguments. |
40 |
40 |
|
*/ |
41 |
41 |
|
public Paths( String dir, String... patterns ) |
|
42 |
+ |
throws IOException |
42 |
43 |
|
{ |
43 |
44 |
|
glob( dir, patterns ); |
44 |
45 |
|
} |
|
@@ -50,22 +51,36 @@ |
50 |
51 |
|
|
51 |
52 |
|
public int count() |
52 |
53 |
|
{ |
53 |
|
- |
return mPaths.size(); |
|
54 |
+ |
return mPaths.count(); |
54 |
55 |
|
} |
55 |
56 |
|
|
56 |
57 |
|
public List<FilePath> getPaths() |
57 |
58 |
|
{ |
58 |
|
- |
return new ArrayList<FilePath>( mPaths ); |
|
59 |
+ |
return mPaths.collectPaths( new ArrayList<FilePath>() ); |
|
60 |
+ |
} |
|
61 |
+ |
|
|
62 |
+ |
public void add( FilePath pFilePath ) |
|
63 |
+ |
{ |
|
64 |
+ |
mPaths.add( pFilePath ); |
59 |
65 |
|
} |
60 |
66 |
|
|
61 |
|
- |
// vvvvvvvvvvvvvvvvvvvvvvv Should These be supported as they can introduce potentially conflicting FileSubPaths vvvvvvvvvvvvvvvvvvvvvvv |
|
67 |
+ |
@SuppressWarnings({"UnusedDeclaration"}) |
|
68 |
+ |
public void add( RootedPaths pRootedPaths ) |
|
69 |
+ |
{ |
|
70 |
+ |
mPaths.add( pRootedPaths ); |
|
71 |
+ |
} |
|
72 |
+ |
|
|
73 |
+ |
public void add( RootedPathsCollection pRootedPathsCollection ) |
|
74 |
+ |
{ |
|
75 |
+ |
mPaths.mergeIn( pRootedPathsCollection ); |
|
76 |
+ |
} |
62 |
77 |
|
|
63 |
78 |
|
/** |
64 |
79 |
|
* Adds all paths from the specified Paths object to this Paths object. |
65 |
80 |
|
*/ |
66 |
81 |
|
public void add( Paths paths ) |
67 |
82 |
|
{ |
68 |
|
- |
this.mPaths.addAll( paths.mPaths ); |
|
83 |
+ |
add( paths.mPaths ); |
69 |
84 |
|
} |
70 |
85 |
|
|
71 |
86 |
|
/** |
|
@@ -73,6 +88,7 @@ |
73 |
88 |
|
*/ |
74 |
89 |
|
@SuppressWarnings({"UnusedDeclaration"}) |
75 |
90 |
|
public void glob( String dir, List<String> patterns ) |
|
91 |
+ |
throws IOException |
76 |
92 |
|
{ |
77 |
93 |
|
glob( dir, (patterns == null) ? Util.EMPTY_STRING_ARRAY : patterns.toArray( new String[patterns.size()] ) ); |
78 |
94 |
|
} |
|
@@ -100,6 +116,7 @@ |
100 |
116 |
|
* patterns would select the paths. |
101 |
117 |
|
*/ |
102 |
118 |
|
public void glob( String dir, String... patterns ) |
|
119 |
+ |
throws IOException |
103 |
120 |
|
{ |
104 |
121 |
|
new PathPatterns( dir, patterns ).addTo( mPaths ); |
105 |
122 |
|
} |
|
@@ -119,7 +136,7 @@ |
119 |
136 |
|
zDest.mkdirs(); |
120 |
137 |
|
|
121 |
138 |
|
Paths newPaths = new Paths(); |
122 |
|
- |
for ( FilePath path : this.mPaths ) |
|
139 |
+ |
for ( FilePath path : getPaths() ) |
123 |
140 |
|
{ |
124 |
141 |
|
String zSubPath = path.getFileSubPath(); |
125 |
142 |
|
Util.copyFile( path.file(), new File( destDir, zSubPath ) ); |
|
@@ -227,10 +244,9 @@ |
227 |
244 |
|
public Paths flatten() |
228 |
245 |
|
{ |
229 |
246 |
|
Paths newPaths = new Paths(); |
230 |
|
- |
for ( FilePath path : mPaths ) |
|
247 |
+ |
for ( File zFile : getFiles() ) |
231 |
248 |
|
{ |
232 |
|
- |
File file = path.file(); |
233 |
|
- |
newPaths.mPaths.add( new FilePath( file.getParentFile(), file.getName() ) ); |
|
249 |
+ |
newPaths.add( new FilePath( zFile.getParentFile(), zFile.getName() ) ); |
234 |
250 |
|
} |
235 |
251 |
|
return newPaths; |
236 |
252 |
|
} |
|
@@ -240,8 +256,9 @@ |
240 |
256 |
|
*/ |
241 |
257 |
|
public List<File> getFiles() |
242 |
258 |
|
{ |
243 |
|
- |
List<File> files = new ArrayList<File>( mPaths.size() ); |
244 |
|
- |
for ( FilePath path : mPaths ) |
|
259 |
+ |
List<FilePath> zPaths = getPaths(); |
|
260 |
+ |
List<File> files = new ArrayList<File>( zPaths.size() ); |
|
261 |
+ |
for ( FilePath path : zPaths ) |
245 |
262 |
|
{ |
246 |
263 |
|
files.add( path.file() ); |
247 |
264 |
|
} |
|
@@ -253,12 +270,13 @@ |
253 |
270 |
|
*/ |
254 |
271 |
|
public List<String> getRelativePaths() |
255 |
272 |
|
{ |
256 |
|
- |
List<String> stringPaths = new ArrayList<String>( mPaths.size() ); |
257 |
|
- |
for ( FilePath path : mPaths ) |
|
273 |
+ |
List<FilePath> zPaths = getPaths(); |
|
274 |
+ |
List<String> rv = new ArrayList<String>( zPaths.size() ); |
|
275 |
+ |
for ( FilePath path : zPaths ) |
258 |
276 |
|
{ |
259 |
|
- |
stringPaths.add( path.getFileSubPath() ); |
|
277 |
+ |
rv.add( path.getFileSubPath() ); |
260 |
278 |
|
} |
261 |
|
- |
return stringPaths; |
|
279 |
+ |
return rv; |
262 |
280 |
|
} |
263 |
281 |
|
|
264 |
282 |
|
/** |
|
@@ -266,12 +284,13 @@ |
266 |
284 |
|
*/ |
267 |
285 |
|
public List<String> getFullPaths() |
268 |
286 |
|
{ |
269 |
|
- |
List<String> stringPaths = new ArrayList<String>( mPaths.size() ); |
270 |
|
- |
for ( File file : getFiles() ) |
|
287 |
+ |
List<File> zFiles = getFiles(); |
|
288 |
+ |
List<String> rv = new ArrayList<String>( zFiles.size() ); |
|
289 |
+ |
for ( File file : zFiles ) |
271 |
290 |
|
{ |
272 |
|
- |
stringPaths.add( file.getPath() ); |
|
291 |
+ |
rv.add( file.getPath() ); |
273 |
292 |
|
} |
274 |
|
- |
return stringPaths; |
|
293 |
+ |
return rv; |
275 |
294 |
|
} |
276 |
295 |
|
|
277 |
296 |
|
/** |
|
@@ -279,42 +298,13 @@ |
279 |
298 |
|
*/ |
280 |
299 |
|
public List<String> getNames() |
281 |
300 |
|
{ |
282 |
|
- |
List<String> stringPaths = new ArrayList<String>( mPaths.size() ); |
283 |
|
- |
for ( File file : getFiles() ) |
|
301 |
+ |
List<File> zFiles = getFiles(); |
|
302 |
+ |
List<String> rv = new ArrayList<String>( zFiles.size() ); |
|
303 |
+ |
for ( File file : zFiles ) |
284 |
304 |
|
{ |
285 |
|
- |
stringPaths.add( file.getName() ); |
|
305 |
+ |
rv.add( file.getName() ); |
286 |
306 |
|
} |
287 |
|
- |
return stringPaths; |
288 |
|
- |
} |
289 |
|
- |
|
290 |
|
- |
/** |
291 |
|
- |
* Iterates over the absolute paths. The iterator supports the remove method. |
292 |
|
- |
*/ |
293 |
|
- |
@Override |
294 |
|
- |
public Iterator<String> iterator() |
295 |
|
- |
{ |
296 |
|
- |
return new Iterator<String>() |
297 |
|
- |
{ |
298 |
|
- |
private Iterator<FilePath> iter = mPaths.iterator(); |
299 |
|
- |
|
300 |
|
- |
@Override |
301 |
|
- |
public void remove() |
302 |
|
- |
{ |
303 |
|
- |
iter.remove(); |
304 |
|
- |
} |
305 |
|
- |
|
306 |
|
- |
@Override |
307 |
|
- |
public String next() |
308 |
|
- |
{ |
309 |
|
- |
return iter.next().canonical(); |
310 |
|
- |
} |
311 |
|
- |
|
312 |
|
- |
@Override |
313 |
|
- |
public boolean hasNext() |
314 |
|
- |
{ |
315 |
|
- |
return iter.hasNext(); |
316 |
|
- |
} |
317 |
|
- |
}; |
|
307 |
+ |
return rv; |
318 |
308 |
|
} |
319 |
309 |
|
|
320 |
310 |
|
/** |
|
@@ -408,8 +398,10 @@ |
408 |
398 |
|
} |
409 |
399 |
|
} |
410 |
400 |
|
|
411 |
|
- |
public void addTo( Collection<FilePath> pPaths ) |
|
401 |
+ |
public void addTo( RootedPathsCollection pPaths ) |
|
402 |
+ |
throws IOException |
412 |
403 |
|
{ |
|
404 |
+ |
mPath = mPath.getCanonicalFile(); |
413 |
405 |
|
if ( mPath.isFile() ) |
414 |
406 |
|
{ |
415 |
407 |
|
pPaths.add( new FilePath( mPath.getParentFile(), mPath.getName() ) ); |
|
@@ -417,6 +409,7 @@ |
417 |
409 |
|
} |
418 |
410 |
|
if ( mPath.isDirectory() ) |
419 |
411 |
|
{ |
|
412 |
+ |
RootedPaths zPaths = new RootedPaths( mPath ); |
420 |
413 |
|
String[] zFileNames = mPath.list(); |
421 |
414 |
|
for ( String zFileName : zFileNames ) |
422 |
415 |
|
{ |
|
@@ -425,21 +418,23 @@ |
425 |
418 |
|
{ |
426 |
419 |
|
if ( !excludedDir( zFileName ) && acceptableDir( zFileName ) ) |
427 |
420 |
|
{ |
428 |
|
- |
addTo( pPaths, zFileName + "/", zFile ); |
|
421 |
+ |
addTo( zPaths, zFileName + "/", zFile ); |
429 |
422 |
|
} |
430 |
423 |
|
} |
431 |
424 |
|
else |
432 |
425 |
|
{ |
433 |
426 |
|
if ( !excludedFile( zFileName ) && acceptableFile( zFileName ) ) |
434 |
427 |
|
{ |
435 |
|
- |
pPaths.add( new FilePath( mPath, zFileName ) ); |
|
428 |
+ |
zPaths.addCanonicalRelativePath( zFileName ); |
436 |
429 |
|
} |
437 |
430 |
|
} |
438 |
431 |
|
} |
|
432 |
+ |
pPaths.add( zPaths ); |
439 |
433 |
|
} |
440 |
434 |
|
} |
441 |
435 |
|
|
442 |
|
- |
private void addTo( Collection<FilePath> pPaths, String pAdditionalDirPath, File pDirectory ) |
|
436 |
+ |
private void addTo( RootedPaths pPaths, String pAdditionalDirPath, File pDirectory ) |
|
437 |
+ |
throws IOException |
443 |
438 |
|
{ |
444 |
439 |
|
String[] zFileNames = pDirectory.list(); |
445 |
440 |
|
for ( String zFileName : zFileNames ) |
|
@@ -457,7 +452,10 @@ |
457 |
452 |
|
{ |
458 |
453 |
|
if ( !excludedFile( zPath ) && acceptableFile( zPath ) ) |
459 |
454 |
|
{ |
460 |
|
- |
pPaths.add( new FilePath( mPath, zPath ) ); |
|
455 |
+ |
|
|
456 |
+ |
String zFullCanonicalPath = zFile.getCanonicalPath(); |
|
457 |
+ |
String zRelativeCanonicalPath = zFullCanonicalPath.substring( mPath.getPath().length() + 1 ); |
|
458 |
+ |
pPaths.addCanonicalRelativePath( zRelativeCanonicalPath ); |
461 |
459 |
|
} |
462 |
460 |
|
} |
463 |
461 |
|
} |
|
@@ -513,6 +511,7 @@ |
513 |
511 |
|
} |
514 |
512 |
|
|
515 |
513 |
|
public static void main( String[] args ) |
|
514 |
+ |
throws Exception |
516 |
515 |
|
{ |
517 |
516 |
|
if ( args.length == 0 ) |
518 |
517 |
|
{ |
|
@@ -521,7 +520,7 @@ |
521 |
520 |
|
} |
522 |
521 |
|
List<String> patterns = Arrays.asList( args ); |
523 |
522 |
|
patterns = patterns.subList( 1, patterns.size() ); |
524 |
|
- |
for ( String path : new Paths( args[0], patterns.toArray( new String[patterns.size()] ) ) ) |
|
523 |
+ |
for ( String path : new Paths( args[0], patterns.toArray( new String[patterns.size()] ) ).getFullPaths() ) |
525 |
524 |
|
{ |
526 |
525 |
|
System.out.println( path ); |
527 |
526 |
|
} |