KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > util > io > v1 > ExtensionFilenameFilter


1 /*
2  * ExtensionFileFilter.java - 0.9.0 01/07/2001 - 12:35:59
3  *
4  * Copyright (C) 2001,,2003 2002 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26  
27  
28 package net.sourceforge.groboutils.util.io.v1;
29  
30 import java.io.IOException JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.FilenameFilter JavaDoc;
33
34
35
36 /**
37  * Allows files with the given extention(s) to be accepted. You can also
38  * specify whether directories are allowed or not. This filter is
39  * case insensitive or sensitive, depending on the settings. By default,
40  * the class is case-insensitive.
41  * <P>
42  * The extension strings passed in are the end-of-name Strings, meaning
43  * that each file must match at least one given string at the end. So,
44  * if you want to match all "DOC" files, pass in ".doc" to match.
45  * <P>
46  * By default, directories are allowed, independent of their names. If
47  * directories are not allowed, then the directory names must match the
48  * extension list.
49  *
50  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
51  * @since January 7, 2001
52  * @version $Date: 2003/02/10 22:52:45 $
53  */

54 public class ExtensionFilenameFilter implements FilenameFilter JavaDoc
55 {
56     //--------------------------------------------------------------
57
// Private fields
58

59     // this list of extensions *must* be all lower-case.
60
private String JavaDoc extensions[] = new String JavaDoc[0];
61     private boolean allowDirs = true;
62     private boolean caseInsensitive = true;
63     
64     
65     //--------------------------------------------------------------
66
// Constructors
67

68     /**
69      * Default Constructor.
70      */

71     public ExtensionFilenameFilter()
72     {
73         // do nothing
74
}
75     
76     /**
77      * Specify a single "end string" to match.
78      */

79     public ExtensionFilenameFilter( String JavaDoc extension )
80     {
81         addExtension( extension );
82     }
83     
84     /**
85      * <P>
86      * Slow, but it works.
87      */

88     public ExtensionFilenameFilter( String JavaDoc exts[] )
89     {
90         if (exts == null)
91         {
92             throw new IllegalArgumentException JavaDoc( "no null args" );
93         }
94         for (int i = exts.length; --i >= 0;)
95         {
96             addExtension( exts[i] );
97         }
98     }
99     
100     
101     /**
102      *
103      */

104     public ExtensionFilenameFilter( boolean caseInsensitive )
105     {
106         this.caseInsensitive = caseInsensitive;
107     }
108      
109     
110     /**
111      *
112      */

113     public ExtensionFilenameFilter( String JavaDoc extension, boolean caseInsensitive )
114     {
115         // must set the case first
116
this.caseInsensitive = caseInsensitive;
117         addExtension( extension );
118     }
119
120     
121     /**
122      *
123      */

124     public ExtensionFilenameFilter( String JavaDoc exts[], boolean caseInsensitive )
125     {
126         if (exts == null)
127         {
128             throw new IllegalArgumentException JavaDoc( "no null args" );
129         }
130
131         // must set the case first
132
this.caseInsensitive = caseInsensitive;
133         for (int i = exts.length; --i >= 0;)
134         {
135             addExtension( exts[i] );
136         }
137     }
138
139     
140     //--------------------------------------------------------------
141
// Public methods
142

143     
144     /**
145      * Adds the given extension to the internal list.
146      */

147     public void addExtension( String JavaDoc ext )
148     {
149         if (ext == null)
150         {
151             throw new IllegalArgumentException JavaDoc( "no null args" );
152         }
153         int len = this.extensions.length;
154         String JavaDoc temp[] = new String JavaDoc[ len+1 ];
155         System.arraycopy( this.extensions, 0, temp, 0, len );
156         if (isCaseInsensitive())
157         {
158             ext = ext.toLowerCase();
159         }
160         temp[ len ] = ext;
161         this.extensions = temp;
162     }
163     
164     
165     /**
166      * @return whether the filter allows directories to pass.
167      */

168     public boolean allowsDirectories()
169     {
170         return this.allowDirs;
171     }
172     
173     
174     /**
175      * @param on <tt>true</tt> if filter allows directories to pass.
176      */

177     public void allowsDirectories( boolean on )
178     {
179         this.allowDirs = on;
180     }
181     
182     
183     /**
184      * @return if the extension is case insensitive.
185      */

186     public boolean isCaseInsensitive()
187     {
188         return this.caseInsensitive;
189     }
190     
191     
192     /**
193      * Accepts some files.
194      */

195     public boolean accept( File JavaDoc dir, String JavaDoc name )
196     {
197         if (name == null)
198         {
199             throw new IllegalArgumentException JavaDoc( "no null args" );
200         }
201         return matches( name, new File JavaDoc( dir, name ) );
202     }
203     
204
205     
206     //--------------------------------------------------------------
207
// Protected methods
208

209     
210     /**
211      * Tests if the given file matches the list of extensions.
212      *
213      * @param filename the name of the file.
214      * @param file the File object version of the file.
215      */

216     protected boolean matches( String JavaDoc filename, File JavaDoc file )
217     {
218         if (filename == null || file == null)
219         {
220             throw new IllegalArgumentException JavaDoc( "no null args" );
221         }
222         if (allowsDirectories() && file.isDirectory())
223         {
224             return true;
225         }
226         if (isCaseInsensitive())
227         {
228             filename = filename.toLowerCase();
229         }
230         
231         for (int i = this.extensions.length; --i >= 0;)
232         {
233             if (filename.endsWith( this.extensions[i] ))
234             {
235                 return true;
236             }
237         }
238         return false;
239     }
240     
241 }
242
Popular Tags