From 0af111dc2d49428b112a296072e06a9713080145 Mon Sep 17 00:00:00 2001 From: Alexander Bielen Date: Thu, 29 Nov 2018 12:16:18 +0100 Subject: [PATCH] [FEATURE] Added local preference manager to store settings in a local json file --- .../environment/api/utils/FileUtil.java | 17 ++- .../api/utils/PBLocalPreferences.java | 113 ++++++++++++++++++ 2 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java diff --git a/src/main/java/org/parabot/environment/api/utils/FileUtil.java b/src/main/java/org/parabot/environment/api/utils/FileUtil.java index ce954d3..6f33897 100644 --- a/src/main/java/org/parabot/environment/api/utils/FileUtil.java +++ b/src/main/java/org/parabot/environment/api/utils/FileUtil.java @@ -1,10 +1,8 @@ package org.parabot.environment.api.utils; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.nio.channels.FileChannel; +import java.nio.file.Files; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -88,4 +86,15 @@ public class FileUtil { } destination.close(); } + + public static String getFileContents(File file) throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } + + public static void writeFileContents(File file, String contents) throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(file.getAbsolutePath())); + writer.write(contents); + + writer.close(); + } } diff --git a/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java b/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java new file mode 100644 index 0000000..66c84b7 --- /dev/null +++ b/src/main/java/org/parabot/environment/api/utils/PBLocalPreferences.java @@ -0,0 +1,113 @@ +package org.parabot.environment.api.utils; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.parabot.core.Directories; + +import java.io.File; +import java.util.HashMap; + +/** + * Manages preferences in a local json file in the Parabot settings folder + * + * @author AlexanderBielen + */ +public class PBLocalPreferences { + private static JSONParser parser = new JSONParser(); + private File settingsFile; + + + public PBLocalPreferences(String fileName) { + settingsFile = new File(Directories.getSettingsPath() + "/" + secureFileName(fileName)); + } + + /** + * Gets all settings inside the file + * + * @return JSONObject or null if anything went wrong + */ + public JSONObject getSettings() { + try { + String stringContents = FileUtil.getFileContents(settingsFile); + return (JSONObject) parser.parse(stringContents); + } catch(Exception ex) { + return null; + } + } + + /** + * Convert a HashMap to json and writes it to the file + * + * @param settings HashMap + * @param append If true, append to existing settings in file + */ + public void writeSettings(HashMap settings, boolean append) { + JSONObject existingSettings; + if(append && (existingSettings = getSettings()) != null) { + existingSettings.putAll(settings); + settings = existingSettings; + } + + try { + if (!settingsFile.exists()) { + settingsFile.createNewFile(); + } + FileUtil.writeFileContents(settingsFile, new JSONObject(settings).toJSONString()); + + } catch (Exception ignore) { + ignore.printStackTrace(); + } + + } + + /** + * Adds a setting, or overwrites it if it exists + * + * @param key + * @param value + */ + public void addSetting(String key, String value) { + HashMap pair = new HashMap<>(); + pair.put(key, value); + writeSettings(pair, true); + } + + /** + * Fetches a setting + * + * @param key + * @return + */ + public String getSetting(String key) { + if(getSettings() == null) { + return null; + } + + return getSettings().get(key).toString(); + } + + /** + * Adjusts an existing setting + * + * @param key + * @param value + */ + public void adjustSetting(String key, String value) { + addSetting(key, value); + } + + /** + * Removes a setting + * + * @param key + */ + public void removeSetting(String key) { + JSONObject json = getSettings(); + json.remove(key); + writeSettings(json, false); + } + + private static String secureFileName(String fileName) { + return fileName.replace("..", ""); + } +}