com.day.cq.commons
Class JS

java.lang.Object
  extended by com.day.cq.commons.JS

public class JS
extends Object

Utility class that provides static methods for properly writing Javascript and JSON snippets.


Constructor Summary
JS()
           
 
Method Summary
static String array(Collection<?> list)
          Returns a Javascript/JSON array from a Java object collection.
static String array(String[] array)
          Returns a Javascript/JSON array in string representation from a Java string array.
static String object(Node node)
          Returns a given JCR Node and its entire subtree as JSON object.
static String object(Node node, int depth)
          Returns a given JCR Node and its subtree up until a given depth as JSON.
static String str(String text)
          Returns a Javascript string in string representation, including the surrounding double-quotes.
static void writeNode(Writer out, Node node)
          Writes the given JCR Node and its entire subtree as JSON into the given writer.
static void writeNode(Writer out, Node node, int depth)
          Writes the given JCR Node and its subtree up until a given depth as JSON into the given writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JS

public JS()
Method Detail

str

public static String str(String text)
Returns a Javascript string in string representation, including the surrounding double-quotes. For example: "foobar".

If the string is null, null (as a Javascript null-value) will be returned.

The Javascript string will be properly escaped.

This is the same as same behaviour as JSONObject.valueToString(text).

Parameters:
text - a Java string
Returns:
a qualified Javascript string literal or the string null

array

public static String array(String[] array)
Returns a Javascript/JSON array in string representation from a Java string array.

Output will have no indentation and no new lines, but a single whitespace between elements: ["one", "two", "three"]

If the given array is null, an empty array will be returned ([]). The Javascript strings will be properly escaped. str(String) is used for each element of the array.

Parameters:
array - a string array
Returns:
a qualified string representation of a Javascript array literal

array

public static String array(Collection<?> list)
Returns a Javascript/JSON array from a Java object collection. Same as new JSONArray(list).toString(); and behaves like array(String[]).

Parameters:
list - a collection of objects
Returns:
a qualified string representation of a Javascript array literal

writeNode

public static void writeNode(Writer out,
                             Node node)
                      throws IOException,
                             RepositoryException,
                             JSONException
Writes the given JCR Node and its entire subtree as JSON into the given writer. Use writeNode(Writer, Node, int) for controlling the node depth.

This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:

 var data = <% JS.writeNode(out, currentNode); %>;
 
which might result in:
 var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
 

This writes directly to the (JSP) output stream and thus needs to flush it. Because of this it also works good for larger trees. To return it as a string, use object(Node). Any exception will be passed to the caller.

Parameters:
out - a writer, such as a JspWriter, to write the JSON into. Will automatically be flushed before and after.
node - the node to write
Throws:
IOException - if there was a problem with the writer
RepositoryException - if some jcr error happened
JSONException - if writing the json failed

writeNode

public static void writeNode(Writer out,
                             Node node,
                             int depth)
                      throws IOException,
                             RepositoryException,
                             JSONException
Writes the given JCR Node and its subtree up until a given depth as JSON into the given writer.

This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:

 var data = <% JS.writeNode(out, currentNode, 0); %>;
 
which might result in:
 var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
 

This writes directly to the (JSP) output stream and thus needs to flush it. Because of this it also works good for larger trees. To return it as a string, use object(Node, int). Any exception will be passed to the caller.

Parameters:
out - a writer, such as a JspWriter, to write the JSON into. Will automatically be flushed before and after.
node - the node to write
depth - until which depth the tree should be written; 0 means the current node and its properties only; -1 means the whole tree.
Throws:
IOException - if there was a problem with the writer
RepositoryException - if some jcr error happened
JSONException - if writing the json failed

object

public static String object(Node node)
                     throws RepositoryException,
                            JSONException
Returns a given JCR Node and its entire subtree as JSON object. Use object(Node, int) for controlling the node depth.

This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:

 var data = <%= JS.object(currentNode) %>;
 
which might result in:
 var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
 

For larger node trees it is probably more efficient to stream it by using writeNode(Writer, Node). Any exception will be passed to the caller.

Parameters:
node - the node to write
Returns:
a JSON object as string representation
Throws:
RepositoryException - if some jcr error happened
JSONException - if writing the json failed

object

public static String object(Node node,
                            int depth)
                     throws RepositoryException,
                            JSONException
Returns a given JCR Node and its subtree up until a given depth as JSON.

This is the same JSON as returned by Sling's default JSON renderer. Can be used in JSPs to inline JSON for javascript code, for example:

 var data = <%= JS.object(currentNode) %>;
 
which might result in:
 var data = { "jcr:primaryType": "nt:unstructured", "property" : "value" };
 

For larger node trees it is probably more efficient to stream it by using writeNode(Writer, Node, int). Any exception will be passed to the caller.

Parameters:
node - the node to write
depth - until which depth the tree should be written; 0 means the current node and its properties only; -1 means the whole tree.
Returns:
a JSON object as string representation
Throws:
RepositoryException - if some jcr error happened
JSONException - if writing the json failed


Copyright © 2011-2013 Adobe Systems Incorporated. All Rights Reserved.