| 1 34 35 package com.micronova.jsp.tag; 36 37 import javax.servlet.jsp.el.*; 38 import java.lang.reflect.*; 39 import java.util.*; 40 import com.micronova.util.*; 41 42 57 58 public class CodecFunctionMapper implements FunctionMapper 59 { 60 protected static CodecFunctionMapper defaultCodecFunctionMapper; 61 62 protected static final String STANDARDFUNCTIONS = "org.apache.taglibs.standard.functions.Functions"; 63 protected static Map standardFunctionsMap; 64 65 public static final String FIND_MIN = "min"; 66 public static final String FIND_MAX = "max"; 67 68 static 69 { 70 defaultCodecFunctionMapper = new CodecFunctionMapper(); 71 72 try 73 { 74 standardFunctionsMap = new HashMap(); 75 76 Method[] methods = Class.forName(STANDARDFUNCTIONS).getMethods(); 77 78 for (int i = 0; i < methods.length; i ++) 79 { 80 Method method = methods[i]; 81 82 standardFunctionsMap.put(method.getName(), method); 83 } 84 } 85 catch (Exception e) 86 { 87 } 88 } 89 90 protected static Method findMethodForName(Class classObject, String name, String findType) 91 { 92 Method[] methods = classObject.getMethods(); 93 94 Method methodFound = null; 95 int methodFoundParamCount = 0; 96 97 for (int i = 0; i < methods.length; i ++) 98 { 99 Method method = methods[i]; 100 101 if (name.equals(method.getName())) 102 { 103 int paramCount = method.getParameterTypes().length; 104 105 boolean isMatch = (methodFound == null); 106 107 if (!isMatch) 108 { 109 if (FIND_MIN.equals(findType)) 110 { 111 isMatch = (paramCount < methodFoundParamCount); 112 } 113 else if (FIND_MAX.equals(findType)) 114 { 115 isMatch = (paramCount > methodFoundParamCount); 116 } 117 } 118 119 if (isMatch) 120 { 121 methodFound = method; 122 methodFoundParamCount = paramCount; 123 } 124 } 125 } 126 127 return methodFound; 128 } 129 130 131 protected CodecFunctionMapper() 132 { 133 super(); 134 } 135 136 public static FunctionMapper getInstance() 137 { 138 return defaultCodecFunctionMapper; 139 } 140 141 public Method resolveFunction(String prefix, String name) 142 { 143 try 144 { 145 147 if ("fn".equals(prefix)) 148 { 149 return (Method)standardFunctionsMap.get(name); 150 } 151 152 List namePartList = StringUtil.split(name, '_'); 153 int namePartListSize = namePartList.size(); 154 155 String codecName = prefix; 156 String codecMethod = name; 157 String argSpec = FIND_MIN; 158 159 if ("m".equals(prefix)) 160 { 161 codecName = (String )namePartList.get(0); 162 codecMethod = (String )namePartList.get(1); 163 164 if (namePartListSize > 2) 165 { 166 argSpec = (String )namePartList.get(2); 167 } 168 } 169 else if (namePartListSize > 1) 170 { 171 codecMethod = (String )namePartList.get(0); 172 argSpec = (String )namePartList.get(1); 173 } 174 175 if (codecName.indexOf('.') < 0) 176 { 177 codecName = EL.CODECPATH + codecName; 178 } 179 180 Class c = Class.forName(codecName); 181 182 if (FIND_MAX.equals(argSpec) || FIND_MIN.equals(argSpec)) 183 { 184 return findMethodForName(c, codecMethod, argSpec); 185 } 186 else 187 { 188 int numArgs = Integer.parseInt(argSpec); 189 190 Class [] types = new Class [numArgs]; 191 192 for (int i = numArgs; --i >=0;) 193 { 194 types[i] = Object .class; 195 } 196 197 return c.getDeclaredMethod(codecMethod, types); 198 } 199 } 200 catch (Exception e) 201 { 202 throw new RuntimeException (e); 203 } 204 } 205 } 206 | Popular Tags |