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.lang.CFTypes;
022import lucee.runtime.exp.ExpressionException;
023import lucee.runtime.type.FunctionArgument;
024import lucee.runtime.type.UDF;
025
026public abstract class UDFFilterSupport {
027
028        protected UDF udf;
029        protected Object[] args=new Object[1];
030        
031        public UDFFilterSupport(UDF udf) throws ExpressionException{
032                this.udf=udf;
033                
034                // check UDF return type
035                int type = udf.getReturnType();
036                if(type!=CFTypes.TYPE_BOOLEAN && type!=CFTypes.TYPE_ANY)
037                        throw new ExpressionException("invalid return type ["+udf.getReturnTypeAsString()+"] for UDF Filter, valid return types are [boolean,any]");
038                
039                // check UDF arguments
040                FunctionArgument[] args = udf.getFunctionArguments();
041                if(args.length>1)
042                        throw new ExpressionException("UDF filter has to many arguments ["+args.length+"], should have at maximum 1 argument");
043                
044                if(args.length==1){
045                        type=args[0].getType();
046                        if(type!=CFTypes.TYPE_STRING && type!=CFTypes.TYPE_ANY)
047                                throw new ExpressionException("invalid type ["+args[0].getTypeAsString()+"] for first argument of UDF Filter, valid return types are [string,any]");
048                }
049                
050                
051        }
052        
053    @Override
054        public String toString() {
055                return "UDFFilter:"+udf;
056        }
057}