01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.rproxy.connectionhandler;
06:
07: /**
08: * A Simple parser for parsing a URL's query string. Decomposes a URL's query
09: * string to name-value pairs. @ author Rajesh T @ date 25-08-2003 @ version 1.0
10: */
11:
12: import java.util.HashMap;
13: import java.util.Map;
14: import java.util.StringTokenizer;
15:
16: public class QueryStringParser extends Object {
17: private String query = null;
18:
19: private HashMap paramMap = null;
20:
21: public QueryStringParser(String aQuery) {
22: query = aQuery;
23: }// constructor
24:
25: /**
26: * parsers the query string and returns a hash map of name as the key and
27: * value as the Value of the Hash Map. Each name-value user is seperated by
28: * '&' Identifies recurrsive query string URL's and parses them.
29: */
30: public Map getParamMap() {
31: if (paramMap == null) {
32: paramMap = new HashMap();
33: }
34:
35: if (query.indexOf('&') >= 0) {
36: StringTokenizer ampersandTokenizer = new StringTokenizer(
37: query, "&");
38: while (ampersandTokenizer.hasMoreTokens()) {
39: populatePropertiesNoAmbersand(ampersandTokenizer
40: .nextToken());
41: }
42: } else {
43: populatePropertiesNoAmbersand(query);
44: }
45:
46: return paramMap;
47: }// getParamMap()
48:
49: /**
50: * Tokenizes a name-value pair based on '=' and pupulates the internal
51: * HashMap. It is possible to have recursive query string such as
52: * http://server1.com/test?testurl=http://server2.com/test?url=.....
53: * Identifies the that the second part contains another query string and
54: * treats the subsequent '&' as parameter to the recursive URL's
55: */
56: private synchronized void populatePropertiesNoAmbersand(
57: String nameValue) {
58: int equalIndex = nameValue.indexOf('=');
59: String key = null;
60: String value = null;
61: key = nameValue.substring(0, equalIndex);
62: value = nameValue.substring(equalIndex + 1, nameValue.length());
63: paramMap.put(key, value);
64: }// populatePropertiesNoAmbersand()
65:
66: }// class QueryStringParser
|