All computer source code presented on this page, unless it includes attribution to another author, is provided by Ed Halley under the Artistic License. Use such code freely and without any expectation of support. I would like to know if you make anything cool with the code, or need questions answered.
python/
    bindings.py
    boards.py
    buzz.py
    caches.py
    cards.py
    constraints.py
    csql.py
    english.py
    getch.py
    getopts.py
    gizmos.py
    goals.py
    improv.py
    interpolations.py
    namespaces.py
    nihongo.py
    nodes.py
    octalplus.py
    patterns.py
    physics.py
    pids.py
    pieces.py
    quizzes.py
    recipes.py
    relays.py
    romaji.py
    ropen.py
    sheets.py
    stores.py
    strokes.py
    subscriptions.py
    svgbuild.py
    testing.py
    things.py
    timing.py
    ucsv.py
    useful.py
    uuid.py
    vectors.py
    weighted.py
java/
    CSVReader.java
    CSVWriter.java
    GlobFilenameFilter.java
    RegexFilenameFilter.java
    StringBufferOutputStream.java
    ThreadSet.java
    Throttle.java
    TracingThread.java
    Utf8ConsoleTest.java
    droid/
        ArrangeViewsTouchListener.java
        DownloadFileTask.java
perl/
    CVQM.pm
    Kana.pm
    Typo.pm
cxx/
    CCache.h
    equalish.cpp
Download GlobFilenameFilter.java
package cc.halley.java.util;

import java.io.File;
import java.io.FilenameFilter;

/**
 * A filter for filenames, given a shell-style wildcard for matching names.
 * 
 * <p>Typical use is with the <tt>File.list()</tt> method.  Example:
 * 
 * <pre>
 *    // Find all text files in the user's home directory.
 *    //
 *    File path = new File(System.getenv("HOME"));
 *    String names = path.list(new GlobFilenameFilter("*.txt"));
 *    for (int i=0; i < names.length; i++)
 *        System.out.println(names[i]);
 * </pre>
 * 
 * <p>Supported Unix-style glob patterns include:
 * <ul>
 *  <li><tt>*</tt> matches an arbitrary, possibly empty string
 *  <li><tt>?</tt> matches a single arbitrary character
 * </ul>
 * 
 * <p>As with any FilenameFilter, this only matches on the basename portion
 * of a pathname.  Globs with <tt>/</tt> or <tt>\</tt> in it will fail.
 */
public class GlobFilenameFilter
        extends RegexFilenameFilter
{
    /* The regular expressions for turning a glob into a pattern.
     * We assume certain common system limitations on filenames:
     * e.g., characters * ? | < > / \ cannot appear in a filename.
     */
        private static final String[][] REGEXES =
                {   { "([(){}\\[\\].+^$])", "\\\\$1" }, // escape regex chars
                    { "\\*", ".*" }, // replace "*" with ".*"
                        { "\\?", "." }, // replace "?" with "."
                        { "^", "^" }, // prefix
                        { "$", "\\$" }, // postfix
                };

        protected boolean ignorecase = false;
        
    /**
         * Creates a GlobFileFilter.
     * @param globs shell glob patterns to match against filenames
     * @param ignore True to ignore uppercase/lowercase distinctions
         */
    public GlobFilenameFilter(String[] globs, boolean ignore)
    {
        super(createPatternsForGlobs(globs, ignore));
    }

    /**
     * Creates a GlobFileFilter.
     * @param glob a shell glob pattern to match against filenames
     * @param ignore True to ignore uppercase/lowercase distinctions
     */
    public GlobFilenameFilter(String glob, boolean ignore)
    {
        super(createPatternsForGlobs(new String[] { glob }, ignore));
    }

    /**
     * Creates a GlobFileFilter.
     * @param glob shell glob to match against filenames (sensitive to case)
     */
    public GlobFilenameFilter(String glob)
    {
        super(createPatternsForGlobs(new String[] { glob }, false));
    }

    /**
     * Converts shell glob patterns into regular expressions patterns.
     * @param globs shell glob patterns
     * @return regular expression equivalents to the given glob patterns
     */
    public static String[] createPatternsForGlobs(String[] globs,
                                                      boolean ignore)
    {
        String[] patterns = new String[globs.length];
        for (int i = 0; i < patterns.length; i++)
        {
            String pattern = globs[i];
            for (int j = 0; j < REGEXES.length; j++)
            {
                String[] replaceStep = REGEXES[j];
                pattern = pattern.replaceAll(replaceStep[0], replaceStep[1]);
            }
            if (ignore)
                pattern = "(?i:" + pattern + ")";
            patterns[i] = pattern;
        }
        return patterns;
    }
    
    /**
     * Convenience function directly searches for filenames matching a glob.
     * @param path the relative or absolute path to search
     * @param glob the shell expression to be matched against filenames
     * @param ignore true for filenames to be matched ignoring letter case
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String path, String glob, boolean ignore)
    {
        File dir = new File(path);
                FilenameFilter filter = new GlobFilenameFilter(glob, ignore);
                return dir.list(filter);
    }
    
    /**
     * Convenience function directly searches for filenames matching a glob.
     * <p>The directory scanned is the current working path, e.g., <tt>.</tt>
     * @param glob the shell expression to be matched against filenames
     * @param ignore true for filenames to be matched ignoring letter case
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String glob, boolean ignore)
        { return find(".", glob, ignore); }
    
    /**
     * Convenience function directly searches for filenames matching a glob.
     * <p>The directory scanned is the current working path, e.g., <tt>.</tt>
     * Filenames are matched only if the letter cases match exactly.
     * @param glob the shell expression to be matched against filenames
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String glob)
        { return find(".", glob, false); }
    
    /**
     * Convenience function directly searches for filenames matching a glob.
     * <p>Filenames are matched only if the letter cases match exactly.
     * @param path the relative or absolute path to search
     * @param glob the shell expression to be matched against filenames
     * @return an array of filenames found which match, or null for errors
     */
    public static String[] find(String path, String glob)
        { return find(path, glob, false); }
}


Contact Ed Halley by email at ed@halley.cc.
Text, code, layout and artwork are Copyright © 1996-2013 Ed Halley.
Copying in whole or in part, with author attribution, is expressly allowed.
Any references to trademarks are illustrative and are controlled by their respective owners.
Make donations with PayPal - it's fast, free and secure!