1 7 package com.inversoft.verge.mvc.controller.form; 8 9 10 import java.util.ArrayList ; 11 import java.util.List ; 12 13 import javax.servlet.http.HttpServletRequest ; 14 15 import com.inversoft.verge.mvc.MVCConstants; 16 import com.inversoft.verge.mvc.MVCURLTools; 17 import com.inversoft.verge.mvc.controller.ControllerMVCInfo; 18 19 20 28 public final class FormURLTools extends MVCURLTools { 29 30 33 public static final String FORM_PREFIX = "f."; 34 35 38 public static final String ACTION_PREFIX = "a."; 39 40 43 public static final String SUBMIT_PARAMETER = "_vs_"; 44 45 48 public static final char MAIN_DELIMITER = '|'; 49 50 51 60 static FormMVCMetaData decodeURL(List values) { 61 String value1 = (values.size() >= 1) ? (String ) values.get(0) : null; 63 String value2 = (values.size() >= 2) ? (String ) values.get(1) : null; 64 FormMVCMetaData md = new FormMVCMetaData(); 65 if (value1 != null) { 66 if (value1.startsWith(FORM_PREFIX)) { 67 md.setForm(value1.substring(FORM_PREFIX.length())); 68 } else if (value1.startsWith(ACTION_PREFIX)) { 69 md.setAction(value1.substring(ACTION_PREFIX.length())); 70 } 71 } 72 73 if (value2 != null && value2.startsWith(ACTION_PREFIX)) { 74 md.setAction(value2.substring(ACTION_PREFIX.length())); 75 } 76 77 return md; 78 } 79 80 88 static void locateAction(HttpServletRequest request, FormMVCMetaData md) { 89 String [] submits = request.getParameterValues(SUBMIT_PARAMETER); 90 if (submits == null) { 91 return; 92 } 93 94 int index; 95 String name; 96 for (int i = 0; i < submits.length; i++) { 97 index = submits[i].indexOf(MAIN_DELIMITER); 98 name = submits[i].substring(0, index); 99 100 if (request.getParameter(name) != null || 104 request.getParameter(name + ".x") != null) { 105 md.setAction(submits[i].substring(index + 1)); 106 break; 107 } 108 } 109 } 110 111 119 public static String generateURL(String form, String action) { 120 List list = new ArrayList (); 121 if (form != null) { 122 list.add(FORM_PREFIX + form); 123 } 124 125 if (action != null) { 126 list.add(ACTION_PREFIX + action); 127 } 128 129 ControllerMVCInfo info = new ControllerMVCInfo(MVCConstants.FORM_NAME, list); 130 return getURLBeginning() + info.encode(); 131 } 132 133 140 public static String generateSubmitParameter(String submitName, String action) { 141 if (submitName == null || action == null) { 142 throw new IllegalArgumentException ("Submit name and action required"); 143 } 144 145 StringBuffer buf = new StringBuffer (); 146 147 buf.append(submitName).append(MAIN_DELIMITER).append(action); 148 149 return buf.toString(); 150 } 151 } | Popular Tags |