JSONWriterUtil.java 2.33 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.xss.XSSProtectionService
 *  org.apache.sling.commons.json.JSONException
 *  org.apache.sling.commons.json.JSONObject
 *  org.apache.sling.commons.json.io.JSONWriter
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.commons;

import com.day.cq.commons.Item;
import com.day.cq.xss.XSSProtectionService;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JSONWriterUtil {
    private static final Logger log = LoggerFactory.getLogger(JSONWriterUtil.class);
    public static final String KEY_SUFFIX_XSS = "_xss";

    public static void put(JSONObject object, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
        Item item = new Item(key, value, writeMode, xss, log);
        if (item.isPlainText()) {
            object.put(item.getKey(), (Object)item.getValue());
        }
        if (item.isAvoidXSS() && null != xss) {
            object.put(item.getXssKey(), (Object)item.getXssValue());
        }
    }

    public static void putOptional(JSONObject object, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
        if (value != null) {
            JSONWriterUtil.put(object, key, value, writeMode, xss);
        }
    }

    public static void write(JSONWriter writer, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
        Item item = new Item(key, value, writeMode, xss, log);
        if (item.isPlainText()) {
            writer.key(item.getKey()).value((Object)item.getValue());
        }
        if (item.isAvoidXSS() && null != xss) {
            writer.key(item.getXssKey()).value((Object)item.getXssValue());
        }
    }

    public static void writeOptional(JSONWriter writer, String key, String value, WriteMode writeMode, XSSProtectionService xss) throws JSONException {
        if (value != null) {
            JSONWriterUtil.write(writer, key, value, writeMode, xss);
        }
    }

    public static enum WriteMode {
        PLAIN_TEXT,
        AVOID_XSS,
        BOTH;
        

        private WriteMode() {
        }
    }

}