jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1084332 - in /jackrabbit/sandbox/jackrabbit-j3/src: main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java main/java/org/apache/jackrabbit/j3/json/NodeImpl.java test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java
Date Tue, 22 Mar 2011 20:05:38 GMT
Author: thomasm
Date: Tue Mar 22 20:05:38 2011
New Revision: 1084332

URL: http://svn.apache.org/viewvc?rev=1084332&view=rev
Log:
Json/Jsop Tokenizer

Modified:
    jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java
    jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java

Modified: jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java?rev=1084332&r1=1084331&r2=1084332&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/JsopTokenizer.java
Tue Mar 22 20:05:38 2011
@@ -82,6 +82,15 @@ public class JsopTokenizer {
     }
 
     /**
+     * Read a string.
+     *
+     * @throws IllegalStateException if the token type doesn't match
+     */
+    public String readString() {
+        return read(STRING);
+    }
+
+    /**
      * Read a token which must match a given token type.
      *
      * @param type the token type
@@ -155,6 +164,8 @@ public class JsopTokenizer {
         case '+':
         case ':':
         case ',':
+        case '>':
+        case '^':
             return ch;
         case '-':
             ch = jsop.charAt(pos);
@@ -294,6 +305,9 @@ public class JsopTokenizer {
      */
     public static String encode(String s) {
         int length = s.length();
+        if (length == 0) {
+            return "\"\"";
+        }
         StringBuilder buff = new StringBuilder(length + 2);
         buff.append('\"');
         for (int i = 0; i < length; i++) {
@@ -329,17 +343,28 @@ public class JsopTokenizer {
                 break;
             default:
                 int ch = c;
-                if (ch >= ' ' && ch <= 127) {
-                    buff.append(c);
-                } else {
+                if (ch < ' ') {
+                    // guaranteed to be 1 or 2 hex digits only
+                    buff.append("\\u00");
+                    String hex = Integer.toHexString(c);
+                    if (hex.length() == 1) {
+                        buff.append('0');
+                    }
+                    buff.append(hex);
+                } else if (ch > 127) {
+                    // ascii only mode
                     buff.append("\\u");
-                    // make sure it's four characters
-                    buff.append(Integer.toHexString(0x10000 | ch).substring(1));
+                    String hex = Integer.toHexString(c);
+                    for (int len = hex.length(); len < 4; len++) {
+                        buff.append('0');
+                    }
+                    buff.append(hex);
+                } else {
+                    buff.append(c);
                 }
             }
         }
-        buff.append('\"');
-        return buff.toString();
+        return buff.append('\"').toString();
     }
 
     private static String getTokenType(int type) {

Modified: jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java?rev=1084332&r1=1084331&r2=1084332&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
Tue Mar 22 20:05:38 2011
@@ -23,6 +23,9 @@ import java.util.Map.Entry;
 import javax.jcr.PropertyType;
 import org.apache.jackrabbit.j3.mc.Val;
 
+/**
+ * A prototype implementation of a node with child nodes.
+ */
 public class NodeImpl {
 
     private HashMap<String, Val> properties;
@@ -88,11 +91,11 @@ public class NodeImpl {
         return parse(t);
     }
 
-    private static NodeImpl parse(JsopTokenizer t) {
+    static NodeImpl parse(JsopTokenizer t) {
         NodeImpl node = new NodeImpl();
         if (!t.matches('}')) {
             do {
-                String key = t.read(JsopTokenizer.STRING);
+                String key = t.readString();
                 t.read(':');
                 if (t.matches('{')) {
                     node.addChildNode(key, parse(t));
@@ -106,7 +109,7 @@ public class NodeImpl {
         return node;
     }
 
-    private static Val parseValue(JsopTokenizer t) {
+    static Val parseValue(JsopTokenizer t) {
         if (t.matches('[')) {
             ArrayList<Val> list = new ArrayList<Val>();
             if (!t.matches(']')) {
@@ -118,7 +121,7 @@ public class NodeImpl {
             Val[] array = new Val[list.size()];
             list.toArray(array);
             return Val.get(array);
-        } else if(t.matches(JsopTokenizer.NUMBER)) {
+        } else if (t.matches(JsopTokenizer.NUMBER)) {
             String token = t.getToken();
             if (token.indexOf('.') >= 0) {
                 if (token.indexOf('e') >= 0) {
@@ -132,8 +135,7 @@ public class NodeImpl {
         } else if (t.matches(JsopTokenizer.FALSE)) {
             return Val.FALSE;
         }
-        t.read(JsopTokenizer.STRING);
-        return Val.get(t.getToken());
+        return Val.get(t.readString());
     }
 
 }

Modified: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java?rev=1084332&r1=1084331&r2=1084332&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestTokenizer.java
Tue Mar 22 20:05:38 2011
@@ -24,6 +24,7 @@ import junit.framework.TestCase;
 public class TestTokenizer extends TestCase {
 
     public void test() {
+
         try {
             JsopTokenizer.decode("test\\");
             fail();
@@ -54,7 +55,12 @@ public class TestTokenizer extends TestC
         test("- \"test/test\"", "-\"test\\/test\"");
         test(" {\n\"x\": 1, \"y\": 2\n}\n", "{\"x\":1, \"y\":2}");
         test(" [\ntrue, false, null\n]\n", "[true, false, null]");
-        test("\"-\\\\-\\\"-\\b-\\f-\\n-\\r-\\t-\\u1234\"", "\"-\\\\-\\\"-\\b-\\f-\\n-\\r-\\t-\\u1234\"");
+        test("\"\"", "\"\"");
+        test("\"\\u0003\"", "\"\\u0003\"");
+        test("\"\\u0012\"", "\"\\u0012\"");
+        test("\"\\u0123\"", "\"\\u0123\"");
+        test("\"\\u1234\"", "\"\\u1234\"");
+        test("\"-\\\\-\\\"-\\b-\\f-\\n-\\r-\\t\"", "\"-\\\\-\\\"-\\b-\\f-\\n-\\r-\\t\"");
         test("\"-\\b-\\f-\\n-\\r-\\t\"", "\"-\b-\f-\n-\r-\t\"");
         test(" [\n0, 12, -1, 0.1, -0.1, -2.3e1, 1e+1, 1.e-20\n]\n", "[0,12,-1,0.1,-0.1,-2.3e1,1e+1,1.e-20]");
         test("\"Hello\"", "\"Hello\"");



Mime
View raw message