You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kr...@apache.org on 2012/10/09 19:45:38 UTC
svn commit: r1396135 [2/3] - in /maven/shared/trunk/maven-shared-utils/src:
main/java/org/apache/maven/shared/utils/
main/java/org/apache/maven/shared/utils/cli/
main/java/org/apache/maven/shared/utils/cli/shell/
main/java/org/apache/maven/shared/utils...
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryScanner.java Tue Oct 9 17:45:37 2012
@@ -161,17 +161,17 @@ public class DirectoryScanner
/**
* The base directory to be scanned.
*/
- protected File basedir;
+ private File basedir;
/**
* The patterns for the files to be included.
*/
- protected String[] includes;
+ private String[] includes;
/**
* The patterns for the files to be excluded.
*/
- protected String[] excludes;
+ private String[] excludes;
private MatchPatterns excludesPatterns;
@@ -181,52 +181,42 @@ public class DirectoryScanner
/**
* The files which matched at least one include and no excludes and were selected.
*/
- protected List<String> filesIncluded;
+ private List<String> filesIncluded;
/**
* The files which did not match any includes or selectors.
*/
- protected List<String> filesNotIncluded;
+ private List<String> filesNotIncluded;
/**
* The files which matched at least one include and at least one exclude.
*/
- protected List<String> filesExcluded;
+ private List<String> filesExcluded;
/**
* The directories which matched at least one include and no excludes and were selected.
*/
- protected List<String> dirsIncluded;
+ private List<String> dirsIncluded;
/**
* The directories which were found and did not match any includes.
*/
- protected List<String> dirsNotIncluded;
+ private List<String> dirsNotIncluded;
/**
* The directories which matched at least one include and at least one exclude.
*/
- protected List<String> dirsExcluded;
-
- /**
- * The files which matched at least one include and no excludes and which a selector discarded.
- */
- protected List<String> filesDeselected;
-
- /**
- * The directories which matched at least one include and no excludes but which a selector discarded.
- */
- protected List<String> dirsDeselected;
+ private List<String> dirsExcluded;
/**
* Whether or not our results were built by a slow scan.
*/
- protected boolean haveSlowResults = false;
+ private boolean haveSlowResults = false;
/**
* Whether or not the file system should be treated as a case sensitive one.
*/
- protected boolean isCaseSensitive = true;
+ private boolean isCaseSensitive = true;
/**
* Whether or not symbolic links should be followed.
@@ -235,15 +225,11 @@ public class DirectoryScanner
*/
private boolean followSymlinks = true;
- /**
- * Whether or not everything tested so far has been included.
- */
- protected boolean everythingIncluded = true;
/**
* A {@link ScanConductor} an control the scanning process.
*/
- protected ScanConductor scanConductor = null;
+ private ScanConductor scanConductor = null;
/**
* The last ScanAction. We need to store this in the instance as the scan() method doesn't return
@@ -258,91 +244,6 @@ public class DirectoryScanner
}
/**
- * Tests whether or not a given path matches the start of a given pattern up to the first "**".
- * <p/>
- * This is not a general purpose test and should only be used if you can live with false positives. For example,
- * <code>pattern=**\a</code> and <code>str=b</code> will yield <code>true</code>.
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The path to match, as a String. Must not be <code>null</code>.
- * @return whether or not a given path matches the start of a given pattern up to the first "**".
- */
- protected static boolean matchPatternStart( final String pattern, final String str )
- {
- return SelectorUtils.matchPatternStart( pattern, str );
- }
-
- /**
- * Tests whether or not a given path matches the start of a given pattern up to the first "**".
- * <p/>
- * This is not a general purpose test and should only be used if you can live with false positives. For example,
- * <code>pattern=**\a</code> and <code>str=b</code> will yield <code>true</code>.
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The path to match, as a String. Must not be <code>null</code>.
- * @param isCaseSensitive Whether or not matching should be performed case sensitively.
- * @return whether or not a given path matches the start of a given pattern up to the first "**".
- */
- protected static boolean matchPatternStart( final String pattern, final String str, final boolean isCaseSensitive )
- {
- return SelectorUtils.matchPatternStart( pattern, str, isCaseSensitive );
- }
-
- /**
- * Tests whether or not a given path matches a given pattern.
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The path to match, as a String. Must not be <code>null</code>.
- * @return <code>true</code> if the pattern matches against the string, or <code>false</code> otherwise.
- */
- protected static boolean matchPath( final String pattern, final String str )
- {
- return SelectorUtils.matchPath( pattern, str );
- }
-
- /**
- * Tests whether or not a given path matches a given pattern.
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The path to match, as a String. Must not be <code>null</code>.
- * @param isCaseSensitive Whether or not matching should be performed case sensitively.
- * @return <code>true</code> if the pattern matches against the string, or <code>false</code> otherwise.
- */
- protected static boolean matchPath( final String pattern, final String str, final boolean isCaseSensitive )
- {
- return SelectorUtils.matchPath( pattern, str, isCaseSensitive );
- }
-
- /**
- * Tests whether or not a string matches against a pattern. The pattern may contain two special characters:<br>
- * '*' means zero or more characters<br>
- * '?' means one and only one character
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The string which must be matched against the pattern. Must not be <code>null</code>.
- * @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise.
- */
- public static boolean match( final String pattern, final String str )
- {
- return SelectorUtils.match( pattern, str );
- }
-
- /**
- * Tests whether or not a string matches against a pattern. The pattern may contain two special characters:<br>
- * '*' means zero or more characters<br>
- * '?' means one and only one character
- *
- * @param pattern The pattern to match against. Must not be <code>null</code>.
- * @param str The string which must be matched against the pattern. Must not be <code>null</code>.
- * @param isCaseSensitive Whether or not matching should be performed case sensitively.
- * @return <code>true</code> if the string matches against the pattern, or <code>false</code> otherwise.
- */
- protected static boolean match( final String pattern, final String str, final boolean isCaseSensitive )
- {
- return SelectorUtils.match( pattern, str, isCaseSensitive );
- }
-
- /**
* Sets the base directory to be scanned. This is the directory which is scanned recursively. All '/' and '\'
* characters are replaced by <code>File.separatorChar</code>, so the separator used need not match
* <code>File.separatorChar</code>.
@@ -462,16 +363,6 @@ public class DirectoryScanner
}
/**
- * Returns whether or not the scanner has included all the files or directories it has come across so far.
- *
- * @return <code>true</code> if all files and directories which have been found so far have been included.
- */
- public boolean isEverythingIncluded()
- {
- return everythingIncluded;
- }
-
- /**
* Scans the base directory for files which match at least one include pattern and don't match any exclude patterns.
* If there are selectors then the files must pass muster there, as well.
*
@@ -500,37 +391,27 @@ public class DirectoryScanner
filesIncluded = new ArrayList<String>();
filesNotIncluded = new ArrayList<String>();
filesExcluded = new ArrayList<String>();
- filesDeselected = new ArrayList<String>();
dirsIncluded = new ArrayList<String>();
dirsNotIncluded = new ArrayList<String>();
dirsExcluded = new ArrayList<String>();
- dirsDeselected = new ArrayList<String>();
scanAction = ScanConductor.ScanAction.CONTINUE;
if ( isIncluded( "" ) )
{
if ( !isExcluded( "" ) )
{
- if ( isSelected( "", basedir ) )
+ if ( scanConductor != null )
{
- if ( scanConductor != null )
- {
- scanAction = scanConductor.visitDirectory( "", basedir );
+ scanAction = scanConductor.visitDirectory( "", basedir );
- if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
- || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction )
- || ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
- {
- return;
- }
+ if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction )
+ || ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
+ {
+ return;
}
-
- dirsIncluded.add( "" );
- }
- else
- {
- dirsDeselected.add( "" );
}
+
+ dirsIncluded.add( "" );
}
else
{
@@ -607,16 +488,16 @@ public class DirectoryScanner
* <p/>
* Returns immediately if a slow scan has already been completed.
*/
- protected void slowScan()
+ void slowScan()
{
if ( haveSlowResults )
{
return;
}
- final String[] excl = dirsExcluded.toArray( new String[]{ } );
+ final String[] excl = dirsExcluded.toArray( new String[dirsExcluded.size()] );
- final String[] notIncl = dirsNotIncluded.toArray( new String[]{ } );
+ final String[] notIncl = dirsNotIncluded.toArray( new String[dirsNotIncluded.size()] );
for ( String anExcl : excl )
{
@@ -646,7 +527,6 @@ public class DirectoryScanner
* @param vpath The path relative to the base directory (needed to prevent problems with an absolute path when using
* dir). Must not be <code>null</code>.
* @param fast Whether or not this call is part of a fast scan.
- * @throws IOException
* @see #filesIncluded
* @see #filesNotIncluded
* @see #filesExcluded
@@ -655,7 +535,7 @@ public class DirectoryScanner
* @see #dirsExcluded
* @see #slowScan
*/
- protected void scandir( final File dir, final String vpath, final boolean fast )
+ void scandir( final File dir, final String vpath, final boolean fast )
{
String[] newfiles = dir.list();
@@ -710,7 +590,7 @@ public class DirectoryScanner
noLinks.add( newfile );
}
}
- newfiles = noLinks.toArray( new String[]{ } );
+ newfiles = noLinks.toArray( new String[noLinks.size()] );
}
for ( final String newfile : newfiles )
@@ -723,53 +603,34 @@ public class DirectoryScanner
{
if ( !isExcluded( name ) )
{
- if ( isSelected( name, file ) )
+ if ( scanConductor != null )
{
- if ( scanConductor != null )
- {
- scanAction = scanConductor.visitDirectory( name, file );
-
- if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
- || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
- {
- return;
- }
- }
+ scanAction = scanConductor.visitDirectory( name, file );
- if ( !ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
+ if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
{
- dirsIncluded.add( name );
- if ( fast )
- {
- scandir( file, name + File.separator, fast );
-
- if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
- {
- return;
- }
- }
+ return;
}
- scanAction = null;
}
- else
+
+ if ( !ScanConductor.ScanAction.NO_RECURSE.equals( scanAction ) )
{
- everythingIncluded = false;
- dirsDeselected.add( name );
- if ( fast && couldHoldIncluded( name ) )
+ dirsIncluded.add( name );
+ if ( fast )
{
scandir( file, name + File.separator, fast );
+
if ( ScanConductor.ScanAction.ABORT.equals( scanAction ) )
{
return;
}
- scanAction = null;
}
}
+ scanAction = null;
}
else
{
- everythingIncluded = false;
dirsExcluded.add( name );
if ( fast && couldHoldIncluded( name ) )
{
@@ -784,7 +645,6 @@ public class DirectoryScanner
}
else
{
- everythingIncluded = false;
if ( fast && couldHoldIncluded( name ) )
{
if ( scanConductor != null )
@@ -826,36 +686,26 @@ public class DirectoryScanner
{
if ( !isExcluded( name ) )
{
- if ( isSelected( name, file ) )
+ if ( scanConductor != null )
{
- if ( scanConductor != null )
- {
- scanAction = scanConductor.visitFile( name, file );
- }
-
- if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
- || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
- {
- return;
- }
-
- filesIncluded.add( name );
+ scanAction = scanConductor.visitFile( name, file );
}
- else
+
+ if ( ScanConductor.ScanAction.ABORT.equals( scanAction )
+ || ScanConductor.ScanAction.ABORT_DIRECTORY.equals( scanAction ) )
{
- everythingIncluded = false;
- filesDeselected.add( name );
+ return;
}
+
+ filesIncluded.add( name );
}
else
{
- everythingIncluded = false;
filesExcluded.add( name );
}
}
else
{
- everythingIncluded = false;
filesNotIncluded.add( name );
}
}
@@ -869,7 +719,7 @@ public class DirectoryScanner
* @return <code>true</code> when the name matches against at least one include pattern, or <code>false</code>
* otherwise.
*/
- protected boolean isIncluded( final String name )
+ boolean isIncluded( final String name )
{
return includesPatterns.matches( name, isCaseSensitive );
}
@@ -881,7 +731,7 @@ public class DirectoryScanner
* @return <code>true</code> when the name matches against the start of at least one include pattern, or
* <code>false</code> otherwise.
*/
- protected boolean couldHoldIncluded( final String name )
+ boolean couldHoldIncluded( final String name )
{
return includesPatterns.matchesPatternStart( name, isCaseSensitive );
}
@@ -893,25 +743,12 @@ public class DirectoryScanner
* @return <code>true</code> when the name matches against at least one exclude pattern, or <code>false</code>
* otherwise.
*/
- protected boolean isExcluded( final String name )
+ boolean isExcluded( final String name )
{
return excludesPatterns.matches( name, isCaseSensitive );
}
/**
- * Tests whether a name should be selected.
- *
- * @param name the filename to check for selecting
- * @param file the java.io.File object for this filename
- * @return <code>false</code> when the selectors says that the file should not be selected, <code>true</code>
- * otherwise.
- */
- protected boolean isSelected( final String name, final File file )
- {
- return true;
- }
-
- /**
* Returns the names of the files which matched at least one of the include patterns and none of the exclude
* patterns. The names are relative to the base directory.
*
@@ -924,8 +761,7 @@ public class DirectoryScanner
{
return ArrayUtils.EMPTY_STRING_ARRAY;
}
- final String[] files = filesIncluded.toArray( new String[]{ } );
- return files;
+ return filesIncluded.toArray( new String[filesIncluded.size()] );
}
/**
@@ -938,8 +774,7 @@ public class DirectoryScanner
public String[] getNotIncludedFiles()
{
slowScan();
- final String[] files = filesNotIncluded.toArray( new String[]{ } );
- return files;
+ return filesNotIncluded.toArray( new String[filesNotIncluded.size()] );
}
/**
@@ -954,27 +789,7 @@ public class DirectoryScanner
public String[] getExcludedFiles()
{
slowScan();
- final String[] files = filesExcluded.toArray( new String[]{ } );
- return files;
- }
-
- /**
- * <p>
- * Returns the names of the files which were selected out and therefore not ultimately included.
- * </p>
- * <p>
- * The names are relative to the base directory. This involves performing a slow scan if one has not already been
- * completed.
- * </p>
- *
- * @return the names of the files which were deselected.
- * @see #slowScan
- */
- public String[] getDeselectedFiles()
- {
- slowScan();
- final String[] files = filesDeselected.toArray( new String[]{ } );
- return files;
+ return filesExcluded.toArray( new String[filesExcluded.size()] );
}
/**
@@ -986,8 +801,7 @@ public class DirectoryScanner
*/
public String[] getIncludedDirectories()
{
- final String[] directories = dirsIncluded.toArray( new String[]{ } );
- return directories;
+ return dirsIncluded.toArray( new String[dirsIncluded.size()] );
}
/**
@@ -1000,8 +814,7 @@ public class DirectoryScanner
public String[] getNotIncludedDirectories()
{
slowScan();
- final String[] directories = dirsNotIncluded.toArray( new String[]{ } );
- return directories;
+ return dirsNotIncluded.toArray( new String[dirsNotIncluded.size()] );
}
/**
@@ -1016,27 +829,7 @@ public class DirectoryScanner
public String[] getExcludedDirectories()
{
slowScan();
- final String[] directories = dirsExcluded.toArray( new String[]{ } );
- return directories;
- }
-
- /**
- * <p>
- * Returns the names of the directories which were selected out and therefore not ultimately included.
- * </p>
- * <p>
- * The names are relative to the base directory. This involves performing a slow scan if one has not already been
- * completed.
- * </p>
- *
- * @return the names of the directories which were deselected.
- * @see #slowScan
- */
- public String[] getDeselectedDirectories()
- {
- slowScan();
- final String[] directories = dirsDeselected.toArray( new String[]{ } );
- return directories;
+ return dirsExcluded.toArray( new String[dirsExcluded.size()] );
}
/**
@@ -1070,7 +863,7 @@ public class DirectoryScanner
* @param name the name of the file to test.
* @since Ant 1.5
*/
- public boolean isSymbolicLink( final File parent, final String name )
+ boolean isSymbolicLink( final File parent, final String name )
throws IOException
{
final File resolvedParent = new File( parent.getCanonicalPath() );
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryWalker.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryWalker.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryWalker.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/DirectoryWalker.java Tue Oct 9 17:45:37 2012
@@ -18,7 +18,6 @@ package org.apache.maven.shared.utils.io
import java.io.File;
import java.util.ArrayList;
-import java.util.Iterator;
import java.util.List;
import java.util.Stack;
@@ -27,7 +26,7 @@ import java.util.Stack;
*
* @version $Id$
*/
-public class DirectoryWalker
+class DirectoryWalker
{
/**
* DirStackEntry is an Item on the {@link DirectoryWalker#dirStack}
@@ -37,12 +36,12 @@ public class DirectoryWalker
/**
* Count of files in the directory.
*/
- public int count;
+ public final int count;
/**
* Current Directory.
*/
- public File dir;
+ public final File dir;
/**
* Index (or offset) within the directory count.
@@ -118,23 +117,19 @@ public class DirectoryWalker
private int baseDirOffset;
- private Stack dirStack;
+ private Stack<DirStackEntry> dirStack;
- private List excludes;
+ private final List<String> excludes;
- private List includes;
+ private final List<String> includes;
- private boolean isCaseSensitive = true;
-
- private List listeners;
-
- private boolean debugEnabled = false;
+ private final List<DirectoryWalkListener> listeners;
public DirectoryWalker()
{
- this.includes = new ArrayList();
- this.excludes = new ArrayList();
- this.listeners = new ArrayList();
+ this.includes = new ArrayList<String>();
+ this.excludes = new ArrayList<String>();
+ this.listeners = new ArrayList<DirectoryWalkListener>();
}
public void addDirectoryWalkListener( DirectoryWalkListener listener )
@@ -142,12 +137,12 @@ public class DirectoryWalker
this.listeners.add( listener );
}
- public void addExclude( String exclude )
+ void addExclude( String exclude )
{
this.excludes.add( fixPattern( exclude ) );
}
- public void addInclude( String include )
+ void addInclude( String include )
{
this.includes.add( fixPattern( include ) );
}
@@ -158,51 +153,41 @@ public class DirectoryWalker
public void addSCMExcludes()
{
String scmexcludes[] = DirectoryScanner.DEFAULTEXCLUDES;
- for ( int i = 0; i < scmexcludes.length; i++ )
- {
- addExclude( scmexcludes[i] );
+ for (String scmexclude : scmexcludes) {
+ addExclude(scmexclude);
}
}
private void fireStep( File file )
{
- DirStackEntry dsEntry = (DirStackEntry) dirStack.peek();
+ DirStackEntry dsEntry = dirStack.peek();
int percentage = dsEntry.getPercentage();
- Iterator it = this.listeners.iterator();
- while ( it.hasNext() )
- {
- DirectoryWalkListener listener = (DirectoryWalkListener) it.next();
- listener.directoryWalkStep( percentage, file );
+ for (DirectoryWalkListener listener : this.listeners) {
+ listener.directoryWalkStep(percentage, file);
}
}
private void fireWalkFinished()
{
- Iterator it = this.listeners.iterator();
- while ( it.hasNext() )
- {
- DirectoryWalkListener listener = (DirectoryWalkListener) it.next();
+ for (Object listener1 : this.listeners) {
+ DirectoryWalkListener listener = (DirectoryWalkListener) listener1;
listener.directoryWalkFinished();
}
}
private void fireWalkStarting()
{
- Iterator it = this.listeners.iterator();
- while ( it.hasNext() )
- {
- DirectoryWalkListener listener = (DirectoryWalkListener) it.next();
- listener.directoryWalkStarting( this.baseDir );
+ for (Object listener1 : this.listeners) {
+ DirectoryWalkListener listener = (DirectoryWalkListener) listener1;
+ listener.directoryWalkStarting(this.baseDir);
}
}
private void fireDebugMessage( String message )
{
- Iterator it = this.listeners.iterator();
- while ( it.hasNext() )
- {
- DirectoryWalkListener listener = (DirectoryWalkListener) it.next();
- listener.debug( message );
+ for (Object listener1 : this.listeners) {
+ DirectoryWalkListener listener = (DirectoryWalkListener) listener1;
+ listener.debug(message);
}
}
@@ -223,35 +208,6 @@ public class DirectoryWalker
return cleanPattern;
}
- public void setDebugMode( boolean debugEnabled )
- {
- this.debugEnabled = debugEnabled;
- }
-
- /**
- * @return Returns the baseDir.
- */
- public File getBaseDir()
- {
- return baseDir;
- }
-
- /**
- * @return Returns the excludes.
- */
- public List getExcludes()
- {
- return excludes;
- }
-
- /**
- * @return Returns the includes.
- */
- public List getIncludes()
- {
- return includes;
- }
-
private boolean isExcluded( String name )
{
return isMatch( this.excludes, name );
@@ -264,12 +220,10 @@ public class DirectoryWalker
private boolean isMatch( List patterns, String name )
{
- Iterator it = patterns.iterator();
- while ( it.hasNext() )
- {
- String pattern = (String) it.next();
- if ( SelectorUtils.matchPath( pattern, name, isCaseSensitive ) )
- {
+ for (Object pattern1 : patterns) {
+ String pattern = (String) pattern1;
+ boolean caseSensitive = true;
+ if (SelectorUtils.matchPath(pattern, name, caseSensitive)) {
return true;
}
}
@@ -283,16 +237,6 @@ public class DirectoryWalker
}
/**
- * Removes a DirectoryWalkListener.
- *
- * @param listener the listener to remove.
- */
- public void removeDirectoryWalkListener( DirectoryWalkListener listener )
- {
- this.listeners.remove( listener );
- }
-
- /**
* Performs a Scan against the provided {@link #setBaseDir(File)}
*/
public void scan()
@@ -318,31 +262,8 @@ public class DirectoryWalker
addInclude( "**" );
}
- if ( debugEnabled )
- {
- Iterator it;
- StringBuffer dbg = new StringBuffer();
- dbg.append( "DirectoryWalker Scan" );
- dbg.append( "\n Base Dir: " ).append( this.baseDir.getAbsolutePath() );
- dbg.append( "\n Includes: " );
- it = this.includes.iterator();
- while ( it.hasNext() )
- {
- String include = (String) it.next();
- dbg.append( "\n - \"" ).append( include ).append( "\"" );
- }
- dbg.append( "\n Excludes: " );
- it = this.excludes.iterator();
- while ( it.hasNext() )
- {
- String exclude = (String) it.next();
- dbg.append( "\n - \"" ).append( exclude ).append( "\"" );
- }
- fireDebugMessage( dbg.toString() );
- }
-
fireWalkStarting();
- dirStack = new Stack();
+ dirStack = new Stack<DirStackEntry>();
scanDir( this.baseDir );
fireWalkFinished();
}
@@ -364,7 +285,7 @@ public class DirectoryWalker
}
else
{
- DirStackEntry previousStackEntry = (DirStackEntry) dirStack.peek();
+ DirStackEntry previousStackEntry = dirStack.peek();
curStackEntry.percentageOffset = previousStackEntry.getNextPercentageOffset();
curStackEntry.percentageSize = previousStackEntry.getNextPercentageSize();
}
@@ -407,38 +328,4 @@ public class DirectoryWalker
this.baseDirOffset = baseDir.getAbsolutePath().length();
}
- /**
- * @param entries The excludes to set.
- */
- public void setExcludes( List entries )
- {
- this.excludes.clear();
- if ( entries != null )
- {
- Iterator it = entries.iterator();
- while ( it.hasNext() )
- {
- String pattern = (String) it.next();
- this.excludes.add( fixPattern( pattern ) );
- }
- }
- }
-
- /**
- * @param entries The includes to set.
- */
- public void setIncludes( List entries )
- {
- this.includes.clear();
- if ( entries != null )
- {
- Iterator it = entries.iterator();
- while ( it.hasNext() )
- {
- String pattern = (String) it.next();
- this.includes.add( fixPattern( pattern ) );
- }
- }
- }
-
}
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/FileUtils.java Tue Oct 9 17:45:37 2012
@@ -37,10 +37,8 @@ import java.net.URL;
import java.nio.channels.FileChannel;
import java.security.SecureRandom;
import java.text.DecimalFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Random;
+import java.util.*;
+
import org.apache.maven.shared.utils.Os;
import org.apache.maven.shared.utils.StringUtils;
@@ -52,19 +50,14 @@ import org.apache.maven.shared.utils.Str
* <p>Methods exist to retrieve the components of a typical file path. For example
* <code>/www/hosted/mysite/index.html</code>, can be broken into:
* <ul>
- * <li><code>/www/hosted/mysite/</code> -- retrievable through {@link #getPath}</li>
- * <li><code>index.html</code> -- retrievable through {@link #removePath}</li>
* <li><code>/www/hosted/mysite/index</code> -- retrievable through {@link #removeExtension}</li>
* <li><code>html</code> -- retrievable through {@link #getExtension}</li>
* </ul>
- * There are also methods to {@link #catPath concatenate two paths}, {@link #resolveFile resolve a
- * path relative to a File} and {@link #normalize} a path.
* </p>
* <p/>
* <h3>File-related methods</h3>
* <p/>
* There are methods to create a {@link #toFile File from a URL}, copy a
- * {@link #copyFileToDirectory File to a directory},
* copy a {@link #copyFile File to another File},
* copy a {@link #copyURLToFile URL's contents to a File},
* as well as methods to {@link #deleteDirectory(File) delete} and {@link #cleanDirectory(File)
@@ -88,25 +81,25 @@ import org.apache.maven.shared.utils.Str
*/
public class FileUtils
{
- private FileUtils()
+ protected FileUtils()
{
- // This is a utility class. We add a private ct to prevent initialisation
+ // This is a utility class. Normally dont instantiate
}
/**
* The number of bytes in a kilobyte.
*/
- public static final int ONE_KB = 1024;
+ private static final int ONE_KB = 1024;
/**
* The number of bytes in a megabyte.
*/
- public static final int ONE_MB = ONE_KB * ONE_KB;
+ private static final int ONE_MB = ONE_KB * ONE_KB;
/**
* The number of bytes in a gigabyte.
*/
- public static final int ONE_GB = ONE_KB * ONE_MB;
+ private static final int ONE_GB = ONE_KB * ONE_MB;
/**
* The file copy buffer size (30 MB)
@@ -116,7 +109,7 @@ public class FileUtils
/**
* The vm line separator
*/
- public static String FS = System.getProperty( "file.separator" );
+ private static final String FS = System.getProperty( "file.separator" );
/**
* Non-valid Characters for naming files, folders under Windows: <code>":", "*", "?", "\"", "<", ">", "|"</code>
@@ -230,7 +223,7 @@ public class FileUtils
* @param suffix the file suffix
* @return the basename of the file
*/
- public static String basename( String filename, String suffix )
+ private static String basename( String filename, String suffix )
{
int i = filename.lastIndexOf( File.separator ) + 1;
int lastDot = ( ( suffix != null ) && ( suffix.length() > 0 ) ) ? filename.lastIndexOf( suffix ) : -1;
@@ -314,7 +307,7 @@ public class FileUtils
* @return the file content using the specified encoding.
* @throws IOException if any
*/
- public static String fileRead( String file, String encoding )
+ private static String fileRead( String file, String encoding )
throws IOException
{
return fileRead( new File( file ), encoding );
@@ -342,7 +335,7 @@ public class FileUtils
public static String fileRead( File file, String encoding )
throws IOException
{
- StringBuffer buf = new StringBuffer();
+ StringBuilder buf = new StringBuilder();
Reader reader = null;
@@ -568,66 +561,11 @@ public class FileUtils
public static void fileDelete( String fileName )
{
File file = new File( fileName );
+ //noinspection ResultOfMethodCallIgnored
file.delete();
}
/**
- * Waits for NFS to propagate a file creation, imposing a timeout.
- *
- * @param fileName The path of the file.
- * @param seconds The maximum time in seconds to wait.
- * @return True if file exists.
- */
- public static boolean waitFor( String fileName, int seconds )
- {
- return waitFor( new File( fileName ), seconds );
- }
-
- /**
- * Waits for NFS to propagate a file creation, imposing a timeout.
- *
- * @param file The file.
- * @param seconds The maximum time in seconds to wait.
- * @return True if file exists.
- */
- public static boolean waitFor( File file, int seconds )
- {
- int timeout = 0;
- int tick = 0;
- while ( !file.exists() )
- {
- if ( tick++ >= 10 )
- {
- tick = 0;
- if ( timeout++ > seconds )
- {
- return false;
- }
- }
- try
- {
- Thread.sleep( 100 );
- }
- catch ( InterruptedException ignore )
- {
- // nop
- }
- }
- return true;
- }
-
- /**
- * Creates a file handle.
- *
- * @param fileName The path of the file.
- * @return A <code>File</code> manager.
- */
- public static File getFile( String fileName )
- {
- return new File( fileName );
- }
-
- /**
* Given a directory and an array of extensions return an array of compliant files.
* <p/>
* TODO Should an ignore list be passed in?
@@ -652,33 +590,27 @@ public class FileUtils
return new String[0];
}
- for ( int i = 0; i < unknownFiles.length; ++i )
- {
- String currentFileName = directory + System.getProperty( "file.separator" ) + unknownFiles[i];
- File currentFile = new File( currentFileName );
+ for (String unknownFile : unknownFiles) {
+ String currentFileName = directory + System.getProperty("file.separator") + unknownFile;
+ File currentFile = new File(currentFileName);
- if ( currentFile.isDirectory() )
- {
+ if (currentFile.isDirectory()) {
//ignore all CVS directories...
- if ( currentFile.getName().equals( "CVS" ) )
- {
+ if (currentFile.getName().equals("CVS")) {
continue;
}
//ok... transverse into this directory and get all the files... then combine
//them with the current list.
- String[] fetchFiles = getFilesFromExtension( currentFileName, extensions );
- files = blendFilesToVector( files, fetchFiles );
- }
- else
- {
+ String[] fetchFiles = getFilesFromExtension(currentFileName, extensions);
+ files = blendFilesToVector(files, fetchFiles);
+ } else {
//ok... add the file
String add = currentFile.getAbsolutePath();
- if ( isValidFile( add, extensions ) )
- {
- files.add( add );
+ if (isValidFile(add, extensions)) {
+ files.add(add);
}
}
}
@@ -695,10 +627,7 @@ public class FileUtils
*/
private static List<String> blendFilesToVector( List<String> v, String[] files )
{
- for ( int i = 0; i < files.length; ++i )
- {
- v.add( files[i] );
- }
+ Collections.addAll(v, files);
return v;
}
@@ -719,10 +648,8 @@ public class FileUtils
//ok.. now that we have the "extension" go through the current know
//excepted extensions and determine if this one is OK.
- for ( int i = 0; i < extensions.length; ++i )
- {
- if ( extensions[i].equals( extension ) )
- {
+ for (String extension1 : extensions) {
+ if (extension1.equals(extension)) {
return true;
}
}
@@ -751,6 +678,7 @@ public class FileUtils
if ( !file.exists() )
{
+ //noinspection ResultOfMethodCallIgnored
file.mkdirs();
}
}
@@ -893,122 +821,6 @@ public class FileUtils
}
/**
- * Remove path from filename. Equivalent to the unix command <code>basename</code>
- * ie.
- * <pre>
- * a/b/c.txt --> c.txt
- * a.txt --> a.txt
- * </pre>
- *
- * @param filepath the path of the file
- * @return the filename minus path
- */
- public static String removePath( final String filepath )
- {
- return removePath( filepath, File.separatorChar );
- }
-
- /**
- * Remove path from filename.
- * ie.
- * <pre>
- * a/b/c.txt --> c.txt
- * a.txt --> a.txt
- * </pre>
- *
- * @param filepath the path of the file
- * @param fileSeparatorChar the file separator character like <b>/</b> on Unix plateforms.
- * @return the filename minus path
- */
- public static String removePath( final String filepath, final char fileSeparatorChar )
- {
- final int index = filepath.lastIndexOf( fileSeparatorChar );
-
- if ( -1 == index )
- {
- return filepath;
- }
-
- return filepath.substring( index + 1 );
- }
-
- /**
- * Get path from filename. Roughly equivalent to the unix command <code>dirname</code>.
- * ie.
- * <pre>
- * a/b/c.txt --> a/b
- * a.txt --> ""
- * </pre>
- *
- * @param filepath the filepath
- * @return the filename minus path
- */
- public static String getPath( final String filepath )
- {
- return getPath( filepath, File.separatorChar );
- }
-
- /**
- * Get path from filename.
- * ie.
- * <pre>
- * a/b/c.txt --> a/b
- * a.txt --> ""
- * </pre>
- *
- * @param filepath the filepath
- * @param fileSeparatorChar the file separator character like <b>/</b> on Unix plateforms.
- * @return the filename minus path
- */
- public static String getPath( final String filepath, final char fileSeparatorChar )
- {
- final int index = filepath.lastIndexOf( fileSeparatorChar );
- if ( -1 == index )
- {
- return "";
- }
-
- return filepath.substring( 0, index );
- }
-
- /**
- * Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it
- * (and any parent directories) will be created. If a file <code>source</code> in
- * <code>destinationDirectory</code> exists, it will be overwritten.
- *
- * @param source An existing <code>File</code> to copy.
- * @param destinationDirectory A directory to copy <code>source</code> into.
- * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
- * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
- * @throws IOException if <code>source</code> does not exist, the file in
- * <code>destinationDirectory</code> cannot be written to, or an IO error occurs during copying.
- */
- public static void copyFileToDirectory( final String source, final String destinationDirectory )
- throws IOException
- {
- copyFileToDirectory( new File( source ), new File( destinationDirectory ) );
- }
-
- /**
- * Copy file from source to destination only if source is newer than the target file.
- * If <code>destinationDirectory</code> does not exist, it
- * (and any parent directories) will be created. If a file <code>source</code> in
- * <code>destinationDirectory</code> exists, it will be overwritten.
- *
- * @param source An existing <code>File</code> to copy.
- * @param destinationDirectory A directory to copy <code>source</code> into.
- * @throws java.io.FileNotFoundException if <code>source</code> isn't a normal file.
- * @throws IllegalArgumentException if <code>destinationDirectory</code> isn't a directory.
- * @throws IOException if <code>source</code> does not exist, the file in
- * <code>destinationDirectory</code> cannot be written to, or an IO error occurs during copying.
- */
- public static void copyFileToDirectoryIfModified( final String source, final String destinationDirectory )
- throws IOException
- {
- copyFileToDirectoryIfModified( new File( source ), new File( destinationDirectory ) );
- }
-
- /**
* Copy file from source to destination. If <code>destinationDirectory</code> does not exist, it
* (and any parent directories) will be created. If a file <code>source</code> in
* <code>destinationDirectory</code> exists, it will be overwritten.
@@ -1044,7 +856,7 @@ public class FileUtils
* @throws IOException if <code>source</code> does not exist, the file in
* <code>destinationDirectory</code> cannot be written to, or an IO error occurs during copying.
*/
- public static void copyFileToDirectoryIfModified( final File source, final File destinationDirectory )
+ private static void copyFileToDirectoryIfModified( final File source, final File destinationDirectory )
throws IOException
{
if ( destinationDirectory.exists() && !destinationDirectory.isDirectory() )
@@ -1067,7 +879,7 @@ public class FileUtils
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
* written to, or an IO error occurs during copying.
* @throws java.io.FileNotFoundException if <code>destination</code> is a directory
- * (use {@link #copyFileToDirectory}).
+ *
*/
public static void copyFile( final File source, final File destination )
throws IOException
@@ -1102,6 +914,7 @@ public class FileUtils
//does destination directory exist ?
if ( destination.getParentFile() != null && !destination.getParentFile().exists() )
{
+ //noinspection ResultOfMethodCallIgnored
destination.getParentFile().mkdirs();
}
}
@@ -1121,7 +934,7 @@ public class FileUtils
output = fos.getChannel();
long size = input.size();
long pos = 0;
- long count = 0;
+ long count;
while ( pos < size )
{
count = size - pos > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : size - pos;
@@ -1149,7 +962,7 @@ public class FileUtils
* @throws IOException if <code>source</code> does not exist, <code>destination</code> cannot be
* written to, or an IO error occurs during copying.
*/
- public static boolean copyFileIfModified( final File source, final File destination )
+ private static boolean copyFileIfModified( final File source, final File destination )
throws IOException
{
if ( destination.lastModified() < source.lastModified() )
@@ -1199,12 +1012,13 @@ public class FileUtils
* <li>an IO error occurs during copying</li>
* </ul>
*/
- public static void copyStreamToFile( final InputStream source, final File destination )
+ private static void copyStreamToFile( final InputStream source, final File destination )
throws IOException
{
//does destination directory exist ?
if ( destination.getParentFile() != null && !destination.getParentFile().exists() )
{
+ //noinspection ResultOfMethodCallIgnored
destination.getParentFile().mkdirs();
}
@@ -1231,204 +1045,6 @@ public class FileUtils
}
/**
- * Normalize a path.
- * Eliminates "/../" and "/./" in a string. Returns <code>null</code> if the ..'s went past the
- * root.
- * Eg:
- * <pre>
- * /foo// --> /foo/
- * /foo/./ --> /foo/
- * /foo/../bar --> /bar
- * /foo/../bar/ --> /bar/
- * /foo/../bar/../baz --> /baz
- * //foo//./bar --> /foo/bar
- * /../ --> null
- * </pre>
- *
- * @param path the path to normalize
- * @return the normalized String, or <code>null</code> if too many ..'s.
- */
- public static String normalize( final String path )
- {
- String normalized = path;
- // Resolve occurrences of "//" in the normalized path
- while ( true )
- {
- int index = normalized.indexOf( "//" );
- if ( index < 0 )
- {
- break;
- }
- normalized = normalized.substring( 0, index ) + normalized.substring( index + 1 );
- }
-
- // Resolve occurrences of "/./" in the normalized path
- while ( true )
- {
- int index = normalized.indexOf( "/./" );
- if ( index < 0 )
- {
- break;
- }
- normalized = normalized.substring( 0, index ) + normalized.substring( index + 2 );
- }
-
- // Resolve occurrences of "/../" in the normalized path
- while ( true )
- {
- int index = normalized.indexOf( "/../" );
- if ( index < 0 )
- {
- break;
- }
- if ( index == 0 )
- {
- return null; // Trying to go outside our context
- }
- int index2 = normalized.lastIndexOf( '/', index - 1 );
- normalized = normalized.substring( 0, index2 ) + normalized.substring( index + 3 );
- }
-
- // Return the normalized path that we have completed
- return normalized;
- }
-
- /**
- * Will concatenate 2 paths. Paths with <code>..</code> will be
- * properly handled.
- * <p>Eg.,<br />
- * <code>/a/b/c</code> + <code>d</code> = <code>/a/b/d</code><br />
- * <code>/a/b/c</code> + <code>../d</code> = <code>/a/d</code><br />
- * </p>
- * <p/>
- * Thieved from Tomcat sources...
- *
- * @param lookupPath a path
- * @param path the path to concatenate
- * @return The concatenated paths, or null if error occurs
- */
- public static String catPath( final String lookupPath, final String path )
- {
- // Cut off the last slash and everything beyond
- int index = lookupPath.lastIndexOf( "/" );
- String lookup = lookupPath.substring( 0, index );
- String pth = path;
-
- // Deal with .. by chopping dirs off the lookup path
- while ( pth.startsWith( "../" ) )
- {
- if ( lookup.length() > 0 )
- {
- index = lookup.lastIndexOf( "/" );
- lookup = lookup.substring( 0, index );
- }
- else
- {
- // More ..'s than dirs, return null
- return null;
- }
-
- index = pth.indexOf( "../" ) + 3;
- pth = pth.substring( index );
- }
-
- return new StringBuffer( lookup ).append( "/" ).append( pth ).toString();
- }
-
- /**
- * Resolve a file <code>filename</code> to it's canonical form. If <code>filename</code> is
- * relative (doesn't start with <code>/</code>), it will be resolved relative to
- * <code>baseFile</code>, otherwise it is treated as a normal root-relative path.
- *
- * @param baseFile Where to resolve <code>filename</code> from, if <code>filename</code> is
- * relative.
- * @param filename Absolute or relative file path to resolve.
- * @return The canonical <code>File</code> of <code>filename</code>.
- */
- public static File resolveFile( final File baseFile, String filename )
- {
- String filenm = filename;
- if ( '/' != File.separatorChar )
- {
- filenm = filename.replace( '/', File.separatorChar );
- }
-
- if ( '\\' != File.separatorChar )
- {
- filenm = filename.replace( '\\', File.separatorChar );
- }
-
- // deal with absolute files
- if ( filenm.startsWith( File.separator ) || ( Os.isFamily( Os.FAMILY_WINDOWS ) && filenm.indexOf( ":" ) > 0 ) )
- {
- File file = new File( filenm );
-
- try
- {
- file = file.getCanonicalFile();
- }
- catch ( final IOException ioe )
- {
- // nop
- }
-
- return file;
- }
- // FIXME: I'm almost certain this // removal is unnecessary, as getAbsoluteFile() strips
- // them. However, I'm not sure about this UNC stuff. (JT)
- final char[] chars = filename.toCharArray();
- final StringBuffer sb = new StringBuffer();
-
- //remove duplicate file separators in succession - except
- //on win32 at start of filename as UNC filenames can
- //be \\AComputer\AShare\myfile.txt
- int start = 0;
- if ( '\\' == File.separatorChar )
- {
- sb.append( filenm.charAt( 0 ) );
- start++;
- }
-
- for ( int i = start; i < chars.length; i++ )
- {
- final boolean doubleSeparator = File.separatorChar == chars[i] && File.separatorChar == chars[i - 1];
-
- if ( !doubleSeparator )
- {
- sb.append( chars[i] );
- }
- }
-
- filenm = sb.toString();
-
- //must be relative
- File file = ( new File( baseFile, filenm ) ).getAbsoluteFile();
-
- try
- {
- file = file.getCanonicalFile();
- }
- catch ( final IOException ioe )
- {
- // nop
- }
-
- return file;
- }
-
- /**
- * Delete a file. If file is directory delete it and all sub-directories.
- *
- * @param file the file path
- * @throws IOException if any
- */
- public static void forceDelete( final String file )
- throws IOException
- {
- forceDelete( new File( file ) );
- }
-
- /**
* Delete a file. If file is directory delete it and all sub-directories.
*
* @param file a file
@@ -1494,92 +1110,6 @@ public class FileUtils
return true;
}
- /**
- * Schedule a file to be deleted when JVM exits.
- * If file is directory delete it and all sub-directories.
- *
- * @param file a file
- * @throws IOException if any
- */
- public static void forceDeleteOnExit( final File file )
- throws IOException
- {
- if ( !file.exists() )
- {
- return;
- }
-
- if ( file.isDirectory() )
- {
- deleteDirectoryOnExit( file );
- }
- else
- {
- file.deleteOnExit();
- }
- }
-
- /**
- * Recursively schedule directory for deletion on JVM exit.
- *
- * @param directory a directory
- * @throws IOException if any
- */
- private static void deleteDirectoryOnExit( final File directory )
- throws IOException
- {
- if ( !directory.exists() )
- {
- return;
- }
-
- cleanDirectoryOnExit( directory );
- directory.deleteOnExit();
- }
-
- /**
- * Clean a directory without deleting it.
- *
- * @param directory a directory
- * @throws IOException if any
- */
- private static void cleanDirectoryOnExit( final File directory )
- throws IOException
- {
- if ( !directory.exists() )
- {
- final String message = directory + " does not exist";
- throw new IllegalArgumentException( message );
- }
-
- if ( !directory.isDirectory() )
- {
- final String message = directory + " is not a directory";
- throw new IllegalArgumentException( message );
- }
-
- IOException exception = null;
-
- final File[] files = directory.listFiles();
- for ( int i = 0; i < files.length; i++ )
- {
- final File file = files[i];
- try
- {
- forceDeleteOnExit( file );
- }
- catch ( final IOException ioe )
- {
- exception = ioe;
- }
- }
-
- if ( null != exception )
- {
- throw exception;
- }
- }
-
/**
* Make a directory.
@@ -1611,7 +1141,7 @@ public class FileUtils
}
else
{
- if ( false == file.mkdirs() )
+ if (!file.mkdirs())
{
final String message = "Unable to create directory " + file;
throw new IOException( message );
@@ -1667,18 +1197,6 @@ public class FileUtils
* @param directory a directory
* @throws IOException if any
*/
- public static void cleanDirectory( final String directory )
- throws IOException
- {
- cleanDirectory( new File( directory ) );
- }
-
- /**
- * Clean a directory without deleting it.
- *
- * @param directory a directory
- * @throws IOException if any
- */
public static void cleanDirectory( final File directory )
throws IOException
{
@@ -1703,15 +1221,10 @@ public class FileUtils
return;
}
- for ( int i = 0; i < files.length; i++ )
- {
- final File file = files[i];
- try
- {
- forceDelete( file );
- }
- catch ( final IOException ioe )
- {
+ for (final File file : files) {
+ try {
+ forceDelete(file);
+ } catch (final IOException ioe) {
exception = ioe;
}
}
@@ -1756,16 +1269,10 @@ public class FileUtils
long size = 0;
final File[] files = directory.listFiles();
- for ( int i = 0; i < files.length; i++ )
- {
- final File file = files[i];
-
- if ( file.isDirectory() )
- {
- size += sizeOfDirectory( file );
- }
- else
- {
+ for (final File file : files) {
+ if (file.isDirectory()) {
+ size += sizeOfDirectory(file);
+ } else {
size += file.length();
}
}
@@ -1844,7 +1351,7 @@ public class FileUtils
* @return a list of files as String
* @throws IOException
*/
- public static List<String> getFileNames( File directory, String includes, String excludes, boolean includeBasedir,
+ private static List<String> getFileNames( File directory, String includes, String excludes, boolean includeBasedir,
boolean isCaseSensitive )
throws IOException
{
@@ -1898,12 +1405,10 @@ public class FileUtils
* @param getFiles true if get files
* @param getDirectories true if get directories
* @return a list of files as String
- * @throws IOException
*/
public static List<String> getFileAndDirectoryNames( File directory, String includes, String excludes,
boolean includeBasedir, boolean isCaseSensitive,
boolean getFiles, boolean getDirectories )
- throws IOException
{
DirectoryScanner scanner = new DirectoryScanner();
@@ -1929,15 +1434,11 @@ public class FileUtils
{
String[] files = scanner.getIncludedFiles();
- for ( int i = 0; i < files.length; i++ )
- {
- if ( includeBasedir )
- {
- list.add( directory + FileUtils.FS + files[i] );
- }
- else
- {
- list.add( files[i] );
+ for (String file : files) {
+ if (includeBasedir) {
+ list.add(directory + FileUtils.FS + file);
+ } else {
+ list.add(file);
}
}
}
@@ -1946,15 +1447,11 @@ public class FileUtils
{
String[] directories = scanner.getIncludedDirectories();
- for ( int i = 0; i < directories.length; i++ )
- {
- if ( includeBasedir )
- {
- list.add( directory + FileUtils.FS + directories[i] );
- }
- else
- {
- list.add( directories[i] );
+ for (String directory1 : directories) {
+ if (includeBasedir) {
+ list.add(directory + FileUtils.FS + directory1);
+ } else {
+ list.add(directory1);
}
}
}
@@ -2003,82 +1500,6 @@ public class FileUtils
}
/**
- * Copies a entire directory layout : no files will be copied only directories
- * <p/>
- * Note:
- * <ul>
- * <li>It will include empty directories.
- * <li>The <code>sourceDirectory</code> must exists.
- * </ul>
- *
- * @param sourceDirectory the source dir
- * @param destinationDirectory the target dir
- * @param includes include pattern
- * @param excludes exlucde pattern
- * @throws IOException if any
- * @since 1.5.7
- */
- public static void copyDirectoryLayout( File sourceDirectory, File destinationDirectory, String[] includes,
- String[] excludes )
- throws IOException
- {
- if ( sourceDirectory == null )
- {
- throw new IOException( "source directory can't be null." );
- }
-
- if ( destinationDirectory == null )
- {
- throw new IOException( "destination directory can't be null." );
- }
-
- if ( sourceDirectory.equals( destinationDirectory ) )
- {
- throw new IOException( "source and destination are the same directory." );
- }
-
- if ( !sourceDirectory.exists() )
- {
- throw new IOException( "Source directory doesn't exists (" + sourceDirectory.getAbsolutePath() + ")." );
- }
-
- DirectoryScanner scanner = new DirectoryScanner();
-
- scanner.setBasedir( sourceDirectory );
-
- if ( includes != null && includes.length >= 1 )
- {
- scanner.setIncludes( includes );
- }
- else
- {
- scanner.setIncludes( new String[]{ "**" } );
- }
-
- if ( excludes != null && excludes.length >= 1 )
- {
- scanner.setExcludes( excludes );
- }
-
- scanner.addDefaultExcludes();
- scanner.scan();
- List<String> includedDirectories = Arrays.asList( scanner.getIncludedDirectories() );
-
- for ( String name : includedDirectories )
- {
- File source = new File( sourceDirectory, name );
-
- if ( source.equals( sourceDirectory ) )
- {
- continue;
- }
-
- File destination = new File( destinationDirectory, name );
- destination.mkdirs();
- }
- }
-
- /**
* Copies a entire directory structure.
* <p/>
* Note:
@@ -2097,25 +1518,6 @@ public class FileUtils
copyDirectoryStructure( sourceDirectory, destinationDirectory, destinationDirectory, false );
}
- /**
- * Copies an entire directory structure but only source files with timestamp later than the destinations'.
- * <p/>
- * Note:
- * <ul>
- * <li>It will include empty directories.
- * <li>The <code>sourceDirectory</code> must exists.
- * </ul>
- *
- * @param sourceDirectory the source dir
- * @param destinationDirectory the target dir
- * @throws IOException if any
- */
- public static void copyDirectoryStructureIfModified( File sourceDirectory, File destinationDirectory )
- throws IOException
- {
- copyDirectoryStructure( sourceDirectory, destinationDirectory, destinationDirectory, true );
- }
-
private static void copyDirectoryStructure( File sourceDirectory, File destinationDirectory,
File rootDestinationDirectory, boolean onlyModifiedFiles )
throws IOException
@@ -2144,48 +1546,35 @@ public class FileUtils
String sourcePath = sourceDirectory.getAbsolutePath();
- for ( int i = 0; i < files.length; i++ )
- {
- File file = files[i];
-
- if ( file.equals( rootDestinationDirectory ) )
- {
+ for (File file : files) {
+ if (file.equals(rootDestinationDirectory)) {
// We don't copy the destination directory in itself
continue;
}
String dest = file.getAbsolutePath();
- dest = dest.substring( sourcePath.length() + 1 );
+ dest = dest.substring(sourcePath.length() + 1);
- File destination = new File( destinationDirectory, dest );
+ File destination = new File(destinationDirectory, dest);
- if ( file.isFile() )
- {
+ if (file.isFile()) {
destination = destination.getParentFile();
- if ( onlyModifiedFiles )
- {
- copyFileToDirectoryIfModified( file, destination );
- }
- else
- {
- copyFileToDirectory( file, destination );
+ if (onlyModifiedFiles) {
+ copyFileToDirectoryIfModified(file, destination);
+ } else {
+ copyFileToDirectory(file, destination);
}
- }
- else if ( file.isDirectory() )
- {
- if ( !destination.exists() && !destination.mkdirs() )
- {
+ } else if (file.isDirectory()) {
+ if (!destination.exists() && !destination.mkdirs()) {
throw new IOException(
- "Could not create destination directory '" + destination.getAbsolutePath() + "'." );
+ "Could not create destination directory '" + destination.getAbsolutePath() + "'.");
}
- copyDirectoryStructure( file, destination, rootDestinationDirectory, onlyModifiedFiles );
- }
- else
- {
- throw new IOException( "Unknown file type: " + file.getAbsolutePath() );
+ copyDirectoryStructure(file, destination, rootDestinationDirectory, onlyModifiedFiles);
+ } else {
+ throw new IOException("Unknown file type: " + file.getAbsolutePath());
}
}
}
@@ -2252,7 +1641,7 @@ public class FileUtils
*/
public static File createTempFile( String prefix, String suffix, File parentDir )
{
- File result = null;
+ File result;
String parent = System.getProperty( "java.io.tmpdir" );
if ( parentDir != null )
{
@@ -2333,10 +1722,8 @@ public class FileUtils
}
Reader reader = fileReader;
- for ( int i = 0; i < wrappers.length; i++ )
- {
- FilterWrapper wrapper = wrappers[i];
- reader = wrapper.getReader( reader );
+ for (FilterWrapper wrapper : wrappers) {
+ reader = wrapper.getReader(reader);
}
IOUtil.copy( reader, fileWriter );
@@ -2401,7 +1788,7 @@ public class FileUtils
* @see #INVALID_CHARACTERS_FOR_WINDOWS_FILE_NAME
* @since 1.5.2
*/
- public static boolean isValidWindowsFileName( File f )
+ private static boolean isValidWindowsFileName( File f )
{
if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
{
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/IOUtil.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/IOUtil.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/IOUtil.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/IOUtil.java Tue Oct 9 17:45:37 2012
@@ -630,18 +630,7 @@ public final class IOUtil
/**
* Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
*/
- public static void copy( final byte[] input, final OutputStream output )
- throws IOException
- {
- copy( input, output, DEFAULT_BUFFER_SIZE );
- }
-
- /**
- * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
- *
- * @param bufferSize Size of internal buffer to use.
- */
- public static void copy( final byte[] input, final OutputStream output, final int bufferSize )
+ public static void copy(final byte[] input, final OutputStream output)
throws IOException
{
output.write( input );
@@ -672,14 +661,7 @@ public final class IOUtil
}
final int ch2 = bufferedInput2.read();
- if ( -1 != ch2 )
- {
- return false;
- }
- else
- {
- return true;
- }
+ return -1 == ch2;
}
// ----------------------------------------------------------------------
@@ -777,7 +759,7 @@ public final class IOUtil
/**
* Closes the writer. The writer can be null and any IOException's will be swallowed.
*
- * @param wrtier The writer to close.
+ * @param writer The writer to close.
*/
public static void close( Writer writer )
{
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/MatchPatterns.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/MatchPatterns.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/MatchPatterns.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/MatchPatterns.java Tue Oct 9 17:45:37 2012
@@ -19,8 +19,6 @@ package org.apache.maven.shared.utils.io
*/
import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
/**
* A list of patterns to be matched
@@ -81,19 +79,4 @@ public class MatchPatterns
return new MatchPatterns( result );
}
- public static MatchPatterns from( Iterable<String> strings )
- {
- return new MatchPatterns( getMatchPatterns( strings ) );
- }
-
- private static MatchPattern[] getMatchPatterns( Iterable<String> items )
- {
- List<MatchPattern> result = new ArrayList<MatchPattern>();
- for ( String string : items )
- {
- result.add( MatchPattern.fromString( string ) );
- }
- return result.toArray( new MatchPattern[result.size()] );
- }
-
}
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/SelectorUtils.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/SelectorUtils.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/SelectorUtils.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/SelectorUtils.java Tue Oct 9 17:45:37 2012
@@ -24,7 +24,6 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
-import java.util.Vector;
/**
* <p>This is a utility class used by selectors and DirectoryScanner. The
@@ -43,7 +42,7 @@ import java.util.Vector;
public final class SelectorUtils
{
- public static final String PATTERN_HANDLER_PREFIX = "[";
+ private static final String PATTERN_HANDLER_PREFIX = "[";
public static final String PATTERN_HANDLER_SUFFIX = "]";
@@ -51,8 +50,6 @@ public final class SelectorUtils
public static final String ANT_HANDLER_PREFIX = "%ant" + PATTERN_HANDLER_PREFIX;
- private static SelectorUtils instance = new SelectorUtils();
-
/**
* Private Constructor
*/
@@ -61,14 +58,6 @@ public final class SelectorUtils
}
/**
- * Retrieves the manager of the Singleton.
- */
- public static SelectorUtils getInstance()
- {
- return instance;
- }
-
- /**
* Tests whether or not a given path matches the start of a given
* pattern up to the first "**".
* <p/>
@@ -105,7 +94,7 @@ public final class SelectorUtils
* @return whether or not a given path matches the start of a given
* pattern up to the first "**".
*/
- public static boolean matchPatternStart( String pattern, String str, boolean isCaseSensitive )
+ private static boolean matchPatternStart( String pattern, String str, boolean isCaseSensitive )
{
if ( isRegexPrefixedPattern( pattern ) )
{
@@ -164,22 +153,7 @@ public final class SelectorUtils
strIdxStart++;
}
- if ( strIdxStart > strIdxEnd )
- {
- // String is exhausted
- return true;
- }
- else if ( patIdxStart > patIdxEnd )
- {
- // String not exhausted, but pattern is. Failure.
- return false;
- }
- else
- {
- // pattern now holds ** while string is not exhausted
- // this will generate false positives but we can live with that.
- return true;
- }
+ return strIdxStart > strIdxEnd || patIdxStart <= patIdxEnd;
}
/**
@@ -261,8 +235,6 @@ public final class SelectorUtils
}
if ( !match( patDir, strDirs.get( strIdxStart ), isCaseSensitive ) )
{
- patDirs = null;
- strDirs = null;
return false;
}
patIdxStart++;
@@ -275,8 +247,6 @@ public final class SelectorUtils
{
if ( !"**".equals( patDirs.get( i ) ) )
{
- patDirs = null;
- strDirs = null;
return false;
}
}
@@ -287,8 +257,6 @@ public final class SelectorUtils
if ( patIdxStart > patIdxEnd )
{
// String not exhausted, but pattern is. Failure.
- patDirs = null;
- strDirs = null;
return false;
}
}
@@ -303,8 +271,6 @@ public final class SelectorUtils
}
if ( !match( patDir, strDirs.get( strIdxEnd ), isCaseSensitive ) )
{
- patDirs = null;
- strDirs = null;
return false;
}
patIdxEnd--;
@@ -317,8 +283,6 @@ public final class SelectorUtils
{
if ( !"**".equals( patDirs.get( i ) ) )
{
- patDirs = null;
- strDirs = null;
return false;
}
}
@@ -366,8 +330,6 @@ public final class SelectorUtils
if ( foundIdx == -1 )
{
- patDirs = null;
- strDirs = null;
return false;
}
@@ -379,8 +341,6 @@ public final class SelectorUtils
{
if ( !"**".equals( patDirs.get( i ) ) )
{
- patDirs = null;
- strDirs = null;
return false;
}
}
@@ -421,7 +381,7 @@ public final class SelectorUtils
* @return <code>true</code> if the string matches against the pattern,
* or <code>false</code> otherwise.
*/
- public static boolean match( String pattern, String str, boolean isCaseSensitive )
+ private static boolean match( String pattern, String str, boolean isCaseSensitive )
{
char[] patArr = pattern.toCharArray();
char[] strArr = str.toCharArray();
@@ -432,10 +392,8 @@ public final class SelectorUtils
char ch;
boolean containsStar = false;
- for ( int i = 0; i < patArr.length; i++ )
- {
- if ( patArr[i] == '*' )
- {
+ for (char aPatArr : patArr) {
+ if (aPatArr == '*') {
containsStar = true;
break;
}
@@ -595,82 +553,26 @@ public final class SelectorUtils
}
/**
- * Breaks a path up into a Vector of path elements, tokenizing on
+ * Breaks a path up into a List of path elements, tokenizing on
* <code>File.separator</code>.
*
* @param path Path to tokenize. Must not be <code>null</code>.
+ * @param separator The separator to use
* @return a Vector of path elements from the tokenized path
*/
- public static Vector tokenizePath( String path )
- {
- return tokenizePath( path, File.separator );
- }
-
- public static Vector tokenizePath( String path, String separator )
+ private static List<String> tokenizePath( String path, String separator )
{
- Vector ret = new Vector();
+ List<String> ret = new ArrayList<String>();
StringTokenizer st = new StringTokenizer( path, separator );
while ( st.hasMoreTokens() )
{
- ret.addElement( st.nextToken() );
+ ret.add(st.nextToken());
}
return ret;
}
- /**
- * Returns dependency information on these two files. If src has been
- * modified later than target, it returns true. If target doesn't exist,
- * it likewise returns true. Otherwise, target is newer than src and
- * is not out of date, thus the method returns false. It also returns
- * false if the src file doesn't even exist, since how could the
- * target then be out of date.
- *
- * @param src the original file
- * @param target the file being compared against
- * @param granularity the amount in milliseconds of slack we will give in
- * determining out of dateness
- * @return whether the target is out of date
- */
- public static boolean isOutOfDate( File src, File target, int granularity )
- {
- if ( !src.exists() )
- {
- return false;
- }
- if ( !target.exists() )
- {
- return true;
- }
- if ( ( src.lastModified() - granularity ) > target.lastModified() )
- {
- return true;
- }
- return false;
- }
-
- /**
- * "Flattens" a string by removing all whitespace (space, tab, linefeed,
- * carriage return, and formfeed). This uses StringTokenizer and the
- * default set of tokens as documented in the single arguement constructor.
- *
- * @param input a String to remove all whitespace.
- * @return a String that has had all whitespace removed.
- */
- public static String removeWhitespace( String input )
- {
- StringBuffer result = new StringBuffer();
- if ( input != null )
- {
- StringTokenizer st = new StringTokenizer( input );
- while ( st.hasMoreTokens() )
- {
- result.append( st.nextToken() );
- }
- }
- return result.toString();
- }
-
+ @SuppressWarnings("SimplifiableIfStatement")
static boolean matchAntPathPatternStart( MatchPattern pattern, String str, String separator,
boolean isCaseSensitive )
{
@@ -693,7 +595,7 @@ public final class SelectorUtils
return ret.toArray( new String[ret.size()] );
}
- static boolean matchAntPathPatternStart( String[] patDirs, String str, String separator, boolean isCaseSensitive )
+ private static boolean matchAntPathPatternStart( String[] patDirs, String str, String separator, boolean isCaseSensitive )
{
String[] strDirs = tokenizePathToString( str, separator );
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/WalkCollector.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/WalkCollector.java?rev=1396135&r1=1396134&r2=1396135&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/WalkCollector.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/io/WalkCollector.java Tue Oct 9 17:45:37 2012
@@ -7,21 +7,21 @@ import java.util.List;
public class WalkCollector
implements DirectoryWalkListener
{
- protected List steps;
+ final List<File> steps;
- protected File startingDir;
+ File startingDir;
- protected int startCount;
+ int startCount;
- protected int finishCount;
+ int finishCount;
- protected int percentageLow;
+ int percentageLow;
- protected int percentageHigh;
+ int percentageHigh;
public WalkCollector()
{
- steps = new ArrayList();
+ steps = new ArrayList<File>();
startCount = 0;
finishCount = 0;
percentageLow = 0;
@@ -50,35 +50,4 @@ public class WalkCollector
{
finishCount++;
}
-
-
- public int getFinishCount()
- {
- return finishCount;
- }
-
- public int getPercentageHigh()
- {
- return percentageHigh;
- }
-
- public int getPercentageLow()
- {
- return percentageLow;
- }
-
- public int getStartCount()
- {
- return startCount;
- }
-
- public File getStartingDir()
- {
- return startingDir;
- }
-
- public List getSteps()
- {
- return steps;
- }
}