KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > controller > ControllerMVCInfo


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.controller;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List 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 import com.inversoft.verge.mvc.MVCRegistry;
20
21
22 /**
23  * <p>
24  * This class is used by the default Verge MVC implementation
25  * to encode and decode the controller information to and
26  * from a single String. This String is the URL path
27  * information used in the request.
28  * </p>
29  *
30  * <p>
31  * The structure of a URL path information is like this:
32  * </p>
33  *
34  * <p>
35  * <code>/controllerHandler[/urlValue]*</code>
36  * </p>
37  *
38  * <p>
39  * The / character is the delimiter that is used by the
40  * default MVC implementation to distinguish values.
41  * </p>
42  *
43  * @author Brian Pontarelli
44  * @since 2.0
45  * @version 2.0
46  */

47 public class ControllerMVCInfo {
48
49     /**
50      * This classes logger
51      */

52     private static final Logger logger = Logger.getLogger(ControllerMVCInfo.class);
53
54     /**
55      * The input delimiter for this MVCInfo implementation.
56      */

57     public static final String JavaDoc INPUT_DELIMITER = "/";
58
59
60     private String JavaDoc controllerHandler;
61     private ControllerHandler handler;
62     private List JavaDoc urlValues = new ArrayList JavaDoc(5);
63
64
65     /**
66      * Constructs a new <code>DefaultModel</code> that is empty and the decode
67      * method must be called prior to using this object.
68      */

69     protected ControllerMVCInfo() {
70     }
71
72     /**
73      * Constructs a new <code>DefaultModel</code> using the given encoded value
74      *
75      * @param encodedValue The model's meta data in encoded form
76      * @throws MVCException If the encodedValue is misformed
77      */

78     public ControllerMVCInfo(String JavaDoc encodedValue) throws MVCException {
79         decode(encodedValue);
80     }
81
82     /**
83      * Constructs a new <code>DefaultModel</code> using the given values which
84      * comprise the model's encoded value
85      */

86     public ControllerMVCInfo(String JavaDoc controllerHandler, List JavaDoc urlValues) {
87         assert (controllerHandler != null) : "controllerHandler == null";
88
89         this.controllerHandler = controllerHandler;
90         this.urlValues = urlValues;
91     }
92
93
94     /**
95      * Returns the controller's controllerHandler value
96      *
97      * @return The controllerHandler value
98      */

99     public String JavaDoc getControllerHandler() {
100         return controllerHandler;
101     }
102
103     /**
104      * Returns the ControllerHandler instance that was looked up from the
105      * MVCRegistry.
106      *
107      * @return The ControllerHandler
108      */

109     public ControllerHandler getHandler() throws MVCException {
110         if (handler == null) {
111             handler = MVCRegistry.lookupControllerHandler(controllerHandler);
112             if (handler == null) {
113                 throw new MVCException("ControllerHandler named: " +
114                     controllerHandler + " does not exist");
115             }
116
117         }
118         return handler;
119     }
120
121     /**
122      * Returns the URL values in a List. These are the values to the rightof the
123      * system value inside the URL extra info.
124      *
125      * @return The values and never null
126      */

127     public List JavaDoc getURLValues() {
128         return urlValues;
129     }
130
131     /**
132      * Returns this model's encoded value
133      *
134      * @return The encoded value as a single String
135      */

136     public String JavaDoc encode() {
137         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(128);
138         buf.append(ControllerMVCInfo.INPUT_DELIMITER);
139         buf.append(controllerHandler);
140
141         Iterator JavaDoc iter = urlValues.iterator();
142         while (iter.hasNext()) {
143             buf.append(ControllerMVCInfo.INPUT_DELIMITER).append(iter.next());
144         }
145
146         return buf.toString();
147     }
148
149     /**
150      * Uses the values encoded into the given string to setup the value of this
151      * object.
152      *
153      * @param encodedValue The encoded values for this object
154      */

155     public void decode(String JavaDoc encodedValue) throws MVCException {
156         if (encodedValue == null) {
157             logger.error("Verge framework encountered an invalid URL [" +
158                 encodedValue + "]");
159             throw new MVCException("Verge framework encountered an invalid URL [" +
160                 encodedValue + "]");
161         }
162
163         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(encodedValue,
164             ControllerMVCInfo.INPUT_DELIMITER);
165         try {
166             controllerHandler = st.nextToken();
167         } catch (NoSuchElementException JavaDoc nsee) {
168             logger.error("Verge framework encountered an invalid URL [" +
169                 encodedValue + "]");
170             throw new MVCException("Verge framework encountered an invalid URL [" +
171                 encodedValue + "]");
172         }
173
174         while (st.hasMoreTokens()) {
175             urlValues.add(st.nextToken());
176         }
177     }
178 }
Popular Tags