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 java.io.File; 022 023import lucee.commons.io.res.Resource; 024import lucee.commons.lang.StringUtil; 025import lucee.runtime.engine.ThreadLocalPageContext; 026import lucee.runtime.exp.ExpressionException; 027import lucee.runtime.exp.PageException; 028import lucee.runtime.exp.PageRuntimeException; 029import lucee.runtime.op.Caster; 030import lucee.runtime.type.UDF; 031 032public class UDFFilter extends UDFFilterSupport implements ResourceAndResourceNameFilter { 033 034 public UDFFilter(UDF udf) throws ExpressionException{ 035 super(udf); 036 } 037 038 public boolean accept(String path) { 039 args[0]=path; 040 try { 041 return Caster.toBooleanValue(udf.call(ThreadLocalPageContext.get(), args, true)); 042 043 } 044 catch (PageException e) { 045 throw new PageRuntimeException(e); 046 } 047 } 048 049 050 public boolean accept(Resource file) { 051 return accept(file.getAbsolutePath()); 052 } 053 054 @Override 055 public boolean accept(Resource parent, String name) { 056 String path=parent.getAbsolutePath(); 057 if(path.endsWith(File.separator)) path+=name; 058 else path+=File.separator+name; 059 return accept(path); 060 } 061 062 @Override 063 public String toString() { 064 return "UDFFilter:"+udf; 065 } 066 067 public static ResourceAndResourceNameFilter createResourceAndResourceNameFilter(Object filter) throws PageException { 068 if(filter instanceof UDF) 069 return createResourceAndResourceNameFilter((UDF)filter); 070 return createResourceAndResourceNameFilter(Caster.toString(filter)); 071 } 072 073 074 public static ResourceAndResourceNameFilter createResourceAndResourceNameFilter(UDF filter) throws PageException { 075 return new UDFFilter(filter); 076 } 077 078 public static ResourceAndResourceNameFilter createResourceAndResourceNameFilter(String pattern) { 079 080 if( !StringUtil.isEmpty(pattern, true) ) 081 return new WildcardPatternFilter( pattern, "|" ); 082 083 return null; 084 } 085}