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.rewriter.yahoo;
06:
07: /**
08: * A Simple parser for parsing a URL's query string. Decomposes a URL's query
09: * string to name-value pairs. Identifies recursive URLs with query string and
10: * parses then. Ideally should have been composite pattern for such generic
11: * requirement consider implementing one in the future.
12: * @ author Rajesh T @ date 27-08-2002 @ version 1.0
13: */
14:
15: import java.util.StringTokenizer;
16:
17: import com.sun.portal.rewriter.util.collections.ListMap;
18:
19: public class QueryParser {
20: /**
21: * parsers the query string and returns a hash map of name as the key and
22: * value as the Value of the Hash Map. Each name-value user is seperated by
23: * '&' Identifies recurrsive query string URL's and parses them.
24: */
25: public static final ListMap parseQueryString(String aQuery) {
26: ListMap lParamMap = new ListMap();
27: if (aQuery.indexOf('&') >= 0) {
28: StringTokenizer ampersandTokenizer = new StringTokenizer(
29: aQuery, "&");
30: while (ampersandTokenizer.hasMoreTokens()) {
31: parseNameAndValue(lParamMap, ampersandTokenizer
32: .nextToken());
33: }
34: } else {
35: parseNameAndValue(lParamMap, aQuery);
36: }
37:
38: return lParamMap;
39: }// parseQueryString()
40:
41: /**
42: * Tokenizes a name-value pair based on '=' and pupulates the internal
43: * HashMap. It is possible to have recursive query string such as
44: * http://server1.com/test?testurl=http://server2.com/test?url=.....
45: * Identifies the that the second part contains another query string and
46: * treats the subsequent '&' as parameter to the recursive URL's
47: */
48: private static final void parseNameAndValue(
49: final ListMap aParamMap, final String aNameAndValue) {
50: int lEqualIndex = aNameAndValue.indexOf('=');
51: String lKey = aNameAndValue.substring(0, lEqualIndex);
52: String lValue = aNameAndValue.substring(lEqualIndex + 1,
53: aNameAndValue.length());
54: aParamMap.put(lKey, lValue);
55: }// parseNameAndValue()
56:
57: }// class QueryParser
|