/* * * ConfigManager * * This class is the central configuration file manager for all other classes and acts as a utility class for other classes to use. * It is typical to define this class as a general object (not static instance) for each class in single-threaded actions. If you need * a special function or action from this class; it is recommended to add it to this class and not make your own. * * */ package jesse.keeblarcraft.ConfigMgr; import java.io.FileWriter; import java.io.File; import java.io.IOException; import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonIOException; import java.util.List; import java.util.ArrayList; // Import all custom exceptions import jesse.keeblarcraft.Utils.CustomExceptions.*; public class ConfigManager { // Pedantic empty constructor public ConfigManager() {} // CreateFile // // Returns true if file is created, will return false if file cannot be created (returns true if already exists) public Boolean CreateFile(String fileName) throws FILE_CREATE_EXCEPTION { Boolean ret = false; File file = new File(fileName); // Check 1: Does the file already exist? ret = file.exists(); System.out.println("Does file exist? " + (ret ? "YES" : "NO")); // Check 2: If the file does not exist, attempt to create it if (ret == false) { try { ret = file.createNewFile(); } catch (IOException e) { // The file could not be created throw new FILE_CREATE_EXCEPTION(); } } else { ret = true; // This might be a hot fix, but technically the file already exists so would this be true or false? System.out.println("File (name:" + fileName + ") was determined to already exist. Exiting out"); } return ret; } // DeleteFile // // Returns true if file is deleted, false if could not be deleted or does not exist public Boolean DeleteFile(String fileName) throws FILE_DELETE_EXCEPTION { Boolean ret = false; File file = new File(fileName); // Step 1: Does file exist? ret = file.exists(); // Step 2: If file exists, attempt to delete if (ret == true) { try { ret = file.delete(); } catch (SecurityException e) { throw new FILE_DELETE_EXCEPTION(); } } else { System.out.println("Cannot delete file " + fileName + " because file does not exist"); } return ret; } // WriteToFile // // Will write or append to file (valid modes: "w" or "a") if file is available. Returns false if not public Boolean WriteToFile(String fileName, String data, String mode) { Boolean ret = false; FileWriter file; try { file = new FileWriter(fileName); switch(mode) { case "w": file.write(data); ret = true; break; case "a": file.append(data); ret = true; break; default: System.out.println("Invalid mode to WriteToFile!"); break; } file.close(); } catch (IOException e) { System.out.println("Could not open file " + fileName + " to write to it! Possibly permission issue?"); } return ret; } // WriteToJsonFile // // Will write to or append to a json file. It will search if the key exists first & update the field; // or add a new entry. It should be noted that json objects *can* be buried inside each other. It is // considered best (and only) practice to call the "GetJsonStringFromFile" function first from this // class and simply iterate to what you would need and then update the entire entry alongside the // top-level key. // // NOTE: THIS DOES NOT SAFE UPDATE THE KEY OBJECT. PRE-EXISTING DATA WILL BE DELETED FOREVER public void WriteToJsonFile(String fileName, String key, Object data, String mode) throws FILE_WRITE_EXCEPTION { Gson gson = new GsonBuilder().setPrettyPrinting().create(); try { gson.toJson(data, new FileWriter(fileName)); } catch (JsonIOException | IOException e) { throw new FILE_WRITE_EXCEPTION(); } } // GetJsonStringFromFile // // Retrieves a json formatted string from the file based on key. Returns empty string if not found public Object GetJsonObjectFromFile(String key, String fileName, Object jsonObject) { Object ret = jsonObject.getClass(); Gson gson = new Gson(); // Step 1: Get file as 1 constant string try { File file = new File(fileName); if (file.exists()) { String str = file.toString(); ret = gson.fromJson(str, jsonObject.getClass()); } else { System.out.println("File does not exist. Cannot convert to json"); } } catch (Exception e) { System.out.println("Something went wrong when converting json file object to object"); } return ret; } public Boolean DoesFileExist(String fileName) { Boolean ret = false; return ret; } public Boolean DoesDirectoryExist(String dirName) { Boolean ret = false; return ret; } public Boolean CreateDirectory(String dirName) throws DIRECTORY_CREATE_EXCEPTION { Boolean ret = false; File dir = new File(dirName); try { if ( ! dir.exists()) { ret = dir.mkdirs(); } } catch (Exception e) { System.out.println("Failed to make directory with name " + dirName); ret = true; /// TODO: Hack to make Setup fn be fine with prev-created files! Make Setup more robust! throw new DIRECTORY_CREATE_EXCEPTION(); } return ret; } public Boolean DeleteDirectory(String dirName) throws DIRECTORY_DELETE_EXCEPTION { Boolean ret = false; File dir = new File(dirName); try { ret = dir.delete(); System.out.println("Deleted directory " + dirName); } catch (Exception e) { System.out.println("Failed to delete directory " + dirName); throw new DIRECTORY_DELETE_EXCEPTION(); } return ret; } // AddToKey // // Adds new text to a key if found inside the config public String AddToKey(String key, String newInfo) { String ret = ""; return ret; } // GetFile // // Returns a file as an arraylist of all the lines in the file. Generally only used for testing // // NOTE: Returns UTF-8 Encoding of file public List GetFile(String fileName) { List ret = new ArrayList(); try { return Files.readLines(new File(fileName), Charsets.UTF_8); } catch (IOException e) { ret.clear(); } return ret; } }