golf 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.gitignore +10 -0
  2. data/Gemfile +6 -0
  3. data/Gemfile.lock +21 -0
  4. data/Rakefile +12 -0
  5. data/bin/golf +8 -0
  6. data/golf-java/README.markdown +41 -0
  7. data/golf-java/THANKS.markdown +14 -0
  8. data/golf-java/build/classes.zip +0 -0
  9. data/golf-java/build/com/thinkminimo/golf/GolfResource$MimeMapping$MimeMappingSingleton.class +0 -0
  10. data/golf-java/build/com/thinkminimo/golf/GolfResource$MimeMapping.class +0 -0
  11. data/golf-java/build/com/thinkminimo/golf/GolfResource.class +0 -0
  12. data/golf-java/build/com/thinkminimo/golf/GolfServlet$1.class +0 -0
  13. data/golf-java/build/com/thinkminimo/golf/GolfServlet$GolfContext.class +0 -0
  14. data/golf-java/build/com/thinkminimo/golf/GolfServlet$GolfParams.class +0 -0
  15. data/golf-java/build/com/thinkminimo/golf/GolfServlet$GolfSession.class +0 -0
  16. data/golf-java/build/com/thinkminimo/golf/GolfServlet$PermanentRedirectException.class +0 -0
  17. data/golf-java/build/com/thinkminimo/golf/GolfServlet$RedirectException.class +0 -0
  18. data/golf-java/build/com/thinkminimo/golf/GolfServlet$StoredJSVM.class +0 -0
  19. data/golf-java/build/com/thinkminimo/golf/GolfServlet.class +0 -0
  20. data/golf-java/build/com/thinkminimo/golf/JsonpTunnel.class +0 -0
  21. data/golf-java/build/com/thinkminimo/golf/Main$1.class +0 -0
  22. data/golf-java/build/com/thinkminimo/golf/Main$RingList.class +0 -0
  23. data/golf-java/build/com/thinkminimo/golf/Main.class +0 -0
  24. data/golf-java/build/com/thinkminimo/golf/ProxyServlet.class +0 -0
  25. data/golf-java/build/com/yahoo/platform/yui/compressor/CssCompressor.class +0 -0
  26. data/golf-java/build/com/yahoo/platform/yui/compressor/JarClassLoader.class +0 -0
  27. data/golf-java/build/com/yahoo/platform/yui/compressor/JavaScriptCompressor.class +0 -0
  28. data/golf-java/build/com/yahoo/platform/yui/compressor/JavaScriptIdentifier.class +0 -0
  29. data/golf-java/build/com/yahoo/platform/yui/compressor/JavaScriptToken.class +0 -0
  30. data/golf-java/build/com/yahoo/platform/yui/compressor/ScriptOrFnScope.class +0 -0
  31. data/golf-java/build/depends.zip +0 -0
  32. data/golf-java/build/golf +2 -0
  33. data/golf-java/build/org/json/JSONArray.class +0 -0
  34. data/golf-java/build/org/json/JSONException.class +0 -0
  35. data/golf-java/build/org/json/JSONObject$1.class +0 -0
  36. data/golf-java/build/org/json/JSONObject$Null.class +0 -0
  37. data/golf-java/build/org/json/JSONObject.class +0 -0
  38. data/golf-java/build/org/json/JSONString.class +0 -0
  39. data/golf-java/build/org/json/JSONStringer.class +0 -0
  40. data/golf-java/build/org/json/JSONTokener.class +0 -0
  41. data/golf-java/build/org/json/JSONWriter.class +0 -0
  42. data/golf-java/build/resources.zip +0 -0
  43. data/golf-java/build.xml +174 -0
  44. data/golf-java/dist/golf.zip +0 -0
  45. data/golf-java/lib/ant-launcher.jar +0 -0
  46. data/golf-java/lib/ant.jar +0 -0
  47. data/golf-java/lib/commons-codec-1.4.jar +0 -0
  48. data/golf-java/lib/commons-collections-3.2.1.jar +0 -0
  49. data/golf-java/lib/commons-fileupload-1.2.1.jar +0 -0
  50. data/golf-java/lib/commons-httpclient-3.1.jar +0 -0
  51. data/golf-java/lib/commons-io-1.4.jar +0 -0
  52. data/golf-java/lib/commons-lang-2.4.jar +0 -0
  53. data/golf-java/lib/commons-logging-1.1.1.jar +0 -0
  54. data/golf-java/lib/cssparser-0.9.5.jar +0 -0
  55. data/golf-java/lib/getopt-0.1-dev.jar +0 -0
  56. data/golf-java/lib/htmlunit-2.6.jar +0 -0
  57. data/golf-java/lib/htmlunit-core-js-2.6.jar +0 -0
  58. data/golf-java/lib/java-xmlbuilder-1.jar +0 -0
  59. data/golf-java/lib/jets3t-0.7.0.jar +0 -0
  60. data/golf-java/lib/jetty-6.1.15.jar +0 -0
  61. data/golf-java/lib/jetty-util-6.1.15.jar +0 -0
  62. data/golf-java/lib/log4j-1.2.15.jar +0 -0
  63. data/golf-java/lib/nekohtml-1.9.13.jar +0 -0
  64. data/golf-java/lib/sac-1.3.jar +0 -0
  65. data/golf-java/lib/serializer-2.7.1.jar +0 -0
  66. data/golf-java/lib/servlet-api-2.5-20081211.jar +0 -0
  67. data/golf-java/lib/xalan-2.7.1.jar +0 -0
  68. data/golf-java/lib/xercesImpl-2.9.1.jar +0 -0
  69. data/golf-java/lib/xml-apis-1.3.04.jar +0 -0
  70. data/golf-java/resources/app_error.html +60 -0
  71. data/golf-java/resources/error.html +29 -0
  72. data/golf-java/resources/forcebot.txt +0 -0
  73. data/golf-java/resources/forceclient.txt +0 -0
  74. data/golf-java/resources/forceproxy.txt +0 -0
  75. data/golf-java/resources/head.html +0 -0
  76. data/golf-java/resources/jquery.address.js +439 -0
  77. data/golf-java/resources/jquery.golf.js +945 -0
  78. data/golf-java/resources/jquery.js +4376 -0
  79. data/golf-java/resources/jsdetect.html +23 -0
  80. data/golf-java/resources/loading.gif +0 -0
  81. data/golf-java/resources/new.html +45 -0
  82. data/golf-java/resources/new.static.html +45 -0
  83. data/golf-java/resources/noscript.forceclient.html +11 -0
  84. data/golf-java/resources/noscript.html +18 -0
  85. data/golf-java/resources/noscript.static.html +15 -0
  86. data/golf-java/resources/project.xml +21 -0
  87. data/golf-java/resources/proxy_project.xml +11 -0
  88. data/golf-java/resources/proxy_web.xml +40 -0
  89. data/golf-java/resources/static_project.xml +37 -0
  90. data/golf-java/resources/version +1 -0
  91. data/golf-java/resources/web.xml +36 -0
  92. data/golf-java/resources/welcome2golf.html +50 -0
  93. data/golf-java/src/com/thinkminimo/golf/GolfResource.java +582 -0
  94. data/golf-java/src/com/thinkminimo/golf/GolfServlet.java +1055 -0
  95. data/golf-java/src/com/thinkminimo/golf/JsonpTunnel.java +86 -0
  96. data/golf-java/src/com/thinkminimo/golf/Main.java +1299 -0
  97. data/golf-java/src/com/thinkminimo/golf/ProxyServlet.java +577 -0
  98. data/golf-java/src/com/yahoo/platform/yui/compressor/CssCompressor.java +188 -0
  99. data/golf-java/src/com/yahoo/platform/yui/compressor/JarClassLoader.java +158 -0
  100. data/golf-java/src/com/yahoo/platform/yui/compressor/JavaScriptCompressor.java +1281 -0
  101. data/golf-java/src/com/yahoo/platform/yui/compressor/JavaScriptIdentifier.java +55 -0
  102. data/golf-java/src/com/yahoo/platform/yui/compressor/JavaScriptToken.java +28 -0
  103. data/golf-java/src/com/yahoo/platform/yui/compressor/ScriptOrFnScope.java +160 -0
  104. data/golf-java/src/org/json/JSONArray.java +934 -0
  105. data/golf-java/src/org/json/JSONException.java +27 -0
  106. data/golf-java/src/org/json/JSONObject.java +1550 -0
  107. data/golf-java/src/org/json/JSONString.java +18 -0
  108. data/golf-java/src/org/json/JSONStringer.java +78 -0
  109. data/golf-java/src/org/json/JSONTokener.java +422 -0
  110. data/golf-java/src/org/json/JSONWriter.java +323 -0
  111. data/golf-java/test/client/golftest/README.markdown +3 -0
  112. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.css +114 -0
  113. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.html +17 -0
  114. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.js +81 -0
  115. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.res/asc.gif +0 -0
  116. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.res/bg.gif +0 -0
  117. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.res/desc.gif +0 -0
  118. data/golf-java/test/client/golftest/components/com/thinkminimo/golf/test/Harness.res/test/test.html +1 -0
  119. data/golf-java/test/client/golftest/controller.js +131 -0
  120. data/golf-java/test/client/golftest/forceclient.txt +0 -0
  121. data/golf-java/test/client/golftest/forceproxy.txt +0 -0
  122. data/golf-java/test/client/golftest/head.html +1 -0
  123. data/golf-java/test/client/golftest/scripts/jquery.tablesort.js +75 -0
  124. data/golf-java/test/client/golftest/styles/style.css +18 -0
  125. data/golf-java/test/client/golftest/test/test.html +1 -0
  126. data/golf.gemspec +24 -0
  127. data/lib/golf/cli.rb +32 -0
  128. data/lib/golf/compiler.rb +12 -0
  129. data/lib/golf/rack.rb +17 -0
  130. data/lib/golf/version.rb +3 -0
  131. data/lib/golf.rb +6 -0
  132. data/template/Gemfile +5 -0
  133. data/template/config.ru +7 -0
  134. data/template/golfapp/components/Test/Test.html +22 -0
  135. data/template/golfapp/components/Test/Test.res/myfile.txt +1 -0
  136. data/template/golfapp/controller.js +10 -0
  137. data/template/golfapp/plugins/mylib.js +12 -0
  138. data/test/test_compiler.rb +10 -0
  139. data/test/test_helper.rb +6 -0
  140. data/test/test_rack.rb +17 -0
  141. metadata +208 -0
