1 20 package org.enhydra.barracuda.plankton.http; 21 22 import java.util.*; 23 24 28 public class HttpConverter { 29 30 33 public static String cvtMapToURLString (Map map) { 34 return cvtMapToURLString(map, "&"); 35 } 36 37 41 public static String cvtMapToURLString (Map map, String delimiter) { 42 if (map==null) return null; 44 45 if (!(map instanceof SortedMap)) { map = new TreeMap(map); 52 } 53 54 StringBuffer sb = new StringBuffer (200); 56 String sep = ""; 57 Iterator it = map.keySet().iterator(); 58 while (it.hasNext()) { 59 Object key = it.next(); 60 Object 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 concat(String sep, Object key, Object value) { 76 return sep+key.toString()+"="+value.toString(); 77 } 78 79 83 public static Map cvtURLStringToMap(String paramStr) { 84 return cvtURLStringToMap(paramStr, "&"); 85 } 86 87 91 public static Map cvtURLStringToMap(String paramStr, String delimiter) { 92 TreeMap map = new TreeMap(); int spos = 0; 96 int epos = -1; 97 int eqpos = -1; 98 if (paramStr.startsWith("?")) paramStr = paramStr.substring(1); 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 key = paramStr.substring (spos,eqpos); 107 String value = paramStr.substring (eqpos+1, epos); 108 if (map.containsKey(key)) { 109 Object 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 |