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 E04A34B32 for ; Fri, 10 Jun 2011 14:24:16 +0000 (UTC) Received: (qmail 46904 invoked by uid 500); 10 Jun 2011 14:24:16 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 46873 invoked by uid 500); 10 Jun 2011 14: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 46866 invoked by uid 99); 10 Jun 2011 14:24:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 10 Jun 2011 14:24:16 +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; Fri, 10 Jun 2011 14:24:13 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4777623889BF; Fri, 10 Jun 2011 14:23:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1134331 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java Date: Fri, 10 Jun 2011 14:23:52 -0000 To: commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110610142352.4777623889BF@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: thomasm Date: Fri Jun 10 14:23:51 2011 New Revision: 1134331 URL: http://svn.apache.org/viewvc?rev=1134331&view=rev Log: Simplify building JSOP, and correctly encode keys and values. 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=1134331&r1=1134330&r2=1134331&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 Fri Jun 10 14:23:51 2011 @@ -107,23 +107,15 @@ public class MicroKernelImpl implements throw new MicroKernelException(e); } - StringBuilder buf = new StringBuilder(); - buf.append("["); + JsopBuilder buff = new JsopBuilder().array(); for (int i = history.size() - 1; i >= 0; i--) { Commit commit = history.get(i); - buf.append("{"); - buf.append("\"id\":\""); - buf.append(commit.getId()); - buf.append("\",\"ts\":\""); - buf.append(commit.getCommitTS()); - buf.append("\"},"); - } - // trim redundant trailing comma - if (buf.charAt(buf.length() - 1) == ',') { - buf.deleteCharAt(buf.length() - 1); + buff.object(). + key("id").value(commit.getId()). + key("ts").value(commit.getCommitTS()). + endObject(); } - buf.append("]"); - return buf.toString(); + return buff.endArray().toString(); } public String getJournal(String fromRevisionId, String toRevisionId) throws MicroKernelException { @@ -149,21 +141,16 @@ public class MicroKernelImpl implements throw new MicroKernelException(e); } - final StringBuilder buf = new StringBuilder(); - buf.append("["); + JsopBuilder commitBuff = new JsopBuilder().array(); for (int i = commits.size() - 1; i >= 0; i--) { Commit commit = commits.get(i); if (commit.getParentId() == null) { continue; } - buf.append("{"); - buf.append("\"id\":\""); - buf.append(commit.getId()); - buf.append("\",\"ts\":\""); - buf.append(commit.getCommitTS()); - buf.append("\",\"changes\":\""); - - // todo escape double quotes in diff + commitBuff.object(). + key("id").value(commit.getId()). + key("ts").value(commit.getCommitTS()); + final JsopBuilder buff = new JsopBuilder(); try { String path = "/"; Node node1 = rep.getNode(commit.getParentId(), path); @@ -171,45 +158,40 @@ public class MicroKernelImpl implements NodeUtils.diff(path, node1, node2, true, rep.getStore(), new NodeDiffHandler() { public void propAdded(String nodePath, String propName, String value) { - buf.append("+\""); - buf.append(PathUtil.concat(nodePath, propName)); - buf.append("\" : \""); - buf.append(value); - buf.append("\"\n"); + buff.append("+ "). + key(PathUtil.concat(nodePath, propName)). + value(value). + append("\n"); } public void propChanged(String nodePath, String propName, String oldValue, String newValue) { - buf.append("^\""); - buf.append(PathUtil.concat(nodePath, propName)); - buf.append("\" : \""); - buf.append(newValue); - buf.append("\"\n"); + buff.append("^ "). + key(PathUtil.concat(nodePath, propName)). + value(newValue). + append("\n"); } public void propDeleted(String nodePath, String propName, String value) { - buf.append("-\""); - buf.append(PathUtil.concat(nodePath, propName)); - buf.append("\"\n"); + buff.append("- "). + key(PathUtil.concat(nodePath, propName)). + append("\n"); } public void childNodeAdded(String nodePath, String childName, String id) { - buf.append("+\""); - buf.append(PathUtil.concat(nodePath, childName)); - buf.append("\" : "); + buff.append("+ "). + key(PathUtil.concat(nodePath, childName)); try { - toJson(rep.getStore().getNode(id), childName, Integer.MAX_VALUE, false, buf); + toJson(rep.getStore().getNode(id), childName, Integer.MAX_VALUE, false, buff); } catch (Exception e) { - buf.append("\"ERROR: failed to retrieve node "); - buf.append(id); - buf.append("\""); + buff.value("ERROR: failed to retrieve node " + id); } - buf.append("\n"); + buff.append("\n"); } public void childNodeDeleted(String nodePath, String childName, String id) { - buf.append("-\""); - buf.append(PathUtil.concat(nodePath, childName)); - buf.append("\"\n"); + buff.append("- "); + buff.value(PathUtil.concat(nodePath, childName)); + buff.append("\n"); } public void childNodeChanged(String nodePath, String childName, String oldId, String newId) { @@ -219,16 +201,9 @@ public class MicroKernelImpl implements } catch (Exception e) { throw new MicroKernelException(e); } - - buf.append("\"},"); - } - // trim redundant trailing comma - if (buf.charAt(buf.length() - 1) == ',') { - buf.deleteCharAt(buf.length() - 1); + commitBuff.key("changes").value(buff.toString()).endObject(); } - buf.append("]"); - - return buf.toString(); + return commitBuff.endArray().toString(); } public boolean nodeExists(String path, String revisionId) throws MicroKernelException { @@ -250,7 +225,7 @@ public class MicroKernelImpl implements } try { - StringBuilder buf = new StringBuilder(); + JsopBuilder buf = new JsopBuilder(); toJson(rep.getNode(revisionId, path), PathUtil.getName(path), depth, true, buf); return buf.toString(); } catch (Exception e) { @@ -265,8 +240,7 @@ public class MicroKernelImpl implements try { Node node = rep.getNode(revisionId, path); - StringBuilder buf = new StringBuilder(); - buf.append('['); + JsopBuilder buf = new JsopBuilder().array(); long pos = 0; for (Map.Entry entry : node.getChildNodeEntries().entrySet()) { if (pos < offset) { @@ -277,15 +251,9 @@ public class MicroKernelImpl implements break; } toJson(rep.getStore().getNode(entry.getValue()), entry.getKey(), depth, true, buf); - buf.append(','); pos++; } - // trim redundant trailing comma - if (buf.charAt(buf.length() - 1) == ',') { - buf.deleteCharAt(buf.length() - 1); - } - buf.append(']'); - return buf.toString(); + return buf.endArray().toString(); } catch (Exception e) { throw new MicroKernelException(e); } @@ -398,36 +366,29 @@ public class MicroKernelImpl implements //-------------------------------------------------------< implementation > - void toJson(Node node, String name, int depth, boolean metaProps, StringBuilder buf) throws Exception { - toJson(node, name, depth, metaProps, JsonBuilder.create(buf)); - } - - void toJson(Node node, String name, int depth, boolean metaProps, JsonObjectBuilder builder) throws Exception { + void toJson(Node node, String name, int depth, boolean metaProps, JsopBuilder builder) throws Exception { if (metaProps) { - builder.value(":name", name); + builder.key(":name").value(name); } for (Map.Entry prop : node.getProperties().entrySet()) { - builder.valueEncoded(prop.getKey(), prop.getValue()); + builder.key(prop.getKey()).encodedValue(prop.getValue()); } long childCount = node.getChildNodeCount(); if (metaProps) { - builder.value(":childNodeCount", childCount); + builder.key(":childNodeCount").value(childCount); } if (childCount > 0 && depth >= 0) { for (Map.Entry child : node.getChildNodeEntries().entrySet()) { String childName = child.getKey(); - JsonObjectBuilder childBuilder = builder.object(childName); + builder.key(childName).object(); if (depth > 0) { String childId = child.getValue(); - toJson(rep.getStore().getNode(childId), childName, depth - 1, metaProps, childBuilder); - } - else { - childBuilder.build(); + toJson(rep.getStore().getNode(childId), childName, depth - 1, metaProps, builder); } + builder.endObject(); } } - builder.build(); } static void addNode(CommitBuilder cb, String path, String name, JSONObject jsonNode) throws Exception {