Skip to content

Commit

Permalink
Fix: it was impossible to use internal data from JAR packaging
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceFox committed Mar 13, 2019
1 parent 15cbe38 commit 79d93c1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if(JavaVersion.current() < JavaVersion.VERSION_1_8){

group 'fr.spacefox'
archivesBaseName = 'confusable-homoglyphs'
version '1.0.0'
version '1.0.1-SNAPSHOT'

sourceCompatibility = 1.8

Expand Down
31 changes: 22 additions & 9 deletions src/main/java/fr/spacefox/confusablehomoglyphs/UsesJsonData.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author SpaceFox
* @version 1.0.0
* @version 1.0.1
*/
abstract class UsesJsonData<T> {

Expand All @@ -28,18 +31,16 @@ abstract class UsesJsonData<T> {

boolean loadJson() {
ClassLoader classLoader = getClass().getClassLoader();
String internalJsonFileName = classLoader.getResource(getInternalResourceName()).getFile();
return loadJson(internalJsonFileName);
InputStream resourceStream = classLoader.getResourceAsStream(getInternalResourceName());
return loadJson(resourceStream);
}

abstract String getInternalResourceName();

boolean loadJson(String jsonFileName) {
try (Reader reader = new FileReader(jsonFileName)) {
setData(new GsonBuilder()
.create()
.fromJson(reader, realDataType));
initialized = true;
boolean initialized = false;
try (FileInputStream stream = new FileInputStream(new File(jsonFileName))) {
initialized = loadJson(stream);
} catch (FileNotFoundException e) {
LOGGER.severe("No " + jsonFileName + " data file found. Please regenerate it from CLI or download it.");
} catch (IOException | JsonSyntaxException e) {
Expand All @@ -49,6 +50,18 @@ boolean loadJson(String jsonFileName) {
return initialized;
}

private boolean loadJson(InputStream stream) {
try (Reader reader = new InputStreamReader(stream)) {
setData(new GsonBuilder()
.create()
.fromJson(reader, realDataType));
initialized = true;
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Unable to load data due to " + e, e);
}
return initialized;
}

abstract void setData(T fromJson);

void assertInitialization() {
Expand Down

0 comments on commit 79d93c1

Please sign in to comment.