JS.java 2.4 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.Node
 *  javax.jcr.RepositoryException
 *  org.apache.sling.commons.json.JSONArray
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.jcr.JsonItemWriter
 */
package com.day.cq.commons;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Set;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.jcr.JsonItemWriter;

public class JS {
    public static String str(String text) {
        try {
            return JSONObject.valueToString((Object)text);
        }
        catch (JSONException e) {
            return "null";
        }
    }

    public static String array(String[] array) {
        if (array == null) {
            return "[]";
        }
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        for (int i = 0; i < array.length; ++i) {
            builder.append(JS.str(array[i]));
            if (i >= array.length - 1) continue;
            builder.append(", ");
        }
        builder.append(']');
        return builder.toString();
    }

    public static String array(Collection<?> list) {
        return new JSONArray(list).toString();
    }

    public static void writeNode(Writer out, Node node) throws IOException, RepositoryException, JSONException {
        JS.writeNode(out, node, -1);
    }

    public static void writeNode(Writer out, Node node, int depth) throws IOException, RepositoryException, JSONException {
        out.flush();
        JsonItemWriter jsonWriter = new JsonItemWriter(null);
        jsonWriter.dump(node, (Writer)new PrintWriter(out), depth);
        out.flush();
    }

    public static String object(Node node) throws RepositoryException, JSONException {
        return JS.object(node, -1);
    }

    public static String object(Node node, int depth) throws RepositoryException, JSONException {
        JsonItemWriter jsonWriter = new JsonItemWriter(null);
        StringWriter out = new StringWriter();
        jsonWriter.dump(node, (Writer)out, depth);
        return out.toString();
    }
}