Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.78 KB | None | 0 0
  1. import com.google.gson.*;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4.  
  5. import java.io.*;
  6. import java.lang.reflect.Type;
  7. import java.nio.charset.StandardCharsets;
  8. import java.nio.file.Files;
  9. import java.nio.file.Path;
  10. import java.nio.file.Paths;
  11. import java.util.*;
  12.  
  13. public class Document {
  14.  
  15. public static Gson GSON = new GsonBuilder().serializeNulls().setPrettyPrinting().disableHtmlEscaping().create();
  16. protected static final JsonParser PARSER = new JsonParser();
  17.  
  18. @Getter
  19. @Setter
  20. protected String name;
  21. @Getter
  22. @Setter
  23. private File file;
  24.  
  25. protected JsonObject dataCatcher;
  26.  
  27. public Document(String name) {
  28. this.name = name;
  29. this.dataCatcher = new JsonObject();
  30. }
  31.  
  32. public Document(String name, JsonObject source) {
  33. this.name = name;
  34. this.dataCatcher = source;
  35. }
  36.  
  37. public Document(File file, JsonObject jsonObject) {
  38. this.file = file;
  39. this.dataCatcher = jsonObject;
  40. }
  41.  
  42. public Document(String key, String value) {
  43. this.dataCatcher = new JsonObject();
  44. this.append(key, value);
  45. }
  46.  
  47. public Document(String key, Object value) {
  48. this.dataCatcher = new JsonObject();
  49. this.append(key, value);
  50. }
  51.  
  52. public Document(String key, Number value) {
  53. this.dataCatcher = new JsonObject();
  54. this.append(key, value);
  55. }
  56.  
  57. public Document(Document defaults) {
  58. this.dataCatcher = defaults.dataCatcher;
  59. }
  60.  
  61. public Document(Document defaults, String name) {
  62. this.dataCatcher = defaults.dataCatcher;
  63. this.name = name;
  64. }
  65.  
  66. public Document() {
  67. this.dataCatcher = new JsonObject();
  68. }
  69.  
  70. public Document(JsonObject source) {
  71. this.dataCatcher = source;
  72. }
  73.  
  74. public JsonObject obj() {
  75. return dataCatcher;
  76. }
  77.  
  78. public boolean contains(String key) {
  79. return this.dataCatcher.has(key);
  80. }
  81.  
  82. public Document append(String key, String value) {
  83. if (value == null) return this;
  84. this.dataCatcher.addProperty(key, value);
  85. return this;
  86. }
  87.  
  88. public Document append(String key, Number value) {
  89. if (value == null) return this;
  90. this.dataCatcher.addProperty(key, value);
  91. return this;
  92. }
  93.  
  94. public Document append(String key, Boolean value) {
  95. if (value == null) return this;
  96. this.dataCatcher.addProperty(key, value);
  97. return this;
  98. }
  99.  
  100. @Override
  101. public Document append(String key, JsonElement value) {
  102. if (value == null) return this;
  103. this.dataCatcher.add(key, value);
  104. return this;
  105. }
  106.  
  107. public Document append(String key, List<String> value) {
  108. if (value == null) return this;
  109. JsonArray jsonElements = new JsonArray();
  110.  
  111. for (String b : value) {
  112. jsonElements.add(b);
  113. }
  114.  
  115. this.dataCatcher.add(key, jsonElements);
  116. return this;
  117. }
  118.  
  119. public Document append(String key, Document value) {
  120. if (value == null) return this;
  121. this.dataCatcher.add(key, value.dataCatcher);
  122. return this;
  123. }
  124.  
  125. @Deprecated
  126. public Document append(String key, Object value) {
  127. if (value == null) return this;
  128. this.dataCatcher.add(key, GSON.toJsonTree(value));
  129. return this;
  130. }
  131.  
  132. public Document appendValues(java.util.Map<String, Object> values) {
  133. for (java.util.Map.Entry<String, Object> valuess : values.entrySet()) {
  134. append(valuess.getKey(), valuess.getValue());
  135. }
  136. return this;
  137. }
  138.  
  139. @Override
  140. public Document remove(String key) {
  141. this.dataCatcher.remove(key);
  142. return this;
  143. }
  144.  
  145. public Set<String> keys() {
  146. Set<String> c = new HashSet<>();
  147.  
  148. for (Map.Entry<String, JsonElement> x : dataCatcher.entrySet()) {
  149. c.add(x.getKey());
  150. }
  151.  
  152. return c;
  153. }
  154.  
  155. public JsonElement get(String key) {
  156. if (!dataCatcher.has(key)) return null;
  157. return dataCatcher.get(key);
  158. }
  159.  
  160. public String getString(String key) {
  161. if (!dataCatcher.has(key)) return null;
  162. return dataCatcher.get(key).getAsString();
  163. }
  164.  
  165. public int getInt(String key) {
  166. if (!dataCatcher.has(key)) return 0;
  167. return dataCatcher.get(key).getAsInt();
  168. }
  169.  
  170. public long getLong(String key) {
  171. if (!dataCatcher.has(key)) return 0L;
  172. return dataCatcher.get(key).getAsLong();
  173. }
  174.  
  175. public double getDouble(String key) {
  176. if (!dataCatcher.has(key)) return 0D;
  177. return dataCatcher.get(key).getAsDouble();
  178. }
  179.  
  180. public boolean getBoolean(String key) {
  181. if (!dataCatcher.has(key)) return false;
  182. return dataCatcher.get(key).getAsBoolean();
  183. }
  184.  
  185. public float getFloat(String key) {
  186. if (!dataCatcher.has(key)) return 0F;
  187. return dataCatcher.get(key).getAsFloat();
  188. }
  189.  
  190. public short getShort(String key) {
  191. if (!dataCatcher.has(key)) return 0;
  192. return dataCatcher.get(key).getAsShort();
  193. }
  194.  
  195. public <T> T getObject(String key, Class<T> class_) {
  196. if (!dataCatcher.has(key)) return null;
  197. JsonElement element = dataCatcher.get(key);
  198.  
  199. return GSON.fromJson(element, class_);
  200. }
  201.  
  202. public Document getDocument(String key) {
  203. Document document = new Document(dataCatcher.get(key).getAsJsonObject());
  204. return document;
  205. }
  206.  
  207. public Document clear() {
  208. for (String key : keys()) {
  209. remove(key);
  210. }
  211. return this;
  212. }
  213.  
  214. public int size() {
  215. return this.dataCatcher.size();
  216. }
  217.  
  218. public Document loadProperties(Properties properties) {
  219. Enumeration<?> enumeration = properties.propertyNames();
  220. while (enumeration.hasMoreElements()) {
  221. Object x = enumeration.nextElement();
  222. this.append(x.toString(), properties.getProperty(x.toString()));
  223. }
  224. return this;
  225. }
  226.  
  227. public boolean isEmpty() {
  228. return this.dataCatcher.size() == 0;
  229. }
  230.  
  231. public JsonArray getArray(String key) {
  232. return dataCatcher.get(key).getAsJsonArray();
  233. }
  234.  
  235. public String convertToJson() {
  236. return GSON.toJson(dataCatcher);
  237. }
  238.  
  239. public String convertToJsonString() {
  240. return dataCatcher.toString();
  241. }
  242.  
  243. public boolean saveAsConfig(File backend) {
  244. if (backend == null) return false;
  245.  
  246. if (backend.exists()) {
  247. backend.delete();
  248. }
  249.  
  250. try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(backend), "UTF-8")) {
  251. GSON.toJson(dataCatcher, (writer));
  252. return true;
  253. } catch (IOException exception) {
  254. exception.getStackTrace();
  255. }
  256. return false;
  257. }
  258.  
  259. @Deprecated
  260. public boolean saveAsConfig0(File backend) {
  261. if (backend == null) return false;
  262.  
  263. if (backend.exists()) {
  264. backend.delete();
  265. }
  266.  
  267. try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(backend), "UTF-8")) {
  268. new Gson().toJson(dataCatcher, (writer));
  269. return true;
  270. } catch (IOException exception) {
  271. exception.getStackTrace();
  272. }
  273. return false;
  274. }
  275.  
  276. public boolean saveAsConfig(Path path) {
  277. try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(Files.newOutputStream(path), "UTF-8")) {
  278. GSON.toJson(dataCatcher, outputStreamWriter);
  279. return true;
  280. } catch (IOException exception) {
  281. exception.printStackTrace();
  282. }
  283. return false;
  284. }
  285.  
  286. public boolean saveAsConfig(String path) {
  287. return saveAsConfig(Paths.get(path));
  288. }
  289.  
  290. public static Document loadDocument(File backend) {
  291. return loadDocument(backend.toPath());
  292. }
  293.  
  294. public static Document $loadDocument(File backend) throws Exception {
  295. try {
  296. return new Document(PARSER.parse(new String(Files.readAllBytes(backend.toPath()), StandardCharsets.UTF_8)).getAsJsonObject());
  297. } catch (Exception exception) {
  298. throw new Exception(exception);
  299. }
  300. }
  301.  
  302. public static Document loadDocument(Path backend) {
  303.  
  304. try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(backend), "UTF-8");
  305. BufferedReader bufferedReader = new BufferedReader(reader)) {
  306. JsonObject object = PARSER.parse(bufferedReader).getAsJsonObject();
  307. return new Document(object);
  308. } catch (Exception exception) {
  309. exception.getStackTrace();
  310. }
  311. return new Document();
  312.  
  313. /*
  314. try
  315. {
  316. return new Document(PARSER.parse(new String(Files.readAllBytes(backend), StandardCharsets.UTF_8)).getAsJsonObject());
  317. }catch (Exception ex) {
  318. ex.printStackTrace();
  319. }
  320. return new Document();
  321. */
  322. }
  323.  
  324. public Document loadToExistingDocument(File backend) {
  325. try (InputStreamReader reader = new InputStreamReader(new FileInputStream(backend), "UTF-8")) {
  326.  
  327. this.dataCatcher = PARSER.parse(reader).getAsJsonObject();
  328. this.file = backend;
  329. return this;
  330. } catch (Exception exception) {
  331. exception.getStackTrace();
  332. }
  333. return new Document();
  334. }
  335.  
  336. public Document loadToExistingDocument(Path path) {
  337. try (InputStreamReader reader = new InputStreamReader(Files.newInputStream(path), "UTF-8")) {
  338.  
  339. this.dataCatcher = PARSER.parse(reader).getAsJsonObject();
  340. return this;
  341. } catch (Exception exception) {
  342. exception.getStackTrace();
  343. }
  344. return new Document();
  345. }
  346.  
  347. public static Document load(String input) {
  348. try (InputStreamReader reader = new InputStreamReader(new StringBufferInputStream(input), "UTF-8")) {
  349. return new Document(PARSER.parse(new BufferedReader(reader)).getAsJsonObject());
  350. } catch (IOException exception) {
  351. exception.printStackTrace();
  352. }
  353. return new Document();
  354. }
  355.  
  356. @Override
  357. public String toString() {
  358. return convertToJsonString();
  359. }
  360.  
  361. public static Document load(JsonObject input) {
  362. return new Document(input);
  363. }
  364.  
  365. public <T> T getObject(String key, Type type) {
  366. if (!contains(key)) return null;
  367.  
  368. return GSON.fromJson(dataCatcher.get(key), type);
  369. }
  370.  
  371. public byte[] toBytesAsUTF_8() {
  372. return convertToJsonString().getBytes(StandardCharsets.UTF_8);
  373. }
  374.  
  375. public byte[] toBytes() {
  376. return convertToJsonString().getBytes();
  377. }
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement