Werkzeug um Belege zu scannen, Texterkennung durchzuführen und Belege sortiert abzulegen
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.
 
 

181 lines
5.4 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;
/**
*
* JSON-based Configuration manager for the Application.
*
* @author Stephan Richter <s.richter@srsoftware.de>
*
*/
public class Configuration {
private static final Logger LOG = LoggerFactory.getLogger(Configuration.class);
private File file;
private JSONObject json = new JSONObject();
/**
* Create/Load configuration for the application.
* @param appName
* @throws IOException
*/
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();
}
}
/**
* Write contents of the configuration object to the underlying file.
* @throws IOException
*/
public 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();
}
/**
* Load an JSON array from the configuration
* @param key Search path in the form path.to.array
* @return
*/
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;
}
/**
* Load an Object from the configuration
* @param <T>
* @param key Search path in the form path.to.array
* @return null, if no such path is found
*/
@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;
}
/**
* Load an Object from the configuration, create it, if it is not present
* @param <T>
* @param key Search path in the form path.to.array
* @param preset Content for the path, if it does not exist
* @return preset, if no such path is found
*/
@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;
}
/**
* Add a new value to the configuration or replace an existing value
* @param key Search path in the form path.to.array
* @param value the value to add/replace
*/
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);
}
}