You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2008/07/30 17:28:48 UTC
svn commit: r681080 - in /incubator/sling/trunk:
commons/json/src/main/java/org/apache/sling/commons/json/io/
commons/json/src/main/java/org/apache/sling/commons/json/jcr/
launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtes...
Author: bdelacretaz
Date: Wed Jul 30 08:28:46 2008
New Revision: 681080
URL: http://svn.apache.org/viewvc?rev=681080&view=rev
Log:
SLING-562 - pretty-print the json dump if the 'tidy' selector is used
Modified:
incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java
incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonRenderingTest.java
incubator/sling/trunk/servlets/get/pom.xml
incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonRendererServlet.java
Modified: incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java?rev=681080&r1=681079&r2=681080&view=diff
==============================================================================
--- incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java (original)
+++ incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/io/JSONWriter.java Wed Jul 30 08:28:46 2008
@@ -60,11 +60,29 @@
* @version 2
*/
public class JSONWriter {
+
// This was previously 20 - increased while creating
// the JsonRenderingTest.testRecursiveInfinity test
private static final int maxdepth = 50;
/**
+ * indentations
+ */
+ private static final String[] INDENTS = new String[maxdepth];
+ static {
+ StringBuffer indent = new StringBuffer();
+ for (int i=0; i<INDENTS.length; i++) {
+ INDENTS[i] = indent.toString();
+ indent.append(" ");
+ }
+ }
+
+ /**
+ * flag indicates that output should be nicely formatted
+ */
+ private boolean tidy;
+
+ /**
* The comma flag determines if a comma should be output before the next
* value.
*/
@@ -107,6 +125,22 @@
}
/**
+ * Checks if the output is nicely formatted.
+ * @return <code>true</code> if nicely formatted
+ */
+ public boolean isTidy() {
+ return tidy;
+ }
+
+ /**
+ * Controls if output should be nicely formatted.
+ * @param tidy <code>true</code> to nicely format.
+ */
+ public void setTidy(boolean tidy) {
+ this.tidy = tidy;
+ }
+
+ /**
* Append a value.
* @param s A string value.
* @return this
@@ -121,6 +155,10 @@
if (this.comma && this.mode == 'a') {
this.writer.write(',');
}
+ if (tidy && this.mode == 'a' && !"{".equals(s) && !"[".equals(s)) {
+ this.writer.write('\n');
+ this.writer.write(INDENTS[top]);
+ }
this.writer.write(s);
} catch (IOException e) {
throw new JSONException(e);
@@ -167,6 +205,10 @@
}
this.pop(m);
try {
+ if (tidy) {
+ this.writer.write('\n');
+ this.writer.write(INDENTS[top]);
+ }
this.writer.write(c);
} catch (IOException e) {
throw new JSONException(e);
@@ -212,8 +254,15 @@
if (this.comma) {
this.writer.write(',');
}
+ if (tidy) {
+ this.writer.write('\n');
+ this.writer.write(INDENTS[top]);
+ }
this.writer.write(JSONObject.quote(s));
this.writer.write(':');
+ if (tidy) {
+ this.writer.write(' ');
+ }
this.comma = false;
this.mode = 'o';
return this;
Modified: incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java?rev=681080&r1=681079&r2=681080&view=diff
==============================================================================
--- incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java (original)
+++ incubator/sling/trunk/commons/json/src/main/java/org/apache/sling/commons/json/jcr/JsonItemWriter.java Wed Jul 30 08:28:46 2008
@@ -81,7 +81,18 @@
/** Dump given node in JSON, optionally recursing into its child nodes */
public void dump(Node node, Writer w, int maxRecursionLevels)
throws RepositoryException, JSONException {
- dump(node, new JSONWriter(w), 0, maxRecursionLevels);
+ dump(node, w, maxRecursionLevels, false);
+ }
+
+ /**
+ * Dump given node in JSON, optionally recursing into its child nodes
+ * @param tidy if <code>true</code> the json dump is nicely formatted
+ */
+ public void dump(Node node, Writer w, int maxRecursionLevels, boolean tidy)
+ throws RepositoryException, JSONException {
+ JSONWriter jw = new JSONWriter(w);
+ jw.setTidy(tidy);
+ dump(node, jw, 0, maxRecursionLevels);
}
/** Dump given property in JSON */
Modified: incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonRenderingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonRenderingTest.java?rev=681080&r1=681079&r2=681080&view=diff
==============================================================================
--- incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonRenderingTest.java (original)
+++ incubator/sling/trunk/launchpad/testing/src/test/java/org/apache/sling/launchpad/webapp/integrationtest/JsonRenderingTest.java Wed Jul 30 08:28:46 2008
@@ -23,6 +23,7 @@
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.commons.testing.integration.HttpTestBase;
+import org.apache.sling.commons.testing.util.TestStringUtil;
import org.apache.sling.servlets.post.SlingPostConstants;
/** Test creating Nodes and rendering them in JSON */
@@ -174,4 +175,51 @@
+ ")");
}
}
+
+ public void testTidyNonRecursive() throws IOException {
+ {
+ final String json = getContent(createdNodeUrl + ".json", CONTENT_TYPE_JSON);
+ final String expected =
+ "{\"jcr:primaryType\":\"nt:unstructured\",\"text\":\"" + testText + "\"}";
+ assertEquals("Without .tidy selector, json should be flat",
+ expected, TestStringUtil.flatten(json));
+ }
+
+ {
+ final String json = getContent(createdNodeUrl + ".tidy.json", CONTENT_TYPE_JSON);
+ final String expected =
+ "{. \"jcr:primaryType\": \"nt:unstructured\",. \"text\": \"" + testText + "\".}";
+ assertEquals("With .tidy selector, json should be pretty-printed",
+ expected, TestStringUtil.flatten(json));
+ }
+ }
+
+ public void testTidyRecursive() throws IOException {
+ final Map<String, String> props = new HashMap<String, String>();
+ props.put("text", testText);
+ props.put("a/b", "yes");
+ final String url = testClient.createNode(postUrl, props);
+
+ {
+ final String json = getContent(url + ".tidy.infinity.json", CONTENT_TYPE_JSON);
+ final String expected =
+ "{. \"jcr:primaryType\": \"nt:unstructured\",. \"text\": \"" + testText
+ + "\",. \"a\": {. \"jcr:primaryType\": \"nt:unstructured\",. \"b\": \"yes\". }"
+ + ".}";
+ assertEquals("With .tidy.infinity selector, json should be pretty-printed",
+ expected, TestStringUtil.flatten(json));
+ }
+
+ {
+ final String json = getContent(url + ".infinity.json", CONTENT_TYPE_JSON);
+ final String expected =
+ "{\"jcr:primaryType\":\"nt:unstructured\",\"text\":"
+ + "\"" + testText + "\",\"a\":{\"jcr:primaryType\":"
+ + "\"nt:unstructured\",\"b\":\"yes\"}}"
+ ;
+ assertEquals("With .infinity selector only, json should be flat",
+ expected, TestStringUtil.flatten(json));
+ }
+
+ }
}
\ No newline at end of file
Modified: incubator/sling/trunk/servlets/get/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/pom.xml?rev=681080&r1=681079&r2=681080&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/get/pom.xml (original)
+++ incubator/sling/trunk/servlets/get/pom.xml Wed Jul 30 08:28:46 2008
@@ -103,7 +103,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.commons.json</artifactId>
- <version>2.0.2-incubator</version>
+ <version>2.0.3-incubator-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
Modified: incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonRendererServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonRendererServlet.java?rev=681080&r1=681079&r2=681080&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonRendererServlet.java (original)
+++ incubator/sling/trunk/servlets/get/src/main/java/org/apache/sling/servlets/get/helpers/JsonRendererServlet.java Wed Jul 30 08:28:46 2008
@@ -54,6 +54,8 @@
/** Recursion level selector that means "all levels" */
public static final String INFINITY = "infinity";
+ public static final String TIDY = "tidy";
+
public JsonRendererServlet() {
itemWriter = new JsonItemWriter(null);
}
@@ -93,16 +95,18 @@
int maxRecursionLevels = 0;
final String[] selectors = req.getRequestPathInfo().getSelectors();
if (selectors != null && selectors.length > 0) {
- String level = selectors[selectors.length - 1];
- if (INFINITY.equals(level)) {
- maxRecursionLevels = -1;
- } else {
- try {
- maxRecursionLevels = Integer.parseInt(level);
- } catch (NumberFormatException nfe) {
- resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
- "Invalid recursion selector value '" + level + "'");
- return;
+ final String level = selectors[selectors.length - 1];
+ if(!TIDY.equals(level)) {
+ if (INFINITY.equals(level)) {
+ maxRecursionLevels = -1;
+ } else {
+ try {
+ maxRecursionLevels = Integer.parseInt(level);
+ } catch (NumberFormatException nfe) {
+ resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
+ "Invalid recursion selector value '" + level + "'");
+ return;
+ }
}
}
}
@@ -112,13 +116,23 @@
// do the dump
try {
- itemWriter.dump(n, resp.getWriter(), maxRecursionLevels);
+ itemWriter.dump(n, resp.getWriter(), maxRecursionLevels, isTidy(req));
} catch (JSONException je) {
reportException(je);
} catch (RepositoryException re) {
reportException(re);
}
}
+
+ /** True if our request wants the "tidy" pretty-printed format */
+ protected boolean isTidy(SlingHttpServletRequest req) {
+ for(String selector : req.getRequestPathInfo().getSelectors()) {
+ if(TIDY.equals(selector)) {
+ return true;
+ }
+ }
+ return false;
+ }
/** Render a Property by dumping its String value */
private void renderProperty(Property p, SlingHttpServletResponse resp)