001    package railo.commons.io.res.util;
002    
003    import java.io.File;
004    
005    import railo.commons.io.res.Resource;
006    import railo.runtime.engine.ThreadLocalPageContext;
007    import railo.runtime.exp.ExpressionException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.exp.PageRuntimeException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.type.UDF;
012    
013    public class UDFFilter extends UDFFilterSupport implements ResourceAndResourceNameFilter {
014    
015            public UDFFilter(UDF udf) throws ExpressionException{
016                    super(udf);
017            }
018            
019        /**
020         * @see railo.commons.io.res.filter.ResourceFilter#accept(railo.commons.io.res.Resource)
021         */
022        public boolean accept(String path) {
023            args[0]=path;
024            try {
025                            return Caster.toBooleanValue(udf.call(ThreadLocalPageContext.get(), args, true));
026                            
027                    } 
028            catch (PageException e) {
029                            throw new PageRuntimeException(e);
030                    }
031        }
032        
033        
034        public boolean accept(Resource file) {
035            return accept(file.getAbsolutePath());
036        }
037    
038            /**
039             * @see railo.commons.io.res.filter.ResourceNameFilter#accept(railo.commons.io.res.Resource, java.lang.String)
040             */
041            public boolean accept(Resource parent, String name) {
042                    String path=parent.getAbsolutePath();
043                    if(path.endsWith(File.separator)) path+=name;
044                    else path+=File.separator+name;
045                    return accept(path);
046            }
047            
048        /**
049             *
050             * @see java.lang.Object#toString()
051             */
052            public String toString() {
053                    return "UDFFilter:"+udf;
054            }
055    }