1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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  
29  import java.io.Writer;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /**
35   * <a href="JSONArrayImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class JSONArrayImpl implements JSONArray {
41  
42      public JSONArrayImpl() {
43          _jsonArray = new org.json.JSONArray();
44      }
45  
46      public JSONArrayImpl(String json) throws JSONException {
47          try {
48              _jsonArray = new org.json.JSONArray(json);
49          }
50          catch (Exception e) {
51              throw new JSONException(e);
52          }
53      }
54  
55      public JSONArrayImpl(org.json.JSONArray jsonArray) {
56          _jsonArray = jsonArray;
57      }
58  
59      public boolean getBoolean(int index) {
60          return _jsonArray.optBoolean(index);
61      }
62  
63      public double getDouble(int index) {
64          return _jsonArray.optDouble(index);
65      }
66  
67      public int getInt(int index) {
68          return _jsonArray.optInt(index);
69      }
70  
71      public org.json.JSONArray getJSONArray() {
72          return _jsonArray;
73      }
74  
75      public JSONArray getJSONArray(int index) {
76          org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
77  
78          if (jsonArray == null) {
79              return null;
80          }
81  
82          return new JSONArrayImpl(jsonArray);
83      }
84  
85      public JSONObject getJSONObject(int index) {
86          org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
87  
88          if (jsonObj == null) {
89              return null;
90          }
91  
92          return new JSONObjectImpl(jsonObj);
93      }
94  
95      public long getLong(int index) {
96          return _jsonArray.optLong(index);
97      }
98  
99      public String getString(int index) {
100         return _jsonArray.optString(index);
101     }
102 
103     public boolean isNull(int index) {
104         return _jsonArray.isNull(index);
105     }
106 
107     public String join(String separator) throws JSONException {
108         try {
109             return _jsonArray.join(separator);
110         }
111         catch (Exception e) {
112             throw new JSONException(e);
113         }
114     }
115 
116     public int length() {
117         return _jsonArray.length();
118     }
119 
120     public JSONArray put(boolean value) {
121         _jsonArray.put(value);
122 
123         return this;
124     }
125 
126     public JSONArray put(double value) {
127         try {
128             _jsonArray.put(value);
129         }
130         catch (Exception e) {
131             if (_log.isWarnEnabled()) {
132                 _log.warn(e, e);
133             }
134         }
135 
136         return this;
137     }
138 
139     public JSONArray put(int value) {
140         _jsonArray.put(value);
141 
142         return this;
143     }
144 
145     public JSONArray put(long value) {
146         _jsonArray.put(value);
147 
148         return this;
149     }
150 
151     public JSONArray put(JSONArray value) {
152         _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
153 
154         return this;
155     }
156 
157     public JSONArray put(JSONObject value) {
158         _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
159 
160         return this;
161     }
162 
163     public JSONArray put(String value) {
164         _jsonArray.put(value);
165 
166         return this;
167     }
168 
169     public String toString() {
170         return _jsonArray.toString();
171     }
172 
173     public String toString(int indentFactor) throws JSONException {
174         try {
175             return _jsonArray.toString(indentFactor);
176         }
177         catch (Exception e) {
178             throw new JSONException(e);
179         }
180     }
181 
182     public Writer write(Writer writer) throws JSONException {
183         try {
184             return _jsonArray.write(writer);
185         }
186         catch (Exception e) {
187             throw new JSONException(e);
188         }
189     }
190 
191     private static Log _log = LogFactory.getLog(JSONArrayImpl.class);
192 
193     private org.json.JSONArray _jsonArray;
194 
195 }