1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.json;
16  
17  import com.liferay.portal.kernel.json.JSONArray;
18  import com.liferay.portal.kernel.json.JSONException;
19  import com.liferay.portal.kernel.json.JSONObject;
20  import com.liferay.portal.kernel.log.Log;
21  import com.liferay.portal.kernel.log.LogFactoryUtil;
22  
23  import java.io.Writer;
24  
25  /**
26   * <a href="JSONArrayImpl.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class JSONArrayImpl implements JSONArray {
31  
32      public JSONArrayImpl() {
33          _jsonArray = new org.json.JSONArray();
34      }
35  
36      public JSONArrayImpl(String json) throws JSONException {
37          try {
38              _jsonArray = new org.json.JSONArray(json);
39          }
40          catch (Exception e) {
41              throw new JSONException(e);
42          }
43      }
44  
45      public JSONArrayImpl(org.json.JSONArray jsonArray) {
46          _jsonArray = jsonArray;
47      }
48  
49      public boolean getBoolean(int index) {
50          return _jsonArray.optBoolean(index);
51      }
52  
53      public double getDouble(int index) {
54          return _jsonArray.optDouble(index);
55      }
56  
57      public int getInt(int index) {
58          return _jsonArray.optInt(index);
59      }
60  
61      public org.json.JSONArray getJSONArray() {
62          return _jsonArray;
63      }
64  
65      public JSONArray getJSONArray(int index) {
66          org.json.JSONArray jsonArray = _jsonArray.optJSONArray(index);
67  
68          if (jsonArray == null) {
69              return null;
70          }
71  
72          return new JSONArrayImpl(jsonArray);
73      }
74  
75      public JSONObject getJSONObject(int index) {
76          org.json.JSONObject jsonObj = _jsonArray.optJSONObject(index);
77  
78          if (jsonObj == null) {
79              return null;
80          }
81  
82          return new JSONObjectImpl(jsonObj);
83      }
84  
85      public long getLong(int index) {
86          return _jsonArray.optLong(index);
87      }
88  
89      public String getString(int index) {
90          return _jsonArray.optString(index);
91      }
92  
93      public boolean isNull(int index) {
94          return _jsonArray.isNull(index);
95      }
96  
97      public String join(String separator) throws JSONException {
98          try {
99              return _jsonArray.join(separator);
100         }
101         catch (Exception e) {
102             throw new JSONException(e);
103         }
104     }
105 
106     public int length() {
107         return _jsonArray.length();
108     }
109 
110     public JSONArray put(boolean value) {
111         _jsonArray.put(value);
112 
113         return this;
114     }
115 
116     public JSONArray put(double value) {
117         try {
118             _jsonArray.put(value);
119         }
120         catch (Exception e) {
121             if (_log.isWarnEnabled()) {
122                 _log.warn(e, e);
123             }
124         }
125 
126         return this;
127     }
128 
129     public JSONArray put(int value) {
130         _jsonArray.put(value);
131 
132         return this;
133     }
134 
135     public JSONArray put(long value) {
136         _jsonArray.put(value);
137 
138         return this;
139     }
140 
141     public JSONArray put(JSONArray value) {
142         _jsonArray.put(((JSONArrayImpl)value).getJSONArray());
143 
144         return this;
145     }
146 
147     public JSONArray put(JSONObject value) {
148         _jsonArray.put(((JSONObjectImpl)value).getJSONObject());
149 
150         return this;
151     }
152 
153     public JSONArray put(String value) {
154         _jsonArray.put(value);
155 
156         return this;
157     }
158 
159     public String toString() {
160         return _jsonArray.toString();
161     }
162 
163     public String toString(int indentFactor) throws JSONException {
164         try {
165             return _jsonArray.toString(indentFactor);
166         }
167         catch (Exception e) {
168             throw new JSONException(e);
169         }
170     }
171 
172     public Writer write(Writer writer) throws JSONException {
173         try {
174             return _jsonArray.write(writer);
175         }
176         catch (Exception e) {
177             throw new JSONException(e);
178         }
179     }
180 
181     private static Log _log = LogFactoryUtil.getLog(JSONArrayImpl.class);
182 
183     private org.json.JSONArray _jsonArray;
184 
185 }