com.bossletsplays.frost.utils.config.JsonWrapper.java Source code

Java tutorial

Introduction

Here is the source code for com.bossletsplays.frost.utils.config.JsonWrapper.java

Source

/*   Copyright 2015 Matthew Rogers "BossLetsPlays"
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.
*/
package com.bossletsplays.frost.utils.config;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import com.bossletsplays.frost.FrostAPI;
import com.bossletsplays.frost.utils.collections.MultiMap;
import com.bossletsplays.frost.utils.debug.Logger;
import com.bossletsplays.frost.utils.exceptions.CollectionException;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * <strong>Project:</strong> FrostAPI <br>
 * <strong>File:</strong> JsonWrapper.java
 * 
 * <p>
 * A wrapper class for writing and reading JSON files.
 * </p>
 *
 * @author <a href = "http://bossletsplays.com"> Matthew Rogers</a>
 * @since 1.0.0
 */
public class JsonWrapper {

    public static final int GENERATE = 0;
    public static final int PARSE = 1;

    private JsonFactory f;
    private JsonGenerator g;
    private ObjectMapper mapper;
    private JsonNode rootNode;
    private int type;
    private MultiMap<String, ConfigObject> elements;
    private boolean elementsSet;
    private String path;

    public JsonWrapper(String path, int type) throws IOException {
        this.path = path;
        this.f = new JsonFactory();
        this.type = type;
        elements = new MultiMap<String, ConfigObject>();

        if (type == GENERATE) {
            mapper = new ObjectMapper(f);
            g = f.createGenerator(new FileWriter(path));
            g.setCodec(mapper);
            g.setPrettyPrinter(new DefaultPrettyPrinter());
            g.writeStartObject();
        } else if (type == PARSE) {
            mapper = new ObjectMapper(f);
            rootNode = mapper.readTree(new File(path));
        }
    }

    private void close() throws IOException {
        if (type == GENERATE) {
            g.writeEndObject();
            g.close();
        }
    }

    public void setElements(MultiMap<String, ConfigObject> elements) {
        this.elements = elements;
        elementsSet = true;
    }

    public void generate() throws IOException {
        if (type != GENERATE)
            return;
        if (!elementsSet)
            new CollectionException("MultiMap elements have not been set.", FrostAPI.Error.SEVERE);
        Logger.info("Generating JSON file <" + path + ">");
        for (String k : elements.get()) {
            g.writeObjectFieldStart(k);
            for (ConfigObject obj : elements.get(k)) {
                Logger.debug("Writing " + obj.getName() + " with value: " + obj.getValue().toString());
                g.writeObjectField(obj.getName(), obj.getValue());
            }
            g.writeEndObject();
        }
        close();
    }

    public MultiMap<String, ConfigObject> parse() throws JsonParseException, IOException {
        if (type != PARSE)
            return null;
        Logger.info("Parsing JSON file <" + path + ">");
        MultiMap<String, ConfigObject> map = new MultiMap<String, ConfigObject>();
        Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
        while (fieldsIterator.hasNext()) {
            Map.Entry<String, JsonNode> field = fieldsIterator.next();
            Logger.debug("Key: " + field.getKey() + "\tValue:" + field.getValue());
            JsonNode node = rootNode.path(field.getKey());
            Iterator<Map.Entry<String, JsonNode>> fit = node.fields();
            while (fit.hasNext()) {
                Map.Entry<String, JsonNode> subField = fit.next();
                Logger.info("Setting option <" + subField.getKey() + "> to <" + subField.getValue() + ">");
                map.put(field.getKey(), new ConfigObject(field.getKey() + "." + subField.getKey(),
                        subField.getKey(), subField.getValue()));
            }
        }
        return map;
    }

}