jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1084333 - in /jackrabbit/sandbox/jackrabbit-j3/src: main/java/org/apache/jackrabbit/j3/json/ main/java/org/apache/jackrabbit/j3/observation/ test/java/org/apache/jackrabbit/j3/json/
Date Tue, 22 Mar 2011 20:06:37 GMT
Author: thomasm
Date: Tue Mar 22 20:06:37 2011
New Revision: 1084333

URL: http://svn.apache.org/viewvc?rev=1084333&view=rev
Log:
Events with Jsop (Json diff) serialization

Added:
    jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/EventImpl.java
    jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestEvent.java
Modified:
    jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/observation/EventAddLock.java
    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/EventImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/EventImpl.java?rev=1084333&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/EventImpl.java
(added)
+++ jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/json/EventImpl.java
Tue Mar 22 20:06:37 2011
@@ -0,0 +1,120 @@
+/*
+ * 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 org.apache.jackrabbit.j3.mc.Val;
+
+/**
+ * A prototype implementation of an event that can be applied to a node.
+ */
+public class EventImpl {
+
+    final static int NODE_ADD = 0, NODE_REMOVE = 1, NODE_MOVE = 2, NODE_COPY = 3;
+    final static int PROPERTY_ADD = 4, PROPERTY_REMOVE = 5, PROPERTY_CHANGE = 6;
+
+    private String path;
+    private String newPath;
+    private int eventType;
+    private String nodeName;
+    private NodeImpl node;
+    private String propertyName;
+    private Val propertyValue;
+
+    public static EventImpl fromString(String json) {
+        JsopTokenizer t = new JsopTokenizer(json);
+        EventImpl event = new EventImpl();
+        event.path = t.readString();
+        if (t.matches('+')) {
+            if (t.matches('>')) {
+                event.eventType = NODE_COPY;
+                event.newPath = t.readString();
+            } else {
+                String name = t.readString();
+                t.read(':');
+                if (t.matches('{')) {
+                    event.eventType = NODE_ADD;
+                    event.nodeName = name;
+                    event.node = NodeImpl.parse(t);
+                } else {
+                    event.eventType = PROPERTY_ADD;
+                    event.propertyName = name;
+                    event.propertyValue = NodeImpl.parseValue(t);
+                }
+            }
+        } else if (t.matches('-')) {
+            event.eventType = NODE_REMOVE;
+            event.nodeName = t.readString();
+        } else if (t.matches('^')) {
+            event.propertyName = t.readString();
+            t.read(':');
+            if (t.matches(JsopTokenizer.NULL)) {
+                event.eventType = PROPERTY_REMOVE;
+            } else {
+                event.eventType = PROPERTY_CHANGE;
+                event.propertyValue = NodeImpl.parseValue(t);
+            }
+        } else if (t.matches('>')) {
+            event.eventType = NODE_MOVE;
+            event.newPath = t.readString();
+        }
+        return event;
+    }
+
+    public String toString() {
+        StringBuilder buff = new StringBuilder();
+        buff.append(JsopTokenizer.encode(path)).append('\n');
+        switch (eventType) {
+        case NODE_ADD:
+            buff.append("+ ");
+            buff.append(JsopTokenizer.encode(nodeName));
+            buff.append(": ");
+            buff.append(node.toString(Integer.MAX_VALUE));
+            break;
+        case NODE_REMOVE:
+            buff.append("- ");
+            buff.append(JsopTokenizer.encode(nodeName));
+            break;
+        case NODE_MOVE:
+            buff.append("> ");
+            buff.append(JsopTokenizer.encode(newPath));
+            break;
+        case NODE_COPY:
+            buff.append("+ > ");
+            buff.append(JsopTokenizer.encode(newPath));
+            break;
+        case PROPERTY_ADD:
+            buff.append("+ ");
+            buff.append(JsopTokenizer.encode(propertyName));
+            buff.append(": ");
+            buff.append(propertyValue.toString());
+            break;
+        case PROPERTY_REMOVE:
+            buff.append("^ ");
+            buff.append(JsopTokenizer.encode(propertyName));
+            buff.append(": null");
+            break;
+        case PROPERTY_CHANGE:
+            buff.append("^ ");
+            buff.append(JsopTokenizer.encode(propertyName));
+            buff.append(": ");
+            buff.append(propertyValue.toString());
+            break;
+        }
+        return buff.toString();
+    }
+
+}

Modified: jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/observation/EventAddLock.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/observation/EventAddLock.java?rev=1084333&r1=1084332&r2=1084333&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/observation/EventAddLock.java
(original)
+++ jackrabbit/sandbox/jackrabbit-j3/src/main/java/org/apache/jackrabbit/j3/observation/EventAddLock.java
Tue Mar 22 20:06:37 2011
@@ -24,7 +24,6 @@ import org.apache.jackrabbit.j3.lock.Loc
 import org.apache.jackrabbit.j3.mc.Bundle;
 import org.apache.jackrabbit.j3.mc.Val;
 
-
 /**
  * Add a lock.
  */

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=1084333&r1=1084332&r2=1084333&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 20:06:37 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(TestEvent.class);
         suite.addTestSuite(TestNode.class);
         suite.addTestSuite(TestTokenizer.class);
 

Added: jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestEvent.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestEvent.java?rev=1084333&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestEvent.java
(added)
+++ jackrabbit/sandbox/jackrabbit-j3/src/test/java/org/apache/jackrabbit/j3/json/TestEvent.java
Tue Mar 22 20:06:37 2011
@@ -0,0 +1,59 @@
+/*
+ * 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 diff and back.
+ */
+public class TestEvent extends TestCase {
+
+    public void test() {
+        EventImpl event;
+
+        // add node
+        event = EventImpl.fromString("\"/testRoot\" + \"x\": { \"id\": 1 }");
+        assertEquals("\"/testRoot\"\n+ \"x\": {\"id\": 1\n}", event.toString());
+
+        // remove node
+        event = EventImpl.fromString("\"/testRoot\" - \"x\"");
+        assertEquals("\"/testRoot\"\n- \"x\"", event.toString());
+
+        // move node
+        event = EventImpl.fromString("\"/testRoot/x\" > \"/testRoot/y\"");
+        assertEquals("\"/testRoot/x\"\n> \"/testRoot/y\"", event.toString());
+
+        // copy node
+        event = EventImpl.fromString("\"/testRoot/x\" + > \"/testRoot/y\"");
+        assertEquals("\"/testRoot/x\"\n+ > \"/testRoot/y\"", event.toString());
+
+        // add property
+        event = EventImpl.fromString("\"/testRoot/x\" + \"id\": 2");
+        assertEquals("\"/testRoot/x\"\n+ \"id\": 2", event.toString());
+
+        // remove property
+        event = EventImpl.fromString("\"/testRoot/x\" ^ \"id\": null");
+        assertEquals("\"/testRoot/x\"\n^ \"id\": null", event.toString());
+
+        // change property
+        event = EventImpl.fromString("\"/testRoot/x\" ^ \"id\": 3");
+        assertEquals("\"/testRoot/x\"\n^ \"id\": 3", event.toString());
+
+    }
+
+}



Mime
View raw message