jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1134534 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Date Sat, 11 Jun 2011 07:23:54 GMT
Author: thomasm
Date: Sat Jun 11 07:23:54 2011
New Revision: 1134534

URL: http://svn.apache.org/viewvc?rev=1134534&view=rev
Log:
Use the JSOP parser in stead of the JSON parser.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java?rev=1134534&r1=1134533&r2=1134534&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Sat Jun 11 07:23:54 2011
@@ -20,9 +20,8 @@ import org.apache.jackrabbit.mk.api.Micr
 import org.apache.jackrabbit.mk.api.MicroKernelException;
 import org.apache.jackrabbit.mk.json.DiffHandler;
 import org.apache.jackrabbit.mk.json.DiffParser;
-import org.apache.jackrabbit.mk.json.JsonBuilder;
 import org.apache.jackrabbit.mk.json.JsopBuilder;
-import org.apache.jackrabbit.mk.json.JsonBuilder.JsonObjectBuilder;
+import org.apache.jackrabbit.mk.json.JsopTokenizer;
 import org.apache.jackrabbit.mk.store.Commit;
 import org.apache.jackrabbit.mk.store.CommitBuilder;
 import org.apache.jackrabbit.mk.store.Node;
@@ -271,7 +270,7 @@ public class MicroKernelImpl implements 
         }
     }
 
-    public String commit(final String path, String jsonDiff, String revisionId) throws MicroKernelException
{
+    public String commitOld(final String path, String jsonDiff, String revisionId) throws
MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
         }
@@ -334,6 +333,73 @@ public class MicroKernelImpl implements 
         return newHeadRev;
     }
 
+    public String commit(String path, String jsonDiff, String revisionId) throws MicroKernelException
{
+        if (rep == null) {
+            throw new IllegalStateException("this instance has already been disposed");
+        }
+        try {
+            JsopTokenizer t = new JsopTokenizer(jsonDiff);
+            CommitBuilder cb = rep.getCommitBuilder(revisionId);
+            while (true) {
+                int r = t.read();
+                if (r == JsopTokenizer.END) {
+                    break;
+                }
+                switch (r) {
+                case '+': {
+                    String relPath = t.readString();
+                    t.read(':');
+                    t.read('{');
+                    String nodePath = PathUtil.concat(path, relPath);
+                    String parentPath = PathUtil.getParentPath(nodePath);
+                    String nodeName = PathUtil.getName(nodePath);
+                    // build the list of added nodes recursively
+                    LinkedList<AddNodeOperation> list = new LinkedList<AddNodeOperation>();
+                    addNode(list, parentPath, nodeName, t);
+                    // add the nodes in reverse order
+                    for (Iterator<AddNodeOperation> it = list.descendingIterator();
it.hasNext(); ) {
+                        AddNodeOperation op = it.next();
+                        cb.addNode(op.path, op.name, op.props);
+                    }
+                    break;
+                }
+                case '-': {
+                    String relPath = t.readString();
+                    cb.removeNode(PathUtil.concat(path, relPath));
+                    break;
+                }
+                case '^': {
+                    String relPath = t.readString();
+                    t.read(':');
+                    String value;
+                    if (t.matches(JsopTokenizer.NULL)) {
+                        value = null;
+                    } else {
+                        JsopBuilder buff = new JsopBuilder();
+                        parseRawValue(t, buff);
+                        value = buff.toString();
+                    }
+                    String nodePath = PathUtil.concat(path, relPath);
+                    String parentPath = PathUtil.getParentPath(nodePath);
+                    String propName = PathUtil.getName(nodePath);
+                    cb.setProperty(parentPath, propName, value);
+                    break;
+                }
+                case '>': {
+                    String relPath = t.readString();
+                    String targetPath = t.readString();
+                    cb.moveNode(PathUtil.concat(path, relPath), targetPath);
+                }
+                default:
+                    throw new AssertionError("token type: " + t.getTokenType());
+                }
+            }
+            return cb.doCommit();
+        } catch (Exception e) {
+            throw new MicroKernelException(e);
+        }
+    }
+
     public long getLength(String blobId) throws MicroKernelException {
         if (rep == null) {
             throw new IllegalStateException("this instance has already been disposed");
@@ -414,7 +480,67 @@ public class MicroKernelImpl implements 
         }
     }
 
+    private static void parseRawValue(JsopTokenizer t, JsopBuilder buff) {
+        if (t.matches('[')) {
+            buff.array();
+            if (!t.matches(']')) {
+                do {
+                    parseRawValue(t, buff);
+                } while (t.matches(','));
+                t.read(']');
+                buff.endArray();
+            }
+        } else if (t.matches('{')) {
+            buff.object();
+            if (!t.matches('}')) {
+                do {
+                    buff.key(t.readString());
+                    t.read(':');
+                    parseRawValue(t, buff);
+                } while (t.matches(','));
+                t.read('}');
+                buff.endObject();
+            }
+        } else if (t.matches(JsopTokenizer.NUMBER)) {
+            buff.encodedValue(t.getToken());
+        } else if (t.matches(JsopTokenizer.TRUE)) {
+            buff.value(true);
+        } else if (t.matches(JsopTokenizer.FALSE)) {
+            buff.value(false);
+        } else if (t.matches(JsopTokenizer.COMMENT)) {
+            buff.append(t.getToken());
+            buff.value(t.readString());
+        } else if (t.matches(JsopTokenizer.STRING)) {
+            buff.encodedValue("\"" + t.lastEncodedString() + "\"");
+        }
+    }
+
+    static void addNode(LinkedList<AddNodeOperation> list, String path, String name,
JsopTokenizer t) throws Exception {
+        AddNodeOperation op = new AddNodeOperation();
+        if (!t.matches('}')) {
+            do {
+                String key = t.readString();
+                t.read(':');
+                if (t.matches('{')) {
+                    addNode(list, PathUtil.concat(path, name), key, t);
+                } else {
+                    JsopBuilder buff = new JsopBuilder();
+                    parseRawValue(t, buff);
+                    op.props.put(key, buff.toString());
+                }
+            } while (t.matches(','));
+            t.read('}');
+        }
+        list.add(op);
+    }
+
     //--------------------------------------------------------< inner classes >
+    static class AddNodeOperation {
+        String path;
+        String name;
+        Map<String, String> props = new HashMap<String, String>();
+    }
+
     static class JSONObject extends LinkedHashMap<String, Object>{};
     static class JSONArray extends LinkedList {
         public String toString() {



Mime
View raw message