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.commons.io.res.filter;
020
021
022import lucee.commons.io.res.Resource;
023import lucee.commons.lang.StringUtil;
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 ExtensionResourceFilter implements ResourceFilter {
030        
031        private String[] extensions;
032        private final boolean allowDir;
033        private final boolean ignoreCase;
034    //private int extLen;
035
036        public static final ExtensionResourceFilter EXTENSION_JAR_NO_DIR = new ExtensionResourceFilter(".jar", false);
037        public static final ExtensionResourceFilter EXTENSION_CLASS_DIR  = new ExtensionResourceFilter(".class", true);
038
039
040        /**
041         * Konstruktor des Filters
042         * @param extension Endung die geprueft werden soll.
043         */
044        public ExtensionResourceFilter(String extension) {
045                this(new String[]{extension},false,true);
046        }
047
048        /**
049         * Konstruktor des Filters
050         * @param extension Endung die geprueft werden soll.
051         */
052        public ExtensionResourceFilter(String extension, boolean allowDir) {
053                this(new String[]{extension},allowDir,true);
054        }
055        
056        public ExtensionResourceFilter(String[] extensions) {
057                this(extensions,false,true);
058        }
059        
060        public ExtensionResourceFilter(String[] extensions, boolean allowDir) {
061                this(extensions,allowDir,true);
062        }
063
064        
065        public ExtensionResourceFilter(String[] extensions, boolean allowDir, boolean ignoreCase) {
066                String[] tmp=new String[extensions.length];
067                for(int i=0;i<extensions.length;i++) {
068                        if(!StringUtil.startsWith(extensions[i],'.'))
069                    tmp[i]="."+extensions[i];
070                        else 
071                                tmp[i]=extensions[i];
072                }
073                this.extensions=tmp;
074        this.allowDir=allowDir;
075        this.ignoreCase=ignoreCase;
076        }
077        
078        public void addExtension(String extension) {
079                String[] tmp=new String[extensions.length+1];
080                // add existing
081                for(int i=0;i<extensions.length;i++) {
082                        tmp[i]=extensions[i];
083                }
084                // add the new one
085                if(!StringUtil.startsWith(extension,'.'))
086            tmp[extensions.length]="."+extension;
087                else 
088                        tmp[extensions.length]=extension;
089                
090                this.extensions=tmp;
091        }
092
093        @Override
094        public boolean accept(Resource res) {
095                if(res.isDirectory()) return allowDir;
096                if(res.exists()) {
097                        String name=res.getName();
098                        for(int i=0;i<extensions.length;i++) {
099                                if(ignoreCase){
100                                        if(StringUtil.endsWithIgnoreCase(name,extensions[i]))
101                                                return true;
102                                }
103                                else {
104                                        if(name.endsWith(extensions[i]))
105                                                return true;
106                                }
107                        }
108                }
109                return false;
110        }
111        
112        public boolean accept(String name) {
113                        for(int i=0;i<extensions.length;i++) {
114                                if(ignoreCase){
115                                        if(StringUtil.endsWithIgnoreCase(name,extensions[i]))
116                                                return true;
117                                }
118                                else {
119                                        if(name.endsWith(extensions[i]))
120                                                return true;
121                                }
122                        }
123                
124                return false;
125        }
126        
127    /**
128     * @return Returns the extension.
129     */
130    public String[] getExtensions() {
131        return extensions;
132    }
133}