001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.loader.util;
020
021
022import java.io.File;
023import java.io.FileFilter;
024
025/**
026 * Filter fuer die <code>listFiles</code> Methode des FIle Objekt, 
027 * zum filtern von FIles mit einer bestimmten Extension.
028 */
029public final class ExtensionFilter implements FileFilter {
030        
031        private final String[] extensions;
032        private final boolean allowDir;
033        private final boolean ignoreCase;
034    //private int extLen;
035        
036
037        /**
038         * Konstruktor des Filters
039         * @param extension Endung die geprueft werden soll.
040         */
041        public ExtensionFilter(String extension) {
042                this(new String[]{extension},false,true);
043        }
044
045        /**
046         * Konstruktor des Filters
047         * @param extension Endung die geprueft werden soll.
048         */
049        public ExtensionFilter(String extension, boolean allowDir) {
050                this(new String[]{extension},allowDir,true);
051        }
052        
053        public ExtensionFilter(String[] extensions) {
054                this(extensions,false,true);
055        }
056        
057        public ExtensionFilter(String[] extensions, boolean allowDir) {
058                this(extensions,allowDir,true);
059        }
060
061        
062        public ExtensionFilter(String[] extensions, boolean allowDir, boolean ignoreCase) {
063                for(int i=0;i<extensions.length;i++) {
064                        if(!extensions[i].startsWith("."))
065                    extensions[i]="."+extensions[i];
066                        if(ignoreCase)extensions[i]=extensions[i].toLowerCase();
067                }
068                this.extensions=extensions;
069        this.allowDir=allowDir;
070        this.ignoreCase=ignoreCase;
071        }
072
073        /**
074         * @see java.io.FileFilter#accept(java.io.File)
075         */
076        public boolean accept(File res) {
077                if(res.isDirectory()) return allowDir;
078                if(res.exists()) {
079                        String name=ignoreCase?res.getName().toLowerCase():res.getName();
080                        for(int i=0;i<extensions.length;i++) {
081                                if(name.endsWith(extensions[i]))
082                                        return true;
083                        }
084                }
085                return false;
086        }
087        
088    /**
089     * @return Returns the extension.
090     */
091    public String[] getExtensions() {
092        return extensions;
093    }
094}