Skip to content

Commit

Permalink
Bugfix in StorageProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Dec 10, 2019
1 parent 2cd86d4 commit 78c65fe
Show file tree
Hide file tree
Showing 6 changed files with 803 additions and 796 deletions.
166 changes: 82 additions & 84 deletions src/main/java/de/leonhard/storage/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@
@Getter
public class Json extends FlatFile {

public Json(Json json) {
super(json.getFile(), json.fileType);
this.fileData = json.getFileData();
}
public Json(Json json) {
super(json.getFile(), json.fileType);
fileData = json.getFileData();
}

public Json(String name, String path) {
this(name, path, null);
}
public Json(String name, String path) {
this(name, path, null);
}

public Json(String name, String path, InputStream inputStream) {
this(name, path, inputStream, null);
}
public Json(String name, String path, InputStream inputStream) {
this(name, path, inputStream, null);
}

public Json(String name, String path, InputStream inputStream, ReloadSettings reloadSettings) {
super(name, path, FileType.JSON);
public Json(String name, String path, InputStream inputStream, ReloadSettings reloadSettings) {
super(name, path, FileType.JSON);

if (create() || file.length() == 0) {
if (inputStream != null) {
FileUtils.writeToFile(file, inputStream);
}
if (create() || file.length() == 0) {
if (inputStream != null) {
FileUtils.writeToFile(file, inputStream);
}

// try (Writer writer = new PrintWriter(new FileWriter(getFile().getAbsolutePath()))) {
// writer.write(new JSONObject().toString(2));
Expand All @@ -49,72 +49,70 @@ public Json(String name, String path, InputStream inputStream, ReloadSettings re
// System.err.println("In '" + FileUtils.getParentDirPath(file) + "'");
// ex.printStackTrace();
// }
}

if (reloadSettings != null) {
this.reloadSettings = reloadSettings;
}

forceReload();
}

public Json(File file) {
super(file, FileType.JSON);
create();
try {
readToMap();
} catch (IOException e) {
e.printStackTrace();
}
}

// ----------------------------------------------------------------------------------------------------
// Methods to override (Points where JSON is unspecific for typical FlatFiles)
// ----------------------------------------------------------------------------------------------------

/**
* Gets a Map by key Although used to get nested objects {@link Json}
*
* @param key Path to Map-List in JSON
* @return Map
*/

@Override
public Map getMap(String key) {
String finalKey = (pathPrefix == null) ? key : pathPrefix + "." + key;
if (!contains(finalKey)) {
return new HashMap();
} else {
Object map = get(key);
if (map instanceof Map) {
return (Map<?, ?>) fileData.get(key);
} else if (map instanceof JSONObject) {
return ((JSONObject) map).toMap();
}
//Exception in casting
throw new IllegalArgumentException("ClassCastEx: Json contains key: '" + key + "' but it is not a Map");
}
}

// ----------------------------------------------------------------------------------------------------
// Abstract methods to implement
// ----------------------------------------------------------------------------------------------------

@Override
protected Map<String, Object> readToMap() throws IOException {
if (file.length() == 0) {
Files.write(file.toPath(), Collections.singletonList("{}"));
}

JSONTokener jsonTokener = new JSONTokener(FileUtils.createInputStream(file));
return new JSONObject(jsonTokener).toMap();
}

@Override
protected void write(FileData data) throws IOException {
try (Writer writer = FileUtils.createWriter(file)) {
writer.write(data.toJsonObject().toString(3));
writer.flush();
}
}
}

if (reloadSettings != null) {
this.reloadSettings = reloadSettings;
}
}

public Json(File file) {
super(file, FileType.JSON);
create();
try {
readToMap();
} catch (IOException e) {
e.printStackTrace();
}
}

// ----------------------------------------------------------------------------------------------------
// Methods to override (Points where JSON is unspecific for typical FlatFiles)
// ----------------------------------------------------------------------------------------------------

/**
* Gets a Map by key Although used to get nested objects {@link Json}
*
* @param key Path to Map-List in JSON
* @return Map
*/

@Override
public Map getMap(String key) {
String finalKey = (pathPrefix == null) ? key : pathPrefix + "." + key;
if (!contains(finalKey)) {
return new HashMap();
} else {
Object map = get(key);
if (map instanceof Map) {
return (Map<?, ?>) fileData.get(key);
} else if (map instanceof JSONObject) {
return ((JSONObject) map).toMap();
}
//Exception in casting
throw new IllegalArgumentException("ClassCastEx: Json contains key: '" + key + "' but it is not a Map");
}
}

// ----------------------------------------------------------------------------------------------------
// Abstract methods to implement
// ----------------------------------------------------------------------------------------------------

@Override
protected Map<String, Object> readToMap() throws IOException {
if (file.length() == 0) {
Files.write(file.toPath(), Collections.singletonList("{}"));
}

JSONTokener jsonTokener = new JSONTokener(FileUtils.createInputStream(file));
return new JSONObject(jsonTokener).toMap();
}

@Override
protected void write(FileData data) throws IOException {
try (Writer writer = FileUtils.createWriter(file)) {
writer.write(data.toJsonObject().toString(3));
writer.flush();
}
}
}
83 changes: 40 additions & 43 deletions src/main/java/de/leonhard/storage/Toml.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,52 @@

public class Toml extends FlatFile {

public Toml(Toml toml) {
super(toml.getFile());
this.fileData = toml.getFileData();
}
public Toml(Toml toml) {
super(toml.getFile());
fileData = toml.getFileData();
}

public Toml(String name, String path) {
this(name, path, null);
}
public Toml(String name, String path) {
this(name, path, null);
}

public Toml(String name, String path, InputStream inputStream) {
this(name, path, inputStream, null);
}
public Toml(String name, String path, InputStream inputStream) {
this(name, path, inputStream, null);
}

public Toml(String name, String path, InputStream inputStream, ReloadSettings reloadSettings) {
super(name, path, FileType.TOML);
if (create() && inputStream != null) {
FileUtils.writeToFile(file, inputStream);
}
public Toml(String name, String path, InputStream inputStream, ReloadSettings reloadSettings) {
super(name, path, FileType.TOML);
if (create() && inputStream != null) {
FileUtils.writeToFile(file, inputStream);
}

if (reloadSettings != null) {
this.reloadSettings = reloadSettings;
}
if (reloadSettings != null) {
this.reloadSettings = reloadSettings;
}
}

forceReload();
}
public Toml(File file) {
super(file);
create();
}

public Toml(File file) {
super(file);
create();
forceReload();
}
// ----------------------------------------------------------------------------------------------------
// Abstract methods to implement
// ----------------------------------------------------------------------------------------------------

// ----------------------------------------------------------------------------------------------------
// Abstract methods to implement
// ----------------------------------------------------------------------------------------------------
@Override
protected Map<String, Object> readToMap() throws IOException {
return com.electronwill.toml.Toml.read(getFile());
}

@Override
protected Map<String, Object> readToMap() throws IOException {
return com.electronwill.toml.Toml.read(getFile());
}

@Override
protected void write(FileData data) {
try {
com.electronwill.toml.Toml.write(data.toMap(), getFile());
} catch (IOException ex) {
System.err.println("Exception while writing fileData to file '" + getName() + "'");
System.err.println("In '" + FileUtils.getParentDirPath(file) + "'");
ex.printStackTrace();
}
}
@Override
protected void write(FileData data) {
try {
com.electronwill.toml.Toml.write(data.toMap(), getFile());
} catch (IOException ex) {
System.err.println("Exception while writing fileData to file '" + getName() + "'");
System.err.println("In '" + FileUtils.getParentDirPath(file) + "'");
ex.printStackTrace();
}
}
}
Loading

0 comments on commit 78c65fe

Please sign in to comment.