Utils.java revision 291476fb2561cbd02d465d763e39686cf4c7a611
1package autotest.common; 2 3import com.google.gwt.http.client.URL; 4import com.google.gwt.json.client.JSONArray; 5import com.google.gwt.json.client.JSONNumber; 6import com.google.gwt.json.client.JSONObject; 7import com.google.gwt.json.client.JSONString; 8import com.google.gwt.json.client.JSONValue; 9 10import java.util.ArrayList; 11import java.util.Collection; 12import java.util.HashMap; 13import java.util.List; 14import java.util.Map; 15 16public class Utils { 17 public static final String JSON_NULL = "<null>"; 18 private static final String[][] escapeMappings = { 19 {"&", "&"}, 20 {">", ">"}, 21 {"<", "<"}, 22 {"\"", """}, 23 {"'", "'"}, 24 }; 25 26 /** 27 * Converts a collection of Java <code>String</code>s into a <code>JSONArray 28 * </code> of <code>JSONString</code>s. 29 */ 30 public static JSONArray stringsToJSON(Collection<String> strings) { 31 JSONArray result = new JSONArray(); 32 for(String s : strings) { 33 result.set(result.size(), new JSONString(s)); 34 } 35 return result; 36 } 37 38 /** 39 * Converts a <code>JSONArray</code> of <code>JSONStrings</code> to an 40 * array of Java <code>Strings</code>. 41 */ 42 public static String[] JSONtoStrings(JSONArray strings) { 43 String[] result = new String[strings.size()]; 44 for (int i = 0; i < strings.size(); i++) { 45 result[i] = jsonToString(strings.get(i)); 46 } 47 return result; 48 } 49 50 /** 51 * Converts a <code>JSONArray</code> of <code>JSONObjects</code> to an 52 * array of Java <code>Strings</code> by grabbing the specified field from 53 * each object. 54 */ 55 public static String[] JSONObjectsToStrings(JSONArray objects, String field) { 56 String[] result = new String[objects.size()]; 57 for (int i = 0; i < objects.size(); i++) { 58 JSONValue fieldValue = objects.get(i).isObject().get(field); 59 result[i] = jsonToString(fieldValue); 60 } 61 return result; 62 } 63 64 /** 65 * Get a value out of an array of size 1. 66 * @return array[0] 67 * @throws IllegalArgumentException if the array is not of size 1 68 */ 69 public static JSONValue getSingleValueFromArray(JSONArray array) { 70 if(array.size() != 1) { 71 throw new IllegalArgumentException("Array is not of size 1"); 72 } 73 return array.get(0); 74 } 75 76 public static JSONObject copyJSONObject(JSONObject source) { 77 JSONObject dest = new JSONObject(); 78 for(String key : source.keySet()) { 79 dest.put(key, source.get(key)); 80 } 81 return dest; 82 } 83 84 public static String escape(String text) { 85 for (String[] mapping : escapeMappings) { 86 text = text.replace(mapping[0], mapping[1]); 87 } 88 return text; 89 } 90 91 public static String unescape(String text) { 92 // must iterate in reverse order 93 for (int i = escapeMappings.length - 1; i >= 0; i--) { 94 text = text.replace(escapeMappings[i][1], escapeMappings[i][0]); 95 } 96 return text; 97 } 98 99 public static <T> List<T> wrapObjectWithList(T object) { 100 List<T> list = new ArrayList<T>(); 101 list.add(object); 102 return list; 103 } 104 105 public static <T> String joinStrings(String joiner, List<T> objects) { 106 StringBuilder result = new StringBuilder(); 107 boolean first = true; 108 for (T object : objects) { 109 String piece = object.toString(); 110 if (piece.equals("")) { 111 continue; 112 } 113 if (first) { 114 first = false; 115 } else { 116 result.append(joiner); 117 } 118 result.append(piece); 119 } 120 return result.toString(); 121 } 122 123 public static Map<String,String> decodeUrlArguments(String urlArguments) { 124 Map<String, String> arguments = new HashMap<String, String>(); 125 String[] components = urlArguments.split("&"); 126 for (String component : components) { 127 String[] parts = component.split("="); 128 if (parts.length > 2) { 129 throw new IllegalArgumentException(); 130 } 131 String key = decodeComponent(parts[0]); 132 String value = ""; 133 if (parts.length == 2) { 134 value = URL.decodeComponent(parts[1]); 135 } 136 arguments.put(key, value); 137 } 138 return arguments; 139 } 140 141 private static String decodeComponent(String component) { 142 return URL.decodeComponent(component.replace("%27", "'")); 143 } 144 145 public static String encodeUrlArguments(Map<String, String> arguments) { 146 List<String> components = new ArrayList<String>(); 147 for (Map.Entry<String, String> entry : arguments.entrySet()) { 148 String key = encodeComponent(entry.getKey()); 149 String value = encodeComponent(entry.getValue()); 150 components.add(key + "=" + value); 151 } 152 return joinStrings("&", components); 153 } 154 155 private static String encodeComponent(String component) { 156 return URL.encodeComponent(component).replace("'", "%27"); 157 } 158 159 /** 160 * @param path should be of the form "123-showard/status.log" or just "123-showard" 161 */ 162 public static String getLogsURL(String path) { 163 String val = URL.encode("/results/" + path); 164 return "/tko/retrieve_logs.cgi?job=" + val; 165 } 166 167 public static String jsonToString(JSONValue value) { 168 JSONString string; 169 JSONNumber number; 170 assert value != null; 171 if ((string = value.isString()) != null) { 172 return string.stringValue(); 173 } 174 if ((number = value.isNumber()) != null) { 175 return Integer.toString((int) number.doubleValue()); 176 } 177 if (value.isNull() != null) { 178 return JSON_NULL; 179 } 180 return value.toString(); 181 } 182 183 public static String setDefaultValue(Map<String, String> map, String key, String defaultValue) { 184 if (map.containsKey(key)) { 185 return map.get(key); 186 } 187 map.put(key, defaultValue); 188 return defaultValue; 189 } 190 191 public static JSONValue setDefaultValue(JSONObject object, String key, JSONValue defaultValue) { 192 if (object.containsKey(key)) { 193 return object.get(key); 194 } 195 object.put(key, defaultValue); 196 return defaultValue; 197 } 198 199 public static List<String> splitList(String list, String splitRegex) { 200 String[] parts = list.split(splitRegex); 201 List<String> finalParts = new ArrayList<String>(); 202 for (String part : parts) { 203 if (!part.equals("")) { 204 finalParts.add(part); 205 } 206 } 207 return finalParts; 208 } 209 210 public static List<String> splitList(String list) { 211 return splitList(list, ","); 212 } 213 214 public static List<String> splitListWithSpaces(String list) { 215 return splitList(list, "[,\\s]+"); 216 } 217 218 public static void updateObject(JSONObject destination, JSONObject source) { 219 if (source == null) { 220 return; 221 } 222 for (String key : source.keySet()) { 223 destination.put(key, source.get(key)); 224 } 225 } 226} 227