litesoft
@ HEAD
litesoft / trunk / Java / ScarPlus / src / com / esotericsoftware / filesystem / Paths.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
package com.esotericsoftware.filesystem; import com.esotericsoftware.utils.*; import com.esotericsoftware.wildcard.*; import java.io.*; import java.util.*; import java.util.zip.*; /** * Collects filesystem paths using wildcards, preserving the directory structure. Copies, deletes, and zips paths. */ public class Paths { static private List<String> sDefaultGlobExcludes = new ArrayList<String>(); /** * Only the Files will be stored! */ private final RootedPathsCollection mPaths = new RootedPathsCollection(); /** * Creates an empty Paths object. */ public Paths() { } /** * Creates a Paths object and calls {@link #glob(String, String[])} with the specified arguments. */ public Paths( String dir, String... patterns ) { glob( dir, patterns ); } public Long getGreatestLastModified() { return isEmpty() ? null : mPaths.getGreatestLastModified(); } public boolean isEmpty() { return mPaths.isEmpty(); } public int count() { return mPaths.count(); } public List<FilePath> getPaths() { return mPaths.collectPaths( new ArrayList<FilePath>() ); } public RootedPaths[] getRootedPaths() { return mPaths.getRootedPaths(); } public void add( FilePath pFilePath ) { mPaths.add( pFilePath ); } @SuppressWarnings({"UnusedDeclaration"}) public void add( RootedPaths pRootedPaths ) { mPaths.add( pRootedPaths ); } public void add( RootedPathsCollection pRootedPathsCollection ) { mPaths.mergeIn( pRootedPathsCollection ); } /** * Adds all paths from the specified Paths object to this Paths object. */ public void add( Paths paths ) { add( paths.mPaths ); } /** * Calls {@link #glob(String, String...)}. */ @SuppressWarnings({"UnusedDeclaration"}) public void glob( String dir, List<String> patterns ) { glob( dir, (patterns == null) ? Util.EMPTY_STRING_ARRAY : patterns.toArray( new String[patterns.size()] ) ); } /** * Collects all files and directories in the specified directory matching the wildcard patterns. * * @param dir The directory containing the paths to collect. If it does not exist, no paths are collected. If null, "." is * assumed. * @param patterns The wildcard patterns of the paths to collect or exclude. Patterns may optionally contain wildcards * represented by asterisks and question marks. If empty or omitted then the dir parameter is split on the "|" * character, the first element is used as the directory and remaining are used as the patterns. If null, ** is * assumed (collects all paths).<br> * <br> * A single question mark (?) matches any single character. Eg, something? collects any path that is named * "something" plus any character.<br> * <br> * A single asterisk (*) matches any characters up to the next slash (/). Eg, *\*\something* collects any path that * has two directories of any name, then a file or directory that starts with the name "something".<br> * <br> * A double asterisk (**) matches any characters. Eg, **\something\** collects any path that contains a directory * named "something".<br> * <br> * A pattern starting with an exclamation point (!) causes paths matched by the pattern to be excluded, even if other * patterns would select the paths. */ public void glob( String dir, String... patterns ) { new PathPatterns( dir, patterns ).addTo( mPaths ); } // ^^^^^^^^^^^^^^^^^^^^^^^ Should These be supported as they can introduce potentially conflicting FileSubPaths ^^^^^^^^^^^^^^^^^^^^^^^ /** * Copies the files and directories to the specified directory. * * @return A paths object containing the paths of the new files. */ @SuppressWarnings({"ResultOfMethodCallIgnored"}) public Paths copyTo( String destDir ) { File zDest = new File( destDir ); zDest.mkdirs(); Paths newPaths = new Paths(); for ( FilePath path : getPaths() ) { String zSubPath = path.getFileSubPath(); FileUtils.copyFile( path.file(), new File( destDir, zSubPath ) ); newPaths.mPaths.add( new FilePath( zDest, zSubPath ) ); } return newPaths; } /** * Updates the files and directories of the specified directory to match 'this' paths. * * @return A paths object containing the paths of the new files. */ @SuppressWarnings({"ResultOfMethodCallIgnored"}) public Paths update( String destDir ) { File zDest = new File( destDir ); if ( !zDest.isDirectory() ) { return copyTo( destDir ); } Paths zExistingPaths = new Paths(); zExistingPaths.glob( destDir, "**" ); if ( zExistingPaths.isEmpty() ) { return copyTo( destDir ); } Paths newPaths = new Paths(); Map<String, File> zDesired = toMap( this ); Map<String, File> zExisting = toMap( zExistingPaths ); for ( Map.Entry<String, File> zEntry : zDesired.entrySet() ) { String zSubPath = zEntry.getKey(); File zDesiredFile = zEntry.getValue(); File zExistingFile = zExisting.remove( zSubPath ); if ( zExistingFile == null ) { FileUtils.copyFile( zDesiredFile, new File( destDir, zSubPath ) ); } else { FileUtils.updateFile( zDesiredFile, zExistingFile ); } newPaths.mPaths.add( new FilePath( zDest, zSubPath ) ); } for ( File zFile : zExisting.values() ) { FileUtils.delete( zFile ); } return newPaths; } private static Map<String, File> toMap( Paths pPaths ) { HashMap<String, File> zMap = new HashMap<String, File>(); for ( FilePath path : pPaths.getPaths() ) { zMap.put( path.getFileSubPath(), path.file() ); } return zMap; } /** * Compresses the files and directories specified by the paths into a new zip file at the specified location. If there are no * paths or all the paths are directories, no zip file will be created. * * @return Files Zipped, 0 means Zip File not even created! */ public int zip( String destFile ) { return zip( destFile, ZipFactory.FOR_ZIPS ); } public int zip( String destFile, ZipFactory pFactory ) { List<FilePath> zPaths = getPaths(); if ( !zPaths.isEmpty() ) { ZipOutputStream out = pFactory.createZOS( destFile, zPaths ); try { for ( FilePath path : zPaths ) { try { out.putNextEntry( pFactory.createZE( path.getFileSubPath().replace( '\\', '/' ) ) ); } catch ( IOException e ) { throw new FileSystemException( e ); } FileInputStream in = FileUtils.createFileInputStream( path.file() ); try { FileUtils.append( in, out ); out.closeEntry(); } catch ( IOException e ) { throw new FileSystemException( e ); } finally { FileUtils.close( in ); } } } finally { FileUtils.close( out ); } } return zPaths.size(); } /** * Returns the absolute paths delimited by the specified character. */ public String toString( String delimiter ) { StringBuilder sb = new StringBuilder( 256 ); for ( String path : getFullPaths() ) { if ( sb.length() > 0 ) { sb.append( delimiter ); } sb.append( path ); } return sb.toString(); } /** * Returns the absolute paths delimited by commas. */ public String toString() { return toString( ", " ); } /** * Returns a Paths object containing the paths that are files, as if each file were selected from its parent directory. */ public Paths flatten() { Paths newPaths = new Paths(); for ( File zFile : getFiles() ) { newPaths.add( new FilePath( zFile.getParentFile(), zFile.getName() ) ); } return newPaths; } /** * Returns the paths as File objects. */ public List<File> getFiles() { List<FilePath> zPaths = getPaths(); List<File> files = new ArrayList<File>( zPaths.size() ); for ( FilePath path : zPaths ) { files.add( path.file() ); } return files; } /** * Returns the portion of the path after the root directory where the path was collected. */ public List<String> getRelativePaths( String pCanonicalJarPath ) { File zCanonicalJarDir = new File( pCanonicalJarPath ).getParentFile(); List<FilePath> zPaths = getPaths(); List<String> rv = new ArrayList<String>( zPaths.size() ); for ( FilePath path : zPaths ) { rv.add( path.relativeFromDir( zCanonicalJarDir ) ); } return rv; } /** * Returns the full paths. */ public List<String> getFullPaths() { List<File> zFiles = getFiles(); List<String> rv = new ArrayList<String>( zFiles.size() ); for ( File file : zFiles ) { rv.add( file.getPath() ); } return rv; } /** * Returns the paths' filenames. */ public List<String> getNames() { List<File> zFiles = getFiles(); List<String> rv = new ArrayList<String>( zFiles.size() ); for ( File file : zFiles ) { rv.add( file.getName() ); } return rv; } /** * Clears the exclude patterns that will be used in addition to the excludes specified for all glob searches. */ @SuppressWarnings({"UnusedDeclaration"}) static public void clearDefaultGlobExcludes() { sDefaultGlobExcludes.clear(); } /** * Adds exclude patterns that will be used in addition to the excludes specified for all glob searches. */ static public void addDefaultGlobExcludes( String... pDefaultGlobExcludes ) { if ( pDefaultGlobExcludes != null ) { sDefaultGlobExcludes.addAll( Arrays.asList( pDefaultGlobExcludes ) ); } } private static class PathPatterns { private final File mPath; private final boolean mIsFile; private final List<Pattern> mIncludes = new ArrayList<Pattern>(); private final List<Pattern> mExcludes = new ArrayList<Pattern>(); public PathPatterns( String pPath, String[] pPatterns ) { pPath = Util.deNull( pPath, "." ).trim(); if ( pPatterns == null || pPatterns.length == 0 ) { String[] split = pPath.split( "\\|" ); // split on a '|' pPath = split[0].trim(); pPatterns = new String[split.length - 1]; for ( int i = 1, n = split.length; i < n; i++ ) { pPatterns[i - 1] = split[i].trim(); } } File zPath = new File( pPath ); if ( zPath.isFile() ) { if ( pPatterns.length != 0 ) { throw new IllegalArgumentException( "Files (e.g. " + zPath + ") may NOT have patterns: " + Arrays.asList( pPatterns ) ); } mIsFile = true; mPath = FileUtils.getCanonicalFile( zPath ); return; } if ( !zPath.isDirectory() ) { throw new IllegalArgumentException( "Path Reference not a File or Directory: " + zPath ); } mIsFile = false; mPath = FileUtils.getCanonicalFile( zPath ); List<String> zIncludes = new ArrayList<String>(); List<String> zExcludes = new ArrayList<String>(); for ( String zPattern : pPatterns ) { if ( null != (zPattern = Util.noEmpty( zPattern )) ) { List<String> zList = zIncludes; if ( zPattern.charAt( 0 ) == '!' ) { if ( null == (zPattern = Util.noEmpty( zPattern.substring( 1 ) )) ) { continue; } zList = zExcludes; } zList.add( zPattern ); } } if ( zIncludes.isEmpty() ) { zIncludes.add( "**" ); } if ( sDefaultGlobExcludes != null ) { zExcludes.addAll( sDefaultGlobExcludes ); } addPatterns( mIncludes, zIncludes ); addPatterns( mExcludes, zExcludes ); } private void addPatterns( List<Pattern> pTargetPatterns, List<String> pSourcePatterns ) { for ( String zPattern : pSourcePatterns ) { pTargetPatterns.add( new Pattern( zPattern ) ); } } public void addTo( RootedPathsCollection pPaths ) { if ( mIsFile ) { pPaths.add( new FilePath( mPath.getParentFile(), mPath.getName() ) ); return; } // Must be a Directory! (See Above) RootedPaths zPaths = new RootedPaths( mPath ); String[] zFileNames = mPath.list(); for ( String zFileName : zFileNames ) { File zFile = new File( mPath, zFileName ); if ( zFile.isDirectory() ) { if ( !excludedDir( zFileName ) && acceptableDir( zFileName ) ) { addTo( zPaths, zFileName + "/", zFile ); } } else { if ( !excludedFile( zFileName ) && acceptableFile( zFileName ) ) { zPaths.addCanonicalRelativePath( zFileName ); } } } pPaths.add( zPaths ); } private void addTo( RootedPaths pPaths, String pAdditionalDirPath, File pDirectory ) { String[] zFileNames = pDirectory.list(); for ( String zFileName : zFileNames ) { String zPath = pAdditionalDirPath + "/" + zFileName; File zFile = new File( pDirectory, zFileName ); if ( zFile.isDirectory() ) { if ( !excludedDir( zPath ) && acceptableDir( zPath ) ) { addTo( pPaths, zPath + "/", zFile ); } } else { if ( !excludedFile( zPath ) && acceptableFile( zPath ) ) { String zFullCanonicalPath = FileUtils.getCanonicalPath( zFile ); String zRelativeCanonicalPath = zFullCanonicalPath.substring( mPath.getPath().length() + 1 ); pPaths.addCanonicalRelativePath( zRelativeCanonicalPath ); } } } } private boolean acceptableDir( String pPath ) { for ( Pattern zInclude : mIncludes ) { if ( zInclude.acceptableDirPath( pPath ) ) { return true; } } return false; } private boolean acceptableFile( String pPath ) { for ( Pattern zInclude : mIncludes ) { if ( zInclude.matchesFilePath( pPath ) ) { return true; } } return false; } private boolean excludedDir( String pPath ) { for ( Pattern zExclude : mExcludes ) { if ( zExclude.matchesDirPathAndChildren( pPath ) ) { return true; } } return false; } private boolean excludedFile( String pPath ) { for ( Pattern zExclude : mExcludes ) { if ( zExclude.matchesFilePath( pPath ) ) { return true; } } return false; } } public static void main( String[] args ) throws Exception { if ( args.length == 0 ) { System.out.println( "Usage: dir [pattern] [, pattern ...]" ); System.exit( 0 ); } List<String> patterns = Arrays.asList( args ); patterns = patterns.subList( 1, patterns.size() ); for ( String path : new Paths( args[0], patterns.toArray( new String[patterns.size()] ) ).getFullPaths() ) { System.out.println( path ); } } } |
Commits for litesoft/trunk/Java/ScarPlus/src/com/esotericsoftware/filesystem/Paths.java
Revision | Author | Commited | Message |
---|---|---|---|
974 Diff | GeorgeS | Mon 25 Aug, 2014 00:35:54 +0000 | Updated to support usage specific artifact repositories |
959 Diff | GeorgeS | Sat 19 Jul, 2014 15:27:50 +0000 | Scar update |
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
943 Diff | GeorgeS | Tue 03 Jun, 2014 04:25:50 +0000 | Extracting commonfoundation |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
889 Diff | GeorgeS | Tue 18 Dec, 2012 00:21:56 +0000 | Correct Relative Path for JARing |
360 Diff | GeorgeS | Sun 07 Aug, 2011 04:10:11 +0000 | |
320 Diff | GeorgeS | Sat 23 Jul, 2011 20:24:31 +0000 | |
316 Diff | GeorgeS | Tue 19 Jul, 2011 01:02:37 +0000 | |
315 | GeorgeS | Sun 17 Jul, 2011 15:48:36 +0000 |