KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > portal > PathParser


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.portal;
24
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.pluto.portalImpl.core.PortalControlParameter;
37
38 /**
39  * @author robert
40  */

41 public class PathParser
42 {
43     private static final Log log = LogFactory.getLog(PathParser.class);
44
45     public static final String JavaDoc MULTI_VALUE = "__";
46
47     private String JavaDoc path;
48     private String JavaDoc action;
49     private Collection JavaDoc parsedPath;
50     private Collection JavaDoc globalNavigation;
51     private Map JavaDoc stateFullParameterMap;
52     private Map JavaDoc stateLessParameterMap;
53     private String JavaDoc pid = null;
54     private String JavaDoc actionControl = null;
55
56     public PathParser(String JavaDoc pPath)
57     {
58         this.path = pPath.substring(0, pPath.lastIndexOf('/'));
59         String JavaDoc tmp = pPath.substring(pPath.lastIndexOf('/') + 1, pPath.length());
60         StringTokenizer JavaDoc actionTok = new StringTokenizer JavaDoc(tmp, ";");
61         while (actionTok.hasMoreTokens())
62         {
63             String JavaDoc token = actionTok.nextToken();
64             if (token.endsWith(".action"))
65             {
66                 this.action = token;
67             }
68             else
69             {
70                 //TODO: implement me, I'm ;jsessionid=123cheese
71
}
72         }
73
74         this.parsedPath = new LinkedList JavaDoc();
75
76         for (StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(this.path, "/."); tok.hasMoreTokens();)
77         {
78             this.parsedPath.add(tok.nextElement());
79         }
80
81         this.stateFullParameterMap = new HashMap JavaDoc();
82         this.stateLessParameterMap = new HashMap JavaDoc();
83         this.globalNavigation = new LinkedList JavaDoc();
84         for (Iterator JavaDoc iter = parsedPath.iterator(); iter.hasNext();)
85         {
86             String JavaDoc element = (String JavaDoc) iter.next();
87
88             if (PortalControlParameter.isControlParameter(element))
89             {
90                 if (PortalControlParameter.isStateFullParameter(element))
91                 {
92                     if (iter.hasNext())
93                     {
94                         stateFullParameterMap.put(element, iter.next());
95                     }
96                 }
97                 else
98                 {
99                     if (iter.hasNext())
100                     {
101                         stateLessParameterMap.put(element, iter.next());
102                     }
103                 }
104
105             }
106             else
107             {
108                 globalNavigation.add(element);
109             }
110         }
111
112         // set the pid from the stateless parametermap
113
this.pid = (String JavaDoc) stateLessParameterMap.get(PortalControlURL.PID);
114         // set the actioncontrol from the stateless parametermap
115
this.actionControl = (String JavaDoc) stateLessParameterMap.get(PortalControlURL.ACTION);
116     }
117
118     public Map JavaDoc getStateFullParameterMap() {
119         return this.stateFullParameterMap;
120     }
121
122     public Map JavaDoc getStateLessParameterMap() {
123         return this.stateLessParameterMap;
124     }
125
126     public Collection JavaDoc getParsedPath() {
127         return parsedPath;
128     }
129
130     public Collection JavaDoc getGlobalNavigation() {
131         return globalNavigation;
132     }
133
134     public String JavaDoc getPath() {
135         return this.path;
136     }
137
138     public String JavaDoc getAction() {
139         return this.action;
140     }
141
142     /**
143      * Copy all parameters of a map into another
144      *
145      * @param parameters Map
146      * @return
147      */

148     public static Map JavaDoc copyParameters(Map JavaDoc parameters)
149     {
150         HashMap JavaDoc map = new HashMap JavaDoc(parameters.size());
151         for (Iterator JavaDoc iter = parameters.keySet().iterator(); iter.hasNext();)
152         {
153             String JavaDoc key = (String JavaDoc) iter.next();
154             Object JavaDoc value = parameters.get(key);
155             map.put(key, value);
156         }
157         
158         return map;
159     }
160
161     /**
162      * Get the path excluding control-params
163      *
164      * @param path The path excluding contextPath
165      * @return "real" path never null
166      */

167     public static String JavaDoc getRealPath(String JavaDoc path) {
168         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
169         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/");
170         while (st.hasMoreTokens()) {
171             String JavaDoc token = st.nextToken();
172             if (token.startsWith("_")) {
173                 break;
174             } else {
175                 buf.append("/");
176                 buf.append(token);
177             }
178         }
179         return buf.toString();
180     }
181
182     /**
183      * Extract control-params from path
184      *
185      * @param prefix Prefix of control-param
186      * @param encodedParameters Path
187      * @param preserveNamespace Keep prefix in key
188      * @return
189      */

190     public static Map JavaDoc parsePathParameters(
191         String JavaDoc prefix,
192         String JavaDoc encodedParameters,
193         boolean preserveNamespace) {
194         log.debug("Parsing '" + encodedParameters + "' for " + prefix);
195         HashMap JavaDoc map = new HashMap JavaDoc(10);
196         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(encodedParameters, "/");
197         String JavaDoc name = (tok.hasMoreTokens() ? tok.nextToken() : null);
198         while (name != null) {
199             if (name.startsWith(prefix)) {
200                 Vector JavaDoc buffer = new Vector JavaDoc();
201
202                 String JavaDoc nextName = decodeValues(tok, buffer);
203                 if (buffer.size() > 0) {
204                     if (!preserveNamespace) {
205                         name = name.substring(prefix.length());
206                     }
207                     log.debug("Adding " + name + "=" + buffer);
208                     map.put(name, (String JavaDoc[]) buffer.toArray(new String JavaDoc[buffer.size()]));
209                 }
210                 name = nextName;
211             } else {
212                 //log.debug("Unknown token: " + name);
213
name = (tok.hasMoreTokens() ? tok.nextToken() : null);
214             }
215         }
216         return map;
217     }
218
219     private static String JavaDoc decodeValues(StringTokenizer JavaDoc tok, List JavaDoc buffer) {
220         if (tok.hasMoreTokens()) {
221             String JavaDoc entry = tok.nextToken();
222             if (entry.startsWith(MULTI_VALUE)) {
223                 // Multi-value
224
while (tok.hasMoreTokens() && entry.startsWith(MULTI_VALUE)) {
225                     buffer.add(entry.substring(MULTI_VALUE.length()));
226                     entry = tok.nextToken().replaceAll("%2F", "/");
227                 }
228                 return entry;
229             } else {
230                 // Single-value
231
buffer.add(entry.replaceAll("%2F", "/"));
232                 return (tok.hasMoreElements() ? tok.nextToken() : null);
233             }
234         }
235         return null;
236     }
237
238     /**
239      * Encode control-param values. Multi-value params will be prefixed: "__value1/__value2"
240      *
241      * @param values
242      * @return
243      */

244     public static String JavaDoc encodeValues(String JavaDoc[] values) {
245         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
246         if (values.length == 1) {
247             result.append(values[0].replaceAll("/", "%2F"));
248         } else {
249             for (int i = 0; i < values.length; i++) {
250                 result.append(MULTI_VALUE);
251                 result.append(values[i].replaceAll("/", "%2F"));
252                 if (i < values.length - 1)
253                     result.append("/");
254             }
255         }
256         return result.toString();
257     }
258
259     /**
260      * Return pid if the pid parameter exists in the path otherwise null is returned.
261      * @return Returns the pid.
262      */

263     public String JavaDoc getPid() {
264         return pid;
265     }
266
267     /**
268      * Return the action control parameter if it exists, otherwise it's null.
269      * @return Returns the actionControl.
270      */

271     public String JavaDoc getActionControl() {
272         return actionControl;
273     }
274
275     public boolean isTargetedRequest() {
276         return (this.pid != null || actionControl != null);
277     }
278 }
279
Popular Tags