jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1197063 - /jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java
Date Thu, 03 Nov 2011 11:07:57 GMT
Author: mduerig
Date: Thu Nov  3 11:07:56 2011
New Revision: 1197063

URL: http://svn.apache.org/viewvc?rev=1197063&view=rev
Log:
Microkernel based Jackrabbit prototype (WIP): minor improvements

Modified:
    jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java

Modified: jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java?rev=1197063&r1=1197062&r2=1197063&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java
(original)
+++ jackrabbit/sandbox/jackrabbit-mk/jackrabbit-spi2microkernel/src/main/java/org/apache/jackrabbit/spi2microkernel/json/DefaultJsonTokenizer.java
Thu Nov  3 11:07:56 2011
@@ -42,31 +42,23 @@ public class DefaultJsonTokenizer extend
     @Override
     protected Token nextToken() {
         skipWhiteSpace();
-        if (pos < json.length()) {
-            switch (json.charAt(pos)) {
-                case '{': return createToken(Type.BEGIN_OBJECT, "{", pos++);
-                case '}': return createToken(Type.END_OBJECT, "}", pos++);
-                case '[': return createToken(Type.BEGIN_ARRAY, "[", pos++);
-                case ']': return createToken(Type.END_ARRAY, "]", pos++);
-                case ':': return createToken(Type.COLON, ":", pos++);
-                case ',': return createToken(Type.COMMA, ",", pos++);
-                case 't': return readLiteral(Type.TRUE, "true");
-                case 'f': return readLiteral(Type.FALSE, "false");
-                case 'n': return readLiteral(Type.NULL, "null");
-                case '"': return readString();
-                default:
-                    Token token = tryReadNumber();
-                    if (token != null) {
-                        return token;
-                    }
-                    else {
-                        return readUnknown();
-                    }
-            }
-        }
-        else {
+        if (pos >= json.length()) {
             return createToken(Type.EOF, "", pos);
         }
+
+        switch (json.charAt(pos)) {
+            case '{': return createToken(Type.BEGIN_OBJECT, "{", pos++);
+            case '}': return createToken(Type.END_OBJECT, "}", pos++);
+            case '[': return createToken(Type.BEGIN_ARRAY, "[", pos++);
+            case ']': return createToken(Type.END_ARRAY, "]", pos++);
+            case ':': return createToken(Type.COLON, ":", pos++);
+            case ',': return createToken(Type.COMMA, ",", pos++);
+            case 't': return readLiteral(Type.TRUE, "true");
+            case 'f': return readLiteral(Type.FALSE, "false");
+            case 'n': return readLiteral(Type.NULL, "null");
+            case '"': return readString();
+            default:  return isNumber() ? readNumber() : readUnknown();
+        }
     }
 
     @Override
@@ -137,7 +129,7 @@ public class DefaultJsonTokenizer extend
     private static final Pattern NUMBER_PATTERN = Pattern.compile(
             "(\\+|-)?(\\d+)((\\.)(\\d+))?(((e|E)(\\+|-)?)(\\d+))?");
 
-    protected Token tryReadNumber() {
+    protected Token readNumber() {
         Matcher matcher = NUMBER_PATTERN.matcher(json.substring(pos));
         if (matcher.lookingAt()) {
             Token token = createToken(Type.NUMBER, matcher.group(), pos);
@@ -145,7 +137,7 @@ public class DefaultJsonTokenizer extend
             return token;
         }
         else {
-            return null;
+            throw new ParseException(pos, "Expected number, found. " + excerpt(json, pos,
40));
         }
     }
 
@@ -159,6 +151,14 @@ public class DefaultJsonTokenizer extend
 
     //------------------------------------------< private >---
 
+    private boolean isNumber() {
+        // true if first character is a digit or a sign and second character is a digit
+        char first = json.charAt(pos);
+        return !(!Character.isDigit(first) &&
+                ('+' != first && '-' != first || pos + 1 >= json.length() ||
+                 !Character.isDigit(json.charAt(pos + 1))));
+    }
+
     private static String excerpt(String string, int pos, int len) {
         return string.substring(pos, Math.min(string.length(), pos + len)) + "...";
     }



Mime
View raw message