jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1134325 - in /jackrabbit/sandbox/microkernel/src: main/java/org/apache/jackrabbit/mk/ main/java/org/apache/jackrabbit/mk/json/ main/java/org/apache/jackrabbit/mk/mem/ test/java/org/apache/jackrabbit/mk/json/
Date Fri, 10 Jun 2011 13:46:33 GMT
Author: thomasm
Date: Fri Jun 10 13:46:33 2011
New Revision: 1134325

URL: http://svn.apache.org/viewvc?rev=1134325&view=rev
Log:
JsopBuilder: use the same method names as JSONWriter / JSONStringer.

Modified:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/MicroKernelImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java
    jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.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=1134325&r1=1134324&r2=1134325&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 13:46:33 2011
@@ -457,16 +457,14 @@ public class MicroKernelImpl implements 
     static class JSONObject extends LinkedHashMap<String, Object>{};
     static class JSONArray extends LinkedList {
         public String toString() {
-            JsopBuilder buff = new JsopBuilder();
-            buff.arrayBegin();
+            JsopBuilder buff = new JsopBuilder().array();
             for (Object o : this) {
                 if (o instanceof String) {
                     o = JsopBuilder.encode(o.toString());
                 }
                 buff.value(o.toString());
             }
-            buff.arrayEnd();
-            return buff.toString();
+            return buff.endArray().toString();
         }
     }
 }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java?rev=1134325&r1=1134324&r2=1134325&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
