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.exp; 020 021import java.util.ArrayList; 022 023import lucee.runtime.PageContext; 024import lucee.runtime.config.ConfigImpl; 025import lucee.runtime.type.util.KeyConstants; 026import lucee.transformer.library.function.FunctionLib; 027import lucee.transformer.library.function.FunctionLibFunction; 028import lucee.transformer.library.function.FunctionLibFunctionArg; 029 030/** 031 * specified exception for Built-In Function 032 */ 033public final class FunctionException extends ExpressionException { 034 035 /* * 036 * constructor of the class 037 * @param pc current Page Context 038 * @param functionName Name of the function that thorw the Exception 039 * @param badArgumentPosition Position of the bad argument in the Argument List of the function 040 * @param badArgumentName Name of the bad Argument 041 * @param message additional Exception message 042 * / 043 public FunctionException(PageContext pc,String functionName, String badArgumentPosition, String badArgumentName, String message) { 044 this((PageContext)pc,functionName,badArgumentPosition,badArgumentName,message); 045 }*/ 046 047 /** 048 * constructor of the class 049 * @param pc current Page Context 050 * @param functionName Name of the function that thorw the Exception 051 * @param badArgumentPosition Position of the bad argument in the Argument List of the function 052 * @param badArgumentName Name of the bad Argument 053 * @param message additional Exception message 054 */ 055 public FunctionException(PageContext pc,String functionName, int badArgumentPosition, String badArgumentName, String message) { 056 this(pc,functionName,toStringBadArgumentPosition(badArgumentPosition),badArgumentName,message,null); 057 } 058 059 public FunctionException(PageContext pc,String functionName, int badArgumentPosition, String badArgumentName, String message, String detail) { 060 this(pc,functionName,toStringBadArgumentPosition(badArgumentPosition),badArgumentName,message,detail); 061 } 062 063 private static String toStringBadArgumentPosition(int pos) { 064 switch(pos) { 065 case 1:return "first"; 066 case 2:return "second"; 067 case 3:return "third"; 068 case 4:return "forth"; 069 case 5:return "fifth"; 070 case 6:return "sixth"; 071 case 7:return "seventh"; 072 case 8:return "eighth"; 073 case 9:return "ninth"; 074 case 10:return "tenth"; 075 case 11:return "eleventh"; 076 case 12:return "twelfth"; 077 } 078 // TODO Auto-generated method stub 079 return pos+"th"; 080 } 081 082 public FunctionException(PageContext pc,String functionName, String badArgumentPosition, String badArgumentName, String message, String detail) { 083 super("invalid call of the function "+functionName+", "+(badArgumentPosition)+" Argument ("+badArgumentName+") is invalid, "+message,detail); 084 setAdditional(KeyConstants._pattern,getFunctionInfo(pc,functionName)); 085 } 086 087 public FunctionException(PageContext pc, String functionName, int min, int max, int actual) { 088 super(actual<min? 089 "too few arguments ["+actual+"] for "+functionName+" function call, you need at least ["+min+"] arguments" 090 : 091 "too many arguments ["+actual+"] for "+functionName+" function call, you cannot pass more than ["+max+"] arguments" 092 ); 093 } 094 095 private static String getFunctionInfo(PageContext pc,String functionName) { 096 FunctionLib[] flds; 097 flds = ((ConfigImpl)pc.getConfig()).getFLDs(); 098 099 FunctionLibFunction function=null; 100 for(int i=0;i<flds.length;i++) { 101 function = flds[i].getFunction(functionName.toLowerCase()); 102 if(function!=null)break; 103 } 104 if(function == null) return ""; 105 106 StringBuilder rtn=new StringBuilder(); 107 rtn.append(function.getName()+"("); 108 109 110 int optionals=0; 111 ArrayList args = function.getArg(); 112 for(int i=0;i<args.size();i++) { 113 FunctionLibFunctionArg arg=(FunctionLibFunctionArg) args.get(i); 114 if(i!=0)rtn.append(", "); 115 if(!arg.getRequired()) { 116 rtn.append("["); 117 optionals++; 118 } 119 rtn.append(arg.getName()); 120 rtn.append(":"); 121 rtn.append(arg.getTypeAsString()); 122 } 123 for(int i=0;i<optionals;i++) 124 rtn.append("]"); 125 rtn.append("):"+function.getReturnTypeAsString()); 126 127 return rtn.toString(); 128 } 129 130 131 132}