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.util;
020
021import lucee.commons.io.SystemUtil;
022import lucee.commons.io.res.Resource;
023import lucee.commons.lang.StringUtil;
024
025import org.apache.oro.text.regex.MalformedPatternException;
026import org.apache.oro.text.regex.Pattern;
027import org.apache.oro.text.regex.PatternMatcher;
028import org.apache.oro.text.regex.Perl5Compiler;
029import org.apache.oro.text.regex.Perl5Matcher;
030
031/**
032 * Wildcard Filter
033 */
034public class WildCardFilter implements ResourceAndResourceNameFilter {
035    
036    private static final String specials="{}[]().+\\^$";
037    private static final boolean IS_WIN=SystemUtil.isWindows();
038    
039        private final Pattern pattern;
040    private final PatternMatcher matcher=new Perl5Matcher();
041        private final String wildcard;
042        private boolean ignoreCase;
043
044    public WildCardFilter(String wildcard) throws MalformedPatternException {
045        this(wildcard,IS_WIN);
046    }
047        
048    /**
049     * @param wildcard
050     * @throws MalformedPatternException
051     */
052    public WildCardFilter(String wildcard,boolean ignoreCase) throws MalformedPatternException {
053        this.wildcard=wildcard;
054        StringBuilder sb = new StringBuilder(wildcard.length());
055        int len=wildcard.length();
056        
057        for(int i=0;i<len;i++) {
058            char c = wildcard.charAt(i);
059            if(c == '*')sb.append(".*");
060            else if(c == '?') sb.append('.');
061            else if(specials.indexOf(c)!=-1)sb.append('\\').append(c);
062            else sb.append(c);
063        }
064        
065        this.ignoreCase=ignoreCase;
066        pattern=new Perl5Compiler().compile(ignoreCase?StringUtil.toLowerCase(sb.toString()):sb.toString());
067    }
068
069    @Override
070    public boolean accept(Resource file) {
071        return matcher.matches(ignoreCase?StringUtil.toLowerCase(file.getName()):file.getName(), pattern);
072    }
073
074        public boolean accept(Resource parent, String name) {
075                return matcher.matches(ignoreCase?StringUtil.toLowerCase(name):name, pattern);
076        }
077        public boolean accept(String name) {
078                return matcher.matches(ignoreCase?StringUtil.toLowerCase(name):name, pattern);
079        }
080
081    @Override
082        public String toString() {
083                return "Wildcardfilter:"+wildcard;
084        }
085        
086}