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.runtime.search.lucene2;
020
021
022
023import lucee.commons.io.res.Resource;
024import lucee.commons.io.res.filter.ResourceFilter;
025import lucee.commons.io.res.util.ResourceUtil;
026
027/**
028 * FilFilter that only allow filter with given extensions 
029 * by constructor or directory, if constructor variable recurse is true
030 */
031public final class LuceneExtensionFileFilter implements ResourceFilter {
032
033    private String[] extensions;
034    private boolean recurse;
035    private boolean noExtension;
036    private boolean allExtension;
037
038    /**
039     * constructor of the class
040     * @param extensions
041     * @param recurse
042     */
043    public LuceneExtensionFileFilter(String[] extensions, boolean recurse) {
044        
045        this.extensions=extensions;
046        
047        for(int i=0;i<extensions.length;i++) {
048            String ext = extensions[i].trim();
049            
050            if(ext.equals("*."))        {
051                noExtension=true;
052                continue;
053            }
054            if(ext.equals(".*") || ext.equals("*.*"))   {
055                allExtension = true;
056                continue;
057            }
058            
059            // asterix
060            int startIndex=ext.indexOf('*');
061            if(startIndex==0) ext=ext.substring(1);
062            
063            // dot
064            int startDot=ext.indexOf('.');
065            if(startDot==0) ext=ext.substring(1);
066            
067            if(ext.equals("*"))ext="";
068            //print.ln(ext);
069            extensions[i]=ext.toLowerCase();
070        }
071        this.recurse=recurse;
072    }
073
074    @Override
075    public boolean accept(Resource res) {
076        if(res.isDirectory()) return recurse;
077        else if(res.isFile()) {
078            String ext=ResourceUtil.getExtension(res,null);
079            if(ext==null) return noExtension;
080            else if(allExtension) return true;
081                        
082            for(int i=0;i<extensions.length;i++) {
083                if(extensions[i].equalsIgnoreCase(ext)) return true;
084            }
085            return false;
086        }
087        return false;
088    }
089}