KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > http > HttpConverter


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: HttpConverter.java,v 1.6 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.http;
21
22 import java.util.*;
23
24 /**
25  * <p>This class provides a simple series of static methods
26  * to convert a Map to a URL String and vica-versa</p>
27  */

28 public class HttpConverter {
29
30     /**
31      * Convert the values in a Map to a URL String (ie. key1=val1&key2=val2&...)
32      */

33     public static String JavaDoc cvtMapToURLString (Map map) {
34         return cvtMapToURLString(map, "&");
35     }
36
37     /**
38      * Convert the values in a Map to a URL String (ie. key1=val1&key2=val2&...). You
39      * can specify an alternate delimeter here.
40      */

41     public static String JavaDoc cvtMapToURLString (Map map, String JavaDoc delimiter) {
42         //eliminate the obvious
43
if (map==null) return null;
44         
45         //if the map is not an instance of TreeMap, convert it so
46
//that the keys are ordered by default. By doing this automatically,
47
//we ensure that you can count on the order always being the same -
48
//you can easily tell if url parameters are the same between requests
49
//simply by comparing the resulting strings
50
if (!(map instanceof SortedMap)) { //csc_013104_1
51
map = new TreeMap(map);
52         }
53     
54         //run through the map and build a new string
55
StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
56         String JavaDoc sep = "";
57         Iterator it = map.keySet().iterator();
58         while (it.hasNext()) {
59             Object JavaDoc key = it.next();
60             Object JavaDoc value = map.get(key);
61             if (value instanceof Set) {
62                 Set set = (Set) value;
63                 Iterator it2 = set.iterator();
64                 while (it2.hasNext()) {
65                     sb.append(concat(sep, key, it2.next()));
66                 }
67             } else {
68                 sb.append(concat(sep, key, value));
69             }
70             sep = delimiter;
71         }
72         return sb.toString();
73     }
74     
75     private static String JavaDoc concat(String JavaDoc sep, Object JavaDoc key, Object JavaDoc value) {
76         return sep+key.toString()+"="+value.toString();
77     }
78     
79     /**
80      * Convert the values in a URL String (ie. key1=val1&key2=val2&...) to a Map
81      * of key/val pairs
82      */

83     public static Map cvtURLStringToMap(String JavaDoc paramStr) {
84         return cvtURLStringToMap(paramStr, "&");
85     }
86     
87     /**
88      * Convert the values in a URL String (ie. key1=val1&key2=val2&...) to a Map
89      * of key/val pairs. You can specify an alternate delimeter here.
90      */

91     public static Map cvtURLStringToMap(String JavaDoc paramStr, String JavaDoc delimiter) {
92         //build the HashMap
93
//csc_010404_1 HashMap map = new HashMap(10);
94
TreeMap map = new TreeMap(); //csc_010404_1
95
int spos = 0;
96         int epos = -1;
97         int eqpos = -1;
98         if (paramStr.startsWith("?")) paramStr = paramStr.substring(1); //csc_010404_1
99
int max = paramStr.length();
100         while (spos>=0 && spos<max) {
101             eqpos = paramStr.indexOf("=",spos+1);
102             if (eqpos<0) break;
103             epos = paramStr.indexOf(delimiter,eqpos+1);
104             if (epos<0 && eqpos>-1) epos = max;
105             if (eqpos>spos || eqpos<epos) {
106                 String JavaDoc key = paramStr.substring (spos,eqpos);
107                 String JavaDoc value = paramStr.substring (eqpos+1, epos);
108                 if (map.containsKey(key)) {
109                     Object JavaDoc mapVal = map.get(key);
110                     if (mapVal instanceof Set) {
111                         ((Set) mapVal).add(value);
112                     } else {
113                         Set set = new HashSet();
114                         map.put(key, set);
115                         set.add(mapVal);
116                         set.add(value);
117                     }
118                 } else {
119                     map.put(key, value);
120                 }
121             }
122             spos = epos+1;
123         }
124         return map;
125     }
126 }
127
Popular Tags