From 28cf3185c1374c4b5af277ef28482299694209a3 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 20 Jun 2022 22:21:16 +0100 Subject: New paper config location (#217) --- .../serverconfig/AbstractServerConfigProvider.java | 63 ++++++++++++++------- .../common/platform/serverconfig/ConfigParser.java | 31 +++++++++++ .../serverconfig/PropertiesConfigParser.java | 61 +++++++++++++++++++++ .../serverconfig/PropertiesFileReader.java | 64 ---------------------- 4 files changed, 136 insertions(+), 83 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java delete mode 100644 spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesFileReader.java (limited to 'spark-common/src/main/java/me') diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java index ead2131..0eef111 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java @@ -21,10 +21,17 @@ package me.lucko.spark.common.platform.serverconfig; import com.google.common.collect.ImmutableMap; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import java.io.BufferedReader; import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -38,29 +45,37 @@ import java.util.stream.Collectors; * *

This implementation is able to delete hidden paths from * the configurations before they are sent to the viewer.

- * - * @param the file type */ -public abstract class AbstractServerConfigProvider> implements ServerConfigProvider { - private final Map files; +public abstract class AbstractServerConfigProvider implements ServerConfigProvider { + private final Map files; private final Collection hiddenPaths; - protected AbstractServerConfigProvider(Map files, Collection hiddenPaths) { + private final Gson gson; + + protected AbstractServerConfigProvider(Map files, Collection hiddenPaths) { this.files = files; this.hiddenPaths = hiddenPaths; + + GsonBuilder gson = new GsonBuilder(); + customiseGson(gson); + this.gson = gson.create(); } @Override public final Map loadServerConfigurations() { ImmutableMap.Builder builder = ImmutableMap.builder(); - this.files.forEach((path, type) -> { + this.files.forEach((path, reader) -> { try { - JsonElement json = load(path, type); - if (json != null) { - delete(json, this.hiddenPaths); - builder.put(path, json); + JsonElement json = load(path, reader); + if (json == null) { + return; } + + delete(json, this.hiddenPaths); + + String name = rewriteConfigPath(path); + builder.put(name, json); } catch (Exception e) { e.printStackTrace(); } @@ -69,15 +84,25 @@ public abstract class AbstractServerConfigProvider> implements return builder.build(); } - /** - * Loads a file from the system. - * - * @param path the name of the file to load - * @param type the type of the file - * @return the loaded file - * @throws IOException if an error occurs performing i/o - */ - protected abstract JsonElement load(String path, T type) throws IOException; + private JsonElement load(String path, ConfigParser parser) throws IOException { + Path filePath = Paths.get(path); + if (!Files.exists(filePath)) { + return null; + } + + try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { + Map values = parser.parse(reader); + return this.gson.toJsonTree(values); + } + } + + protected void customiseGson(GsonBuilder gson) { + + } + + protected String rewriteConfigPath(String path) { + return path; + } /** * Deletes the given paths from the json element. diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java new file mode 100644 index 0000000..2dd15fe --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java @@ -0,0 +1,31 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.platform.serverconfig; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Map; + +public interface ConfigParser { + + Map parse(BufferedReader reader) throws IOException; + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java new file mode 100644 index 0000000..4c7c2c1 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesConfigParser.java @@ -0,0 +1,61 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.platform.serverconfig; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * A {@link ConfigParser} that can parse a .properties file. + */ +public enum PropertiesConfigParser implements ConfigParser { + INSTANCE; + + @Override + public Map parse(BufferedReader reader) throws IOException { + Properties properties = new Properties(); + properties.load(reader); + + Map values = new HashMap<>(); + properties.forEach((k, v) -> { + String key = k.toString(); + String value = v.toString(); + + if ("true".equals(value) || "false".equals(value)) { + values.put(key, Boolean.parseBoolean(value)); + } else if (value.matches("\\d+")) { + try { + values.put(key, Long.parseLong(value)); + } catch (NumberFormatException e) { + values.put(key, value); + } + } else { + values.put(key, value); + } + }); + + return values; + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesFileReader.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesFileReader.java deleted file mode 100644 index 8fc89d7..0000000 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/PropertiesFileReader.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * This file is part of spark. - * - * Copyright (c) lucko (Luck) - * Copyright (c) contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.lucko.spark.common.platform.serverconfig; - -import java.io.FilterReader; -import java.io.IOException; -import java.io.Reader; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -/** - * A {@link Reader} that can parse a .properties file. - */ -public class PropertiesFileReader extends FilterReader { - - public PropertiesFileReader(Reader in) { - super(in); - } - - public Map readProperties() throws IOException { - Properties properties = new Properties(); - properties.load(this); - - Map values = new HashMap<>(); - properties.forEach((k, v) -> { - String key = k.toString(); - String value = v.toString(); - - if ("true".equals(value) || "false".equals(value)) { - values.put(key, Boolean.parseBoolean(value)); - } else if (value.matches("\\d+")) { - try { - values.put(key, Long.parseLong(value)); - } catch (NumberFormatException e) { - values.put(key, value); - } - } else { - values.put(key, value); - } - }); - - return values; - } - -} -- cgit