2 * Copyright (C) 2010 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.i18n.addressinput;
19 import org.json.JSONArray;
20 import org.json.JSONException;
21 import org.json.JSONObject;
22 import org.json.JSONTokener;
24 import java.util.ArrayList;
25 import java.util.Iterator;
28 * Compatibility methods on top of the JSON data.
30 class JsoMap extends JSONObject {
33 * Construct a JsoMap object given some json text. This method directly evaluates the String
34 * that you pass in; no error or safety checking is performed, so be very careful about the
35 * source of your data.
37 * @param json JSON text describing an address format
38 * @return a JsoMap object made from the supplied JSON.
40 static JsoMap buildJsoMap(String json) throws JSONException {
41 return new JsoMap(new JSONTokener(json));
45 * Construct an empty JsoMap.
47 * @return the empty object.
49 static JsoMap createEmptyJsoMap() {
59 private JsoMap(JSONTokener readFrom) throws JSONException {
63 private JsoMap(JSONObject copyFrom, String[] names) throws JSONException {
64 super(copyFrom, names);
68 * Remove the specified key.
70 * @param key key name.
72 void delKey(String key) {
77 * Retrieve the string value for specified key.
79 * @param key key name.
80 * @return string value.
81 * @throws ClassCastException, IllegalArgumentException.
84 public String get(String key) { // throws ClassCastException, IllegalArgumentException
86 Object o = super.get(key);
87 if (o instanceof String) {
89 } else if (o instanceof Integer) {
90 throw new IllegalArgumentException();
92 throw new ClassCastException();
94 } catch (JSONException e) {
100 * Access JSONObject.get(String) which is shadowed by JsoMap.get(String).
102 * @param name A key string.
103 * @return The object associated with the key.
104 * @throws JSONException if the key is not found.
106 private Object getObject(String name) throws JSONException {
107 return super.get(name);
111 * Retrieves the integer value for specified key.
113 * @return integer value or -1 if value is undefined.
116 public int getInt(String key) {
118 Object o = super.get(key);
119 if (o instanceof Integer) {
120 return ((Integer) o).intValue();
122 throw new RuntimeException("Something other than an int was returned");
124 } catch (JSONException e) {
130 * Collect all the keys and return as a JSONArray.
132 * @return A JSONArray that contains all the keys.
134 JSONArray getKeys() {
135 return super.names();
139 * Retrieve the JsoMap object for specified key.
141 * @param key key name.
142 * @return JsoMap object.
143 * @throws ClassCastException, IllegalArgumentException.
145 @SuppressWarnings("unchecked")
146 // JSONObject.keys() has no type information.
147 JsoMap getObj(String key)
148 throws ClassCastException, IllegalArgumentException {
150 Object o = super.get(key);
151 if (o instanceof JSONObject) {
152 JSONObject value = (JSONObject) o;
153 ArrayList<String> keys = new ArrayList<String>(value.length());
154 for (Iterator<String> it = value.keys(); it.hasNext();) {
157 String[] names = new String[keys.size()];
158 return new JsoMap(value, keys.toArray(names));
159 } else if (o instanceof Integer) {
160 throw new IllegalArgumentException();
162 throw new ClassCastException();
164 } catch (JSONException e) {
170 * Check if the object has specified key.
172 * @param key The key name to be checked.
173 * @return true if key can be found.
175 boolean containsKey(String key) {
176 return super.has(key);
180 * Merge those keys not found in this object from specified object.
182 * @param obj The other object to be merged.
184 void mergeData(JsoMap obj) {
189 JSONArray names = obj.names();
194 for (int i = 0; i < names.length(); i++) {
196 String name = names.getString(i);
198 if (!super.has(name)) {
199 super.put(name, obj.getObject(name));
201 } catch (JSONException e) {
202 throw new RuntimeException(e);
204 } catch (JSONException e) {
211 * Save a string to string mapping into this map.
213 * @param key the string key.
214 * @param value the String value.
216 void put(String key, String value) {
218 super.put(key, value);
219 } catch (JSONException e) {
220 throw new RuntimeException(e);
225 * Save a string to integer mapping into this map.
227 * @param key the string key.
228 * @param value the integer value.
230 void putInt(String key, int value) {
232 super.put(key, value);
233 } catch (JSONException e) {
234 throw new RuntimeException(e);
239 * Save a string to JSONObject mapping into this map.
241 * @param key the string key.
242 * @param value a JSONObject as value.
244 void putObj(String key, JSONObject value) {
246 super.put(key, value);
247 } catch (JSONException e) {
248 throw new RuntimeException(e);
252 String string() throws ClassCastException, IllegalArgumentException {
253 StringBuilder sb = new StringBuilder("JsoMap[\n");
254 JSONArray keys = getKeys();
255 for (int i = 0; i < keys.length(); i++) {
258 key = keys.getString(i);
259 } catch (JSONException e) {
260 throw new RuntimeException(e);
262 sb.append('(').append(key).append(':').append(get(key)).append(')').append('\n');
265 return sb.toString();
268 String map() throws ClassCastException, IllegalArgumentException {
269 StringBuilder sb = new StringBuilder("JsoMap[\n");
270 JSONArray keys = getKeys();
271 for (int i = 0; i < keys.length(); i++) {
274 key = keys.getString(i);
275 } catch (JSONException e) {
276 throw new RuntimeException(e);
278 sb.append('(').append(key).append(':').append(getObj(key).string()).append(')')
282 return sb.toString();