@@ -0,0 +1,934 @@
1
+ package org.json;
2
+
3
+ /*
4
+ Copyright (c) 2002 JSON.org
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ The Software shall be used for Good, not Evil.
17
+
18
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24
+ SOFTWARE.
25
+ */
26
+
27
+ import java.io.IOException;
28
+ import java.io.Writer;
29
+ import java.lang.reflect.Array;
30
+ import java.util.ArrayList;
31
+ import java.util.Collection;
32
+ import java.util.Iterator;
33
+ import java.util.Map;
34
+
35
+ /**
36
+ * A JSONArray is an ordered sequence of values. Its external text form is a
37
+ * string wrapped in square brackets with commas separating the values. The
38
+ * internal form is an object having <code>get</code> and <code>opt</code>
39
+ * methods for accessing the values by index, and <code>put</code> methods for
40
+ * adding or replacing values. The values can be any of these types:
41
+ * <code>Boolean</code>, <code>JSONArray</code>, <code>JSONObject</code>,
42
+ * <code>Number</code>, <code>String</code>, or the
43
+ * <code>JSONObject.NULL object</code>.
44
+ * <p>
45
+ * The constructor can convert a JSON text into a Java object. The
46
+ * <code>toString</code> method converts to JSON text.
47
+ * <p>
48
+ * A <code>get</code> method returns a value if one can be found, and throws an
49
+ * exception if one cannot be found. An <code>opt</code> method returns a
50
+ * default value instead of throwing an exception, and so is useful for
51
+ * obtaining optional values.
52
+ * <p>
53
+ * The generic <code>get()</code> and <code>opt()</code> methods return an
54
+ * object which you can cast or query for type. There are also typed
55
+ * <code>get</code> and <code>opt</code> methods that do type checking and type
56
+ * coercion for you.
57
+ * <p>
58
+ * The texts produced by the <code>toString</code> methods strictly conform to
59
+ * JSON syntax rules. The constructors are more forgiving in the texts they will
60
+ * accept:
61
+ * <ul>
62
+ * <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
63
+ * before the closing bracket.</li>
64
+ * <li>The <code>null</code> value will be inserted when there
65
+ * is <code>,</code>&nbsp;<small>(comma)</small> elision.</li>
66
+ * <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
67
+ * quote)</small>.</li>
68
+ * <li>Strings do not need to be quoted at all if they do not begin with a quote
69
+ * or single quote, and if they do not contain leading or trailing spaces,
70
+ * and if they do not contain any of these characters:
71
+ * <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
72
+ * and if they are not the reserved words <code>true</code>,
73
+ * <code>false</code>, or <code>null</code>.</li>
74
+ * <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
75
+ * well as by <code>,</code> <small>(comma)</small>.</li>
76
+ * <li>Numbers may have the <code>0-</code> <small>(octal)</small> or
77
+ * <code>0x-</code> <small>(hex)</small> prefix.</li>
78
+ * </ul>
79
+
80
+ * @author JSON.org
81
+ * @version 2008-09-18
82
+ */
83
+ public class JSONArray {
84
+
85
+
86
+ /**
87
+ * The arrayList where the JSONArray's properties are kept.
88
+ */
89
+ private ArrayList myArrayList;
90
+
91
+
92
+ /**
93
+ * Construct an empty JSONArray.
94
+ */
95
+ public JSONArray() {
96
+ this.myArrayList = new ArrayList();
97
+ }
98
+
99
+ /**
100
+ * Construct a JSONArray from a JSONTokener.
101
+ * @param x A JSONTokener
102
+ * @throws JSONException If there is a syntax error.
103
+ */
104
+ public JSONArray(JSONTokener x) throws JSONException {
105
+ this();
106
+ char c = x.nextClean();
107
+ char q;
108
+ if (c == '[') {
109
+ q = ']';
110
+ } else if (c == '(') {
111
+ q = ')';
112
+ } else {
113
+ throw x.syntaxError("A JSONArray text must start with '['");
114
+ }
115
+ if (x.nextClean() == ']') {
116
+ return;
117
+ }
118
+ x.back();
119
+ for (;;) {
120
+ if (x.nextClean() == ',') {
121
+ x.back();
122
+ this.myArrayList.add(null);
123
+ } else {
124
+ x.back();
125
+ this.myArrayList.add(x.nextValue());
126
+ }
127
+ c = x.nextClean();
128
+ switch (c) {
129
+ case ';':
130
+ case ',':
131
+ if (x.nextClean() == ']') {
132
+ return;
133
+ }
134
+ x.back();
135
+ break;
136
+ case ']':
137
+ case ')':
138
+ if (q != c) {
139
+ throw x.syntaxError("Expected a '" + new Character(q) + "'");
140
+ }
141
+ return;
142
+ default:
143
+ throw x.syntaxError("Expected a ',' or ']'");
144
+ }
145
+ }
146
+ }
147
+
148
+
149
+ /**
150
+ * Construct a JSONArray from a source JSON text.
151
+ * @param source A string that begins with
152
+ * <code>[</code>&nbsp;<small>(left bracket)</small>
153
+ * and ends with <code>]</code>&nbsp;<small>(right bracket)</small>.
154
+ * @throws JSONException If there is a syntax error.
155
+ */
156
+ public JSONArray(String source) throws JSONException {
157
+ this(new JSONTokener(source));
158
+ }
159
+
160
+
161
+ /**
162
+ * Construct a JSONArray from a Collection.
163
+ * @param collection A Collection.
164
+ */
165
+ public JSONArray(Collection collection) {
166
+ this.myArrayList = (collection == null) ?
167
+ new ArrayList() :
168
+ new ArrayList(collection);
169
+ }
170
+
171
+ /**
172
+ * Construct a JSONArray from a collection of beans.
173
+ * The collection should have Java Beans.
174
+ *
175
+ * @throws JSONException If not an array.
176
+ */
177
+
178
+ public JSONArray(Collection collection,boolean includeSuperClass) {
179
+ this.myArrayList = new ArrayList();
180
+ if(collection != null) {
181
+ for (Iterator iter = collection.iterator(); iter.hasNext();) {
182
+ this.myArrayList.add(new JSONObject(iter.next(),includeSuperClass));
183
+ }
184
+ }
185
+ }
186
+
187
+
188
+ /**
189
+ * Construct a JSONArray from an array
190
+ * @throws JSONException If not an array.
191
+ */
192
+ public JSONArray(Object array) throws JSONException {
193
+ this();
194
+ if (array.getClass().isArray()) {
195
+ int length = Array.getLength(array);
196
+ for (int i = 0; i < length; i += 1) {
197
+ this.put(Array.get(array, i));
198
+ }
199
+ } else {
200
+ throw new JSONException("JSONArray initial value should be a string or collection or array.");
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Construct a JSONArray from an array with a bean.
206
+ * The array should have Java Beans.
207
+ *
208
+ * @throws JSONException If not an array.
209
+ */
210
+ public JSONArray(Object array,boolean includeSuperClass) throws JSONException {
211
+ this();
212
+ if (array.getClass().isArray()) {
213
+ int length = Array.getLength(array);
214
+ for (int i = 0; i < length; i += 1) {
215
+ this.put(new JSONObject(Array.get(array, i),includeSuperClass));
216
+ }
217
+ } else {
218
+ throw new JSONException("JSONArray initial value should be a string or collection or array.");
219
+ }
220
+ }
221
+
222
+
223
+
224
+ /**
225
+ * Get the object value associated with an index.
226
+ * @param index
227
+ * The index must be between 0 and length() - 1.
228
+ * @return An object value.
229
+ * @throws JSONException If there is no value for the index.
230
+ */
231
+ public Object get(int index) throws JSONException {
232
+ Object o = opt(index);
233
+ if (o == null) {
234
+ throw new JSONException("JSONArray[" + index + "] not found.");
235
+ }
236
+ return o;
237
+ }
238
+
239
+
240
+ /**
241
+ * Get the boolean value associated with an index.
242
+ * The string values "true" and "false" are converted to boolean.
243
+ *
244
+ * @param index The index must be between 0 and length() - 1.
245
+ * @return The truth.
246
+ * @throws JSONException If there is no value for the index or if the
247
+ * value is not convertable to boolean.
248
+ */
249
+ public boolean getBoolean(int index) throws JSONException {
250
+ Object o = get(index);
251
+ if (o.equals(Boolean.FALSE) ||
252
+ (o instanceof String &&
253
+ ((String)o).equalsIgnoreCase("false"))) {
254
+ return false;
255
+ } else if (o.equals(Boolean.TRUE) ||
256
+ (o instanceof String &&
257
+ ((String)o).equalsIgnoreCase("true"))) {
258
+ return true;
259
+ }
260
+ throw new JSONException("JSONArray[" + index + "] is not a Boolean.");
261
+ }
262
+
263
+
264
+ /**
265
+ * Get the double value associated with an index.
266
+ *
267
+ * @param index The index must be between 0 and length() - 1.
268
+ * @return The value.
269
+ * @throws JSONException If the key is not found or if the value cannot
270
+ * be converted to a number.
271
+ */
272
+ public double getDouble(int index) throws JSONException {
273
+ Object o = get(index);
274
+ try {
275
+ return o instanceof Number ?
276
+ ((Number)o).doubleValue() :
277
+ Double.valueOf((String)o).doubleValue();
278
+ } catch (Exception e) {
279
+ throw new JSONException("JSONArray[" + index +
280
+ "] is not a number.");
281
+ }
282
+ }
283
+
284
+
285
+ /**
286
+ * Get the int value associated with an index.
287
+ *
288
+ * @param index The index must be between 0 and length() - 1.
289
+ * @return The value.
290
+ * @throws JSONException If the key is not found or if the value cannot
291
+ * be converted to a number.
292
+ * if the value cannot be converted to a number.
293
+ */
294
+ public int getInt(int index) throws JSONException {
295
+ Object o = get(index);
296
+ return o instanceof Number ?
297
+ ((Number)o).intValue() : (int)getDouble(index);
298
+ }
299
+
300
+
301
+ /**
302
+ * Get the JSONArray associated with an index.
303
+ * @param index The index must be between 0 and length() - 1.
304
+ * @return A JSONArray value.
305
+ * @throws JSONException If there is no value for the index. or if the
306
+ * value is not a JSONArray
307
+ */
308
+ public JSONArray getJSONArray(int index) throws JSONException {
309
+ Object o = get(index);
310
+ if (o instanceof JSONArray) {
311
+ return (JSONArray)o;
312
+ }
313
+ throw new JSONException("JSONArray[" + index +
314
+ "] is not a JSONArray.");
315
+ }
316
+
317
+
318
+ /**
319
+ * Get the JSONObject associated with an index.
320
+ * @param index subscript
321
+ * @return A JSONObject value.
322
+ * @throws JSONException If there is no value for the index or if the
323
+ * value is not a JSONObject
324
+ */
325
+ public JSONObject getJSONObject(int index) throws JSONException {
326
+ Object o = get(index);
327
+ if (o instanceof JSONObject) {
328
+ return (JSONObject)o;
329
+ }
330
+ throw new JSONException("JSONArray[" + index +
331
+ "] is not a JSONObject.");
332
+ }
333
+
334
+
335
+ /**
336
+ * Get the long value associated with an index.
337
+ *
338
+ * @param index The index must be between 0 and length() - 1.
339
+ * @return The value.
340
+ * @throws JSONException If the key is not found or if the value cannot
341
+ * be converted to a number.
342
+ */
343
+ public long getLong(int index) throws JSONException {
344
+ Object o = get(index);
345
+ return o instanceof Number ?
346
+ ((Number)o).longValue() : (long)getDouble(index);
347
+ }
348
+
349
+
350
+ /**
351
+ * Get the string associated with an index.
352
+ * @param index The index must be between 0 and length() - 1.
353
+ * @return A string value.
354
+ * @throws JSONException If there is no value for the index.
355
+ */
356
+ public String getString(int index) throws JSONException {
357
+ return get(index).toString();
358
+ }
359
+
360
+
361
+ /**
362
+ * Determine if the value is null.
363
+ * @param index The index must be between 0 and length() - 1.
364
+ * @return true if the value at the index is null, or if there is no value.
365
+ */
366
+ public boolean isNull(int index) {
367
+ return JSONObject.NULL.equals(opt(index));
368
+ }
369
+
370
+
371
+ /**
372
+ * Make a string from the contents of this JSONArray. The
373
+ * <code>separator</code> string is inserted between each element.
374
+ * Warning: This method assumes that the data structure is acyclical.
375
+ * @param separator A string that will be inserted between the elements.
376
+ * @return a string.
377
+ * @throws JSONException If the array contains an invalid number.
378
+ */
379
+ public String join(String separator) throws JSONException {
380
+ int len = length();
381
+ StringBuffer sb = new StringBuffer();
382
+
383
+ for (int i = 0; i < len; i += 1) {
384
+ if (i > 0) {
385
+ sb.append(separator);
386
+ }
387
+ sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
388
+ }
389
+ return sb.toString();
390
+ }
391
+
392
+
393
+ /**
394
+ * Get the number of elements in the JSONArray, included nulls.
395
+ *
396
+ * @return The length (or size).
397
+ */
398
+ public int length() {
399
+ return this.myArrayList.size();
400
+ }
401
+
402
+
403
+ /**
404
+ * Get the optional object value associated with an index.
405
+ * @param index The index must be between 0 and length() - 1.
406
+ * @return An object value, or null if there is no
407
+ * object at that index.
408
+ */
409
+ public Object opt(int index) {
410
+ return (index < 0 || index >= length()) ?
411
+ null : this.myArrayList.get(index);
412
+ }
413
+
414
+
415
+ /**
416
+ * Get the optional boolean value associated with an index.
417
+ * It returns false if there is no value at that index,
418
+ * or if the value is not Boolean.TRUE or the String "true".
419
+ *
420
+ * @param index The index must be between 0 and length() - 1.
421
+ * @return The truth.
422
+ */
423
+ public boolean optBoolean(int index) {
424
+ return optBoolean(index, false);
425
+ }
426
+
427
+
428
+ /**
429
+ * Get the optional boolean value associated with an index.
430
+ * It returns the defaultValue if there is no value at that index or if
431
+ * it is not a Boolean or the String "true" or "false" (case insensitive).
432
+ *
433
+ * @param index The index must be between 0 and length() - 1.
434
+ * @param defaultValue A boolean default.
435
+ * @return The truth.
436
+ */
437
+ public boolean optBoolean(int index, boolean defaultValue) {
438
+ try {
439
+ return getBoolean(index);
440
+ } catch (Exception e) {
441
+ return defaultValue;
442
+ }
443
+ }
444
+
445
+
446
+ /**
447
+ * Get the optional double value associated with an index.
448
+ * NaN is returned if there is no value for the index,
449
+ * or if the value is not a number and cannot be converted to a number.
450
+ *
451
+ * @param index The index must be between 0 and length() - 1.
452
+ * @return The value.
453
+ */
454
+ public double optDouble(int index) {
455
+ return optDouble(index, Double.NaN);
456
+ }
457
+
458
+
459
+ /**
460
+ * Get the optional double value associated with an index.
461
+ * The defaultValue is returned if there is no value for the index,
462
+ * or if the value is not a number and cannot be converted to a number.
463
+ *
464
+ * @param index subscript
465
+ * @param defaultValue The default value.
466
+ * @return The value.
467
+ */
468
+ public double optDouble(int index, double defaultValue) {
469
+ try {
470
+ return getDouble(index);
471
+ } catch (Exception e) {
472
+ return defaultValue;
473
+ }
474
+ }
475
+
476
+
477
+ /**
478
+ * Get the optional int value associated with an index.
479
+ * Zero is returned if there is no value for the index,
480
+ * or if the value is not a number and cannot be converted to a number.
481
+ *
482
+ * @param index The index must be between 0 and length() - 1.
483
+ * @return The value.
484
+ */
485
+ public int optInt(int index) {
486
+ return optInt(index, 0);
487
+ }
488
+
489
+
490
+ /**
491
+ * Get the optional int value associated with an index.
492
+ * The defaultValue is returned if there is no value for the index,
493
+ * or if the value is not a number and cannot be converted to a number.
494
+ * @param index The index must be between 0 and length() - 1.
495
+ * @param defaultValue The default value.
496
+ * @return The value.
497
+ */
498
+ public int optInt(int index, int defaultValue) {
499
+ try {
500
+ return getInt(index);
501
+ } catch (Exception e) {
502
+ return defaultValue;
503
+ }
504
+ }
505
+
506
+
507
+ /**
508
+ * Get the optional JSONArray associated with an index.
509
+ * @param index subscript
510
+ * @return A JSONArray value, or null if the index has no value,
511
+ * or if the value is not a JSONArray.
512
+ */
513
+ public JSONArray optJSONArray(int index) {
514
+ Object o = opt(index);
515
+ return o instanceof JSONArray ? (JSONArray)o : null;
516
+ }
517
+
518
+
519
+ /**
520
+ * Get the optional JSONObject associated with an index.
521
+ * Null is returned if the key is not found, or null if the index has
522
+ * no value, or if the value is not a JSONObject.
523
+ *
524
+ * @param index The index must be between 0 and length() - 1.
525
+ * @return A JSONObject value.
526
+ */
527
+ public JSONObject optJSONObject(int index) {
528
+ Object o = opt(index);
529
+ return o instanceof JSONObject ? (JSONObject)o : null;
530
+ }
531
+
532
+
533
+ /**
534
+ * Get the optional long value associated with an index.
535
+ * Zero is returned if there is no value for the index,
536
+ * or if the value is not a number and cannot be converted to a number.
537
+ *
538
+ * @param index The index must be between 0 and length() - 1.
539
+ * @return The value.
540
+ */
541
+ public long optLong(int index) {
542
+ return optLong(index, 0);
543
+ }
544
+
545
+
546
+ /**
547
+ * Get the optional long value associated with an index.
548
+ * The defaultValue is returned if there is no value for the index,
549
+ * or if the value is not a number and cannot be converted to a number.
550
+ * @param index The index must be between 0 and length() - 1.
551
+ * @param defaultValue The default value.
552
+ * @return The value.
553
+ */
554
+ public long optLong(int index, long defaultValue) {
555
+ try {
556
+ return getLong(index);
557
+ } catch (Exception e) {
558
+ return defaultValue;
559
+ }
560
+ }
561
+
562
+
563
+ /**
564
+ * Get the optional string value associated with an index. It returns an
565
+ * empty string if there is no value at that index. If the value
566
+ * is not a string and is not null, then it is coverted to a string.
567
+ *
568
+ * @param index The index must be between 0 and length() - 1.
569
+ * @return A String value.
570
+ */
571
+ public String optString(int index) {
572
+ return optString(index, "");
573
+ }
574
+
575
+
576
+ /**
577
+ * Get the optional string associated with an index.
578
+ * The defaultValue is returned if the key is not found.
579
+ *
580
+ * @param index The index must be between 0 and length() - 1.
581
+ * @param defaultValue The default value.
582
+ * @return A String value.
583
+ */
584
+ public String optString(int index, String defaultValue) {
585
+ Object o = opt(index);
586
+ return o != null ? o.toString() : defaultValue;
587
+ }
588
+
589
+
590
+ /**
591
+ * Append a boolean value. This increases the array's length by one.
592
+ *
593
+ * @param value A boolean value.
594
+ * @return this.
595
+ */
596
+ public JSONArray put(boolean value) {
597
+ put(value ? Boolean.TRUE : Boolean.FALSE);
598
+ return this;
599
+ }
600
+
601
+
602
+ /**
603
+ * Put a value in the JSONArray, where the value will be a
604
+ * JSONArray which is produced from a Collection.
605
+ * @param value A Collection value.
606
+ * @return this.
607
+ */
608
+ public JSONArray put(Collection value) {
609
+ put(new JSONArray(value));
610
+ return this;
611
+ }
612
+
613
+
614
+ /**
615
+ * Append a double value. This increases the array's length by one.
616
+ *
617
+ * @param value A double value.
618
+ * @throws JSONException if the value is not finite.
619
+ * @return this.
620
+ */
621
+ public JSONArray put(double value) throws JSONException {
622
+ Double d = new Double(value);
623
+ JSONObject.testValidity(d);
624
+ put(d);
625
+ return this;
626
+ }
627
+
628
+
629
+ /**
630
+ * Append an int value. This increases the array's length by one.
631
+ *
632
+ * @param value An int value.
633
+ * @return this.
634
+ */
635
+ public JSONArray put(int value) {
636
+ put(new Integer(value));
637
+ return this;
638
+ }
639
+
640
+
641
+ /**
642
+ * Append an long value. This increases the array's length by one.
643
+ *
644
+ * @param value A long value.
645
+ * @return this.
646
+ */
647
+ public JSONArray put(long value) {
648
+ put(new Long(value));
649
+ return this;
650
+ }
651
+
652
+
653
+ /**
654
+ * Put a value in the JSONArray, where the value will be a
655
+ * JSONObject which is produced from a Map.
656
+ * @param value A Map value.
657
+ * @return this.
658
+ */
659
+ public JSONArray put(Map value) {
660
+ put(new JSONObject(value));
661
+ return this;
662
+ }
663
+
664
+
665
+ /**
666
+ * Append an object value. This increases the array's length by one.
667
+ * @param value An object value. The value should be a
668
+ * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
669
+ * JSONObject.NULL object.
670
+ * @return this.
671
+ */
672
+ public JSONArray put(Object value) {
673
+ this.myArrayList.add(value);
674
+ return this;
675
+ }
676
+
677
+
678
+ /**
679
+ * Put or replace a boolean value in the JSONArray. If the index is greater
680
+ * than the length of the JSONArray, then null elements will be added as
681
+ * necessary to pad it out.
682
+ * @param index The subscript.
683
+ * @param value A boolean value.
684
+ * @return this.
685
+ * @throws JSONException If the index is negative.
686
+ */
687
+ public JSONArray put(int index, boolean value) throws JSONException {
688
+ put(index, value ? Boolean.TRUE : Boolean.FALSE);
689
+ return this;
690
+ }
691
+
692
+
693
+ /**
694
+ * Put a value in the JSONArray, where the value will be a
695
+ * JSONArray which is produced from a Collection.
696
+ * @param index The subscript.
697
+ * @param value A Collection value.
698
+ * @return this.
699
+ * @throws JSONException If the index is negative or if the value is
700
+ * not finite.
701
+ */
702
+ public JSONArray put(int index, Collection value) throws JSONException {
703
+ put(index, new JSONArray(value));
704
+ return this;
705
+ }
706
+
707
+
708
+ /**
709
+ * Put or replace a double value. If the index is greater than the length of
710
+ * the JSONArray, then null elements will be added as necessary to pad
711
+ * it out.
712
+ * @param index The subscript.
713
+ * @param value A double value.
714
+ * @return this.
715
+ * @throws JSONException If the index is negative or if the value is
716
+ * not finite.
717
+ */
718
+ public JSONArray put(int index, double value) throws JSONException {
719
+ put(index, new Double(value));
720
+ return this;
721
+ }
722
+
723
+
724
+ /**
725
+ * Put or replace an int value. If the index is greater than the length of
726
+ * the JSONArray, then null elements will be added as necessary to pad
727
+ * it out.
728
+ * @param index The subscript.
729
+ * @param value An int value.
730
+ * @return this.
731
+ * @throws JSONException If the index is negative.
732
+ */
733
+ public JSONArray put(int index, int value) throws JSONException {
734
+ put(index, new Integer(value));
735
+ return this;
736
+ }
737
+
738
+
739
+ /**
740
+ * Put or replace a long value. If the index is greater than the length of
741
+ * the JSONArray, then null elements will be added as necessary to pad
742
+ * it out.
743
+ * @param index The subscript.
744
+ * @param value A long value.
745
+ * @return this.
746
+ * @throws JSONException If the index is negative.
747
+ */
748
+ public JSONArray put(int index, long value) throws JSONException {
749
+ put(index, new Long(value));
750
+ return this;
751
+ }
752
+
753
+
754
+ /**
755
+ * Put a value in the JSONArray, where the value will be a
756
+ * JSONObject which is produced from a Map.
757
+ * @param index The subscript.
758
+ * @param value The Map value.
759
+ * @return this.
760
+ * @throws JSONException If the index is negative or if the the value is
761
+ * an invalid number.
762
+ */
763
+ public JSONArray put(int index, Map value) throws JSONException {
764
+ put(index, new JSONObject(value));
765
+ return this;
766
+ }
767
+
768
+
769
+ /**
770
+ * Put or replace an object value in the JSONArray. If the index is greater
771
+ * than the length of the JSONArray, then null elements will be added as
772
+ * necessary to pad it out.
773
+ * @param index The subscript.
774
+ * @param value The value to put into the array. The value should be a
775
+ * Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the
776
+ * JSONObject.NULL object.
777
+ * @return this.
778
+ * @throws JSONException If the index is negative or if the the value is
779
+ * an invalid number.
780
+ */
781
+ public JSONArray put(int index, Object value) throws JSONException {
782
+ JSONObject.testValidity(value);
783
+ if (index < 0) {
784
+ throw new JSONException("JSONArray[" + index + "] not found.");
785
+ }
786
+ if (index < length()) {
787
+ this.myArrayList.set(index, value);
788
+ } else {
789
+ while (index != length()) {
790
+ put(JSONObject.NULL);
791
+ }
792
+ put(value);
793
+ }
794
+ return this;
795
+ }
796
+
797
+
798
+ /**
799
+ * Produce a JSONObject by combining a JSONArray of names with the values
800
+ * of this JSONArray.
801
+ * @param names A JSONArray containing a list of key strings. These will be
802
+ * paired with the values.
803
+ * @return A JSONObject, or null if there are no names or if this JSONArray
804
+ * has no values.
805
+ * @throws JSONException If any of the names are null.
806
+ */
807
+ public JSONObject toJSONObject(JSONArray names) throws JSONException {
808
+ if (names == null || names.length() == 0 || length() == 0) {
809
+ return null;
810
+ }
811
+ JSONObject jo = new JSONObject();
812
+ for (int i = 0; i < names.length(); i += 1) {
813
+ jo.put(names.getString(i), this.opt(i));
814
+ }
815
+ return jo;
816
+ }
817
+
818
+
819
+ /**
820
+ * Make a JSON text of this JSONArray. For compactness, no
821
+ * unnecessary whitespace is added. If it is not possible to produce a
822
+ * syntactically correct JSON text then null will be returned instead. This
823
+ * could occur if the array contains an invalid number.
824
+ * <p>
825
+ * Warning: This method assumes that the data structure is acyclical.
826
+ *
827
+ * @return a printable, displayable, transmittable
828
+ * representation of the array.
829
+ */
830
+ public String toString() {
831
+ try {
832
+ return '[' + join(",") + ']';
833
+ } catch (Exception e) {
834
+ return null;
835
+ }
836
+ }
837
+
838
+
839
+ /**
840
+ * Make a prettyprinted JSON text of this JSONArray.
841
+ * Warning: This method assumes that the data structure is acyclical.
842
+ * @param indentFactor The number of spaces to add to each level of
843
+ * indentation.
844
+ * @return a printable, displayable, transmittable
845
+ * representation of the object, beginning
846
+ * with <code>[</code>&nbsp;<small>(left bracket)</small> and ending
847
+ * with <code>]</code>&nbsp;<small>(right bracket)</small>.
848
+ * @throws JSONException
849
+ */
850
+ public String toString(int indentFactor) throws JSONException {
851
+ return toString(indentFactor, 0);
852
+ }
853
+
854
+
855
+ /**
856
+ * Make a prettyprinted JSON text of this JSONArray.
857
+ * Warning: This method assumes that the data structure is acyclical.
858
+ * @param indentFactor The number of spaces to add to each level of
859
+ * indentation.
860
+ * @param indent The indention of the top level.
861
+ * @return a printable, displayable, transmittable
862
+ * representation of the array.
863
+ * @throws JSONException
864
+ */
865
+ String toString(int indentFactor, int indent) throws JSONException {
866
+ int len = length();
867
+ if (len == 0) {
868
+ return "[]";
869
+ }
870
+ int i;
871
+ StringBuffer sb = new StringBuffer("[");
872
+ if (len == 1) {
873
+ sb.append(JSONObject.valueToString(this.myArrayList.get(0),
874
+ indentFactor, indent));
875
+ } else {
876
+ int newindent = indent + indentFactor;
877
+ sb.append('\n');
878
+ for (i = 0; i < len; i += 1) {
879
+ if (i > 0) {
880
+ sb.append(",\n");
881
+ }
882
+ for (int j = 0; j < newindent; j += 1) {
883
+ sb.append(' ');
884
+ }
885
+ sb.append(JSONObject.valueToString(this.myArrayList.get(i),
886
+ indentFactor, newindent));
887
+ }
888
+ sb.append('\n');
889
+ for (i = 0; i < indent; i += 1) {
890
+ sb.append(' ');
891
+ }
892
+ }
893
+ sb.append(']');
894
+ return sb.toString();
895
+ }
896
+
897
+
898
+ /**
899
+ * Write the contents of the JSONArray as JSON text to a writer.
900
+ * For compactness, no whitespace is added.
901
+ * <p>
902
+ * Warning: This method assumes that the data structure is acyclical.
903
+ *
904
+ * @return The writer.
905
+ * @throws JSONException
906
+ */
907
+ public Writer write(Writer writer) throws JSONException {
908
+ try {
909
+ boolean b = false;
910
+ int len = length();
911
+
912
+ writer.write('[');
913
+
914
+ for (int i = 0; i < len; i += 1) {
915
+ if (b) {
916
+ writer.write(',');
917
+ }
918
+ Object v = this.myArrayList.get(i);
919
+ if (v instanceof JSONObject) {
920
+ ((JSONObject)v).write(writer);
921
+ } else if (v instanceof JSONArray) {
922
+ ((JSONArray)v).write(writer);
923
+ } else {
924
+ writer.write(JSONObject.valueToString(v));
925
+ }
926
+ b = true;
927
+ }
928
+ writer.write(']');
929
+ return writer;
930
+ } catch (IOException e) {
931
+ throw new JSONException(e);
932
+ }
933
+ }
934
+ }