aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/Configuration.java
diff options
context:
space:
mode:
authorPinapelz <yukais@pinapelz.com>2024-10-02 13:42:08 -0700
committerPinapelz <yukais@pinapelz.com>2024-10-02 13:42:08 -0700
commiteeab121612d5bb8bb02d383ae9cfd12b3a9febe0 (patch)
tree66af77963a8e9d56941680ec7fd64cc8e5df4b17 /src/main/java/Configuration.java
parent110ad8f7746aca49a91fe4048e2b0f4329944a74 (diff)
add button to change output dir w/ filechooser, remove blacklist feature, change config files to use json
Diffstat (limited to 'src/main/java/Configuration.java')
-rw-r--r--src/main/java/Configuration.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/Configuration.java b/src/main/java/Configuration.java
new file mode 100644
index 0000000..90b30fd
--- /dev/null
+++ b/src/main/java/Configuration.java
@@ -0,0 +1,83 @@
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.util.HashMap;
+
+public class Configuration {
+ /**
+ * Reads the configuration json file and returns a hashmap with the kv pairs
+ * @return HashMap of config data json
+ */
+ public HashMap<String, String> readConfigurationData() {
+ HashMap<String, String> configData = new HashMap<>();
+ File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json");
+ try (FileReader reader = new FileReader(configFile)) {
+ JSONTokener tokener = new JSONTokener(reader);
+ JSONObject jsonObject = new JSONObject(tokener);
+
+ for (String key : jsonObject.keySet()) {
+ configData.put(key, jsonObject.getString(key));
+ }
+ } catch (IOException e) {
+ System.out.println(e);
+ System.out.println("Failed to read the configuration file.");
+ }
+
+ return configData;
+ }
+
+ /**
+ * Creates a configuration file if one doesn't already exist
+ * @return true/false if a file was created
+ */
+ public boolean createConfigurationFile(){
+ if(new File(System.getProperty("user.dir") +"/config").mkdir())
+ System.out.println("Created new config directory");
+ File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json");
+ if(configFile.exists())
+ return false; // File already exists
+ System.out.println("Creating new Configuration File");
+ JSONObject configurationData = new JSONObject();
+ configurationData.put("lastFile","" );
+ configurationData.put("outputPath", "completed");
+ configurationData.put("blacklistFile", "");
+ try (FileWriter file = new FileWriter(configFile)) {
+ file.write(configurationData.toString(4));
+ System.out.println("Successfully created a stub config file");
+ return true;
+ } catch (IOException e) {
+ System.out.println("Failed to create a stub config file");
+ return false;
+ }
+ }
+
+ public boolean modifyConfigurationValue(String key, String newValue) {
+ File configFile = new File(System.getProperty("user.dir") +"/config/configuration.json");
+ try (FileReader reader = new FileReader(configFile)) {
+ JSONTokener tokener = new JSONTokener(reader);
+ JSONObject jsonObject = new JSONObject(tokener);
+
+ if (!jsonObject.has(key)) {
+ System.out.println("Key does not exist in the configuration.");
+ return false;
+ }
+
+ jsonObject.put(key, newValue);
+
+ try (FileWriter writer = new FileWriter(configFile)) {
+ writer.write(jsonObject.toString(4)); // Indent with 4 spaces for readability
+ return true;
+ } catch (IOException e) {
+ System.out.println("Failed to write to the configuration file.");
+ return false;
+ }
+ } catch (IOException e) {
+ System.out.println("Failed to read the configuration file.");
+ return false;
+ }
+ }
+}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage