jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1084155 - in /jackrabbit/sandbox/jackrabbit-j3/src: main/java/org/apache/jackrabbit/j3/json/NodeImpl.java test/java/org/apache/jackrabbit/j3/json/TestAll.java test/java/org/apache/jackrabbit/j3/json/TestNode.java
Date Tue, 22 Mar 2011 13:00:07 GMT
Author: thomasm
Date: Tue Mar 22 13:00:07 2011
New Revision: 1084155

URL: http://svn.apache.org/viewvc?rev=1084155&view=rev
Log:
Node <-> Json

Added:
    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/TestNode.java
Modified:
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestAll.java

Added: 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=1084155&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
(added)
+++ jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/NodeImpl.java
Tue Mar 22 13:00:07 2011
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.j3.json;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map.Entry;
+import javax.jcr.PropertyType;
+import org.apache.jackrabbit.j3.mc.Val;
+
+public class NodeImpl {
+
+    private HashMap<String, Val> properties;
+    private LinkedHashMap<String, NodeImpl> childNodes;
+
+    void setProperty(String name, Val value) {
+        if (properties == null) {
+            properties = new HashMap<String, Val>();
+        }
+        if (value == null) {
+            properties.remove(name);
+        } else {
+            properties.put(name, value);
+        }
+    }
+
+    void addChildNode(String name, NodeImpl node) {
+        if (childNodes == null) {
+            childNodes = new LinkedHashMap<String, NodeImpl>();
+        }
+        childNodes.put(name, node);
+    }
+
+    public String toString() {
+        return toString(0);
+    }
+
+    public String toString(int childNodeLevels) {
+        StringBuilder buff = new StringBuilder();
+        buff.append('{');
+        int i = 0;
+        if (properties != null) {
+            for (Entry<String, Val> p : properties.entrySet()) {
+                if (i++ > 0) {
+                    buff.append(",\n");
+                }
+                buff.append(JsopTokenizer.encode(p.getKey())).append(": ");
+                buff.append(p.getValue());
+            }
+        }
+        if (childNodes != null) {
+            if (i++ > 0) {
+                buff.append(",\n");
+            }
+            buff.append("\":childNodeCount\": ").append(childNodes.size());
+            if (childNodeLevels > 0) {
+                for (Entry<String, NodeImpl> c : childNodes.entrySet()) {
+                    if (i++ > 0) {
+                        buff.append(",\n");
+                    }
+                    buff.append(JsopTokenizer.encode(c.getKey())).append(": ");
+                    buff.append(c.getValue().toString(childNodeLevels - 1));
+                }
+            }
+        }
+        buff.append("\n}");
+        return buff.toString();
+    }
+
+    public static NodeImpl fromString(String json) {
+        JsopTokenizer t = new JsopTokenizer(json);
+        t.read('{');
+        return parse(t);
+    }
+
+    private static NodeImpl parse(JsopTokenizer t) {
+        NodeImpl node = new NodeImpl();
+        if (!t.matches('}')) {
+            do {
+                String key = t.read(JsopTokenizer.STRING);
+                t.read(':');
+                if (t.matches('{')) {
+                    node.addChildNode(key, parse(t));
+                } else {
+                    Val val = parseValue(t);
+                    node.setProperty(key, val);
+                }
+            } while (t.matches(','));
+            t.read('}');
+        }
+        return node;
+    }
+
+    private static Val parseValue(JsopTokenizer t) {
+        if (t.matches('[')) {
+            ArrayList<Val> list = new ArrayList<Val>();
+            if (!t.matches(']')) {
+                do {
+                    list.add(parseValue(t));
+                } while (t.matches(','));
+                t.read(']');
+            }
+            Val[] array = new Val[list.size()];
+            list.toArray(array);
+            return Val.get(array);
+        } else if(t.matches(JsopTokenizer.NUMBER)) {
+            String token = t.getToken();
+            if (token.indexOf('.') >= 0) {
+                if (token.indexOf('e') >= 0) {
+                    return Val.get(Double.parseDouble(token));
+                }
+                return Val.get(PropertyType.DECIMAL, token);
+            }
+            return Val.get(Long.parseLong(token));
+        } else if (t.matches(JsopTokenizer.TRUE)) {
+            return Val.TRUE;
+        } else if (t.matches(JsopTokenizer.FALSE)) {
+            return Val.FALSE;
+        }
+        t.read(JsopTokenizer.STRING);
+        return Val.get(t.getToken());
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestAll.java?rev=1084155&r1=1084154&r2=1084155&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestAll.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestAll.java
Tue Mar 22 13:00:07 2011
@@ -35,6 +35,7 @@ public class TestAll extends TestCase {
     public static Test suite() {
         TestSuite suite = new TestSuite("org.apache.jackrabbit.j3.json");
 
+        suite.addTestSuite(TestNode.class);
         suite.addTestSuite(TestTokenizer.class);
 
         return suite;

Added: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestNode.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestNode.java?rev=1084155&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestNode.java
(added)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestNode.java
Tue Mar 22 13:00:07 2011
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.j3.json;
+
+import junit.framework.TestCase;
+
+/**
+ * Test converting a node to Json and back.
+ */
+public class TestNode extends TestCase {
+
+    public void test() {
+        NodeImpl node;
+
+        node = NodeImpl.fromString("{\"id\": 1}");
+        assertEquals("{\"id\": 1\n}", node.toString());
+
+        node = NodeImpl.fromString("{\"id\": 1, \"name\": \"Hello World\"}");
+        assertEquals("{\"id\": 1,\n\"name\": \"Hello World\"\n}", node.toString());
+
+        node = NodeImpl.fromString("{\"id\": 1, \"c1\": {}}");
+        assertEquals("{\"id\": 1,\n\":childNodeCount\": 1\n}", node.toString());
+        assertEquals("{\"id\": 1,\n\":childNodeCount\": 1\n}", node.toString(0));
+        assertEquals("{\"id\": 1,\n\":childNodeCount\": 1,\n\"c1\": {\n}\n}", node.toString(1));
+
+        node = NodeImpl.fromString("{\"t\": true, \"f\": false}");
+        assertEquals("{\"f\": false,\n\"t\": true\n}", node.toString());
+
+        node = NodeImpl.fromString("{\"id\": []}");
+        assertEquals("{\"id\": [  ]\n}", node.toString());
+        node = NodeImpl.fromString("{\"id\": [1]}");
+        assertEquals("{\"id\": [ 1 ]\n}", node.toString());
+        node = NodeImpl.fromString("{\"id\": [1, 2]}");
+        assertEquals("{\"id\": [ 1, 2 ]\n}", node.toString());
+        node.setProperty("id", null);
+        assertEquals("{\n}", node.toString());
+
+        node = NodeImpl.fromString("{\"long\": -230, \"decimal\": 230.0, \"double\": 230.0e1}");
+        assertEquals("{\"double\": /* Double */ 2300.0,\n\"long\": -230,\n\"decimal\": /*
Decimal */ 230.0\n}", node.toString());
+
+    }
+
+}



Mime
View raw message