Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. package com.exoticdev.wallcrafthg.gson;
  2.  
  3. import com.exoticdev.wallcrafthg.file.FileInput;
  4. import com.google.gson.*;
  5. import org.apache.commons.io.IOUtils;
  6.  
  7. import java.io.*;
  8. import java.net.URL;
  9. import java.net.URLConnection;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.Map;
  13.  
  14. /**
  15. * Created by ExoticDev on 2017-10-16
  16. */
  17.  
  18. public class GsonFactory {
  19.  
  20. private File file;
  21. private String json;
  22.  
  23. public GsonFactory(File file) {
  24. this.file = file;
  25.  
  26. File directories = file.getParentFile();
  27.  
  28. if(!directories.exists()) {
  29. directories.mkdirs();
  30. }
  31.  
  32. if(!file.exists()) {
  33. try {
  34. file.createNewFile();
  35. } catch (IOException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. if(FileInput.getFileInput(file).isEmpty()) {
  41. this.json = this.checkForTemplate(file.getName());
  42. } else {
  43. this.json = FileInput.getFileInput(file);
  44. }
  45. }
  46.  
  47. public GsonFactory(String input) {
  48. this.json = input;
  49. }
  50.  
  51. public void writeData(String path, Object object) {
  52. String[] elements = path.split("\\.");
  53.  
  54. elements = Arrays.copyOf(elements, elements.length - 1);
  55.  
  56. String property = path.split("\\.")[elements.length];
  57.  
  58. Gson gson = new GsonBuilder().create();
  59. GsonEntry entry = this.getObject(path);
  60.  
  61. JsonObject jsonObject = null;
  62. JsonObject absoluteRaw = null;
  63. Object finalObject = null;
  64.  
  65. if(entry == null || entry.getJsonObject() == null) {
  66. jsonObject = new JsonObject();
  67. jsonObject.add(property, new JsonPrimitive("null"));
  68.  
  69. absoluteRaw = new JsonObject();
  70.  
  71. finalObject = "\"null\"";
  72. }
  73.  
  74. String startingJson = this.getJson();
  75.  
  76. if(jsonObject == null) {
  77. jsonObject = entry.getJsonObject();
  78. absoluteRaw = entry.getJsonObject();
  79. finalObject = entry.getFinalObject();
  80. }
  81.  
  82. String replacingProperty = "\"" + property + "\":" + finalObject.toString();
  83.  
  84. String json;
  85.  
  86. if(object != null) {
  87. if(object instanceof List) {
  88. json = jsonObject.toString().replaceFirst(replacingProperty, "\"" + property + "\":" + gson.toJson(object));
  89. } else {
  90. json = jsonObject.toString().replaceFirst(replacingProperty, "\"" + property + "\":" + object.toString());
  91. }
  92. } else {
  93. json = jsonObject.toString().replaceFirst(replacingProperty, "");
  94. }
  95.  
  96. Map<String, Object> startingJsonMap = gson.fromJson(startingJson, Map.class);
  97. Map<String, Object> newJsonMap = gson.fromJson(absoluteRaw.toString(), Map.class);
  98.  
  99. String parent = null;
  100.  
  101. for(Map.Entry<String, Object> values : startingJsonMap.entrySet()) {
  102. if(gson.toJson(values.getValue()).equals(gson.toJson(newJsonMap))) {
  103. parent = values.getKey();
  104. }
  105. }
  106.  
  107. if(parent == null) {
  108. this.updateJson(json);
  109. } else {
  110. startingJsonMap.replace(parent, gson.fromJson(json, Map.class));
  111.  
  112. this.updateJson(gson.toJson(startingJsonMap));
  113. }
  114. }
  115.  
  116. public Boolean getBoolean(String path) {
  117. return Boolean.parseBoolean((String) this.getObject(path).getFinalObject());
  118. }
  119.  
  120. public Float getFloat(String path) {
  121. return Float.parseFloat((String) this.getObject(path).getFinalObject());
  122. }
  123.  
  124. public Double getDouble(String path) {
  125. return Double.parseDouble((String) this.getObject(path).getFinalObject());
  126. }
  127.  
  128. public Integer getInteger(String path) {
  129. return Integer.parseInt((String) this.getObject(path).getFinalObject());
  130. }
  131.  
  132. public String getString(String path) {
  133. return (String) this.getObject(path).getFinalObject();
  134. }
  135.  
  136. public List<String> getStringList(String path) {
  137. return (List<String>) this.getObject(path).getFinalObject();
  138. }
  139.  
  140. public List<Object> getObjectList(String path) {
  141. return (List<Object>) this.getObject(path).getFinalObject();
  142. }
  143.  
  144. public boolean contains(String path) {
  145. return this.getObject(path) != null;
  146. }
  147.  
  148. public File getFile() {
  149. return this.file;
  150. }
  151.  
  152. public void refreshConfig() {
  153. if(this.file != null && this.file.exists())
  154. this.json = FileInput.getFileInput(this.file);
  155. }
  156.  
  157. private <T extends GsonEntry> T getObject(String path) {
  158. JsonParser parser = new JsonParser();
  159. JsonElement element = parser.parse(this.json);
  160.  
  161. JsonObject currentObject = null;
  162.  
  163. if(element.isJsonObject()) {
  164. currentObject = element.getAsJsonObject();
  165.  
  166. String[] elements = path.split("\\.");
  167.  
  168. if(elements.length <= 1) {
  169. if(currentObject.get(path) == null)
  170. return null;
  171.  
  172. if(currentObject.get(path) instanceof JsonArray)
  173. return (T) new GsonEntry(currentObject.getAsJsonArray(path), currentObject);
  174. else
  175. return (T) new GsonEntry(currentObject.getAsJsonPrimitive(path).getAsString(), currentObject);
  176. }
  177.  
  178. for(int i = 0; i < elements.length; i++) {
  179. String currentElement = elements[i];
  180.  
  181. if((i + 1) == elements.length) {
  182. if(currentObject == null || currentObject.get(currentElement) == null)
  183. return null;
  184.  
  185. if(currentObject.get(currentElement) instanceof JsonArray)
  186. return (T) new GsonEntry(currentObject.getAsJsonArray(currentElement), currentObject);
  187. else
  188. return (T) new GsonEntry(currentObject.getAsJsonPrimitive(currentElement).getAsString(), currentObject);
  189. } else {
  190. currentObject = currentObject.getAsJsonObject(currentElement);
  191. }
  192. }
  193. }
  194.  
  195. return null;
  196. }
  197.  
  198. private String getJson() {
  199. return this.json;
  200. }
  201.  
  202. private void updateJson(String json) {
  203. if(this.file != null && this.file.exists()) {
  204. PrintStream printStream = null;
  205.  
  206. try {
  207. printStream = new PrintStream(new FileOutputStream(this.file));
  208.  
  209. JsonParser parser = new JsonParser();
  210. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  211.  
  212. JsonElement element = parser.parse(json);
  213.  
  214. printStream.print(gson.toJson(element));
  215. } catch (FileNotFoundException exception) {
  216. exception.printStackTrace();
  217. }
  218.  
  219. if(printStream != null)
  220. printStream.close();
  221. }
  222.  
  223. this.json = json;
  224. }
  225.  
  226. private String checkForTemplate(String configName) {
  227. if(configName == null) {
  228. throw new IllegalArgumentException("Filename cannot be null");
  229. }
  230.  
  231. try {
  232. URL url = this.getClass().getClassLoader().getResource(configName);
  233.  
  234. if(url == null) {
  235. return "{}";
  236. }
  237.  
  238. URLConnection connection = url.openConnection();
  239.  
  240. connection.setUseCaches(false);
  241.  
  242. InputStream stream = connection.getInputStream();
  243.  
  244. return IOUtils.toString(stream);
  245. } catch (IOException ex) {
  246. return "{}";
  247. }
  248. }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement