//============================================================================== // ForeachFilter.java //============================================================================== package tribble.net.ftp.shell; // System imports import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.lang.Exception; import java.lang.String; // Local imports import tribble.util.FilenamePattern; /******************************************************************************* * FTP command interpreter 'foreach' filename filter. * * * @version $Revision: 1.3 $ $Date: 2007/05/16 23:08:53 $ * @since 2007-04-13 * @author David R. Tribble (david@tribble.com). *
* Copyright ©2007 by David R. Tribble, all rights reserved.
* Permission is granted to any person or entity except those designated by
* by the United States Department of State as a terrorist, or terrorist
* government or agency, to use and distribute this source code provided
* that the original copyright notice remains present and unaltered.
*
* @see Interp
*/
class ForeachFilter
implements java.io.FilenameFilter
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/net/ftp/shell/ForeachFilter.java $Revision: 1.3 $ $Date: 2007/05/16 23:08:53 $\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
/** Filename patterns to match. */
private FilenamePattern[] m_patterns;
/** Maximum filename patterns to accept (0 indicates no maximum). */
private int m_max;
/** Filenames matched. */
private int m_matches;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constructors
/***************************************************************************
* Constructor.
* Filename matching is case-sensitive or insensitive, as appropriate for the
* native operating system.
*
* @param patterns
* Filename patterns to match.
*
* @since 1.1, 2007-04-13
*/
ForeachFilter(String[] patterns)
{
// Initialize
m_patterns = new FilenamePattern[patterns.length];
for (int i = 0; i < patterns.length; i++)
m_patterns[i] = new FilenamePattern(patterns[i]);
}
/***************************************************************************
* Constructor.
*
* @param patterns
* Filename patterns to match.
*
* @param ignoreCase
* If true, filename matching is not case-sensitive (i.e., alphabetic case is
* ignored), otherwise it is (case is not ignored).
*
* @since 1.3, 2007-05-14
*/
ForeachFilter(String[] patterns, boolean ignoreCase)
{
// Initialize
m_patterns = new FilenamePattern[patterns.length];
for (int i = 0; i < patterns.length; i++)
m_patterns[i] = new FilenamePattern(patterns[i], ignoreCase);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Methods
/***************************************************************************
* Set the maximum number of filename matches.
*
* @param max
* Maximum number of filenames to match. Zero (0) indicates that there is no
* maximum.
*
* @since 1.3, 2007-05-14
*/
void setMax(int max)
{
m_max = max;
}
/***************************************************************************
* Determine if a filename matches one of the given patterns.
*
* @since 1.1, 2007-04-13
*/
public boolean accept(File dir, String fname)
//implements java.io.FilenameFilter
{
// Check for max matching limit exceeded
if (m_max > 0 && m_matches >= m_max)
return (false);
// Check the filename against all the search patterns
for (int i = 0; i < m_patterns.length; i++)
{
if (m_patterns[i].matches(fname))
{
m_matches++;
return (true);
}
}
return (false);
}
}
// End ForeachFilter.java