You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2010/01/28 21:37:02 UTC

svn commit: r904258 - in /sling/trunk: bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/ launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/ launchpad/testing/src/test/resources/integration-...

Author: justin
Date: Thu Jan 28 20:37:01 2010
New Revision: 904258

URL: http://svn.apache.org/viewvc?rev=904258&view=rev
Log:
SLING-1341 - adding builder.write() method

Added:
    sling/trunk/launchpad/testing/src/test/resources/integration-test/builder2.groovy
      - copied, changed from r904233, sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy
Modified:
    sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/JSONGroovyBuilder.java
    sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JSONGroovyBuilderIntegrationTest.java
    sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy

Modified: sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/JSONGroovyBuilder.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/JSONGroovyBuilder.java?rev=904258&r1=904257&r2=904258&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/JSONGroovyBuilder.java (original)
+++ sling/trunk/bundles/extensions/groovy/src/main/java/org/apache/sling/extensions/groovy/json/JSONGroovyBuilder.java Thu Jan 28 20:37:01 2010
@@ -19,6 +19,8 @@
 import groovy.lang.Closure;
 import groovy.util.BuilderSupport;
 
+import java.io.IOException;
+import java.io.Writer;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -39,6 +41,19 @@
 public class JSONGroovyBuilder extends BuilderSupport {
 
     /**
+     * Construct a JSONGroovyBuilder.
+     */
+    public JSONGroovyBuilder() {
+    }
+
+    /**
+     * Construct a JSONGroovyBuilder which will output to a writer upon completion.
+     */
+    public JSONGroovyBuilder(Writer writer) {
+        this.writer = writer;
+    }
+
+    /**
      * The string 'json' which indicates that the root node should be used
      * as-is. Otherwise, the root node is wrapped in a JSON object.
      */
@@ -52,7 +67,22 @@
     /**
      * A stack containing the names of created nodes.
      */
-    protected Stack<String> nodeNames = new Stack<String>();
+    private Stack<String> nodeNames = new Stack<String>();
+
+    /**
+     * If not null, the writer to output to upon completion.
+     */
+    private Writer writer;
+
+    /**
+     * Create a JSONGroovyBuilder around the provided writer object.
+     *
+     * @param writer the writer
+     * @return a new builder
+     */
+    public static JSONGroovyBuilder write(Writer writer) {
+        return new JSONGroovyBuilder(writer);
+    }
 
     private void addFromMap(JSONObject obj, Map attributes) {
         for (Iterator it = attributes.keySet().iterator(); it.hasNext();) {
@@ -180,19 +210,27 @@
      */
     @Override
     protected Object postNodeCompletion(Object parent, Object node) {
-        if (parent == null && !nodeNames.empty()) {
-            String rootName = nodeNames.pop();
-            if (!JSON.equals(rootName)) {
-                JSONObject obj = new JSONObject();
+        if (parent == null) {
+            Object finalNode = node;
+            if (!nodeNames.empty()) {
+                String rootName = nodeNames.pop();
+                if (!JSON.equals(rootName)) {
+                    finalNode = new JSONObject();
+                    try {
+                        ((JSONObject)finalNode).put(rootName, node);
+                    } catch (JSONException e) {
+                        logger.error("Unable to create container JSON Object", e);
+                    }
+                }
+            }
+            if (writer != null) {
                 try {
-                    return obj.put(rootName, node);
-                } catch (JSONException e) {
-                    logger.error("Unable to create container JSON Object", e);
-                    return super.postNodeCompletion(parent, node);
+                    writer.write(finalNode.toString());
+                } catch (IOException e) {
+                    logger.error("Unable to write JSON Object", e);
                 }
-            } else {
-                return super.postNodeCompletion(parent, node);
             }
+            return finalNode;
         } else {
             return super.postNodeCompletion(parent, node);
         }

Modified: sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JSONGroovyBuilderIntegrationTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JSONGroovyBuilderIntegrationTest.java?rev=904258&r1=904257&r2=904258&view=diff
==============================================================================
--- sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JSONGroovyBuilderIntegrationTest.java (original)
+++ sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JSONGroovyBuilderIntegrationTest.java Thu Jan 28 20:37:01 2010
@@ -60,4 +60,17 @@
             testClient.delete(toDelete);
         }
     }
+
+    public void testJSONGroovyBuilder2() throws IOException, JSONException {
+        final String toDelete = uploadTestScript("builder2.groovy","json.groovy");
+        try {
+            final String content = getContent(displayUrl + ".json", CONTENT_TYPE_JSON);
+            JSONObject jo = new JSONObject(content);
+            assertEquals("Content contained wrong number of items", 1, jo.length());
+            assertEquals("Content contained wrong key", "text", jo.keys().next());
+            assertEquals("Content contained wrong data", testText, jo.get("text"));
+        } finally {
+            testClient.delete(toDelete);
+        }
+    }
 }

Modified: sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy?rev=904258&r1=904257&r2=904258&view=diff
==============================================================================
--- sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy (original)
+++ sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy Thu Jan 28 20:37:01 2010
@@ -18,5 +18,5 @@
  */
 
 out.write jsonBuilder.json {
-    text currentNode.getProperty("text").string
+	text currentNode.getProperty("text").string
 } as String

Copied: sling/trunk/launchpad/testing/src/test/resources/integration-test/builder2.groovy (from r904233, sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy)
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/testing/src/test/resources/integration-test/builder2.groovy?p2=sling/trunk/launchpad/testing/src/test/resources/integration-test/builder2.groovy&p1=sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy&r1=904233&r2=904258&rev=904258&view=diff
==============================================================================
--- sling/trunk/launchpad/testing/src/test/resources/integration-test/builder.groovy (original)
+++ sling/trunk/launchpad/testing/src/test/resources/integration-test/builder2.groovy Thu Jan 28 20:37:01 2010
@@ -17,6 +17,6 @@
  * under the License.
  */
 
-out.write jsonBuilder.json {
+jsonBuilder.write(out).json {
     text currentNode.getProperty("text").string
-} as String
+}
\ No newline at end of file