org.openstatic.util.JSONUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.openstatic.util.JSONUtil.java

Source

/*
Copyright (C) 2010 Brian Dunigan
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.openstatic.util;

import org.json.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class JSONUtil {
    public static String htmlEntityEncode(String s) {
        StringBuilder buf = new StringBuilder(s.length());
        int len = s.length();
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9') {
                buf.append(c);
            } else {
                buf.append("&#").append((int) c).append(";");
            }
        }
        return buf.toString();
    }

    public static InputStream json2html(InputStream is) throws Exception {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int inputByte;
            while ((inputByte = is.read()) > -1) {
                baos.write(inputByte);
            }
            is.close();
            String jdata = "<html><body>" + json2html(new String(baos.toByteArray())) + "</body></html>";
            baos.reset();
            ByteArrayInputStream data = new ByteArrayInputStream(jdata.getBytes());
            return data;
        } catch (Exception e) {
            System.err.println(e.getMessage());
            e.printStackTrace(System.err);
            return null;
        }
    }

    public static String json2html(String jdata) throws Exception {
        if (jdata.trim().startsWith("[")) {
            return json2table(new JSONArray(jdata));
        } else if (jdata.trim().startsWith("{")) {
            return json2table(new JSONObject(jdata));
        } else {
            return null;
        }
    }

    public static String json2table(JSONArray jarray) throws Exception {
        String return_string = "<table cellpadding=\"4\">";
        for (int m = 0; m < jarray.length(); m++) {
            return_string += "<tr><td style=\"border-bottom: 1px dashed #999999;\">";
            Object value = jarray.get(m);
            if (value != null) {
                if (value instanceof JSONObject) {
                    return_string += json2table((JSONObject) value);
                } else if (value instanceof JSONArray) {
                    return_string += json2table((JSONArray) value);
                } else {
                    return_string += htmlEntityEncode(jarray.getString(m));
                }
            }
            return_string += "</td></tr>";
        }
        return_string += "</table>";
        return return_string;
    }

    public static String json2table(JSONObject jobject) throws Exception {
        String return_string = "<table cellspacing=\"0\" cellpadding=\"4\" style=\"border-color: #000000; border-style: solid; border-width: 1px; background-color: #FFFFFF; color: #000000;\">\r\n";
        String[] fnames = JSONObject.getNames(jobject);
        if (fnames != null) {
            return_string += "<tr style=\"background-color: #AAAAAA; color: #000000;\"><th>KEY</th><th>VALUE</th></tr>";
            for (int i = 0; i < fnames.length; i++) {
                Object value = jobject.get(fnames[i]);
                String string_value = "";
                if (value != null) {
                    if (value instanceof JSONObject)
                        string_value = json2table((JSONObject) value);
                    else if (value instanceof JSONArray) {
                        string_value = json2table((JSONArray) value);
                    } else {
                        string_value = htmlEntityEncode(jobject.getString(fnames[i]));
                    }
                }
                return_string += "<tr><td style=\"border-bottom: 1px dotted #999999; border-right: 1px dotted #999999; vertical-align: text-top;\">"
                        + fnames[i] + "<td style=\"border-bottom: 1px dotted #999999;\">" + string_value
                        + "</td></tr>";
            }
        }
        return_string += "</table>";
        return return_string;
    }
}