Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/common/util/UrlUtil.java


1   /*
2    * UrlUtil.java
3    *
4    * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5    *
6    * See the file LICENSE for terms of use.
7    *
8    */
9   
10  package com.aendvari.common.util;
11  
12  import java.util.*;
13  import java.lang.*;
14  
15  import java.net.URLDecoder;
16  
17  import java.lang.reflect.*;
18  import java.lang.NoSuchMethodException;
19  import java.lang.IllegalAccessException;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.http.*;
24  import javax.servlet.jsp.*;
25  import javax.servlet.jsp.tagext.*;
26  
27  import com.aendvari.tethys.*;
28  import com.aendvari.tethys.context.*;
29  
30  
31  /**
32   * <p>Utility class for URL access and manipulation.</p>
33   *
34   * @author Scott Milne
35   *
36   */
37  
38  public class UrlUtil
39  {
40    /**
41     * <p>Returns {@link java.util.HashMap} of name/value pairs derived from the given string.</p>
42     *
43     * @param  nameValueString    The string holding the name/value pairs.
44     * @param  pairSeparator    The string that separates the name/value pairs from each other.
45     * @param  nameValueSeparator  The string that separates the name from the value.
46     * @param  urlDecode      Should the name/value string be URLDecoded before adding to the map.
47     *
48     * @return            <code>HashMap</code> of name/value pairs.
49     *
50     */
51  
52    public static HashMap getNameValuePairs( String nameValueString, String pairSeparator, String nameValueSeparator, boolean urlDecode )
53    {
54      HashMap map = new HashMap();
55      StringTokenizer tokens = new StringTokenizer(nameValueString, pairSeparator);
56  
57      while (tokens.hasMoreTokens())
58      {
59        String segment = tokens.nextToken();
60  
61        int nameLen = segment.indexOf(nameValueSeparator);
62        int valueStart = segment.indexOf(nameValueSeparator) + 1;
63  
64        String name = "";
65        if (nameLen >= 0)
66          name = segment.substring(0, nameLen);
67  
68        String value = "";
69        if (valueStart >= 0)
70          value = segment.substring(valueStart);
71  
72        if (urlDecode)
73        {
74          map.put( URLDecoder.decode(name), URLDecoder.decode(value) );
75        }
76        else
77        {
78          map.put( name, value );
79        }
80      }
81  
82      return map;
83    }
84  }
85