001: /*
002: * ExtensionFileFilter.java - 0.9.0 01/07/2001 - 12:35:59
003: *
004: * Copyright (C) 2001,,2003 2002 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.io.v1;
028:
029: import java.io.IOException;
030: import java.io.File;
031: import java.io.FilenameFilter;
032:
033: /**
034: * Allows files with the given extention(s) to be accepted. You can also
035: * specify whether directories are allowed or not. This filter is
036: * case insensitive or sensitive, depending on the settings. By default,
037: * the class is case-insensitive.
038: * <P>
039: * The extension strings passed in are the end-of-name Strings, meaning
040: * that each file must match at least one given string at the end. So,
041: * if you want to match all "DOC" files, pass in ".doc" to match.
042: * <P>
043: * By default, directories are allowed, independent of their names. If
044: * directories are not allowed, then the directory names must match the
045: * extension list.
046: *
047: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
048: * @since January 7, 2001
049: * @version $Date: 2003/02/10 22:52:45 $
050: */
051: public class ExtensionFilenameFilter implements FilenameFilter {
052: //--------------------------------------------------------------
053: // Private fields
054:
055: // this list of extensions *must* be all lower-case.
056: private String extensions[] = new String[0];
057: private boolean allowDirs = true;
058: private boolean caseInsensitive = true;
059:
060: //--------------------------------------------------------------
061: // Constructors
062:
063: /**
064: * Default Constructor.
065: */
066: public ExtensionFilenameFilter() {
067: // do nothing
068: }
069:
070: /**
071: * Specify a single "end string" to match.
072: */
073: public ExtensionFilenameFilter(String extension) {
074: addExtension(extension);
075: }
076:
077: /**
078: * <P>
079: * Slow, but it works.
080: */
081: public ExtensionFilenameFilter(String exts[]) {
082: if (exts == null) {
083: throw new IllegalArgumentException("no null args");
084: }
085: for (int i = exts.length; --i >= 0;) {
086: addExtension(exts[i]);
087: }
088: }
089:
090: /**
091: *
092: */
093: public ExtensionFilenameFilter(boolean caseInsensitive) {
094: this .caseInsensitive = caseInsensitive;
095: }
096:
097: /**
098: *
099: */
100: public ExtensionFilenameFilter(String extension,
101: boolean caseInsensitive) {
102: // must set the case first
103: this .caseInsensitive = caseInsensitive;
104: addExtension(extension);
105: }
106:
107: /**
108: *
109: */
110: public ExtensionFilenameFilter(String exts[],
111: boolean caseInsensitive) {
112: if (exts == null) {
113: throw new IllegalArgumentException("no null args");
114: }
115:
116: // must set the case first
117: this .caseInsensitive = caseInsensitive;
118: for (int i = exts.length; --i >= 0;) {
119: addExtension(exts[i]);
120: }
121: }
122:
123: //--------------------------------------------------------------
124: // Public methods
125:
126: /**
127: * Adds the given extension to the internal list.
128: */
129: public void addExtension(String ext) {
130: if (ext == null) {
131: throw new IllegalArgumentException("no null args");
132: }
133: int len = this .extensions.length;
134: String temp[] = new String[len + 1];
135: System.arraycopy(this .extensions, 0, temp, 0, len);
136: if (isCaseInsensitive()) {
137: ext = ext.toLowerCase();
138: }
139: temp[len] = ext;
140: this .extensions = temp;
141: }
142:
143: /**
144: * @return whether the filter allows directories to pass.
145: */
146: public boolean allowsDirectories() {
147: return this .allowDirs;
148: }
149:
150: /**
151: * @param on <tt>true</tt> if filter allows directories to pass.
152: */
153: public void allowsDirectories(boolean on) {
154: this .allowDirs = on;
155: }
156:
157: /**
158: * @return if the extension is case insensitive.
159: */
160: public boolean isCaseInsensitive() {
161: return this .caseInsensitive;
162: }
163:
164: /**
165: * Accepts some files.
166: */
167: public boolean accept(File dir, String name) {
168: if (name == null) {
169: throw new IllegalArgumentException("no null args");
170: }
171: return matches(name, new File(dir, name));
172: }
173:
174: //--------------------------------------------------------------
175: // Protected methods
176:
177: /**
178: * Tests if the given file matches the list of extensions.
179: *
180: * @param filename the name of the file.
181: * @param file the File object version of the file.
182: */
183: protected boolean matches(String filename, File file) {
184: if (filename == null || file == null) {
185: throw new IllegalArgumentException("no null args");
186: }
187: if (allowsDirectories() && file.isDirectory()) {
188: return true;
189: }
190: if (isCaseInsensitive()) {
191: filename = filename.toLowerCase();
192: }
193:
194: for (int i = this .extensions.length; --i >= 0;) {
195: if (filename.endsWith(this .extensions[i])) {
196: return true;
197: }
198: }
199: return false;
200: }
201:
202: }
|