KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > actionflow > ActionFlowURLTools


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.actionflow;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Map JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import javax.servlet.http.HttpServletRequest JavaDoc;
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 /**
24  * <p>
25  * This class is a toolkit to help with ActionFlow MVC URLs
26  * and the like.
27  * </p>
28  *
29  * @author Brian Pontarelli
30  */

31 public final class ActionFlowURLTools extends MVCURLTools {
32
33     /**
34      * The main delimiter for request parameters that store the action, submit
35      * name and extra info.
36      */

37     public static final char MAIN_DELIMITER = 30;
38
39     /**
40      * The main delimiter for request parameters that store the action, submit
41      * name and extra info.
42      */

43     public static final char SUB_DELIMITER = 31;
44
45     /**
46      * The prefix used in the URLs to denote a form.
47      */

48     public static final String JavaDoc FORM_PREFIX = "f.";
49
50     /**
51      * The prefix used in URLs to denote an action.
52      */

53     public static final String JavaDoc ACTION_PREFIX = "a.";
54
55     /**
56      * The prefix used in URLs to denote a namespace.
57      */

58     public static final String JavaDoc NAMESPACE_PREFIX = "n.";
59
60     /**
61      * The prefix used in URLs to denote an entry.
62      */

63     public static final String JavaDoc ENTRY_PREFIX = "e.";
64
65     /**
66      * The name of the request parameter that holds the controller meta data
67      */

68     public static final String JavaDoc SUBMIT_PARAMETER = "_vs_";
69
70     /**
71      * The parameter within the extra parameters of a submit parameter for the
72      * entry node.
73      */

74     public static final String JavaDoc ENTRY_PARAM = "en";
75
76     /**
77      * The name of the parameter for the model enabling
78      */

79     public static final String JavaDoc MODEL_ENABLED_PARAM = "me";
80
81     /**
82      * The name of the parameter for the validation enabling
83      */

84     public static final String JavaDoc VALIDATION_ENABLED_PARAM = "ve";
85
86
87     /**
88      * Uses the list of values to determine which namespace, action, form and
89      * entry are being requested (if any). The list can contain up to four values.
90      *
91      * @param values The values to use
92      * @return A new FlowInfo struct
93      */

94     static ActionFlowMetaData decodeURL(List JavaDoc values) {
95         // Look for the info
96
ActionFlowMetaData md = new ActionFlowMetaData();
97         Iterator JavaDoc iter = values.iterator();
98         String JavaDoc value = null;
99         while (iter.hasNext()) {
100             value = (String JavaDoc) 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     /**
116      * If the action could not be found in the URL, this method tries to locate it
117      * in the request parameters. If one can be found, this also extracts any
118      * additional parameters from the request parameter where the action was
119      * found.
120      *
121      * @param request The HttpServletRequest to get the parameters from.
122      * @param md The ActionFlowMetaData to set the action into if found. If it
123      * can not be found, the action remains as it was when passed in.
124      */

125     static void locateAction(HttpServletRequest JavaDoc request, ActionFlowMetaData md) {
126         String JavaDoc [] submits = request.getParameterValues(SUBMIT_PARAMETER);
127         if (submits == null) {
128             return;
129         }
130
131         StringTokenizer JavaDoc st;
132         String JavaDoc name;
133         for (int i = 0; i < submits.length; i++) {
134             st = new StringTokenizer JavaDoc(submits[i], "" + MAIN_DELIMITER);
135             if (st.hasMoreTokens()) {
136                 name = st.nextToken();
137             } else {
138                 continue;
139             }
140
141             // Check for the request parameter for the submit button. If this is
142
// an image submit, there will always be two parameters, Z.x and Z.y
143
// where Z is the name of the image submit tag
144
if (request.getParameter(name) != null ||
145                     request.getParameter(name + ".x") != null) {
146                 md.setAction(st.nextToken());
147
148                 // See if there is are extra parameters
149
while (st.hasMoreTokens()) {
150
151                     StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(st.nextToken(),
152                         "" + SUB_DELIMITER);
153                     String JavaDoc param;
154                     String JavaDoc key;
155                     String JavaDoc 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     /**
179      * Generates a URL based on the form, action, namespace and entry given.
180      * This prepends the prefixes if necessary and then constructs the URL.
181      *
182      * @param namespace The namespace
183      * @param form (Optional) The form
184      * @param action (Optional) The action
185      * @param entry (Optional) The entry
186      * @return The URL
187      */

188     public static String JavaDoc generateURL(String JavaDoc namespace, String JavaDoc form, String JavaDoc action,
189             String JavaDoc entry) {
190         if (namespace == null) {
191             throw new IllegalArgumentException JavaDoc("The namespace is required");
192         }
193
194         List JavaDoc list = new ArrayList JavaDoc();
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     /**
214      * Using the submitName, action and possibly the extra parameters and entry
215      * node name given, this builds a submit parameter value (not the name).
216      *
217      * @param submitName The submitName
218      * @param action The action
219      * @param entry (Optional) The entry node
220      * @param extraParams (Optional) The extra parameters
221      */

222     public static String JavaDoc generateSubmitParameter(String JavaDoc submitName, String JavaDoc action,
223             String JavaDoc entry, Map JavaDoc extraParams) {
224         if (submitName == null || action == null) {
225             throw new IllegalArgumentException JavaDoc("Submit name and action required");
226         }
227
228         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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 JavaDoc iter = extraParams.keySet().iterator();
241             Object JavaDoc 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