/** * Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray. * * @param names A JSONArray containing a list of key strings. These will be paired with the * values. * @return A JSONObject, or null if there are no names or if this JSONArray has no values. * @throws JSONException If any of the names are null. */ public JSONObject toJSONObject(JSONArray names) throws JSONException { if (names == null || names.length() == 0 || length() == 0) { return null; } JSONObject jo = new JSONObject(); for (int i = 0; i < names.length(); i += 1) { jo.put(names.getString(i), this.opt(i)); } return jo; }
/* * (non-Javadoc) * * @see activitipoc.IFormHandler#parseResult(java.lang.String) */ public FormResult parseResult(String data) { final Map<String, Object> parameters = new LinkedHashMap<String, Object>(); final Map<String, Collection<String>> routes = new LinkedHashMap<String, Collection<String>>(); JSONObject jObject = new JSONObject(data); Iterator<?> keys = jObject.keys(); while (keys.hasNext()) { // TODO dangerous cast String key = (String) keys.next(); // separate properties from roles if (key.startsWith(SINGLE_USER_KEY_PREFIX)) { routes.put( key.replaceFirst(SINGLE_USER_KEY_PREFIX, ""), Arrays.asList(jObject.getString(key))); } else if (key.startsWith(GROUP_USER_KEY_PREFIX)) { Set<String> users = new HashSet<String>(); JSONArray usersJ = jObject.getJSONArray(key); for (int i = 0; i < usersJ.length(); i++) { users.add(usersJ.getString(i)); } routes.put(key.replaceFirst(GROUP_USER_KEY_PREFIX, ""), users); } else { Object value = jObject.get(key); parameters.put(key, value); } } return new FormResult() { public Map<String, Collection<String>> getRolesToUsersMapping() { return routes; } public Map<String, Object> getProperties() { return parameters; } }; }