01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: **/package org.araneaframework.http.util;
16:
17: import java.util.ArrayList;
18: import java.util.Iterator;
19: import java.util.List;
20: import java.util.Map;
21: import java.util.StringTokenizer;
22:
23: /**
24: *
25: * @author Jevgeni Kabanov (ekabanov <i>at</i> araneaframework <i>dot</i> org)
26: */
27: public abstract class URLUtil {
28: /**
29: * Removes all leading and trailing slashes.
30: */
31: public static String normalizeURI(String uri) {
32: if (uri == null)
33: return null;
34:
35: // lose the first slashes
36: while (uri.indexOf("/") == 0 && uri.length() > 0)
37: uri = uri.substring(1);
38:
39: // lose the last slashes
40: while (uri.lastIndexOf("/") == (uri.length() - 1)
41: && uri.length() > 0)
42: uri = uri.substring(0, uri.length() - 1);
43:
44: return uri;
45: }
46:
47: public static String[] splitURI(String uri) {
48: List result = new ArrayList();
49: uri = normalizeURI(uri);
50:
51: StringTokenizer tokenizer = new StringTokenizer(uri, "/");
52: while (tokenizer.hasMoreTokens()) {
53: result.add(tokenizer.nextToken());
54: }
55:
56: return (String[]) result.toArray(new String[result.size()]);
57: }
58:
59: public static String parametrizeURI(String uri, Map parameters) {
60: StringBuffer sb = new StringBuffer(uri);
61:
62: if (parameters != null && parameters.size() > 0) {
63: sb.append('?');
64: for (Iterator i = parameters.entrySet().iterator(); i
65: .hasNext();) {
66: Map.Entry pair = (Map.Entry) i.next();
67: sb.append((String) pair.getKey());
68: sb.append('=');
69: sb.append(pair.getValue());
70: if (i.hasNext())
71: sb.append('&');
72: }
73: }
74:
75: return sb.toString();
76: }
77: }
|