Archive for the 'General Java' Category

Dos style file filter

Posted in General Java on May 7th, 2008 by matt

Can’t remember which forum I found this on, but I’m pretty sure it wasn’t licensed, if I am wrong then please contact me.

The following class can be used with ListFiles to filter the files returned using dos style * and ? wildcards.

        public class RegexFilenameFilter implements FilenameFilter {
                String _regex = null; // setting the filter regex to null causes any

                // name to be accepted (same as ".*")
                public RegexFilenameFilter() {
                }

                /**
                 * Set the filter from a wildcard expression as known from the windows
                 * command line ("?" = "any character", "*" = zero or more occurances of
                 * any character")
                 *
                 * @param sWild
                 *            the wildcard pattern
                 *
                 * @return this
                 */

                public RegexFilenameFilter setWildcard(String sWild) {
                        _regex = wildcardToRegex(sWild);
                        Pattern.compile(_regex);
                        return this;
                }

                /**
                 * Set the regular expression of the filter
                 *
                 * @param regex
                 *            the regular expression of the filter
                 *
                 * @return this
                 */

                public RegexFilenameFilter setRegex(String regex)
                                throws java.util.regex.PatternSyntaxException {
                        _regex = regex.toLowerCase();
                        Pattern.compile(_regex); // throw PatternSyntaxException if the
                        // pattern is not valid
                        return this;
                }

                /**
                 * Tests if a specified file should be included in a file list.
                 *
                 * @param dir
                 *            the directory in which the file was found.
                 *
                 * @param name
                 *            the name of the file.
                 *
                 * @return true if and only if the name should be included in the file
                 *         list; false otherwise.
                 */

                public boolean accept(File dir, String name) {
                        boolean bAccept;

                        if (_regex == null)
                                bAccept = true;
                        else
                                bAccept = name.toLowerCase().matches(_regex);

                        return bAccept;
                }

                /**
                 * Converts a windows wildcard pattern to a regex pattern
                 *
                 * @param wild -
                 *            Wildcard patter containing * and ?
                 *
                 * @return - a regex pattern that is equivalent to the windows wildcard
                 *         pattern
                 */

                private String wildcardToRegex(String wild) {
                        if (wild == null)
                                return null;

                        StringBuffer buffer = new StringBuffer();

                        char[] chars = wild.toCharArray();

                        for (int i = 0; i < chars.length; ++i) {
                                if (chars[i] == ‘*’)
                                        buffer.append(".*");
                                else if (chars[i] == ‘?’)
                                        buffer.append(".");
                                else if ("+()^$.{}[]|".indexOf(chars[i]) != -1)
                                        buffer.append(\\).append(chars[i]);
                                else
                                        buffer.append(chars[i]);
                        }

                        return buffer.toString().toLowerCase();
                }
        }
 

create an isntance, call setWidcard, then pass to listFiles.