1 21 package net.mlw.vlh.web; 22 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.StringTokenizer ; 28 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.jsp.PageContext ; 31 32 import net.mlw.vlh.ValueListInfo; 33 import net.mlw.vlh.web.tag.ActionTag; 34 35 43 public final class ValueListRequestUtil 44 { 45 46 47 private ValueListRequestUtil() 48 { 49 } 50 51 public static Map getRequestParameterMap(HttpServletRequest request) 52 { 53 return getRequestParameterMap(request, ""); 54 } 55 56 public static Map getRequestParameterMap(Map requestParameterMap, String id) 57 { 58 59 if (requestParameterMap == null) 60 { 61 return new HashMap (); 62 } 63 int lenghtOfId = (id == null) ? 0 : id.length(); 64 65 Map parameters = new HashMap (requestParameterMap.size()); 66 for (Iterator keys = requestParameterMap.keySet().iterator(); keys.hasNext();) 67 { 68 String key = (String ) keys.next(); 69 70 String [] values; 71 Object valuesObj = requestParameterMap.get(key); 72 if (valuesObj instanceof String ) 73 { 74 values = new String [] {(String ) valuesObj}; 75 } 76 else 77 { 78 values = (String []) valuesObj; 79 } 80 81 Object value = ((values == null) ? null : (values.length == 1 ? (Object ) values[0] : (Object ) values)); 82 83 if (lenghtOfId > 0) 84 { 85 if (key.endsWith(id)) 86 { 87 parameters.put(key.substring(0, key.length() - lenghtOfId), value); 88 } 89 } 90 else 91 { 92 parameters.put(key, value); 93 } 94 95 } 96 return parameters; 97 } 98 99 105 public static Map getRequestParameterMap(HttpServletRequest request, String id) 106 { 107 int lenghtOfId = (id == null) ? 0 : id.length(); 108 109 Map parameters = new HashMap (request.getParameterMap().size()); 110 for (Enumeration keys = request.getParameterNames(); keys.hasMoreElements();) 111 { 112 String key = (String ) keys.nextElement(); 113 String [] values = request.getParameterValues(key); 114 Object value = ((values == null) ? null : (values.length == 1 ? (Object ) values[0] : (Object ) values)); 115 116 if (lenghtOfId > 0) 117 { 118 if (key.endsWith(id)) 119 { 120 parameters.put(key.substring(0, key.length() - lenghtOfId), value); 121 } 122 } 123 else 124 { 125 parameters.put(key, value); 126 } 127 128 } 129 return parameters; 130 } 131 132 140 public static ValueListInfo buildValueListInfo(HttpServletRequest request) 141 { 142 return buildValueListInfo(request, ""); 143 } 144 145 151 public static ValueListInfo buildValueListInfo(HttpServletRequest request, String id) 152 { 153 Map parameters = getRequestParameterMap(request, id); 154 return builtValueListInfo(parameters); 155 } 156 157 162 private static ValueListInfo builtValueListInfo(Map parameters) 163 { 164 return new ValueListInfo(parameters); 165 } 166 167 192 public static Map getAllForwardParameters(PageContext pageContext, String userForwardParameters, String id) 193 { 194 195 Map forwardedParams = new HashMap (); 196 197 for (StringTokenizer st = new StringTokenizer (userForwardParameters, "|"); st.hasMoreTokens();) 198 { 199 String key = st.nextToken(); 200 201 if (key.equals("#")) 202 { 203 forwardedParams.putAll(getForwardParamsOfOtherTables(pageContext, id)); 204 } 205 else 206 { 207 if (key.endsWith("*")) 208 { 209 key = key.substring(0, key.length() - 1); 210 forwardedParams.putAll(getForwardParamsOfKey(pageContext, id, key)); 211 } 212 else 213 { 214 215 if (key.equals("$")) 216 { 217 forwardedParams.putAll(getForwardActionTempParamsOfOtherTables(pageContext, id)); 218 } 219 else 220 { 221 222 String [] values = pageContext.getRequest().getParameterValues(key); 223 if (values != null && values.length > 0) 224 { 225 forwardedParams.put(key, values); 226 } 227 } 228 } 229 } 230 } 231 232 return forwardedParams; 233 } 234 235 243 private static Map getForwardParamsOfKey(PageContext pageContext, String id, String prefix) 244 { 245 Map paramsOfKey = new HashMap (); 246 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();) 247 { 248 String requestParamName = (String ) enumer.nextElement(); 249 if (requestParamName.startsWith(prefix) && (!requestParamName.startsWith(ValueListInfo.DO_FOCUS + id)) 250 && (!requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX))) 251 { 252 String [] values = pageContext.getRequest().getParameterValues(requestParamName); 253 if (values != null) 254 { 255 paramsOfKey.put(requestParamName, values); 256 } 257 } 258 } 259 return paramsOfKey; 260 } 261 262 267 private static Map getForwardActionTempParamsOfOtherTables(PageContext pageContext, String id) 268 { 269 270 Map actionTempParamsOfOthers = new HashMap (); 271 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();) 272 { 273 String requestParamName = (String ) enumer.nextElement(); 274 275 if (requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX) && !requestParamName.endsWith(id)) 276 { 277 String [] values = pageContext.getRequest().getParameterValues(requestParamName); 278 if (values != null) 279 { 280 actionTempParamsOfOthers.put(requestParamName, values); 281 } 282 } 283 284 } 285 return actionTempParamsOfOthers; 286 } 287 288 295 private static Map getForwardParamsOfOtherTables(PageContext pageContext, String id) 296 { 297 Map otherTablesParams = new HashMap (); 298 for (Enumeration enumer = pageContext.getRequest().getParameterNames(); enumer.hasMoreElements();) 299 { 300 String requestParamName = (String ) enumer.nextElement(); 301 302 if ((!requestParamName.endsWith(id) && !requestParamName.startsWith(ActionTag.ACTION_TEMP_PARAM_PREFIX)) 303 && (requestParamName.startsWith(ValueListInfo.FOCUS_PARAM_PREFIX) 304 || requestParamName.startsWith(ValueListInfo.PAGING_PARAM_PREFIX) || requestParamName 305 .startsWith(ValueListInfo.SORT_PARAM_PREFIX))) 306 { 307 String [] values = pageContext.getRequest().getParameterValues(requestParamName); 308 if (values != null) 309 { 310 otherTablesParams.put(requestParamName, values); 311 } 312 } 313 314 } 315 return otherTablesParams; 316 } 317 318 324 public static String getActionTempParamName(String userParamName) 325 { 326 return ActionTag.ACTION_TEMP_PARAM_PREFIX + userParamName; 327 } 328 329 } | Popular Tags |