Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / org / json / CDL.java @ 59

History | View | Annotate | Download (9.1 KB)

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
/**
28
 * This provides static methods to convert comma delimited text into a JSONArray, and to covert a JSONArray into comma delimited
29
 * text. Comma delimited text is a very popular format for data interchange. It is understood by most database, spreadsheet, and
30
 * organizer programs.
31
 * <p>
32
 * Each row of text represents a row in a table or a data record. Each row ends with a NEWLINE character. Each row contains one or
33
 * more values. Values are separated by commas. A value can contain any character except for comma, unless is is wrapped in single
34
 * quotes or double quotes.
35
 * <p>
36
 * The first row usually contains the names of the columns.
37
 * <p>
38
 * A comma delimited list can be converted into a JSONArray of JSONObjects. The names for the elements in the JSONObjects can be
39
 * taken from the names in the first row.
40
 *
41
 * @author JSON.org
42
 * @version 2
43
 */
44
public class CDL {
45

    
46
   /**
47
    * Get the next value. The value can be wrapped in quotes. The value can be empty.
48
    *
49
    * @param x
50
    *                A JSONTokener of the source text.
51
    * @return The value string, or null if empty.
52
    * @throws JSONException
53
    *                 if the quoted string is badly formed.
54
    */
55
   private static String getValue(final JSONTokener x) throws JSONException {
56
      char c;
57
      do {
58
         c = x.next();
59
      }
60
      while (c <= ' ' && c != 0);
61
      switch (c) {
62
         case 0:
63
            return null;
64
         case '"':
65
         case '\'':
66
            return x.nextString(c);
67
         case ',':
68
            x.back();
69
            return "";
70
         default:
71
            x.back();
72
            return x.nextTo(',');
73
      }
74
   }
75

    
76

    
77
   /**
78
    * Produce a JSONArray of strings from a row of comma delimited values.
79
    *
80
    * @param x
81
    *                A JSONTokener of the source text.
82
    * @return A JSONArray of strings.
83
    * @throws JSONException
84
    */
85
   public static JSONArray rowToJSONArray(final JSONTokener x) throws JSONException {
86
      final JSONArray ja = new JSONArray();
87
      for (;;) {
88
         final String value = getValue(x);
89
         if (value == null) {
90
            return null;
91
         }
92
         ja.put(value);
93
         for (;;) {
94
            final char c = x.next();
95
            if (c == ',') {
96
               break;
97
            }
98
            if (c != ' ') {
99
               if (c == '\n' || c == '\r' || c == 0) {
100
                  return ja;
101
               }
102
               throw x.syntaxError("Bad character '" + c + "' (" + (int) c + ").");
103
            }
104
         }
105
      }
106
   }
107

    
108

    
109
   /**
110
    * Produce a JSONObject from a row of comma delimited text, using a parallel JSONArray of strings to provides the names of the
111
    * elements.
112
    *
113
    * @param names
114
    *                A JSONArray of names. This is commonly obtained from the first row of a comma delimited text file using the
115
    *                rowToJSONArray method.
116
    * @param x
117
    *                A JSONTokener of the source text.
118
    * @return A JSONObject combining the names and values.
119
    * @throws JSONException
120
    */
121
   public static JSONObject rowToJSONObject(final JSONArray names,
122
                                            final JSONTokener x) throws JSONException {
123
      final JSONArray ja = rowToJSONArray(x);
124
      return ja != null ? ja.toJSONObject(names) : null;
125
   }
126

    
127

    
128
   /**
129
    * Produce a JSONArray of JSONObjects from a comma delimited text string, using the first row as a source of names.
130
    *
131
    * @param string
132
    *                The comma delimited text.
133
    * @return A JSONArray of JSONObjects.
134
    * @throws JSONException
135
    */
136
   public static JSONArray toJSONArray(final String string) throws JSONException {
137
      return toJSONArray(new JSONTokener(string));
138
   }
139

    
140

    
141
   /**
142
    * Produce a JSONArray of JSONObjects from a comma delimited text string, using the first row as a source of names.
143
    *
144
    * @param x
145
    *                The JSONTokener containing the comma delimited text.
146
    * @return A JSONArray of JSONObjects.
147
    * @throws JSONException
148
    */
149
   public static JSONArray toJSONArray(final JSONTokener x) throws JSONException {
150
      return toJSONArray(rowToJSONArray(x), x);
151
   }
152

    
153

    
154
   /**
155
    * Produce a JSONArray of JSONObjects from a comma delimited text string using a supplied JSONArray as the source of element
156
    * names.
157
    *
158
    * @param names
159
    *                A JSONArray of strings.
160
    * @param string
161
    *                The comma delimited text.
162
    * @return A JSONArray of JSONObjects.
163
    * @throws JSONException
164
    */
165
   public static JSONArray toJSONArray(final JSONArray names,
166
                                       final String string) throws JSONException {
167
      return toJSONArray(names, new JSONTokener(string));
168
   }
169

    
170

    
171
   /**
172
    * Produce a JSONArray of JSONObjects from a comma delimited text string using a supplied JSONArray as the source of element
173
    * names.
174
    *
175
    * @param names
176
    *                A JSONArray of strings.
177
    * @param x
178
    *                A JSONTokener of the source text.
179
    * @return A JSONArray of JSONObjects.
180
    * @throws JSONException
181
    */
182
   public static JSONArray toJSONArray(final JSONArray names,
183
                                       final JSONTokener x) throws JSONException {
184
      if (names == null || names.length() == 0) {
185
         return null;
186
      }
187
      final JSONArray ja = new JSONArray();
188
      for (;;) {
189
         final JSONObject jo = rowToJSONObject(names, x);
190
         if (jo == null) {
191
            break;
192
         }
193
         ja.put(jo);
194
      }
195
      if (ja.length() == 0) {
196
         return null;
197
      }
198
      return ja;
199
   }
200

    
201

    
202
   /**
203
    * Produce a comma delimited text row from a JSONArray. Values containing the comma character will be quoted.
204
    *
205
    * @param ja
206
    *                A JSONArray of strings.
207
    * @return A string ending in NEWLINE.
208
    */
209
   public static String rowToString(final JSONArray ja) {
210
      final StringBuffer sb = new StringBuffer();
211
      for (int i = 0; i < ja.length(); i += 1) {
212
         if (i > 0) {
213
            sb.append(',');
214
         }
215
         final Object o = ja.opt(i);
216
         if (o != null) {
217
            final String s = o.toString();
218
            if (s.indexOf(',') >= 0) {
219
               if (s.indexOf('"') >= 0) {
220
                  sb.append('\'');
221
                  sb.append(s);
222
                  sb.append('\'');
223
               }
224
               else {
225
                  sb.append('"');
226
                  sb.append(s);
227
                  sb.append('"');
228
               }
229
            }
230
            else {
231
               sb.append(s);
232
            }
233
         }
234
      }
235
      sb.append('\n');
236
      return sb.toString();
237

    
238
   }
239

    
240

    
241
   /**
242
    * Produce a comma delimited text from a JSONArray of JSONObjects. The first row will be a list of names obtained by inspecting
243
    * the first JSONObject.
244
    *
245
    * @param ja
246
    *                A JSONArray of JSONObjects.
247
    * @return A comma delimited text.
248
    * @throws JSONException
249
    */
250
   public static String toString(final JSONArray ja) throws JSONException {
251
      final JSONObject jo = ja.optJSONObject(0);
252
      if (jo != null) {
253
         final JSONArray names = jo.names();
254
         if (names != null) {
255
            return rowToString(names) + toString(names, ja);
256
         }
257
      }
258
      return null;
259
   }
260

    
261

    
262
   /**
263
    * Produce a comma delimited text from a JSONArray of JSONObjects using a provided list of names. The list of names is not
264
    * included in the output.
265
    *
266
    * @param names
267
    *                A JSONArray of strings.
268
    * @param ja
269
    *                A JSONArray of JSONObjects.
270
    * @return A comma delimited text.
271
    * @throws JSONException
272
    */
273
   public static String toString(final JSONArray names,
274
                                 final JSONArray ja) throws JSONException {
275
      if (names == null || names.length() == 0) {
276
         return null;
277
      }
278
      final StringBuffer sb = new StringBuffer();
279
      for (int i = 0; i < ja.length(); i += 1) {
280
         final JSONObject jo = ja.optJSONObject(i);
281
         if (jo != null) {
282
            sb.append(rowToString(jo.toJSONArray(names)));
283
         }
284
      }
285
      return sb.toString();
286
   }
287
}