|
27 | 27 | import java.util.Collections; |
28 | 28 | import java.util.List; |
29 | 29 | import java.util.Map; |
30 | | -import java.util.regex.Matcher; |
31 | 30 | import java.util.regex.Pattern; |
32 | 31 |
|
33 | 32 | import com.google.gson.GsonBuilder; |
@@ -251,60 +250,56 @@ public void write(OutputStream outStream, LanguageBundle languageBundle, |
251 | 250 | static List<KeyPiece> splitKeyPieces(String key) { |
252 | 251 | if (USE_JSONPATH_PATTERN.matcher(key).matches()) { |
253 | 252 | List<KeyPiece> result = new ArrayList<KeyPiece>(); |
254 | | - Matcher onlyDigits = Pattern.compile("^\\d+$").matcher(""); |
| 253 | + boolean inQuotes = false; |
| 254 | + StringBuilder currentToken = new StringBuilder(); |
255 | 255 | // Disregard $ at the beginning - it's not really part of the key... |
256 | | - List<String> tokens = findTokens(key.substring(JSONPATH_ROOT.length())); |
257 | | - for (String s : tokens) { |
258 | | - if (s.startsWith("'")) { |
259 | | - // Turn any "\u0027" in the key back into ' |
260 | | - String modifiedKeyPiece = s.substring(1, s.length() - 1).replaceAll("\\\\u0027", "'"); |
261 | | - result.add(new KeyPiece(modifiedKeyPiece, JsonToken.BEGIN_OBJECT)); |
262 | | - } else if (onlyDigits.reset(s).matches()) { // BAD |
263 | | - result.add(new KeyPiece(s, JsonToken.BEGIN_ARRAY)); |
264 | | - } else if (false && s.startsWith("[") && (s.length() > 1)) { |
265 | | - result.add(new KeyPiece(s.substring(1, s.length() - 1), JsonToken.BEGIN_ARRAY)); |
266 | | - } else { |
267 | | - for (String s2 : s.split("\\.")) { |
268 | | - if (!s2.isEmpty()) { |
269 | | - result.add(new KeyPiece(s2, JsonToken.BEGIN_OBJECT)); |
| 256 | + StringCharacterIterator i = new StringCharacterIterator(key.substring(JSONPATH_ROOT.length())); |
| 257 | + boolean inSubscript = false; |
| 258 | + while (i.current() != StringCharacterIterator.DONE) { |
| 259 | + char c = i.current(); |
| 260 | + if (c == '\'') { |
| 261 | + inQuotes = !inQuotes; |
| 262 | + } |
| 263 | + if (!inQuotes && (c == '.' || c == '[' || c == ']')) { |
| 264 | + if (currentToken.length() > 0) { |
| 265 | + addToken(result, currentToken.toString(), inSubscript); |
| 266 | + currentToken.setLength(0); |
| 267 | + if (inSubscript) { |
| 268 | + inSubscript = false; |
270 | 269 | } |
271 | 270 | } |
| 271 | + if (c == '[') { |
| 272 | + inSubscript = true; // Record that the next token had an |
| 273 | + // array subscript on it. |
| 274 | + } |
| 275 | + } else { |
| 276 | + currentToken.append(c); |
272 | 277 | } |
| 278 | + i.next(); |
273 | 279 | } |
| 280 | + addToken(result, currentToken.toString(), inSubscript); |
| 281 | + |
274 | 282 | return Collections.unmodifiableList(result); |
275 | 283 | } |
276 | 284 | // Otherwise, this is a plain JSON object label |
277 | 285 | return Collections.singletonList(new KeyPiece(key, JsonToken.BEGIN_OBJECT)); |
278 | 286 | } |
279 | 287 |
|
280 | | - static List<String> findTokens(String data) { |
281 | | - List<String> tokens = new ArrayList<String>(); |
282 | | - boolean inQuotes = false; |
283 | | - StringBuilder currentToken = new StringBuilder(); |
284 | | - StringCharacterIterator i = new StringCharacterIterator(data); |
285 | | - while (i.current() != StringCharacterIterator.DONE) { |
286 | | - char c = i.current(); |
287 | | - if (c == '\'') { |
288 | | - inQuotes = !inQuotes; |
289 | | - } |
290 | | - if (!inQuotes && (c == '.' || c == '[' || c == ']')) { |
291 | | - // if (c == ']') { |
292 | | - // currentToken.append(c); |
293 | | - // } |
294 | | - if (currentToken.length() > 0) { |
295 | | - tokens.add(currentToken.toString()); |
296 | | - currentToken.setLength(0); |
| 288 | + static void addToken(List<KeyPiece> result, String s, boolean inSubscript) { |
| 289 | + if (s.startsWith("'")) { |
| 290 | + // Turn any "\u0027" in the key back into ' |
| 291 | + String modifiedKeyPiece = s.substring(1, s.length() - 1).replaceAll("\\\\u0027", "'"); |
| 292 | + result.add(new KeyPiece(modifiedKeyPiece, JsonToken.BEGIN_OBJECT)); |
| 293 | + } else if (inSubscript) { |
| 294 | + // [0] produces an array |
| 295 | + result.add(new KeyPiece(s, JsonToken.BEGIN_ARRAY)); |
| 296 | + } else { |
| 297 | + for (String s2 : s.split("\\.")) { |
| 298 | + if (!s2.isEmpty()) { |
| 299 | + result.add(new KeyPiece(s2, JsonToken.BEGIN_OBJECT)); |
297 | 300 | } |
298 | | - // if (c == '[') { |
299 | | - // currentToken.append(c); |
300 | | - // } |
301 | | - } else { |
302 | | - currentToken.append(c); |
303 | 301 | } |
304 | | - i.next(); |
305 | 302 | } |
306 | | - tokens.add(currentToken.toString()); |
307 | | - return Collections.unmodifiableList(tokens); |
308 | 303 | } |
309 | 304 |
|
310 | 305 | // TODO: Implement merge method |
|
0 commit comments