org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.files.JsonFile.java Source code

Java tutorial

Introduction

Here is the source code for org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.files.JsonFile.java

Source

/**
 * The MIT License (MIT)
 * 
 * Copyright (c) 2015 Imperium Studios <https://imperiumstudios.org>
 * Copyright (c) 2015 Kevin Olinger <https://kevinolinger.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.files;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.Core;
import org.imperiumstudios.Imperium1871Bungee.BukkitWarpHelper.Main;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

/**
 * Reads any Config File.
 * @author Kevin Olinger
 */
public class JsonFile {

    Main pl;
    Core IMP;

    File jsonFile;
    Integer updateRequired = 0;

    Map<String, String> fileContent = new TreeMap<String, String>();
    HashMap<String, String> jsonFileContent = new HashMap<String, String>();

    /**
     * Creates the general Config File, if it doesn't exists.
     */
    public void checkFile() {
        File dataFolder = jsonFile.getParentFile();

        if (!dataFolder.exists() && !dataFolder.mkdirs()) {
            IMP.log.error("JsonFile.java > checkFile() > Unable to create folder for " + jsonFile
                    + ". Disabling this plugin ..");
            IMP.disable();
        }

        if (!jsonFile.exists()) {
            try {
                JsonWriter writer = new JsonWriter(new FileWriter(jsonFile));

                writer.setIndent("  ");
                writer.beginObject();

                writer.name("Version").value(pl.getDescription().getVersion());

                writer.endObject();
                writer.close();

                IMP.log.info("JsonFile.java > checkFile() > " + jsonFile + " not found. Creating it ..");
            } catch (IOException ex) {
                IMP.log.error("JsonFile.java > checkFile() > Unable to create " + jsonFile
                        + ". Disabling this plugin ..");
                IMP.log.error("JsonFile.java > checkFile() > Error: " + ex.getMessage());
                IMP.disable();
            }
        }
    }

    /**
     * Read the config File into RAM.
     */
    public void readFile() {
        try {
            JsonReader reader = new JsonReader(new FileReader(jsonFile));

            reader.beginObject();

            while (reader.hasNext())
                jsonFileContent.put(reader.nextName(), reader.nextString());

            reader.endObject();
            reader.close();
        } catch (IOException ex) {
            IMP.log.error("JsonFile.java > readFile() > Unable to read " + jsonFile + ". Disabling this plugin ..");
            IMP.log.error("JsonFile.java > readFile() > Error: " + ex.getMessage());
            IMP.disable();
        }
    }

    /**
     * Reads a config key into from the buffer from readFile into an other buffer.
     * @param key The Key you want to write.
     * @param defaultValue A default value.
     */
    public void checkValue(String key, String defaultValue) {
        if (!jsonFileContent.containsKey(key)) {
            jsonFileContent.put(key, defaultValue);
            fileContent.put(key, defaultValue);
            updateRequired = 1;
        } else
            fileContent.put(key, jsonFileContent.get(key));
    }

    /**
     * Injects config values wish stored via checkValue's default value into the file.
     */
    public void updateFile() {
        if (updateRequired == 1) {
            try {
                JsonWriter writer = new JsonWriter(new FileWriter(jsonFile));

                writer.setIndent("  ");
                writer.beginObject();

                for (Map.Entry<String, String> entry : fileContent.entrySet())
                    writer.name(entry.getKey()).value(entry.getValue());

                writer.endObject();
                writer.close();

                IMP.log.info("JsonFile.java > updateFile() > " + jsonFile + " updated.");
            } catch (IOException ex) {
                IMP.log.info("JsonFile.java > updateFile() > Unable to update " + jsonFile + ".");
            }
        }
    }

    /**
     * Get the config value to the key. Must requested by calling checkValue first.
     * @param key The key you want to have.
     * @return The String Value of the key.
     */
    public String get(String key) {
        if (fileContent.containsKey(key))
            return fileContent.get(key);
        else {
            IMP.log.error("JsonFile.java > get() > Undefined key '" + key + "' requested.");
            return "{Undefined key}";
        }
    }

}