jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1134331 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
Date Fri, 10 Jun 2011 14:23:52 GMT
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<String, String> 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<String, String> 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<String, String> 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 {



Mime
View raw message