View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.util;
19  
20  import java.net.URI;
21  import java.util.HashMap;
22  import java.util.Map;
23  import java.util.StringTokenizer;
24  
25  /***
26   * Some URI based helper methods.
27   *
28   * @version $Revision: 1.1 $
29   */
30  public class URIHelper {
31  
32      /***
33       * Parsers the query string of the URI into a map of key-value pairs
34       */
35      public static Map parseQuery(URI uri) {
36          return parseQuery(uri.getQuery());
37      }
38  
39      /***
40       * Parsers the query string of the URI into a map of key-value pairs
41       */
42      public static Map parseQuery(String query) {
43          Map answer = new HashMap();
44          if (query != null) {
45              StringTokenizer iter = new StringTokenizer(query, "&");
46              while (iter.hasMoreTokens()) {
47                  String pair = iter.nextToken();
48                  addKeyValuePair(answer, pair);
49              }
50          }
51          return answer;
52      }
53  
54      protected static void addKeyValuePair(Map answer, String pair) {
55          int idx = pair.indexOf('=');
56          String name = null;
57          String value = null;
58          if (idx >= 0) {
59              name = pair.substring(0, idx);
60              if (++idx < pair.length()) {
61                  value = pair.substring(idx);
62              }
63          }
64          else {
65              name = pair;
66          }
67          answer.put(name, value);
68      }
69  }