KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > ModelMVCInfo


1 /*
2  * Copyright (c) 2003, Inversoft
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.model;
8
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.NoSuchElementException JavaDoc;
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.apache.log4j.Logger;
17
18 import com.inversoft.verge.mvc.MVCException;
19
20
21 /**
22  * <p>
23  * This class is used by the default Inversoft MVC
24  * implementation to encode and decode the model information
25  * to and from a single String.
26  * </p>
27  *
28  * <p>
29  * The structure of a single input parameter is like this:
30  * </p>
31  *
32  * <p>
33  * <code>inputName|modelResolver|modelHandler|modelDefinition[|extraParameters]*</code>
34  * </p>
35  *
36  * <p>
37  * The | character is the delimiter that is used by the
38  * default MVC implementation to distinguish values.
39  * Currently, this delimiter character can not be used in
40  * either the modelType or the modelDefinition values,
41  * but this may change in the future.
42  * </p>
43  *
44  * <p>
45  * The last piece of the input's value is a list of extra
46  * parameter that are stored in a HashMap that is passed to
47  * the ModelResolver and ModelHandler respectively. These may
48  * aid in the function of the model setting. Each extra
49  * parameter is structured like this:
50  * </p>
51  *
52  * <p>
53  * <code>name=value</code>
54  * </p>
55  *
56  * <p>
57  * In addition, each extra parameter must separated by the
58  * delimiter character.
59  * </p>
60  *
61  * @author Brian Pontarelli
62  * @since 2.0
63  * @version 2.0
64  */

65 public class ModelMVCInfo {
66
67     /**
68      * This classes logger
69      */

70     private static final Logger logger = Logger.getLogger(ModelMVCInfo.class);
71
72     /**
73      * The delimiter character used to decode the input parameters.
74      */

75     public static final String JavaDoc INPUT_DELIMITER = "|";
76
77     /**
78      * The delimiter character used to decode the extra parameters.
79      */

80     public static final String JavaDoc EXTRA_PARAM_DELIMITER = "=";
81
82
83     private String JavaDoc inputName;
84     private String JavaDoc modelSystem;
85     private String JavaDoc modelDefinition;
86     private Map JavaDoc extraParams = new HashMap JavaDoc();
87
88
89
90     /**
91      * Constructs a new <code>DefaultModel</code> that is empty. You must use the
92      * decode method to decode the information otherwise this class will fail
93      * miserably.
94      */

95     public ModelMVCInfo() {
96     }
97
98     /**
99      * Constructs a new <code>DefaultModel</code> using the given encoded value
100      *
101      * @param encodedValue The model's meta data in encoded form
102      * @throws MVCException If the encodedValue is misformed
103      */

104     public ModelMVCInfo(String JavaDoc encodedValue) throws MVCException {
105         decode(encodedValue);
106     }
107
108     /**
109      * Constructs a new <code>DefaultModel</code> using the given values which
110      * comprise the model's encoded value
111      */

112     public ModelMVCInfo(String JavaDoc inputName, String JavaDoc modelSystem, String JavaDoc modelDefinition,
113             Map JavaDoc extraParams) {
114         assert (inputName != null) : "inputName == null";
115         assert (modelSystem != null) : "modelSystem == null";
116         assert (modelDefinition != null) : "modelDefinition == null";
117
118         this.inputName = inputName;
119         this.modelSystem = modelSystem;
120         this.modelDefinition = modelDefinition;
121         this.extraParams = extraParams;
122     }
123
124
125     /**
126      * Returns the model's inputName value
127      *
128      * @return The inputName value
129      */

130     public String JavaDoc getInputName() {
131         return inputName;
132     }
133
134     /**
135      * Returns the model's system name
136      *
137      * @return The model system name
138      */

139     public String JavaDoc getModelSystem() {
140         return modelSystem;
141     }
142
143     /**
144      * Returns the model's modelDefinition value
145      *
146      * @return The modelDefinition value
147      */

148     public String JavaDoc getModelDefinition() {
149         return modelDefinition;
150     }
151
152     /**
153      * Returns the model's extraParams value.
154      *
155      * @return The extraParams value
156      */

157     public Map JavaDoc getExtraParams() {
158         return extraParams;
159     }
160
161     /**
162      * Decodes the meta data from the encoded value passed in
163      *
164      * @param encodedValue The encodedValue to decode
165      */

166     public void decode(String JavaDoc encodedValue) throws MVCException {
167         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(encodedValue, INPUT_DELIMITER);
168         try {
169             inputName = st.nextToken();
170             modelSystem = st.nextToken();
171             modelDefinition = st.nextToken();
172         } catch (NoSuchElementException JavaDoc nsee) {
173             logger.error("Invalid input parameter format: " + encodedValue);
174             throw new MVCException("Invalid input parameter format: " +
175                 encodedValue);
176         }
177
178         extraParams.clear();
179         decodeExtraParams(st);
180     }
181
182     /**
183      * Returns this model's encoded value
184      *
185      * @return The encoded value as a single String
186      */

187     public String JavaDoc encode() {
188         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
189         buf.append(inputName).append(INPUT_DELIMITER);
190         buf.append(modelSystem).append(INPUT_DELIMITER);
191         buf.append(modelDefinition);
192
193         encodeExtraParams(buf);
194
195         return buf.toString();
196     }
197
198     /**
199      * Using the StringTokenizer, this retrieves the extra params if there are
200      * any.
201      *
202      * @param st The StringTokenizer to retrieve the params from. This
203      * tokenizer must be sitting directly before the first parameter
204      * string
205      * @throws com.inversoft.verge.mvc.MVCException If any of the parameter strings was misformated
206      */

207     protected void decodeExtraParams(StringTokenizer JavaDoc st) throws MVCException {
208
209         // Optionally get the extra parameters out of the request
210
if (st.hasMoreTokens()) {
211
212             String JavaDoc extraParam;
213             int index;
214
215             do {
216                 extraParam = st.nextToken();
217                 index = extraParam.indexOf(EXTRA_PARAM_DELIMITER);
218                 if (index == -1 || index == (extraParam.length() - 1)) {
219                     logger.error("Invalid extra parameter string: " + extraParam);
220                     throw new MVCException("Invalid extra parameter string: " +
221                         extraParam);
222                 }
223
224                 extraParams.put(extraParam.substring(0, index),
225                     extraParam.substring(index + 1));
226             } while (st.hasMoreTokens());
227         }
228     }
229
230     /**
231      * Encodes the extra parameter map into the given StringBuffer.
232      *
233      * @param buf The StringBuffer to add the encoded extra params to
234      */

235     protected void encodeExtraParams(StringBuffer JavaDoc buf) {
236         if (extraParams != null && extraParams.size() > 0) {
237             Iterator JavaDoc iter = extraParams.entrySet().iterator();
238             Map.Entry JavaDoc entry;
239             while (iter.hasNext()) {
240                 entry = (Map.Entry JavaDoc) iter.next();
241                 buf.append(INPUT_DELIMITER).append(entry.getKey());
242                 buf.append(EXTRA_PARAM_DELIMITER).append(entry.getValue());
243             }
244         }
245     }
246 }
Popular Tags