1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.json;
21  
22  import com.liferay.portal.kernel.json.JSONArray;
23  import com.liferay.portal.kernel.json.JSONException;
24  import com.liferay.portal.kernel.json.JSONObject;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.Writer;
29  
30  /**
31   * <a href="JSONArrayImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   *
35   */
36  public class JSONArrayImpl implements JSONArray {
37  
38      public JSONArrayImpl() {
39          _jsonArray = new org.json.JSONArray();
40      }
41  
42      public JSONArrayImpl(String json) throws JSONException {
43          try {
44              _jsonArray = new org.json.JSONArray(json);
45          }
46          catch (Exception e) {
47              throw new JSONException(e);
48          }
49      }
50  
51      public JSONArrayImpl(org.json.JSONArray jsonArray) {
52          _jsonArray = jsonArray;
53      }
54  
55      public boolean getBoolean(int index) {
56          return _jsonArray.optBoolean(index);
57      }
58  
59      public double getDouble(int index) {
60          return _jsonArray.optDouble(index);
61      }
62  
63      public int getInt(int index) {
64          return _jsonArray.optInt(index);
65      }
66  
67      public org.json.JSONArray getJSONArray() {
68          return _jsonArray;
69      }
70  
71      public JSONArray getJSONArray(int index) {
72          org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
73  
74          if (jsonArray == null) {
75              return null;
76          }
77  
78          return new JSONArrayImpl(jsonArray);
79      }
80  
81      public JSONObject getJSONObject(int index) {
82          org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
83  
84          if (jsonObj == null) {
85              return null;
86          }
87  
88          return new JSONObjectImpl(jsonObj);
89      }
90  
91      public long getLong(int index) {
92          return _jsonArray.optLong(index);
93      }
94  
95      public String getString(int index) {
96          return _jsonArray.optString(index);
97      }
98  
99      public boolean isNull(int index) {
100         return _jsonArray.isNull(index);
101     }
102 
103     public String join(String separator) throws JSONException {
104         try {
105             return _jsonArray.join(separator);
106         }
107         catch (Exception e) {
108             throw new JSONException(e);
109         }
110     }
111 
112     public int length() {
113         return _jsonArray.length();
114     }
115 
116     public JSONArray put(boolean value) {
117         _jsonArray.put(value);
118 
119         return this;
120     }
121 
122     public JSONArray put(double value) {
123         try {
124             _jsonArray.put(value);
125         }
126         catch (Exception e) {
127             if (_log.isWarnEnabled()) {
128                 _log.warn(e, e);
129             }
130         }
131 
132         return this;
133     }
134 
135     public JSONArray put(int value) {
136         _jsonArray.put(value);
137 
138         return this;
139     }
140 
141     public JSONArray put(long value) {
142         _jsonArray.put(value);
143 
144         return this;
145     }
146 
147     public JSONArray put(JSONArray value) {
148         _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
149 
150         return this;
151     }
152 
153     public JSONArray put(JSONObject value) {
154         _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
155 
156         return this;
157     }
158 
159     public JSONArray put(String value) {
160         _jsonArray.put(value);
161 
162         return this;
163     }
164 
165     public String toString() {
166         return _jsonArray.toString();
167     }
168 
169     public String toString(int indentFactor) throws JSONException {
170         try {
171             return _jsonArray.toString(indentFactor);
172         }
173         catch (Exception e) {
174             throw new JSONException(e);
175         }
176     }
177 
178     public Writer write(Writer writer) throws JSONException {
179         try {
180             return _jsonArray.write(writer);
181         }
182         catch (Exception e) {
183             throw new JSONException(e);
184         }
185     }
186 
187     private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
188 
189     private org.json.JSONArray _jsonArray;
190 
191 }