Skip to content

Commit 8eca0b1

Browse files
committed
Jakarta FileFilters.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140315 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8eeb52f commit 8eca0b1

10 files changed

Lines changed: 714 additions & 0 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
import java.io.FilenameFilter;
4+
import java.io.File;
5+
6+
/**
7+
* An abstract class which brings the FileFilter and FilenameFilter
8+
* interfaces together, through the IO.FileFilter interface. Note that
9+
* you <b>must</b> override one of the methods ellse your class will
10+
* infinitely loop.
11+
*/
12+
public abstract class AbstractFileFilter
13+
implements FileFilter
14+
{
15+
16+
/** Defined in FileFilter */
17+
public boolean accept( File f) {
18+
return accept( f.getParentFile(), f.getName());
19+
}
20+
21+
/** Defined in FilenameFilter */
22+
public boolean accept( File dir, String name) {
23+
return accept( new File( dir.getName() + name ) );
24+
}
25+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
57+
import java.io.File;
58+
59+
/**
60+
* Accepts a selection if it is acceptable to both of two {@link FilenameFilter}s.
61+
* This takes two {@link FilenameFilter}s as input.
62+
*
63+
* <p>Eg., to print all files beginning with <code>A</code> and ending with <code>.java</code>:</p>
64+
*
65+
* <pre>
66+
* File dir = new File(".");
67+
* String[] files = dir.list( new AndFileFilter(
68+
* new PrefixFileFilter("A"),
69+
* new ExtensionFileFilter(".java")
70+
* )
71+
* );
72+
* for ( int i=0; i&lt;files.length; i++ )
73+
* {
74+
* System.out.println(files[i]);
75+
* }
76+
* </pre>
77+
*
78+
* @author Harmeet Bedi <harmeet@kodemuse.com>
79+
* @version $Revision: 1.1 $ $Date: 2002/07/28 03:10:01 $
80+
* @since 4.0
81+
*/
82+
public class AndFileFilter
83+
extends AbstractFileFilter
84+
{
85+
private final FileFilter m_filter1;
86+
private final FileFilter m_filter2;
87+
88+
public AndFileFilter( final FileFilter filter1, final FileFilter filter2 )
89+
{
90+
m_filter1 = filter1;
91+
m_filter2 = filter2;
92+
}
93+
94+
public boolean accept( final File file, final String name )
95+
{
96+
return m_filter1.accept( file, name ) && m_filter2.accept( file, name );
97+
}
98+
}
99+
100+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
import java.io.FilenameFilter;
4+
import java.io.FileFilter;
5+
import java.io.File;
6+
7+
/**
8+
*/
9+
public class DelegateFileFilter
10+
extends AbstractFileFilter
11+
{
12+
13+
private FilenameFilter filename;
14+
private java.io.FileFilter file;
15+
16+
public DelegateFileFilter(FilenameFilter filter) {
17+
if(filter == null) {
18+
throw new NullPointerException("Setting a filter to null will "+
19+
"infinitely loop. Use a NullFileFilter instead. ");
20+
}
21+
this.filename = filter;
22+
}
23+
24+
public DelegateFileFilter(java.io.FileFilter filter) {
25+
if(filter == null) {
26+
throw new NullPointerException("Setting a filter to null will "+
27+
"infinitely loop. Use a NullFileFilter instead. ");
28+
}
29+
this.file = filter;
30+
}
31+
32+
public boolean accept( File f) {
33+
if(file != null) {
34+
return file.accept(f);
35+
} else {
36+
return super.accept(f);
37+
}
38+
}
39+
40+
public boolean accept( File dir, String name) {
41+
if(filename != null) {
42+
return filename.accept(dir, name);
43+
} else {
44+
return super.accept(dir, name);
45+
}
46+
}
47+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
import java.io.File;
57+
58+
/**
59+
* This filter accepts <code>File</code>s that are directories.
60+
* <p>Eg., here is how to print out a list of the current directory's subdirectories:</p>
61+
*
62+
* <pre>
63+
* File dir = new File(".");
64+
* String[] files = dir.list( new DirectoryFileFilter() );
65+
* for ( int i=0; i&lt;files.length; i++ )
66+
* {
67+
* System.out.println(files[i]);
68+
* }
69+
* </pre>
70+
*
71+
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
72+
* @version $Revision: 1.1 $ $Date: 2002/07/28 03:10:01 $
73+
* @since 4.0
74+
*/
75+
public class DirectoryFileFilter
76+
extends AbstractFileFilter
77+
{
78+
public boolean accept( final File file )
79+
{
80+
return file.isDirectory();
81+
}
82+
}
83+
84+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
57+
import java.io.File;
58+
59+
/**
60+
* This filters files based on the extension (what the filename
61+
* ends with). This is used in retrieving all the files of a
62+
* particular type.
63+
*
64+
* <p>Eg., to retrieve and print all <code>*.java</code> files in the current directory:</p>
65+
*
66+
* <pre>
67+
* File dir = new File(".");
68+
* String[] files = dir.list( new ExtensionFileFilter( new String[]{"java"} ) );
69+
* for (int i=0; i&lt;files.length; i++)
70+
* {
71+
* System.out.println(files[i]);
72+
* }
73+
* </pre>
74+
*
75+
* @author Federico Barbieri <fede@apache.org>
76+
* @author Serge Knystautas <sergek@lokitech.com>
77+
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
78+
* @version CVS $Revision: 1.1 $ $Date: 2002/07/28 03:10:01 $
79+
* @since 4.0
80+
*/
81+
public class ExtensionFileFilter
82+
extends AbstractFileFilter
83+
{
84+
private String[] m_extensions;
85+
86+
public ExtensionFileFilter( final String[] extensions )
87+
{
88+
m_extensions = extensions;
89+
}
90+
91+
public ExtensionFileFilter( final String extension )
92+
{
93+
m_extensions = new String[]{extension};
94+
}
95+
96+
public boolean accept( final File file, final String name )
97+
{
98+
for( int i = 0; i < m_extensions.length; i++ )
99+
{
100+
if( name.endsWith( m_extensions[ i ] ) )
101+
{
102+
return true;
103+
}
104+
}
105+
return false;
106+
}
107+
}
108+
109+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.apache.commons.io.filefilter;
2+
3+
import java.io.FilenameFilter;
4+
import java.io.File;
5+
6+
/**
7+
* An interface which brings the FileFilter and FilenameFilter
8+
* interfaces together.
9+
*/
10+
public interface FileFilter
11+
extends java.io.FileFilter, FilenameFilter
12+
{
13+
14+
/** Defined in java.io.FileFilter */
15+
public boolean accept( File f);
16+
17+
/** Defined in FilenameFilter */
18+
public boolean accept( File dir, String name);
19+
}

0 commit comments

Comments
 (0)