1 7 package com.inversoft.verge.mvc.controller.actionflow; 8 9 10 import java.util.ArrayList ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 import java.util.Map ; 14 import java.util.StringTokenizer ; 15 16 import javax.servlet.http.HttpServletRequest ; 17 18 import com.inversoft.verge.mvc.MVCConstants; 19 import com.inversoft.verge.mvc.MVCURLTools; 20 import com.inversoft.verge.mvc.controller.ControllerMVCInfo; 21 22 23 31 public final class ActionFlowURLTools extends MVCURLTools { 32 33 37 public static final char MAIN_DELIMITER = 30; 38 39 43 public static final char SUB_DELIMITER = 31; 44 45 48 public static final String FORM_PREFIX = "f."; 49 50 53 public static final String ACTION_PREFIX = "a."; 54 55 58 public static final String NAMESPACE_PREFIX = "n."; 59 60 63 public static final String ENTRY_PREFIX = "e."; 64 65 68 public static final String SUBMIT_PARAMETER = "_vs_"; 69 70 74 public static final String ENTRY_PARAM = "en"; 75 76 79 public static final String MODEL_ENABLED_PARAM = "me"; 80 81 84 public static final String VALIDATION_ENABLED_PARAM = "ve"; 85 86 87 94 static ActionFlowMetaData decodeURL(List values) { 95 ActionFlowMetaData md = new ActionFlowMetaData(); 97 Iterator iter = values.iterator(); 98 String value = null; 99 while (iter.hasNext()) { 100 value = (String ) iter.next(); 101 if (value.startsWith(NAMESPACE_PREFIX)) { 102 md.setNamespace(value.substring(NAMESPACE_PREFIX.length())); 103 } else if (value.startsWith(FORM_PREFIX)) { 104 md.setForm(value.substring(FORM_PREFIX.length())); 105 } else if (value.startsWith(ACTION_PREFIX)) { 106 md.setAction(value.substring(ACTION_PREFIX.length())); 107 } else if (value.startsWith(ENTRY_PREFIX)) { 108 md.setEntry(value.substring(ENTRY_PREFIX.length())); 109 } 110 } 111 112 return md; 113 } 114 115 125 static void locateAction(HttpServletRequest request, ActionFlowMetaData md) { 126 String [] submits = request.getParameterValues(SUBMIT_PARAMETER); 127 if (submits == null) { 128 return; 129 } 130 131 StringTokenizer st; 132 String name; 133 for (int i = 0; i < submits.length; i++) { 134 st = new StringTokenizer (submits[i], "" + MAIN_DELIMITER); 135 if (st.hasMoreTokens()) { 136 name = st.nextToken(); 137 } else { 138 continue; 139 } 140 141 if (request.getParameter(name) != null || 145 request.getParameter(name + ".x") != null) { 146 md.setAction(st.nextToken()); 147 148 while (st.hasMoreTokens()) { 150 151 StringTokenizer st2 = new StringTokenizer (st.nextToken(), 152 "" + SUB_DELIMITER); 153 String param; 154 String key; 155 String value; 156 int index; 157 while (st2.hasMoreTokens()) { 158 param = st2.nextToken(); 159 index = param.indexOf('='); 160 key = param.substring(0, index); 161 value = param.substring(index + 1); 162 163 if (key.equals(ENTRY_PARAM)) { 164 md.setEntry(value); 165 } else if (key.equals(VALIDATION_ENABLED_PARAM)) { 166 md.setValidationEnabled(Boolean.valueOf(value).booleanValue()); 167 } else if (key.equals(MODEL_ENABLED_PARAM)) { 168 md.setModelEnabled(Boolean.valueOf(value).booleanValue()); 169 } 170 } 171 } 172 173 break; 174 } 175 } 176 } 177 178 188 public static String generateURL(String namespace, String form, String action, 189 String entry) { 190 if (namespace == null) { 191 throw new IllegalArgumentException ("The namespace is required"); 192 } 193 194 List list = new ArrayList (); 195 list.add(NAMESPACE_PREFIX + namespace); 196 197 if (form != null) { 198 list.add(FORM_PREFIX + form); 199 } 200 201 if (action != null) { 202 list.add(ACTION_PREFIX + action); 203 } 204 205 if (entry != null) { 206 list.add(ENTRY_PREFIX + entry); 207 } 208 209 ControllerMVCInfo info = new ControllerMVCInfo(MVCConstants.ACTIONFLOW_NAME, list); 210 return getURLBeginning() + info.encode(); 211 } 212 213 222 public static String generateSubmitParameter(String submitName, String action, 223 String entry, Map extraParams) { 224 if (submitName == null || action == null) { 225 throw new IllegalArgumentException ("Submit name and action required"); 226 } 227 228 StringBuffer buf = new StringBuffer (); 229 230 buf.append(submitName).append(MAIN_DELIMITER).append(action); 231 232 if (entry != null) { 233 buf.append(MAIN_DELIMITER); 234 buf.append(ENTRY_PARAM).append('=').append(entry); 235 } 236 237 if (extraParams != null && extraParams.size() > 0) { 238 buf.append(MAIN_DELIMITER); 239 240 Iterator iter = extraParams.keySet().iterator(); 241 Object key; 242 while (iter.hasNext()) { 243 key = iter.next(); 244 buf.append(key).append('=').append(extraParams.get(key)); 245 246 if (iter.hasNext()) { 247 buf.append(SUB_DELIMITER); 248 } 249 } 250 } 251 252 return buf.toString(); 253 } 254 } | Popular Tags |