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