Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.exoticdev.wallcrafthg.gson;
- import com.exoticdev.wallcrafthg.file.FileInput;
- import com.google.gson.*;
- import org.apache.commons.io.IOUtils;
- import java.io.*;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- /**
- * Created by ExoticDev on 2017-10-16
- */
- public class GsonFactory {
- private File file;
- private String json;
- public GsonFactory(File file) {
- this.file = file;
- File directories = file.getParentFile();
- if(!directories.exists()) {
- directories.mkdirs();
- }
- if(!file.exists()) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(FileInput.getFileInput(file).isEmpty()) {
- this.json = this.checkForTemplate(file.getName());
- } else {
- this.json = FileInput.getFileInput(file);
- }
- }
- public GsonFactory(String input) {
- this.json = input;
- }
- public void writeData(String path, Object object) {
- String[] elements = path.split("\\.");
- elements = Arrays.copyOf(elements, elements.length - 1);
- String property = path.split("\\.")[elements.length];
- Gson gson = new GsonBuilder().create();
- GsonEntry entry = this.getObject(path);
- JsonObject jsonObject = null;
- JsonObject absoluteRaw = null;
- Object finalObject = null;
- if(entry == null || entry.getJsonObject() == null) {
- jsonObject = new JsonObject();
- jsonObject.add(property, new JsonPrimitive("null"));
- absoluteRaw = new JsonObject();
- finalObject = "\"null\"";
- }
- String startingJson = this.getJson();
- if(jsonObject == null) {
- jsonObject = entry.getJsonObject();
- absoluteRaw = entry.getJsonObject();
- finalObject = entry.getFinalObject();
- }
- String replacingProperty = "\"" + property + "\":" + finalObject.toString();
- String json;
- if(object != null) {
- if(object instanceof List) {
- json = jsonObject.toString().replaceFirst(replacingProperty, "\"" + property + "\":" + gson.toJson(object));
- } else {
- json = jsonObject.toString().replaceFirst(replacingProperty, "\"" + property + "\":" + object.toString());
- }
- } else {
- json = jsonObject.toString().replaceFirst(replacingProperty, "");
- }
- Map<String, Object> startingJsonMap = gson.fromJson(startingJson, Map.class);
- Map<String, Object> newJsonMap = gson.fromJson(absoluteRaw.toString(), Map.class);
- String parent = null;
- for(Map.Entry<String, Object> values : startingJsonMap.entrySet()) {
- if(gson.toJson(values.getValue()).equals(gson.toJson(newJsonMap))) {
- parent = values.getKey();
- }
- }
- if(parent == null) {
- this.updateJson(json);
- } else {
- startingJsonMap.replace(parent, gson.fromJson(json, Map.class));
- this.updateJson(gson.toJson(startingJsonMap));
- }
- }
- public Boolean getBoolean(String path) {
- return Boolean.parseBoolean((String) this.getObject(path).getFinalObject());
- }
- public Float getFloat(String path) {
- return Float.parseFloat((String) this.getObject(path).getFinalObject());
- }
- public Double getDouble(String path) {
- return Double.parseDouble((String) this.getObject(path).getFinalObject());
- }
- public Integer getInteger(String path) {
- return Integer.parseInt((String) this.getObject(path).getFinalObject());
- }
- public String getString(String path) {
- return (String) this.getObject(path).getFinalObject();
- }
- public List<String> getStringList(String path) {
- return (List<String>) this.getObject(path).getFinalObject();
- }
- public List<Object> getObjectList(String path) {
- return (List<Object>) this.getObject(path).getFinalObject();
- }
- public boolean contains(String path) {
- return this.getObject(path) != null;
- }
- public File getFile() {
- return this.file;
- }
- public void refreshConfig() {
- if(this.file != null && this.file.exists())
- this.json = FileInput.getFileInput(this.file);
- }
- private <T extends GsonEntry> T getObject(String path) {
- JsonParser parser = new JsonParser();
- JsonElement element = parser.parse(this.json);
- JsonObject currentObject = null;
- if(element.isJsonObject()) {
- currentObject = element.getAsJsonObject();
- String[] elements = path.split("\\.");
- if(elements.length <= 1) {
- if(currentObject.get(path) == null)
- return null;
- if(currentObject.get(path) instanceof JsonArray)
- return (T) new GsonEntry(currentObject.getAsJsonArray(path), currentObject);
- else
- return (T) new GsonEntry(currentObject.getAsJsonPrimitive(path).getAsString(), currentObject);
- }
- for(int i = 0; i < elements.length; i++) {
- String currentElement = elements[i];
- if((i + 1) == elements.length) {
- if(currentObject == null || currentObject.get(currentElement) == null)
- return null;
- if(currentObject.get(currentElement) instanceof JsonArray)
- return (T) new GsonEntry(currentObject.getAsJsonArray(currentElement), currentObject);
- else
- return (T) new GsonEntry(currentObject.getAsJsonPrimitive(currentElement).getAsString(), currentObject);
- } else {
- currentObject = currentObject.getAsJsonObject(currentElement);
- }
- }
- }
- return null;
- }
- private String getJson() {
- return this.json;
- }
- private void updateJson(String json) {
- if(this.file != null && this.file.exists()) {
- PrintStream printStream = null;
- try {
- printStream = new PrintStream(new FileOutputStream(this.file));
- JsonParser parser = new JsonParser();
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- JsonElement element = parser.parse(json);
- printStream.print(gson.toJson(element));
- } catch (FileNotFoundException exception) {
- exception.printStackTrace();
- }
- if(printStream != null)
- printStream.close();
- }
- this.json = json;
- }
- private String checkForTemplate(String configName) {
- if(configName == null) {
- throw new IllegalArgumentException("Filename cannot be null");
- }
- try {
- URL url = this.getClass().getClassLoader().getResource(configName);
- if(url == null) {
- return "{}";
- }
- URLConnection connection = url.openConnection();
- connection.setUseCaches(false);
- InputStream stream = connection.getInputStream();
- return IOUtils.toString(stream);
- } catch (IOException ex) {
- return "{}";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement