1 |
|
package org.json; |
2 |
|
|
3 |
|
import static java.lang.String.format; |
4 |
|
|
5 |
|
import java.io.UnsupportedEncodingException; |
6 |
|
import java.net.URLDecoder; |
7 |
|
import java.net.URLEncoder; |
8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.Collections; |
10 |
|
import java.util.List; |
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
@author |
52 |
|
@version |
53 |
|
|
|
|
| 0% |
Uncovered Elements: 91 (91) |
Complexity: 26 |
Complexity Density: 0.43 |
|
54 |
|
public class JSONPointer |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
private static final String ENCODING = "utf-8"; |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 4 |
Complexity Density: 0.57 |
|
64 |
|
public static class Builder |
65 |
|
{ |
66 |
|
|
67 |
|
|
68 |
|
private final List<String> refTokens = new ArrayList<String>(); |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@link |
73 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
74 |
0 |
public JSONPointer build()... |
75 |
|
{ |
76 |
0 |
return new JSONPointer(this.refTokens); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@param |
90 |
|
|
91 |
|
@return |
92 |
|
@throws |
93 |
|
|
94 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
95 |
0 |
public Builder append(String token)... |
96 |
|
{ |
97 |
0 |
if (token == null) |
98 |
|
{ |
99 |
0 |
throw new NullPointerException("token cannot be null"); |
100 |
|
} |
101 |
0 |
this.refTokens.add(token); |
102 |
0 |
return this; |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@param |
110 |
|
|
111 |
|
@return |
112 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
113 |
0 |
public Builder append(int arrayIndex)... |
114 |
|
{ |
115 |
0 |
this.refTokens.add(String.valueOf(arrayIndex)); |
116 |
0 |
return this; |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
|
@link |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
@return |
135 |
|
|
136 |
|
@link |
137 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
0 |
public static Builder builder()... |
139 |
|
{ |
140 |
0 |
return new Builder(); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
private final List<String> refTokens; |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@param |
153 |
|
|
154 |
|
|
155 |
|
@throws |
156 |
|
|
157 |
|
|
|
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 11 |
Complexity Density: 0.41 |
|
158 |
0 |
public JSONPointer(final String pointer)... |
159 |
|
{ |
160 |
0 |
if (pointer == null) |
161 |
|
{ |
162 |
0 |
throw new NullPointerException("pointer cannot be null"); |
163 |
|
} |
164 |
0 |
if (pointer.isEmpty() || pointer.equals("#")) |
165 |
|
{ |
166 |
0 |
this.refTokens = Collections.emptyList(); |
167 |
0 |
return; |
168 |
|
} |
169 |
0 |
String refs; |
170 |
0 |
if (pointer.startsWith("#/")) |
171 |
|
{ |
172 |
0 |
refs = pointer.substring(2); |
173 |
0 |
try |
174 |
|
{ |
175 |
0 |
refs = URLDecoder.decode(refs, ENCODING); |
176 |
|
} catch (UnsupportedEncodingException e) |
177 |
|
{ |
178 |
0 |
throw new RuntimeException(e); |
179 |
|
} |
180 |
|
} |
181 |
0 |
else if (pointer.startsWith("/")) |
182 |
|
{ |
183 |
0 |
refs = pointer.substring(1); |
184 |
|
} |
185 |
|
else |
186 |
|
{ |
187 |
0 |
throw new IllegalArgumentException( |
188 |
|
"a JSON pointer should start with '/' or '#/'"); |
189 |
|
} |
190 |
0 |
this.refTokens = new ArrayList<String>(); |
191 |
0 |
int slashIdx = -1; |
192 |
0 |
int prevSlashIdx = 0; |
193 |
0 |
do |
194 |
|
{ |
195 |
0 |
prevSlashIdx = slashIdx + 1; |
196 |
0 |
slashIdx = refs.indexOf('/', prevSlashIdx); |
197 |
0 |
if (prevSlashIdx == slashIdx || prevSlashIdx == refs.length()) |
198 |
|
{ |
199 |
|
|
200 |
|
|
201 |
0 |
this.refTokens.add(""); |
202 |
|
} |
203 |
0 |
else if (slashIdx >= 0) |
204 |
|
{ |
205 |
0 |
final String token = refs.substring(prevSlashIdx, slashIdx); |
206 |
0 |
this.refTokens.add(unescape(token)); |
207 |
|
} |
208 |
|
else |
209 |
|
{ |
210 |
|
|
211 |
0 |
final String token = refs.substring(prevSlashIdx); |
212 |
0 |
this.refTokens.add(unescape(token)); |
213 |
|
} |
214 |
0 |
} while (slashIdx >= 0); |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
} |
221 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
222 |
0 |
public JSONPointer(List<String> refTokens)... |
223 |
|
{ |
224 |
0 |
this.refTokens = new ArrayList<String>(refTokens); |
225 |
|
} |
226 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
227 |
0 |
private String unescape(String token)... |
228 |
|
{ |
229 |
0 |
return token.replace("~1", "/").replace("~0", "~").replace("\\\"", "\"") |
230 |
|
.replace("\\\\", "\\"); |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
@link@link |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
@param |
241 |
|
|
242 |
|
@return |
243 |
|
@throws |
244 |
|
|
245 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
246 |
0 |
public Object queryFrom(Object document) throws JSONPointerException... |
247 |
|
{ |
248 |
0 |
if (this.refTokens.isEmpty()) |
249 |
|
{ |
250 |
0 |
return document; |
251 |
|
} |
252 |
0 |
Object current = document; |
253 |
0 |
for (String token : this.refTokens) |
254 |
|
{ |
255 |
0 |
if (current instanceof JSONObject) |
256 |
|
{ |
257 |
0 |
current = ((JSONObject) current).opt(unescape(token)); |
258 |
|
} |
259 |
0 |
else if (current instanceof JSONArray) |
260 |
|
{ |
261 |
0 |
current = readByIndexToken(current, token); |
262 |
|
} |
263 |
|
else |
264 |
|
{ |
265 |
0 |
throw new JSONPointerException(format( |
266 |
|
"value [%s] is not an array or object therefore its key %s cannot be resolved", |
267 |
|
current, token)); |
268 |
|
} |
269 |
|
} |
270 |
0 |
return current; |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
@param |
277 |
|
|
278 |
|
@param |
279 |
|
|
280 |
|
@return |
281 |
|
@throws |
282 |
|
|
283 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 4 |
Complexity Density: 0.44 |
|
284 |
0 |
private Object readByIndexToken(Object current, String indexToken)... |
285 |
|
throws JSONPointerException |
286 |
|
{ |
287 |
0 |
try |
288 |
|
{ |
289 |
0 |
int index = Integer.parseInt(indexToken); |
290 |
0 |
JSONArray currentArr = (JSONArray) current; |
291 |
0 |
if (index >= currentArr.length()) |
292 |
|
{ |
293 |
0 |
throw new JSONPointerException(format( |
294 |
|
"index %d is out of bounds - the array has %d elements", |
295 |
|
index, currentArr.length())); |
296 |
|
} |
297 |
0 |
try |
298 |
|
{ |
299 |
0 |
return currentArr.get(index); |
300 |
|
} catch (JSONException e) |
301 |
|
{ |
302 |
0 |
throw new JSONPointerException( |
303 |
|
"Error reading value at index position " + index, e); |
304 |
|
} |
305 |
|
} catch (NumberFormatException e) |
306 |
|
{ |
307 |
0 |
throw new JSONPointerException( |
308 |
|
format("%s is not an array index", indexToken), e); |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
316 |
0 |
@Override... |
317 |
|
public String toString() |
318 |
|
{ |
319 |
0 |
StringBuilder rval = new StringBuilder(""); |
320 |
0 |
for (String token : this.refTokens) |
321 |
|
{ |
322 |
0 |
rval.append('/').append(escape(token)); |
323 |
|
} |
324 |
0 |
return rval.toString(); |
325 |
|
} |
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
@param |
333 |
|
|
334 |
|
@return |
335 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
336 |
0 |
private String escape(String token)... |
337 |
|
{ |
338 |
0 |
return token.replace("~", "~0").replace("/", "~1").replace("\\", "\\\\") |
339 |
|
.replace("\"", "\\\""); |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
346 |
0 |
public String toURIFragment()... |
347 |
|
{ |
348 |
0 |
try |
349 |
|
{ |
350 |
0 |
StringBuilder rval = new StringBuilder("#"); |
351 |
0 |
for (String token : this.refTokens) |
352 |
|
{ |
353 |
0 |
rval.append('/').append(URLEncoder.encode(token, ENCODING)); |
354 |
|
} |
355 |
0 |
return rval.toString(); |
356 |
|
} catch (UnsupportedEncodingException e) |
357 |
|
{ |
358 |
0 |
throw new RuntimeException(e); |
359 |
|
} |
360 |
|
} |
361 |
|
|
362 |
|
} |