KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > form > FormURLTools


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.controller.form;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.List JavaDoc;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
21  * <p>
22  * This class is a toolkit to help with Form-Based MVC URLs
23  * and the like.
24  * </p>
25  *
26  * @author Brian Pontarelli
27  */

28 public final class FormURLTools extends MVCURLTools {
29
30     /**
31      * The prefix used in the URLs to denote a form.
32      */

33     public static final String JavaDoc FORM_PREFIX = "f.";
34
35     /**
36      * The prefix used in URLs to denote an action.
37      */

38     public static final String JavaDoc ACTION_PREFIX = "a.";
39
40     /**
41      * The name of the request parameter that holds the controller meta data
42      */

43     public static final String JavaDoc SUBMIT_PARAMETER = "_vs_";
44
45     /**
46      * The main delimiter used to separate the submit parameters.
47      */

48     public static final char MAIN_DELIMITER = '|';
49
50
51     /**
52      * Uses the list of values to determine which form and action are being
53      * requested. The list can contain up to two values. If there is one value,
54      * it can be either a form or action name. If there are two values, they are
55      * the form and value respectively.
56      *
57      * @param values The values to use
58      * @return A new FormAction struct
59      */

60     static FormMVCMetaData decodeURL(List JavaDoc values) {
61         // Look for the form/action
62
String JavaDoc value1 = (values.size() >= 1) ? (String JavaDoc) values.get(0) : null;
63         String JavaDoc value2 = (values.size() >= 2) ? (String JavaDoc) 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     /**
81      * If the action could not be found in the URL, this method tries to locate it
82      * in the request parameters.
83      *
84      * @param request The HttpServletRequest to get the parameters from.
85      * @param md The FormMVCMetaData to set the action into if found. If it can not
86      * be found, the action remains as it was when passed in.
87      */

88     static void locateAction(HttpServletRequest JavaDoc request, FormMVCMetaData md) {
89         String JavaDoc [] submits = request.getParameterValues(SUBMIT_PARAMETER);
90         if (submits == null) {
91             return;
92         }
93
94         int index;
95         String JavaDoc 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             // Check for the request parameter for the submit button. If this is
101
// an image submit, there will always be two parameters, Z.x and Z.y
102
// where Z is the name of the image submit tag
103
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     /**
112      * Generates a URL based on the form and action given. This prepends the
113      * prefixes if necessary and then constructs the URL.
114      *
115      * @param form The form
116      * @param action The action
117      * @return The URL
118      */

119     public static String JavaDoc generateURL(String JavaDoc form, String JavaDoc action) {
120         List JavaDoc list = new ArrayList JavaDoc();
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     /**
134      * Using the submitName and action this builds a submit parameter value (not
135      * the name).
136      *
137      * @param submitName The submitName
138      * @param action The action
139      */

140     public static String JavaDoc generateSubmitParameter(String JavaDoc submitName, String JavaDoc action) {
141         if (submitName == null || action == null) {
142             throw new IllegalArgumentException JavaDoc("Submit name and action required");
143         }
144
145         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
146
147         buf.append(submitName).append(MAIN_DELIMITER).append(action);
148
149         return buf.toString();
150     }
151 }
Popular Tags