Class JS


  • public class JS
    extends java.lang.Object
    Utility class that provides static methods for properly writing Javascript and JSON snippets.
    • Constructor Summary

      Constructors 
      Constructor Description
      JS()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String array​(java.lang.String[] array)
      Returns a Javascript/JSON array in string representation from a Java string array.
      static java.lang.String array​(java.util.Collection<?> list)
      Returns a Javascript/JSON array from a Java object collection.
      static java.lang.String object​(Node node)
      Returns a given JCR Node and its entire subtree as JSON object.
      static java.lang.String object​(Node node, int depth)
      Returns a given JCR Node and its subtree up until a given depth as JSON.
      static java.lang.String str​(java.lang.String text)
      Returns a Javascript string in string representation, including the surrounding double-quotes.
      static void writeNode​(java.io.Writer out, Node node)
      Writes the given JCR Node and its entire subtree as JSON into the given writer.
      static void writeNode​(java.io.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

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JS

        public JS()
    • Method Detail

      • str

        public static java.lang.String str​(java.lang.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 java.lang.String array​(java.lang.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 java.lang.String array​(java.util.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​(java.io.Writer out,
                                     Node node)
                              throws java.io.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 javax.servlet.jsp.JspWriter, to write the JSON into. Will automatically be flushed before and after.
        node - the node to write
        Throws:
        java.io.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​(java.io.Writer out,
                                     Node node,
                                     int depth)
                              throws java.io.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 javax.servlet.jsp.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:
        java.io.IOException - if there was a problem with the writer
        RepositoryException - if some jcr error happened
        JSONException - if writing the json failed
      • object

        public static java.lang.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 java.lang.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