You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.5 KiB
147 lines
4.5 KiB
package de.srsoftware.belegscanner; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
import java.io.File; |
|
import java.io.FileReader; |
|
import java.io.FileWriter; |
|
import java.io.IOException; |
|
import java.io.OutputStreamWriter; |
|
import java.nio.charset.StandardCharsets; |
|
|
|
import org.json.JSONArray; |
|
import org.json.JSONObject; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
public class Configuration { |
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Configuration.class); |
|
private File file; |
|
private JSONObject json = new JSONObject(); |
|
|
|
public Configuration(String appName) throws IOException { |
|
String home = System.getProperty("user.home"); |
|
String fileName = new StringBuilder().append(home).append(File.separator).append(".config").append(File.separator).append(appName).append(File.separator).append("config.json").toString(); |
|
file = new File(fileName); |
|
if (file.exists()) { |
|
LOG.info("{} found, loading…",file); |
|
FileReader in = new FileReader(file); |
|
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); |
|
OutputStreamWriter writer = new OutputStreamWriter(buffer); |
|
in.transferTo(writer); |
|
writer.close(); |
|
String jsonString = buffer.toString(StandardCharsets.UTF_8); |
|
LOG.debug("Loaded json: {}",jsonString); |
|
json = jsonString.startsWith("{") ? new JSONObject(jsonString) : new JSONObject(); |
|
in.close(); |
|
} else { |
|
LOG.info("{} not found, creating new config.",file); |
|
json = new JSONObject(); |
|
} |
|
} |
|
|
|
private void save() throws IOException { |
|
LOG.debug("Trying to save config…"); |
|
File dir = file.getParentFile(); |
|
if (!dir.exists()) dir.mkdirs(); |
|
FileWriter out = new FileWriter(file); |
|
json.write(out, 2, 0); |
|
out.close(); |
|
} |
|
|
|
public JSONArray getOrCreateArray(String key) { |
|
LOG.debug("requesting {} from config.",key); |
|
String[] parts = key.split("\\."); |
|
JSONObject localJson = json; |
|
boolean update = false; |
|
JSONArray result = null; |
|
for (int i=0; i<parts.length; i++) { |
|
String localKey = parts[i]; |
|
LOG.debug("localKey = {}",localKey); |
|
boolean lastPart = i == parts.length-1; |
|
if (!localJson.has(localKey)) { |
|
LOG.debug("{} not present in {}, creating…",localKey,localJson); |
|
localJson.put(localKey, lastPart ? new JSONArray() : new JSONObject()); |
|
update = true; |
|
} |
|
if (lastPart) { |
|
result = localJson.getJSONArray(localKey); |
|
} else localJson = localJson.getJSONObject(localKey); |
|
} |
|
if (update) try { |
|
save(); |
|
} catch (IOException ex) { |
|
LOG.warn("Was not able to write config to {}!",file); |
|
} |
|
return result; |
|
} |
|
|
|
@SuppressWarnings("unchecked") |
|
public <T> T get(String key) { |
|
LOG.debug("requesting {} from config.",key); |
|
String[] parts = key.split("\\."); |
|
JSONObject localJson = json; |
|
for (int i=0; i<parts.length; i++) { |
|
String localKey = parts[i]; |
|
LOG.debug("localKey = {}",localKey); |
|
boolean lastPart = i == parts.length-1; |
|
if (!localJson.has(localKey)) return null; |
|
if (lastPart) return (T) localJson.get(localKey); |
|
localJson = localJson.getJSONObject(localKey); |
|
} |
|
return null; |
|
} |
|
|
|
@SuppressWarnings("unchecked") |
|
public <T> T getOrCreate(String key, T preset) { |
|
LOG.debug("requesting {} from config.",key); |
|
String[] parts = key.split("\\."); |
|
JSONObject localJson = json; |
|
boolean update = false; |
|
Object result = preset; |
|
for (int i=0; i<parts.length; i++) { |
|
String localKey = parts[i]; |
|
LOG.debug("localKey = {}",localKey); |
|
boolean lastPart = i == parts.length-1; |
|
if (!localJson.has(localKey)) { |
|
LOG.debug("{} not present in {}, creating…",localKey,localJson); |
|
localJson.put(localKey, lastPart ? preset : new JSONObject()); |
|
update = true; |
|
} |
|
if (lastPart) { |
|
result = localJson.get(localKey); |
|
} else localJson = localJson.getJSONObject(localKey); |
|
} |
|
if (update) try { |
|
save(); |
|
} catch (IOException ex) { |
|
LOG.warn("Was not able to write config to {}!",file); |
|
} |
|
return (T) result; |
|
} |
|
|
|
public void set(String key, Object value) { |
|
LOG.debug("setting {} to {}",key,value); |
|
String[] parts = key.split("\\."); |
|
JSONObject localJson = json; |
|
for (int i=0; i<parts.length; i++) { |
|
String localKey = parts[i]; |
|
LOG.debug("localKey = {}",localKey); |
|
boolean lastPart = i == parts.length-1; |
|
if (!lastPart && !localJson.has(localKey)) { |
|
localJson.put(localKey, new JSONObject()); |
|
} |
|
if (lastPart) { |
|
localJson.put(localKey, value); |
|
break; |
|
} |
|
localJson = localJson.getJSONObject(localKey); |
|
} |
|
LOG.debug("altered json: {}",json); |
|
try { |
|
save(); |
|
} catch (IOException ex) { |
|
LOG.warn("Was not able to write config to {}!",file); |
|
} |
|
} |
|
}
|
|
|