aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/Configuration.java
blob: 6e01e82fb64e61634cf4a407c5bf1816ea776d6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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", "");
        configurationData.put("browser", "");
        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, creating new key...");
                jsonObject.put(key, newValue);
                try (FileWriter writer = new FileWriter(configFile)) {
                    writer.write(jsonObject.toString(4));
                }
                return true;
            }

            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