jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thom...@apache.org
Subject svn commit: r1170072 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/json/JsopBuilder.java
Date Tue, 13 Sep 2011 07:57:52 GMT
Author: thomasm
Date: Tue Sep 13 07:57:51 2011
New Revision: 1170072

URL: http://svn.apache.org/viewvc?rev=1170072&view=rev
Log:
Jsop pretty printer

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

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=1170072&r1=1170071&r2=1170072&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
Tue Sep 13 07:57:51 2011
@@ -292,8 +292,95 @@ public class JsopBuilder {
         }
     }
 
+    /**
+     * Get the buffer length.
+     *
+     * @return the length
+     */
     public int length() {
         return buff.length();
     }
 
+    /**
+     * Beautify (format) the json / jsop string.
+     *
+     * @param jsop the jsop string
+     * @return the formatted string
+     */
+    public static String prettyPrint(String jsop) {
+        StringBuilder buff = new StringBuilder();
+        JsopTokenizer t = new JsopTokenizer(jsop);
+        while (true) {
+            prettyPrint(buff, t, "  ");
+            if (t.getTokenType() == JsopTokenizer.END) {
+                return buff.toString();
+            }
+        }
+    }
+
+    static String prettyPrint(StringBuilder buff, JsopTokenizer t, String ident) {
+        String space = "";
+        boolean inArray = false;
+        while (true) {
+            switch (t.read()) {
+                case JsopTokenizer.END:
+                    return buff.toString();
+                case JsopTokenizer.STRING:
+                    buff.append('\"').append(t.getEscapedToken()).append('\"');
+                    break;
+                case JsopTokenizer.NUMBER:
+                case JsopTokenizer.TRUE:
+                case JsopTokenizer.FALSE:
+                case JsopTokenizer.NULL:
+                case JsopTokenizer.IDENTIFIER:
+                case JsopTokenizer.ERROR:
+                    buff.append(t.getEscapedToken());
+                    break;
+                case '{':
+                    if (t.matches('}')) {
+                        buff.append("{}");
+                    } else {
+                        buff.append("{\n").append(space += ident);
+                    }
+                    break;
+                case '}':
+                    space = space.substring(0, space.length() - ident.length());
+                    buff.append('\n').append(space).append("}");
+                    break;
+                case '[':
+                    inArray = true;
+                    buff.append("[");
+                    break;
+                case ']':
+                    inArray = false;
+                    buff.append("]");
+                    break;
+                case ',':
+                    if (!inArray) {
+                        buff.append(",\n").append(space);
+                    } else {
+                        buff.append(", ");
+                    }
+                    break;
+                case ':':
+                    buff.append(": ");
+                    break;
+                case '+':
+                    buff.append("+ ");
+                    break;
+                case '-':
+                    buff.append("- ");
+                    break;
+                case '^':
+                    buff.append("^ ");
+                    break;
+                case '/':
+                    buff.append("/ ");
+                    break;
+                default:
+                    throw new AssertionError("token type: " + t.getTokenType());
+            }
+        }
+    }
+
 }



Mime
View raw message