keys = keys();
979 | StringBuffer sb = new StringBuffer("{");
980 | int newindent = indent + indentFactor;
981 | Object o;
982 | if (n == 1) {
983 | o = keys.next();
984 | sb.append(quote(o.toString()));
985 | sb.append(": ");
986 | sb.append(valueToString(this.myHashMap.get(o), indentFactor,
987 | indent));
988 | } else {
989 | while (keys.hasNext()) {
990 | o = keys.next();
991 | if (sb.length() > 1) {
992 | sb.append(",\n");
993 | } else {
994 | sb.append('\n');
995 | }
996 | for (i = 0; i < newindent; i += 1) {
997 | sb.append(' ');
998 | }
999 | sb.append(quote(o.toString()));
1000 | sb.append(": ");
1001 | sb.append(valueToString(this.myHashMap.get(o), indentFactor,
1002 | newindent));
1003 | }
1004 | if (sb.length() > 1) {
1005 | sb.append('\n');
1006 | for (i = 0; i < indent; i += 1) {
1007 | sb.append(' ');
1008 | }
1009 | }
1010 | }
1011 | sb.append('}');
1012 | return sb.toString();
1013 | }
1014 |
1015 |
1016 | /**
1017 | * Make a JSON text of an object value.
1018 | *
1019 | * Warning: This method assumes that the data structure is acyclical.
1020 | * @param value The value to be serialized.
1021 | * @return a printable, displayable, transmittable
1022 | * representation of the object, beginning
1023 | * with {
(left brace) and ending
1024 | * with }
(right brace).
1025 | * @throws JSONException If the value is or contains an invalid number.
1026 | */
1027 | static String valueToString(Object value) throws JSONException {
1028 | if (value == null) {
1029 | return "null";
1030 | }
1031 | if (value instanceof Number) {
1032 | return numberToString((Number) value);
1033 | }
1034 | if (value instanceof Boolean || value instanceof JSONObject ||
1035 | value instanceof JSONArray) {
1036 | return value.toString();
1037 | }
1038 | return quote(value.toString());
1039 | }
1040 |
1041 |
1042 | /**
1043 | * Make a prettyprinted JSON text of an object value.
1044 | *
1045 | * Warning: This method assumes that the data structure is acyclical.
1046 | * @param value The value to be serialized.
1047 | * @param indentFactor The number of spaces to add to each level of
1048 | * indentation.
1049 | * @param indent The indentation of the top level.
1050 | * @return a printable, displayable, transmittable
1051 | * representation of the object, beginning
1052 | * with {
(left brace) and ending
1053 | * with }
(right brace).
1054 | * @throws JSONException If the object contains an invalid number.
1055 | */
1056 | static String valueToString(Object value, int indentFactor, int indent)
1057 | throws JSONException {
1058 | if (value == null) {
1059 | return "null";
1060 | }
1061 | if (value instanceof Number) {
1062 | return numberToString((Number) value);
1063 | }
1064 | if (value instanceof Boolean) {
1065 | return value.toString();
1066 | }
1067 | if (value instanceof JSONObject) {
1068 | return ((JSONObject)value).toString(indentFactor, indent);
1069 | }
1070 | if (value instanceof JSONArray) {
1071 | return ((JSONArray)value).toString(indentFactor, indent);
1072 | }
1073 | return quote(value.toString());
1074 | }
1075 | }
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/json/JSONStringer.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2005 JSON.org
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 all
12 | copies or substantial portions of the Software.
13 |
14 | The Software shall be used for Good, not Evil.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | package jp.codic.plugins.intellij.json;
25 |
26 | /**
27 | * JSONStringer provides a quick and convenient way of producing JSON text.
28 | * The texts produced strictly conform to JSON syntax rules. No whitespace is
29 | * added, so the results are ready for transmission or storage. Each instance of
30 | * JSONStringer can produce one JSON text.
31 | *
32 | * A JSONStringer instance provides a value
method for appending
33 | * values to the
34 | * text, and a key
35 | * method for adding keys before values in objects. There are array
36 | * and endArray
methods that make and bound array values, and
37 | * object
and endObject
methods which make and bound
38 | * object values. All of these methods return the JSONStringer instance,
39 | * permitting cascade style. For example,
40 | * myString = new JSONStringer()
41 | * .object()
42 | * .key("JSON").value("Hello, World!")
43 | * .endObject()
44 | * .toString();
which produces the string
45 | * {"JSON":"Hello, World!"}
46 | *
47 | * The first method called must be array
or object
.
48 | * There are no methods for adding commas or colons. JSONStringer adds them for
49 | * you. Objects and arrays can be nested up to 20 levels deep.
50 | *
51 | * This can sometimes be easier than using a JSONObject to build a string.
52 | * @author JSON.org
53 | * @version 2
54 | */
55 | public class JSONStringer {
56 | private static final int maxdepth = 20;
57 |
58 | /**
59 | * The comma flag determines if a comma should be output before the next
60 | * value.
61 | */
62 | private boolean comma;
63 |
64 | /**
65 | * The current mode. Values:
66 | * 'a' (array),
67 | * 'd' (done),
68 | * 'i' (initial),
69 | * 'k' (key),
70 | * 'o' (object).
71 | */
72 | private char mode;
73 |
74 | /**
75 | * The string buffer that holds the JSON text that is built.
76 | */
77 | private StringBuffer sb;
78 |
79 | /**
80 | * The object/array stack.
81 | */
82 | private char stack[];
83 |
84 | /**
85 | * The stack top index. A value of 0 indicates that the stack is empty.
86 | */
87 | private int top;
88 |
89 | /**
90 | * Make a fresh JSONStringer. It can be used to build one JSON text.
91 | */
92 | public JSONStringer() {
93 | this.sb = new StringBuffer();
94 | this.stack = new char[maxdepth];
95 | this.top = 0;
96 | this.mode = 'i';
97 | this.comma = false;
98 | }
99 |
100 | /**
101 | * Append a value.
102 | * @param s A string value.
103 | * @return this
104 | * @throws JSONException If the value is out of sequence.
105 | */
106 | private JSONStringer append(String s)
107 | throws JSONException {
108 | if (s == null) {
109 | throw new JSONException("Null pointer");
110 | }
111 | if (this.mode == 'o' || this.mode == 'a') {
112 | if (this.comma && this.mode == 'a') {
113 | this.sb.append(',');
114 | }
115 | this.sb.append(s);
116 | if (this.mode == 'o') {
117 | this.mode = 'k';
118 | }
119 | this.comma = true;
120 | return this;
121 | }
122 | throw new JSONException("Value out of sequence.");
123 | }
124 |
125 | /**
126 | * Begin appending a new array. All values until the balancing
127 | * endArray
will be appended to this array. The
128 | * endArray
method must be called to mark the array's end.
129 | * @return this
130 | * @throws JSONException If the nesting is too deep, or if the object is
131 | * started in the wrong place (for example as a key or after the end of the
132 | * outermost array or object).
133 | */
134 | public JSONStringer array() throws JSONException {
135 | if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {
136 | push('a');
137 | this.append("[");
138 | this.comma = false;
139 | return this;
140 | }
141 | throw new JSONException("Misplaced array.");
142 | }
143 |
144 | /**
145 | * End something.
146 | * @param m Mode
147 | * @param c Closing character
148 | * @return this
149 | * @throws JSONException If unbalanced.
150 | */
151 | private JSONStringer end(char m, char c) throws JSONException {
152 | if (this.mode != m) {
153 | throw new JSONException(m == 'o' ? "Misplaced endObject." :
154 | "Misplaced endArray.");
155 | }
156 | pop(m);
157 | this.sb.append(c);
158 | this.comma = true;
159 | return this;
160 | }
161 |
162 | /**
163 | * End an array. This method most be called to balance calls to
164 | * array
.
165 | * @return this
166 | * @throws JSONException If incorrectly nested.
167 | */
168 | public JSONStringer endArray() throws JSONException {
169 | return end('a', ']');
170 | }
171 |
172 | /**
173 | * End an object. This method most be called to balance calls to
174 | * object
.
175 | * @return this
176 | * @throws JSONException If incorrectly nested.
177 | */
178 | public JSONStringer endObject() throws JSONException {
179 | return end('k', '}');
180 | }
181 |
182 | /**
183 | * Append a key. The key will be associated with the next value. In an
184 | * object, every value must be preceded by a key.
185 | * @param s A key string.
186 | * @return this
187 | * @throws JSONException If the key is out of place. For example, keys
188 | * do not belong in arrays or if the key is null.
189 | */
190 | public JSONStringer key(String s)
191 | throws JSONException {
192 | if (s == null) {
193 | throw new JSONException("Null key.");
194 | }
195 | if (this.mode == 'k') {
196 | if (this.comma) {
197 | this.sb.append(',');
198 | }
199 | this.sb.append(JSONObject.quote(s));
200 | this.sb.append(':');
201 | this.comma = false;
202 | this.mode = 'o';
203 | return this;
204 | }
205 | throw new JSONException("Misplaced key.");
206 | }
207 |
208 |
209 | /**
210 | * Begin appending a new object. All keys and values until the balancing
211 | * endObject
will be appended to this object. The
212 | * endObject
method must be called to mark the object's end.
213 | * @return this
214 | * @throws JSONException If the nesting is too deep, or if the object is
215 | * started in the wrong place (for example as a key or after the end of the
216 | * outermost array or object).
217 | */
218 | public JSONStringer object() throws JSONException {
219 | if (this.mode == 'i') {
220 | this.mode = 'o';
221 | }
222 | if (this.mode == 'o' || this.mode == 'a') {
223 | this.append("{");
224 | push('k');
225 | this.comma = false;
226 | return this;
227 | }
228 | throw new JSONException("Misplaced object.");
229 |
230 | }
231 |
232 |
233 | /**
234 | * Pop an array or object scope.
235 | * @param c The scope to close.
236 | * @throws JSONException If nesting is wrong.
237 | */
238 | private void pop(char c) throws JSONException {
239 | if (this.top <= 0 || this.stack[this.top - 1] != c) {
240 | throw new JSONException("Nesting error.");
241 | }
242 | this.top -= 1;
243 | this.mode = this.top == 0 ? 'd' : this.stack[this.top - 1];
244 | }
245 |
246 | /**
247 | * Push an array or object scope.
248 | * @param c The scope to open.
249 | * @throws JSONException If nesting is too deep.
250 | */
251 | private void push(char c) throws JSONException {
252 | if (this.top >= maxdepth) {
253 | throw new JSONException("Nesting too deep.");
254 | }
255 | this.stack[this.top] = c;
256 | this.mode = c;
257 | this.top += 1;
258 | }
259 |
260 |
261 | /**
262 | * Append either the value true
or the value
263 | * false
.
264 | * @param b A boolean.
265 | * @return this
266 | * @throws JSONException
267 | */
268 | public JSONStringer value(boolean b) throws JSONException {
269 | return this.append(b ? "true" : "false");
270 | }
271 |
272 | /**
273 | * Append a double value.
274 | * @param d A double.
275 | * @return this
276 | * @throws JSONException If the number is not finite.
277 | */
278 | public JSONStringer value(double d) throws JSONException {
279 | return this.value(new Double(d));
280 | }
281 |
282 | /**
283 | * Append a long value.
284 | * @param l A long.
285 | * @return this
286 | * @throws JSONException
287 | */
288 | public JSONStringer value(long l) throws JSONException {
289 | return this.append(Long.toString(l));
290 | }
291 |
292 |
293 | /**
294 | * Append an object value.
295 | * @param o The object to append. It can be null, or a Boolean, Number,
296 | * String, JSONObject, or JSONArray.
297 | * @return this
298 | * @throws JSONException If the value is out of sequence.
299 | */
300 | public JSONStringer value(Object o) throws JSONException {
301 | if (JSONObject.NULL.equals(o)) {
302 | return this.append("null");
303 | }
304 | if (o instanceof Number) {
305 | JSONObject.testValidity(o);
306 | return this.append(JSONObject.numberToString((Number)o));
307 | }
308 | if (o instanceof Boolean ||
309 | o instanceof JSONArray || o instanceof JSONObject) {
310 | return this.append(o.toString());
311 | }
312 | return this.append(JSONObject.quote(o.toString()));
313 | }
314 |
315 | /**
316 | * Return the JSON text. This method is used to obtain the product of the
317 | * JSONStringer instance. It will return null
if there was a
318 | * problem in the construction of the JSON text (such as the calls to
319 | * array
were not properly balanced with calls to
320 | * endArray
).
321 | * @return The JSON text.
322 | */
323 | public String toString() {
324 | return this.mode == 'd' ? this.sb.toString() : null;
325 | }
326 | }
327 |
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/json/JSONTokener.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2002 JSON.org
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 all
12 | copies or substantial portions of the Software.
13 |
14 | The Software shall be used for Good, not Evil.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | package jp.codic.plugins.intellij.json;
25 |
26 | /**
27 | * A JSONTokener takes a source string and extracts characters and tokens from
28 | * it. It is used by the JSONObject and JSONArray constructors to parse
29 | * JSON source strings.
30 | * @author JSON.org
31 | * @version 2
32 | */
33 | public class JSONTokener {
34 |
35 | /**
36 | * The index of the next character.
37 | */
38 | private int myIndex;
39 |
40 |
41 | /**
42 | * The source string being tokenized.
43 | */
44 | private String mySource;
45 |
46 |
47 | /**
48 | * Construct a JSONTokener from a string.
49 | *
50 | * @param s A source string.
51 | */
52 | public JSONTokener(String s) {
53 | this.myIndex = 0;
54 | this.mySource = s;
55 | }
56 |
57 |
58 | /**
59 | * Back up one character. This provides a sort of lookahead capability,
60 | * so that you can test for a digit or letter before attempting to parse
61 | * the next number or identifier.
62 | */
63 | public void back() {
64 | if (this.myIndex > 0) {
65 | this.myIndex -= 1;
66 | }
67 | }
68 |
69 |
70 |
71 | /**
72 | * Get the hex value of a character (base16).
73 | * @param c A character between '0' and '9' or between 'A' and 'F' or
74 | * between 'a' and 'f'.
75 | * @return An int between 0 and 15, or -1 if c was not a hex digit.
76 | */
77 | public static int dehexchar(char c) {
78 | if (c >= '0' && c <= '9') {
79 | return c - '0';
80 | }
81 | if (c >= 'A' && c <= 'F') {
82 | return c - ('A' - 10);
83 | }
84 | if (c >= 'a' && c <= 'f') {
85 | return c - ('a' - 10);
86 | }
87 | return -1;
88 | }
89 |
90 |
91 | /**
92 | * Determine if the source string still contains characters that next()
93 | * can consume.
94 | * @return true if not yet at the end of the source.
95 | */
96 | public boolean more() {
97 | return this.myIndex < this.mySource.length();
98 | }
99 |
100 |
101 | /**
102 | * Get the next character in the source string.
103 | *
104 | * @return The next character, or 0 if past the end of the source string.
105 | */
106 | public char next() {
107 | if (more()) {
108 | char c = this.mySource.charAt(this.myIndex);
109 | this.myIndex += 1;
110 | return c;
111 | }
112 | return 0;
113 | }
114 |
115 |
116 | /**
117 | * Consume the next character, and check that it matches a specified
118 | * character.
119 | * @param c The character to match.
120 | * @return The character.
121 | * @throws JSONException if the character does not match.
122 | */
123 | public char next(char c) throws JSONException {
124 | char n = next();
125 | if (n != c) {
126 | throw syntaxError("Expected '" + c + "' and instead saw '" +
127 | n + "'.");
128 | }
129 | return n;
130 | }
131 |
132 |
133 | /**
134 | * Get the next n characters.
135 | *
136 | * @param n The number of characters to take.
137 | * @return A string of n characters.
138 | * @throws JSONException
139 | * Substring bounds error if there are not
140 | * n characters remaining in the source string.
141 | */
142 | public String next(int n) throws JSONException {
143 | int i = this.myIndex;
144 | int j = i + n;
145 | if (j >= this.mySource.length()) {
146 | throw syntaxError("Substring bounds error");
147 | }
148 | this.myIndex += n;
149 | return this.mySource.substring(i, j);
150 | }
151 |
152 |
153 | /**
154 | * Get the next char in the string, skipping whitespace
155 | * and comments (slashslash, slashstar, and hash).
156 | * @throws JSONException
157 | * @return A character, or 0 if there are no more characters.
158 | */
159 | public char nextClean() throws JSONException {
160 | for (;;) {
161 | char c = next();
162 | if (c == '/') {
163 | switch (next()) {
164 | case '/':
165 | do {
166 | c = next();
167 | } while (c != '\n' && c != '\r' && c != 0);
168 | break;
169 | case '*':
170 | for (;;) {
171 | c = next();
172 | if (c == 0) {
173 | throw syntaxError("Unclosed comment.");
174 | }
175 | if (c == '*') {
176 | if (next() == '/') {
177 | break;
178 | }
179 | back();
180 | }
181 | }
182 | break;
183 | default:
184 | back();
185 | return '/';
186 | }
187 | } else if (c == '#') {
188 | do {
189 | c = next();
190 | } while (c != '\n' && c != '\r' && c != 0);
191 | } else if (c == 0 || c > ' ') {
192 | return c;
193 | }
194 | }
195 | }
196 |
197 |
198 | /**
199 | * Return the characters up to the next close quote character.
200 | * Backslash processing is done. The formal JSON format does not
201 | * allow strings in single quotes, but an implementation is allowed to
202 | * accept them.
203 | * @param quote The quoting character, either
204 | * "
(double quote) or
205 | * '
(single quote).
206 | * @return A String.
207 | * @throws JSONException Unterminated string.
208 | */
209 | public String nextString(char quote) throws JSONException {
210 | char c;
211 | StringBuffer sb = new StringBuffer();
212 | for (;;) {
213 | c = next();
214 | switch (c) {
215 | case 0:
216 | case '\n':
217 | case '\r':
218 | throw syntaxError("Unterminated string");
219 | case '\\':
220 | c = next();
221 | switch (c) {
222 | case 'b':
223 | sb.append('\b');
224 | break;
225 | case 't':
226 | sb.append('\t');
227 | break;
228 | case 'n':
229 | sb.append('\n');
230 | break;
231 | case 'f':
232 | sb.append('\f');
233 | break;
234 | case 'r':
235 | sb.append('\r');
236 | break;
237 | case 'u':
238 | sb.append((char)Integer.parseInt(next(4), 16));
239 | break;
240 | case 'x' :
241 | sb.append((char) Integer.parseInt(next(2), 16));
242 | break;
243 | default:
244 | sb.append(c);
245 | }
246 | break;
247 | default:
248 | if (c == quote) {
249 | return sb.toString();
250 | }
251 | sb.append(c);
252 | }
253 | }
254 | }
255 |
256 |
257 | /**
258 | * Get the text up but not including the specified character or the
259 | * end of line, whichever comes first.
260 | * @param d A delimiter character.
261 | * @return A string.
262 | */
263 | public String nextTo(char d) {
264 | StringBuffer sb = new StringBuffer();
265 | for (;;) {
266 | char c = next();
267 | if (c == d || c == 0 || c == '\n' || c == '\r') {
268 | if (c != 0) {
269 | back();
270 | }
271 | return sb.toString().trim();
272 | }
273 | sb.append(c);
274 | }
275 | }
276 |
277 |
278 | /**
279 | * Get the text up but not including one of the specified delimeter
280 | * characters or the end of line, whichever comes first.
281 | * @param delimiters A set of delimiter characters.
282 | * @return A string, trimmed.
283 | */
284 | public String nextTo(String delimiters) {
285 | char c;
286 | StringBuffer sb = new StringBuffer();
287 | for (;;) {
288 | c = next();
289 | if (delimiters.indexOf(c) >= 0 || c == 0 ||
290 | c == '\n' || c == '\r') {
291 | if (c != 0) {
292 | back();
293 | }
294 | return sb.toString().trim();
295 | }
296 | sb.append(c);
297 | }
298 | }
299 |
300 |
301 | /**
302 | * Get the next value. The value can be a Boolean, Double, Integer,
303 | * JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
304 | * @throws JSONException If syntax error.
305 | *
306 | * @return An object.
307 | */
308 | public Object nextValue() throws JSONException {
309 | char c = nextClean();
310 | String s;
311 |
312 | switch (c) {
313 | case '"':
314 | case '\'':
315 | return nextString(c);
316 | case '{':
317 | back();
318 | return new JSONObject(this);
319 | case '[':
320 | back();
321 | return new JSONArray(this);
322 | }
323 |
324 | /*
325 | * Handle unquoted text. This could be the values true, false, or
326 | * null, or it can be a number. An implementation (such as this one)
327 | * is allowed to also accept non-standard forms.
328 | *
329 | * Accumulate characters until we reach the end of the text or a
330 | * formatting character.
331 | */
332 |
333 | StringBuffer sb = new StringBuffer();
334 | char b = c;
335 | while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) {
336 | sb.append(c);
337 | c = next();
338 | }
339 | back();
340 |
341 | /*
342 | * If it is true, false, or null, return the proper value.
343 | */
344 |
345 | s = sb.toString().trim();
346 | if (s.equals("")) {
347 | throw syntaxError("Missing value.");
348 | }
349 | if (s.equalsIgnoreCase("true")) {
350 | return Boolean.TRUE;
351 | }
352 | if (s.equalsIgnoreCase("false")) {
353 | return Boolean.FALSE;
354 | }
355 | if (s.equalsIgnoreCase("null")) {
356 | return JSONObject.NULL;
357 | }
358 |
359 | /*
360 | * If it might be a number, try converting it. We support the 0- and 0x-
361 | * conventions. If a number cannot be produced, then the value will just
362 | * be a string. Note that the 0-, 0x-, plus, and implied string
363 | * conventions are non-standard. A JSON parser is free to accept
364 | * non-JSON forms as long as it accepts all correct JSON forms.
365 | */
366 |
367 | if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') {
368 | if (b == '0') {
369 | if (s.length() > 2 &&
370 | (s.charAt(1) == 'x' || s.charAt(1) == 'X')) {
371 | try {
372 | return new Integer(Integer.parseInt(s.substring(2),
373 | 16));
374 | } catch (Exception e) {
375 | /* Ignore the error */
376 | }
377 | } else {
378 | try {
379 | return new Integer(Integer.parseInt(s, 8));
380 | } catch (Exception e) {
381 | /* Ignore the error */
382 | }
383 | }
384 | }
385 | try {
386 | return new Integer(s);
387 | } catch (Exception e) {
388 | try {
389 | return new Long(s);
390 | } catch (Exception f) {
391 | try {
392 | return new Double(s);
393 | } catch (Exception g) {
394 | return s;
395 | }
396 | }
397 | }
398 | }
399 | return s;
400 | }
401 |
402 |
403 | /**
404 | * Skip characters until the next character is the requested character.
405 | * If the requested character is not found, no characters are skipped.
406 | * @param to A character to skip to.
407 | * @return The requested character, or zero if the requested character
408 | * is not found.
409 | */
410 | public char skipTo(char to) {
411 | char c;
412 | int index = this.myIndex;
413 | do {
414 | c = next();
415 | if (c == 0) {
416 | this.myIndex = index;
417 | return c;
418 | }
419 | } while (c != to);
420 | back();
421 | return c;
422 | }
423 |
424 |
425 | /**
426 | * Skip characters until past the requested string.
427 | * If it is not found, we are left at the end of the source.
428 | * @param to A string to skip past.
429 | */
430 | public void skipPast(String to) {
431 | this.myIndex = this.mySource.indexOf(to, this.myIndex);
432 | if (this.myIndex < 0) {
433 | this.myIndex = this.mySource.length();
434 | } else {
435 | this.myIndex += to.length();
436 | }
437 | }
438 |
439 |
440 | /**
441 | * Make a JSONException to signal a syntax error.
442 | *
443 | * @param message The error message.
444 | * @return A JSONException object, suitable for throwing
445 | */
446 | public JSONException syntaxError(String message) {
447 | return new JSONException(message + toString());
448 | }
449 |
450 |
451 | /**
452 | * Make a printable string of this JSONTokener.
453 | *
454 | * @return " at character [this.myIndex] of [this.mySource]"
455 | */
456 | public String toString() {
457 | return " at character " + this.myIndex + " of " + this.mySource;
458 | }
459 | }
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/json/JSONType.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2002 JSON.org
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 all
12 | copies or substantial portions of the Software.
13 |
14 | The Software shall be used for Good, not Evil.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | package jp.codic.plugins.intellij.json;
25 |
26 | public enum JSONType {
27 | OBJECT,
28 | ARRAY,
29 | SCALAR
30 | }
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/json/JSONValue.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2002 JSON.org
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 all
12 | copies or substantial portions of the Software.
13 |
14 | The Software shall be used for Good, not Evil.
15 |
16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 | SOFTWARE.
23 | */
24 | package jp.codic.plugins.intellij.json;
25 |
26 | public interface JSONValue {
27 | public JSONType getType();
28 | }
29 |
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/util/Debouncer.java:
--------------------------------------------------------------------------------
1 | package jp.codic.plugins.intellij.util;
2 |
3 | import java.util.concurrent.Executors;
4 | import java.util.concurrent.ScheduledExecutorService;
5 | import java.util.concurrent.ScheduledFuture;
6 | import java.util.concurrent.TimeUnit;
7 |
8 | public class Debouncer {
9 | private ScheduledExecutorService scheduledExecutorService ;
10 | private long delay = 0;
11 |
12 | private ScheduledFuture future;
13 |
14 | public Debouncer(long delay) {
15 | this.delay = delay;
16 | scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
17 | }
18 |
19 | public void push(Runnable runnable) {
20 | if (future != null) {
21 | future.cancel(false);
22 | future = null;
23 | }
24 | future = scheduledExecutorService.schedule(runnable, delay, TimeUnit.MILLISECONDS);
25 | }
26 |
27 | public void showdown() {
28 | scheduledExecutorService.shutdownNow();
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/util/DefaultKeyListener.java:
--------------------------------------------------------------------------------
1 | package jp.codic.plugins.intellij.util;
2 |
3 | import java.awt.event.KeyEvent;
4 | import java.awt.event.KeyListener;
5 | import java.awt.event.MouseEvent;
6 | import java.awt.event.MouseListener;
7 |
8 | public abstract class DefaultKeyListener implements KeyListener {
9 | @Override
10 | public void keyTyped(KeyEvent keyEvent) {
11 |
12 | }
13 |
14 | @Override
15 | public void keyPressed(KeyEvent keyEvent) {
16 |
17 | }
18 |
19 | @Override
20 | public void keyReleased(KeyEvent keyEvent) {
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/jp/codic/plugins/intellij/util/DefaultMouseListener.java:
--------------------------------------------------------------------------------
1 | package jp.codic.plugins.intellij.util;
2 |
3 | import java.awt.event.MouseEvent;
4 | import java.awt.event.MouseListener;
5 |
6 | public abstract class DefaultMouseListener implements MouseListener {
7 |
8 | @Override
9 | public void mouseClicked(MouseEvent mouseEvent) {
10 |
11 | }
12 |
13 | @Override
14 | public void mousePressed(MouseEvent mouseEvent) {
15 |
16 | }
17 |
18 | @Override
19 | public void mouseReleased(MouseEvent mouseEvent) {
20 |
21 | }
22 |
23 | @Override
24 | public void mouseEntered(MouseEvent mouseEvent) {
25 |
26 | }
27 |
28 | @Override
29 | public void mouseExited(MouseEvent mouseEvent) {
30 |
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------