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