Fri Jun 10 13:46:33 2011
@@ -20,27 +20,45 @@ import org.apache.jackrabbit.mk.mem.Cons
 
 /**
  * A builder for Json and Jsop strings. It encodes string values, and knows when
- * a comma is needed.
+ * a comma is needed. A comma is appended before '{', '[', a value, or a key;
+ * but only if the last appended token was '}', ']', or a value. There is no
+ * limit to the number of nesting levels.
  */
 public class JsopBuilder {
 
     private StringBuilder buff = new StringBuilder();
     private boolean needComma;
 
-    public JsopBuilder append(String token) {
-        buff.append(token);
+    /**
+     * Append an already formatted string, comment, or whitespace.
+     *
+     * @param string the string to append
+     * @return this
+     */
+    public JsopBuilder append(String string) {
+        buff.append(string);
         needComma = false;
         return this;
     }
 
-    public JsopBuilder objectBegin() {
+    /**
+     * Append '{'. A comma is appended first if needed.
+     *
+     * @return this
+     */
+    public JsopBuilder object() {
         optionalComma();
         buff.append('{');
         needComma = false;
         return this;
     }
 
-    public JsopBuilder objectEnd() {
+    /**
+     * Append '}'.
+     *
+     * @return this
+     */
+    public JsopBuilder endObject() {
         if (Constants.JSON_NEWLINES) {
             buff.append("\n}");
         } else {
@@ -50,44 +68,86 @@ public class JsopBuilder {
         return this;
     }
 
-    public JsopBuilder arrayBegin() {
+    /**
+     * Append '['. A comma is appended first if needed.
+     *
+     * @return this
+     */
+    public JsopBuilder array() {
         optionalComma();
         buff.append('[');
         needComma = false;
         return this;
     }
 
-    public JsopBuilder arrayEnd() {
+    /**
+     * Append ']'.
+     *
+     * @return this
+     */
+    public JsopBuilder endArray() {
         buff.append("]");
         needComma = true;
         return this;
     }
 
-    public JsopBuilder key(String propertyName) {
+    /**
+     * Append the key (in quotes) plus a colon. A comma is appended first if
+     * needed.
+     *
+     * @param name the key
+     * @return this
+     */
+    public JsopBuilder key(String name) {
         optionalComma();
         if (Constants.JSON_NEWLINES) {
             buff.append('\n');
         }
-        buff.append(encode(propertyName)).append(':');
+        buff.append(encode(name)).append(':');
         needComma = false;
         return this;
     }
 
+    /**
+     * Append a number. A comma is appended first if needed.
+     *
+     * @param value the value
+     * @return this
+     */
     public JsopBuilder value(long value) {
         return encodedValue(Long.toString(value));
     }
 
+    /**
+     * Append the boolean value 'true' or 'false'. A comma is appended first if
+     * needed.
+     *
+     * @param value the value
+     * @return this
+     */
     public JsopBuilder value(boolean value) {
         return encodedValue(Boolean.toString(value));
     }
 
+    /**
+     * Append a string or null. A comma is appended first if needed.
+     *
+     * @param value the value
+     * @return this
+     */
     public JsopBuilder value(String value) {
         return encodedValue(encode(value));
     }
 
-    public JsopBuilder encodedValue(String encodedValue) {
+    /**
+     * Append an already encoded value. A comma is appended first if needed.
+     *
+     * @param value the value
+     * @return this
+     */
+    public JsopBuilder encodedValue(String value) {
         optionalComma();
-        buff.append(encodedValue);
+        buff.append(value);
         needComma = true;
         return this;
     }
@@ -98,6 +158,9 @@ public class JsopBuilder {
         }
     }
 
+    /**
+     * Get the generated string.
+     */
     public String toString() {
         return buff.toString();
     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java?rev=1134325&r1=1134324&r2=1134325&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
(original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/NodeImpl.java
Fri Jun 10 13:46:33 2011
@@ -147,7 +147,7 @@ public class NodeImpl {
     public String toString(boolean includeProperties, int depth, int childOffset, int childCount)
{
         JsopBuilder json = new JsopBuilder();
         if (includeProperties) {
-            json.objectBegin();
+            json.object();
             if (properties != null) {
                 for (Entry<String, Val> e : properties.entrySet()) {
                     json.key(e.getKey()).encodedValue(e.getValue().toString());
@@ -167,7 +167,7 @@ public class NodeImpl {
             }
         }
         if (includeProperties) {
-            json.objectEnd();
+            json.endObject();
         }
         return json.toString();
     }

Modified: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java?rev=1134325&r1=1134324&r2=1134325&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java (original)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/mem/Val.java Fri
Jun 10 13:46:33 2011
@@ -294,14 +294,14 @@ public class Val implements Comparable<V
         switch (type) {
         case TYPE_MULTI_VALUE: {
             JsopBuilder buff = new JsopBuilder();
-            buff.arrayBegin();
+            buff.array();
             for (Val v : (Val[]) value) {
                 // should in theory never be null (unless there is a bug)
                 if (v != null) {
                     buff.encodedValue(v.toString());
                 }
             }
-            return buff.arrayEnd().toString();
+            return buff.endArray().toString();
         }
         case PropertyType.BOOLEAN:
         case PropertyType.LONG:

Modified: jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java?rev=1134325&r1=1134324&r2=1134325&view=diff
==============================================================================
--- jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
(original)
+++ jackrabbit/sandbox/microkernel/src/test/java/org/apache/jackrabbit/mk/json/JsopTest.java
Fri Jun 10 13:46:33 2011
@@ -191,20 +191,20 @@ public class JsopTest extends TestCase {
     public void testBuilder() throws IOException {
 
         JsopBuilder buff = new JsopBuilder();
-        buff.append("+ ").objectBegin().
+        buff.append("+ ").object().
             key("foo").value("bar").
             key("int").value(3).
             key("decimal").encodedValue("3.0").
-            key("obj").objectBegin().
+            key("obj").object().
                 key("boolean").value(true).
                 key("null").value(null).
-                key("arr").arrayBegin().
+                key("arr").array().
                     value(1).
                     value("42").
-                arrayEnd().
-            objectEnd().
+                endArray().
+            endObject().
             key("some").value("more").
-        objectEnd();
+        endObject();
 
         String json = buff.toString();
         assertEquals("+ {\"foo\":\"bar\",\"int\":3,\"decimal\":3.0," +



Mime
View raw message