Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7A2C46961 for ; Sat, 11 Jun 2011 07:24:16 +0000 (UTC) Received: (qmail 50091 invoked by uid 500); 11 Jun 2011 07:24:16 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 50057 invoked by uid 500); 11 Jun 2011 07:24:16 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 50050 invoked by uid 99); 11 Jun 2011 07:24:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 11 Jun 2011 07:24:15 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 11 Jun 2011 07:24:14 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 69E5523889BF; Sat, 11 Jun 2011 07:23:54 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1134534 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java Date: Sat, 11 Jun 2011 07:23:54 -0000 To: commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110611072354.69E5523889BF@eris.apache.org> 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 list = new LinkedList(); + addNode(list, parentPath, nodeName, t); + // add the nodes in reverse order + for (Iterator 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 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 props = new HashMap(); + } + static class JSONObject extends LinkedHashMap{}; static class JSONArray extends LinkedList { public String toString() {