1   /**
2    * Copyright (c) 2000-2009 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  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   */
39  public class JSONArrayImpl implements JSONArray {
40  
41      public JSONArrayImpl() {
42          _jsonArray = new org.json.JSONArray();
43      }
44  
45      public JSONArrayImpl(String json) throws JSONException {
46          try {
47              _jsonArray = new org.json.JSONArray(json);
48          }
49          catch (Exception e) {
50              throw new JSONException(e);
51          }
52      }
53  
54      public JSONArrayImpl(org.json.JSONArray jsonArray) {
55          _jsonArray = jsonArray;
56      }
57  
58      public boolean getBoolean(int index) {
59          return _jsonArray.optBoolean(index);
60      }
61  
62      public double getDouble(int index) {
63          return _jsonArray.optDouble(index);
64      }
65  
66      public int getInt(int index) {
67          return _jsonArray.optInt(index);
68      }
69  
70      public org.json.JSONArray getJSONArray() {
71          return _jsonArray;
72      }
73  
74      public JSONArray getJSONArray(int index) {
75          org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
76  
77          if (jsonArray == null) {
78              return null;
79          }
80  
81          return new JSONArrayImpl(jsonArray);
82      }
83  
84      public JSONObject getJSONObject(int index) {
85          org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
86  
87          if (jsonObj == null) {
88              return null;
89          }
90  
91          return new JSONObjectImpl(jsonObj);
92      }
93  
94      public long getLong(int index) {
95          return _jsonArray.optLong(index);
96      }
97  
98      public String getString(int index) {
99          return _jsonArray.optString(index);
100     }
101 
102     public boolean isNull(int index) {
103         return _jsonArray.isNull(index);
104     }
105 
106     public String join(String separator) throws JSONException {
107         try {
108             return _jsonArray.join(separator);
109         }
110         catch (Exception e) {
111             throw new JSONException(e);
112         }
113     }
114 
115     public int length() {
116         return _jsonArray.length();
117     }
118 
119     public JSONArray put(boolean value) {
120         _jsonArray.put(value);
121 
122         return this;
123     }
124 
125     public JSONArray put(double value) {
126         try {
127             _jsonArray.put(value);
128         }
129         catch (Exception e) {
130             if (_log.isWarnEnabled()) {
131                 _log.warn(e, e);
132             }
133         }
134 
135         return this;
136     }
137 
138     public JSONArray put(int value) {
139         _jsonArray.put(value);
140 
141         return this;
142     }
143 
144     public JSONArray put(long value) {
145         _jsonArray.put(value);
146 
147         return this;
148     }
149 
150     public JSONArray put(JSONArray value) {
151         _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
152 
153         return this;
154     }
155 
156     public JSONArray put(JSONObject value) {
157         _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
158 
159         return this;
160     }
161 
162     public JSONArray put(String value) {
163         _jsonArray.put(value);
164 
165         return this;
166     }
167 
168     public String toString() {
169         return _jsonArray.toString();
170     }
171 
172     public String toString(int indentFactor) throws JSONException {
173         try {
174             return _jsonArray.toString(indentFactor);
175         }
176         catch (Exception e) {
177             throw new JSONException(e);
178         }
179     }
180 
181     public Writer write(Writer writer) throws JSONException {
182         try {
183             return _jsonArray.write(writer);
184         }
185         catch (Exception e) {
186             throw new JSONException(e);
187         }
188     }
189 
190     private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
191 
192     private org.json.JSONArray _jsonArray;
193 
194 }