001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/framework/util/ConvenienceFileFilter.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.framework.util;
037    
038    import java.io.File;
039    import java.io.FilenameFilter;
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    /**
044     * Class to be used with
045     *
046     * @see java.io.File for reading all file/directory names starting at a given root directory
047     *
048     * @version $Revision: 18195 $
049     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
050     * @author last edited by: $Author: mschneider $
051     *
052     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
053     *
054     * @since 2.0
055     */
056    public class ConvenienceFileFilter implements FilenameFilter {
057    
058        private List<String> extensions = null;
059    
060        private boolean returnDirsAsTrue = false;
061    
062        /**
063         *
064         * @param extensions
065         *            list of considered file extensions (e.g. tif, BMP, Gif ..) The is is not case
066         *            sensitive; use '*' for returning all files
067         * @param returnDirsAsTrue
068         *            if set too true the method also return directory names
069         * @see #accept(java.io.File, String) will return 'true' if the passed
070         * @see File is a directory
071         */
072        public ConvenienceFileFilter( List<String> extensions, boolean returnDirsAsTrue ) {
073            this.extensions = new ArrayList<String>();
074            for ( int i = 0; i < extensions.size(); i++ ) {
075                this.extensions.add( extensions.get( i ).toUpperCase() );
076            }
077            this.returnDirsAsTrue = returnDirsAsTrue;
078        }
079    
080        /**
081         *
082         * @param returnDirsAsTrue
083         *            if set too true the method also return directory names
084         * @param extensions
085         *            list of considered file extensions (e.g. tif, BMP, Gif ..) The is is not case
086         *            sensitive; use '*' for returning all files
087         */
088        public ConvenienceFileFilter( boolean returnDirsAsTrue, String... extensions ) {
089            this.extensions = new ArrayList<String>( extensions.length );
090            for ( int i = 0; i < extensions.length; i++ ) {
091                this.extensions.add( extensions[i].toUpperCase() );
092            }
093            this.returnDirsAsTrue = returnDirsAsTrue;
094        }
095    
096        /**
097         * @return *.* (all files)
098         */
099        public String getDescription() {
100            return "*.*";
101        }
102    
103        /*
104         * (non-Javadoc)
105         *
106         * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
107         */
108        public boolean accept( java.io.File file, String name ) {
109            int pos = name.lastIndexOf( "." );
110            String ext = name.substring( pos + 1 ).toUpperCase();
111            String s = file.getAbsolutePath() + '/' + name;
112            File tmp = new File( s );
113            if ( file.isDirectory() ) {
114                if ( tmp.isDirectory() && returnDirsAsTrue ) {
115                    return true;
116                }
117            }
118            return ( extensions.contains( ext ) || ( extensions.contains( "*" ) ) && !tmp.isDirectory() );
119        }
120    
121    }