001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.json;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    
023    import java.io.Writer;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class JSONArrayImpl implements JSONArray {
029    
030            public JSONArrayImpl() {
031                    _jsonArray = new org.json.JSONArray();
032            }
033    
034            public JSONArrayImpl(String json) throws JSONException {
035                    try {
036                            _jsonArray = new org.json.JSONArray(json);
037                    }
038                    catch (Exception e) {
039                            throw new JSONException(e);
040                    }
041            }
042    
043            public JSONArrayImpl(org.json.JSONArray jsonArray) {
044                    _jsonArray = jsonArray;
045            }
046    
047            public boolean getBoolean(int index) {
048                    return _jsonArray.optBoolean(index);
049            }
050    
051            public double getDouble(int index) {
052                    return _jsonArray.optDouble(index);
053            }
054    
055            public int getInt(int index) {
056                    return _jsonArray.optInt(index);
057            }
058    
059            public org.json.JSONArray getJSONArray() {
060                    return _jsonArray;
061            }
062    
063            public JSONArray getJSONArray(int index) {
064                    org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
065    
066                    if (jsonArray == null) {
067                            return null;
068                    }
069    
070                    return new JSONArrayImpl(jsonArray);
071            }
072    
073            public JSONObject getJSONObject(int index) {
074                    org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
075    
076                    if (jsonObj == null) {
077                            return null;
078                    }
079    
080                    return new JSONObjectImpl(jsonObj);
081            }
082    
083            public long getLong(int index) {
084                    return _jsonArray.optLong(index);
085            }
086    
087            public String getString(int index) {
088                    return _jsonArray.optString(index);
089            }
090    
091            public boolean isNull(int index) {
092                    return _jsonArray.isNull(index);
093            }
094    
095            public String join(String separator) throws JSONException {
096                    try {
097                            return _jsonArray.join(separator);
098                    }
099                    catch (Exception e) {
100                            throw new JSONException(e);
101                    }
102            }
103    
104            public int length() {
105                    return _jsonArray.length();
106            }
107    
108            public JSONArray put(boolean value) {
109                    _jsonArray.put(value);
110    
111                    return this;
112            }
113    
114            public JSONArray put(double value) {
115                    try {
116                            _jsonArray.put(value);
117                    }
118                    catch (Exception e) {
119                            if (_log.isWarnEnabled()) {
120                                    _log.warn(e, e);
121                            }
122                    }
123    
124                    return this;
125            }
126    
127            public JSONArray put(int value) {
128                    _jsonArray.put(value);
129    
130                    return this;
131            }
132    
133            public JSONArray put(long value) {
134                    _jsonArray.put(value);
135    
136                    return this;
137            }
138    
139            public JSONArray put(JSONArray value) {
140                    _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
141    
142                    return this;
143            }
144    
145            public JSONArray put(JSONObject value) {
146                    _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
147    
148                    return this;
149            }
150    
151            public JSONArray put(String value) {
152                    _jsonArray.put(value);
153    
154                    return this;
155            }
156    
157            public String toString() {
158                    return _jsonArray.toString();
159            }
160    
161            public String toString(int indentFactor) throws JSONException {
162                    try {
163                            return _jsonArray.toString(indentFactor);
164                    }
165                    catch (Exception e) {
166                            throw new JSONException(e);
167                    }
168            }
169    
170            public Writer write(Writer writer) throws JSONException {
171                    try {
172                            return _jsonArray.write(writer);
173                    }
174                    catch (Exception e) {
175                            throw new JSONException(e);
176                    }
177            }
178    
179            private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
180    
181            private org.json.JSONArray _jsonArray;
182    
183    }