Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Base64;

import java.util.Iterator;

import java.util.Map.Entry;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

public class Main {
    private static final String CDATA_CLOSE = "]]>";
    private static final String CDATA_OPEN = "<![CDATA[";
    private static final String ESQ = "=\"";
    private static final String SPACE = " ";
    private static final String EQ = "\"";
    private static final String EMPTY = "";
    private static final String ATTR_TEXT = "#text";
    private static final String ATTR_CDATA = "#cdata-section";
    private static final String LTS = "</";
    private static final String GT = ">";
    private static final String LT = "<";

    public static String toXml(String json) {
        JsonParser parser = new JsonParser();
        JsonElement rootJson = parser.parse(json);
        StringBuffer buff = new StringBuffer();
        serializeObjectAsXml((JsonObject) rootJson, buff);
        return buff.toString();
    }

    private static void serializeObjectAsXml(JsonObject objectJson, StringBuffer buff) {
        Iterator elements = objectJson.entrySet().iterator();
        while (elements.hasNext()) {
            Entry entry = (Entry) elements.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof JsonObject) {
                buff.append(LT).append(key);
                serializeObjectAttributes((JsonObject) value, buff);
                buff.append(GT);
                serializeObjectAsXml((JsonObject) value, buff);
                buff.append(LTS).append(key).append(GT);
            } else if (value instanceof JsonArray) {
                serializeArrayAsXml(buff, key, value);
            } else if (value instanceof JsonPrimitive) {
                if (ATTR_TEXT.equals(key)) {
                    buff.append(value.toString().replace(EQ, EMPTY));
                } else if (ATTR_CDATA.equals(key)) {
                    buff.append(CDATA_OPEN)
                            .append(new String(Base64.getDecoder().decode(value.toString().replace(EQ, EMPTY))))
                            .append(CDATA_CLOSE);
                } else {
                    if (!key.toString().startsWith("-")) {
                        buff.append(LT).append(key.toString()).append(GT)
                                .append(value.toString().replace(EQ, EMPTY)).append(LTS).append(key.toString())
                                .append(GT);
                    }
                }
            } else {
                System.err.println("ERROR: unhandled element");
            }
        }
    }

    private static void serializeObjectAttributes(JsonObject objectJson, StringBuffer buff) {
        Iterator elements = objectJson.entrySet().iterator();
        while (elements.hasNext()) {
            Entry entry = (Entry) elements.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof JsonPrimitive) {
                if (key.toString().startsWith("-")) {
                    buff.append(SPACE).append(key.toString().substring(1)).append(ESQ)
                            .append(value.toString().replace(EQ, EMPTY)).append(EQ);
                }
            }
        }
    }

    private static void serializeArrayAsXml(StringBuffer buff, Object key, Object value) {
        JsonArray array = (JsonArray) value;
        for (int i = 0; i < array.size(); i++) {
            JsonElement elementJson = array.get(i);
            if (elementJson instanceof JsonObject) {
                buff.append(LT).append(key);
                serializeObjectAttributes((JsonObject) elementJson, buff);
                buff.append(GT);
                serializeObjectAsXml((JsonObject) elementJson, buff);
                buff.append(LTS).append(key).append(GT);
            } else if (elementJson instanceof JsonArray) {
                serializeArrayAsXml(buff, key, elementJson);
            } else if (elementJson instanceof JsonPrimitive) {
                JsonPrimitive elementPrimitive = (JsonPrimitive) elementJson;
                if (ATTR_TEXT.equals(key)) {
                    buff.append(elementPrimitive.toString().replace(EQ, EMPTY));
                } else if (ATTR_CDATA.equals(key)) {
                    buff.append(CDATA_OPEN)
                            .append(new String(
                                    Base64.getDecoder().decode(elementPrimitive.toString().replace(EQ, EMPTY))))
                            .append(CDATA_CLOSE);
                } else {
                    // System.err.println("ERROR: content attributes must be #text");
                    buff.append(LT).append(key);
                    buff.append(GT);
                    buff.append(elementPrimitive.toString().replace(EQ, EMPTY));
                    buff.append(LTS).append(key).append(GT);
                }
            }
        }
    }
}