mirror of
https://github.com/SimplexDevelopment/Configurator.git
synced 2025-07-01 14:06:41 +00:00
Initial Commit
This is like 60% done, still need to implement a few more things in each respective configuration type.
This commit is contained in:
152
src/main/java/app/simplexdev/Configurator.java
Normal file
152
src/main/java/app/simplexdev/Configurator.java
Normal file
@ -0,0 +1,152 @@
|
||||
package app.simplexdev;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.data.Resource;
|
||||
import app.simplexdev.parser.JSONParser;
|
||||
import app.simplexdev.parser.PropertiesParser;
|
||||
import app.simplexdev.parser.TomlParser;
|
||||
import app.simplexdev.parser.YamlParser;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public final class Configurator
|
||||
{
|
||||
private static final Logger LOGGER = Logger.getLogger(Configurator.class.getSimpleName());
|
||||
|
||||
private Configuration configuration = null;
|
||||
|
||||
public Configurator(final String name, final File location, final ConfigType type)
|
||||
{
|
||||
this(name, location, type, null);
|
||||
}
|
||||
|
||||
public Configurator(final String name, final File location, final ConfigType type, final Resource defaultResource)
|
||||
{
|
||||
this(name, location, type, defaultResource, false);
|
||||
}
|
||||
|
||||
public Configurator(final String name, final File location, final ConfigType type, final Resource defaultResource,
|
||||
final boolean copyDefaults)
|
||||
{
|
||||
if (copyDefaults)
|
||||
{
|
||||
copyDefaults(name, location, type, defaultResource);
|
||||
}
|
||||
else
|
||||
{
|
||||
processNormally(name, location, type, defaultResource);
|
||||
}
|
||||
}
|
||||
|
||||
private void processNormally(String name, File location, ConfigType type, Resource defaultResource)
|
||||
{
|
||||
if (location.mkdirs())
|
||||
getLogger().info("Created directory " + location.getAbsolutePath());
|
||||
|
||||
final File config = new File(location, name + type.getExtension());
|
||||
try
|
||||
{
|
||||
if (config.createNewFile() && defaultResource != null)
|
||||
{
|
||||
try (final FileOutputStream fos = new FileOutputStream(config))
|
||||
{
|
||||
fos.write(defaultResource.getContent());
|
||||
}
|
||||
|
||||
getLogger().info("Created file " + config.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
catch (final IOException ex)
|
||||
{
|
||||
getLogger().severe("Failed to create file " + config.getAbsolutePath());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
try (final FileInputStream fis = new FileInputStream(config))
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON -> this.configuration = new JSONParser().parse(fis);
|
||||
case YAML -> this.configuration = new YamlParser().parse(fis);
|
||||
case PROPERTIES -> this.configuration = new PropertiesParser().parse(fis);
|
||||
case TOML -> this.configuration = new TomlParser().parse(fis);
|
||||
default -> throw new IllegalArgumentException("Invalid config type.");
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
getLogger().severe("Failed to read file " + config.getAbsolutePath());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDefaults(String name, File location, ConfigType type, Resource defaultResource)
|
||||
{
|
||||
if (defaultResource == null)
|
||||
throw new IllegalArgumentException("Default resource cannot be null.");
|
||||
|
||||
if (!defaultResource.getName().split("\\.")[0].equalsIgnoreCase(name))
|
||||
throw new IllegalArgumentException(
|
||||
"The name of the default resource must be the same as the name of the configuration file.");
|
||||
|
||||
if (location.mkdirs())
|
||||
getLogger().info("Created directory " + location.getAbsolutePath());
|
||||
|
||||
final File config = new File(location, name + type.getExtension());
|
||||
try
|
||||
{
|
||||
if (config.createNewFile())
|
||||
getLogger().info("Created file " + config.getAbsolutePath());
|
||||
}
|
||||
catch (final IOException ex)
|
||||
{
|
||||
getLogger().severe("Failed to create file " + config.getAbsolutePath());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
try (final FileOutputStream fos = new FileOutputStream(config))
|
||||
{
|
||||
fos.write(defaultResource.getContent());
|
||||
}
|
||||
catch (final IOException ex)
|
||||
{
|
||||
getLogger().severe("Failed to write to file " + config.getAbsolutePath());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
try (final FileInputStream fis = new FileInputStream(config))
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case JSON -> this.configuration = new JSONParser().parse(fis);
|
||||
case YAML -> this.configuration = new YamlParser().parse(fis);
|
||||
case PROPERTIES -> this.configuration = new PropertiesParser().parse(fis);
|
||||
case TOML -> this.configuration = new TomlParser().parse(fis);
|
||||
default -> throw new IllegalArgumentException("Invalid config type.");
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
getLogger().severe("Failed to read file " + config.getAbsolutePath());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Logger getLogger()
|
||||
{
|
||||
return LOGGER;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Configuration getConfig()
|
||||
{
|
||||
return this.configuration;
|
||||
}
|
||||
}
|
12
src/main/java/app/simplexdev/config/Configuration.java
Normal file
12
src/main/java/app/simplexdev/config/Configuration.java
Normal file
@ -0,0 +1,12 @@
|
||||
package app.simplexdev.config;
|
||||
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.parser.Parser;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Configuration extends Section
|
||||
{
|
||||
ConfigType getConfigurationType();
|
||||
|
||||
Parser<? extends Configuration> getConfigurationParser();
|
||||
}
|
39
src/main/java/app/simplexdev/config/Section.java
Normal file
39
src/main/java/app/simplexdev/config/Section.java
Normal file
@ -0,0 +1,39 @@
|
||||
package app.simplexdev.config;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface Section
|
||||
{
|
||||
String getName();
|
||||
|
||||
Section getSection(final String path);
|
||||
|
||||
Boolean getBoolean(final String path);
|
||||
|
||||
Integer getInteger(final String path);
|
||||
|
||||
Double getDouble(final String path);
|
||||
|
||||
String getString(final String path);
|
||||
|
||||
Long getLong(final String path);
|
||||
|
||||
Float getFloat(final String path);
|
||||
|
||||
Short getShort(final String path);
|
||||
|
||||
Byte getByte(final String path);
|
||||
|
||||
Object get(final String path);
|
||||
|
||||
List<String> getStringList(final String path);
|
||||
|
||||
<T> List<T> getList(final String path, final Class<T> type);
|
||||
|
||||
Map<String, Object> getMap(String path, Class<?> valueType);
|
||||
|
||||
<T> T get(final String path, final Class<T> type);
|
||||
|
||||
void set(final String path, final Object value);
|
||||
}
|
154
src/main/java/app/simplexdev/config/json/JsonConfig.java
Normal file
154
src/main/java/app/simplexdev/config/json/JsonConfig.java
Normal file
@ -0,0 +1,154 @@
|
||||
package app.simplexdev.config.json;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.config.Section;
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.parser.JSONParser;
|
||||
import app.simplexdev.parser.Parser;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JsonConfig implements Configuration
|
||||
{
|
||||
|
||||
private final JSONParser configurationParser;
|
||||
private final String name;
|
||||
private final JsonObject jsonObject;
|
||||
|
||||
public JsonConfig(JsonObject jsonObject) {
|
||||
this.configurationParser = new JSONParser();
|
||||
this.name = "config";
|
||||
this.jsonObject = jsonObject;
|
||||
}
|
||||
|
||||
public JsonObject getJsonObject() {
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigType getConfigurationType() {
|
||||
return ConfigType.JSON;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parser<? extends Configuration> getConfigurationParser() {
|
||||
return configurationParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Section getSection(String path) {
|
||||
JsonObject sectionObject = jsonObject.getAsJsonObject(path);
|
||||
if (sectionObject != null) {
|
||||
return new JsonConfig(sectionObject);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsInt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsLong();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getFloat(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsFloat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getShort(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsShort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getByte(String path) {
|
||||
return jsonObject.getAsJsonPrimitive(path).getAsByte();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path) {
|
||||
return jsonObject.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path) {
|
||||
JsonArray jsonArray = jsonObject.getAsJsonArray(path);
|
||||
if (jsonArray != null) {
|
||||
List<String> stringList = new ArrayList<>();
|
||||
for (JsonElement element : jsonArray) {
|
||||
stringList.add(element.getAsString());
|
||||
}
|
||||
return stringList;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> getList(String path, Class<T> type) {
|
||||
JsonArray jsonArray = jsonObject.getAsJsonArray(path);
|
||||
if (jsonArray != null) {
|
||||
List<T> list = new ArrayList<>();
|
||||
for (JsonElement element : jsonArray) {
|
||||
list.add(new Gson().fromJson(element, type));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(String path, Class<?> valueType) {
|
||||
JsonObject mapObject = jsonObject.getAsJsonObject(path);
|
||||
if (mapObject != null) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
for (Map.Entry<String, JsonElement> entry : mapObject.entrySet()) {
|
||||
String key = new Gson().fromJson(entry.getKey(), String.class);
|
||||
Object value = new Gson().fromJson(entry.getValue(), valueType);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String path, Class<T> type) {
|
||||
return new Gson().fromJson(jsonObject.get(path), type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value) {
|
||||
jsonObject.add(path, new Gson().toJsonTree(value));
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package app.simplexdev.config.properties;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.config.Section;
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.parser.PropertiesParser;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PropertiesConfig implements Configuration
|
||||
{
|
||||
@Override
|
||||
public ConfigType getConfigurationType()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertiesParser<PropertiesConfig> getConfigurationParser()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Section getSection(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getFloat(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getShort(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getByte(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> getList(String path, Class<T> type)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(String path, Class<?> valueType)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String path, Class<T> type)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
123
src/main/java/app/simplexdev/config/toml/TomlConfig.java
Normal file
123
src/main/java/app/simplexdev/config/toml/TomlConfig.java
Normal file
@ -0,0 +1,123 @@
|
||||
package app.simplexdev.config.toml;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.config.Section;
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.parser.Parser;
|
||||
|
||||
import app.simplexdev.parser.TomlParser;
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class TomlConfig implements Configuration
|
||||
{
|
||||
|
||||
private final Parser<? extends Configuration> configurationParser;
|
||||
private final String name;
|
||||
private final TomlConfiguration tomlConfiguration;
|
||||
|
||||
public TomlConfig(TomlConfiguration tomlConfiguration) {
|
||||
this.configurationParser = new TomlParser();
|
||||
this.name = "config";
|
||||
this.tomlConfiguration = tomlConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigType getConfigurationType() {
|
||||
return ConfigType.TOML;
|
||||
}
|
||||
|
||||
public TomlConfiguration getToml() {
|
||||
return tomlConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parser<? extends Configuration> getConfigurationParser() {
|
||||
return configurationParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Section getSection(String path) {
|
||||
Object sectionObject = tomlConfiguration.get(path);
|
||||
if (sectionObject instanceof TomlConfiguration) {
|
||||
return new TomlConfig((TomlConfiguration) sectionObject);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String path) {
|
||||
return tomlConfiguration.getBoolean(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String path) {
|
||||
return tomlConfiguration.getLong(path).intValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String path) {
|
||||
return tomlConfiguration.getDouble(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
return tomlConfiguration.getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String path) {
|
||||
return tomlConfiguration.getLong(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getFloat(String path) {
|
||||
return tomlConfiguration.getDouble(path).floatValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getShort(String path) {
|
||||
return tomlConfiguration.getLong(path).shortValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getByte(String path) {
|
||||
return tomlConfiguration.getLong(path).byteValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path) {
|
||||
return tomlConfiguration.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path) {
|
||||
return tomlConfiguration.getStringList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> getList(String path, Class<T> type) {
|
||||
return tomlConfiguration.getList(path, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(String path, Class<?> valueType) {
|
||||
return tomlConfiguration.getTable(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String path, Class<T> type) throws UnsupportedOperationException {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value) {
|
||||
tomlConfiguration.set(path, value);
|
||||
}
|
||||
}
|
139
src/main/java/app/simplexdev/config/toml/TomlConfiguration.java
Normal file
139
src/main/java/app/simplexdev/config/toml/TomlConfiguration.java
Normal file
@ -0,0 +1,139 @@
|
||||
package app.simplexdev.config.toml;
|
||||
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public final class TomlConfiguration
|
||||
{
|
||||
|
||||
private Toml toml;
|
||||
|
||||
public TomlConfiguration()
|
||||
{
|
||||
this.toml = new Toml();
|
||||
}
|
||||
|
||||
public TomlConfiguration(final Toml toml) {
|
||||
this.toml = toml;
|
||||
}
|
||||
|
||||
public void load(String filePath) throws IOException
|
||||
{
|
||||
try (FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8))
|
||||
{
|
||||
toml.read(reader);
|
||||
}
|
||||
}
|
||||
|
||||
public void save(String filePath) throws IOException
|
||||
{
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8))
|
||||
{
|
||||
writer.write(toml.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String path)
|
||||
{
|
||||
return toml.getBoolean(path);
|
||||
}
|
||||
|
||||
public Integer getInteger(String path)
|
||||
{
|
||||
return toml.getLong(path).intValue();
|
||||
}
|
||||
|
||||
public Double getDouble(String path)
|
||||
{
|
||||
return toml.getDouble(path);
|
||||
}
|
||||
|
||||
public String getString(String path)
|
||||
{
|
||||
return toml.getString(path);
|
||||
}
|
||||
|
||||
public Long getLong(String path)
|
||||
{
|
||||
return toml.getLong(path);
|
||||
}
|
||||
|
||||
public Float getFloat(String path)
|
||||
{
|
||||
return toml.getDouble(path).floatValue();
|
||||
}
|
||||
|
||||
public Short getShort(String path)
|
||||
{
|
||||
return toml.getLong(path).shortValue();
|
||||
}
|
||||
|
||||
public Byte getByte(String path)
|
||||
{
|
||||
return toml.getLong(path).byteValue();
|
||||
}
|
||||
|
||||
public Object get(String path) throws UnsupportedOperationException
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return toml.getList(path);
|
||||
}
|
||||
|
||||
public <T> List<T> getList(String path, Class<T> type)
|
||||
{
|
||||
return toml.getList(path);
|
||||
}
|
||||
|
||||
public Map<String, Object> getTable(String path)
|
||||
{
|
||||
return toml.getTable(path).toMap();
|
||||
}
|
||||
|
||||
public <T> T get(String path, Class<T> type) throws UnsupportedOperationException
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public void set(String path, Object value) throws IOException
|
||||
{
|
||||
Map<String, Object> tomlData = toml.toMap();
|
||||
setRecursive(tomlData, path, value);
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
TomlWriter tomlWriter = new TomlWriter.Builder().build();
|
||||
tomlWriter.write(tomlData, outputStream);
|
||||
|
||||
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
|
||||
toml = new Toml().read(inputStream);
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void setRecursive(Map<String, Object> data, String path, Object value)
|
||||
{
|
||||
String[] parts = path.split("\\.");
|
||||
|
||||
Map<String, Object> current = data;
|
||||
for (int i = 0; i < parts.length - 1; i++)
|
||||
{
|
||||
String part = parts[i];
|
||||
if (!current.containsKey(part) || !(current.get(part) instanceof Map))
|
||||
{
|
||||
current.put(part, new HashMap<>());
|
||||
}
|
||||
current = (Map<String, Object>) current.get(part);
|
||||
}
|
||||
current.put(parts[parts.length - 1], value);
|
||||
}
|
||||
}
|
118
src/main/java/app/simplexdev/config/yaml/YamlConfig.java
Normal file
118
src/main/java/app/simplexdev/config/yaml/YamlConfig.java
Normal file
@ -0,0 +1,118 @@
|
||||
package app.simplexdev.config.yaml;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.config.Section;
|
||||
import app.simplexdev.data.ConfigType;
|
||||
import app.simplexdev.parser.Parser;
|
||||
|
||||
import app.simplexdev.parser.YamlParser;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class YamlConfig implements Configuration
|
||||
{
|
||||
|
||||
private final Parser<? extends Configuration> configurationParser;
|
||||
private final String name;
|
||||
private final YamlConfiguration yamlConfiguration;
|
||||
|
||||
public YamlConfig(YamlConfiguration yamlConfiguration) {
|
||||
this.configurationParser = new YamlParser();
|
||||
this.name = "config";
|
||||
this.yamlConfiguration = yamlConfiguration;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigType getConfigurationType() {
|
||||
return ConfigType.YAML;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Parser<? extends Configuration> getConfigurationParser() {
|
||||
return configurationParser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Section getSection(String path) {
|
||||
Object sectionObject = yamlConfiguration.get(path);
|
||||
if (sectionObject instanceof YamlConfiguration) {
|
||||
return new YamlConfig((YamlConfiguration) sectionObject);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getBoolean(String path) {
|
||||
return yamlConfiguration.getBoolean(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getInteger(String path) {
|
||||
return yamlConfiguration.getInteger(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double getDouble(String path) {
|
||||
return yamlConfiguration.getDouble(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String path) {
|
||||
return yamlConfiguration.getString(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getLong(String path) {
|
||||
return yamlConfiguration.getLong(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float getFloat(String path) {
|
||||
return yamlConfiguration.getFloat(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Short getShort(String path) {
|
||||
return yamlConfiguration.getShort(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Byte getByte(String path) {
|
||||
return yamlConfiguration.getByte(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object get(String path) {
|
||||
return yamlConfiguration.get(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path) {
|
||||
return yamlConfiguration.getStringList(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> getList(String path, Class<T> type) {
|
||||
return yamlConfiguration.getList(path, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getMap(String path, Class<?> valueType) {
|
||||
return yamlConfiguration.getMap(path, valueType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String path, Class<T> type) {
|
||||
return yamlConfiguration.get(path, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(String path, Object value) {
|
||||
yamlConfiguration.set(path, value);
|
||||
}
|
||||
}
|
173
src/main/java/app/simplexdev/config/yaml/YamlConfiguration.java
Normal file
173
src/main/java/app/simplexdev/config/yaml/YamlConfiguration.java
Normal file
@ -0,0 +1,173 @@
|
||||
package app.simplexdev.config.yaml;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
public final class YamlConfiguration
|
||||
{
|
||||
|
||||
private final Map<String, Object> data;
|
||||
|
||||
public YamlConfiguration()
|
||||
{
|
||||
this.data = new HashMap<>();
|
||||
}
|
||||
|
||||
public void load(String filePath) throws IOException
|
||||
{
|
||||
try (FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8))
|
||||
{
|
||||
Yaml yaml = new Yaml();
|
||||
this.data.clear();
|
||||
this.data.putAll(yaml.load(reader));
|
||||
}
|
||||
}
|
||||
|
||||
public void save(String filePath) throws IOException
|
||||
{
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8))
|
||||
{
|
||||
Yaml yaml = new Yaml();
|
||||
yaml.dump(data, writer);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean getBoolean(String path)
|
||||
{
|
||||
return get(path, Boolean.class);
|
||||
}
|
||||
|
||||
public Integer getInteger(String path)
|
||||
{
|
||||
return get(path, Integer.class);
|
||||
}
|
||||
|
||||
public Double getDouble(String path)
|
||||
{
|
||||
return get(path, Double.class);
|
||||
}
|
||||
|
||||
public String getString(String path)
|
||||
{
|
||||
return get(path, String.class);
|
||||
}
|
||||
|
||||
public Long getLong(String path)
|
||||
{
|
||||
return get(path, Long.class);
|
||||
}
|
||||
|
||||
public Float getFloat(String path)
|
||||
{
|
||||
return get(path, Float.class);
|
||||
}
|
||||
|
||||
public Short getShort(String path)
|
||||
{
|
||||
return get(path, Short.class);
|
||||
}
|
||||
|
||||
public Byte getByte(String path)
|
||||
{
|
||||
return get(path, Byte.class);
|
||||
}
|
||||
|
||||
public List<String> getStringList(String path)
|
||||
{
|
||||
return get(path, List.class);
|
||||
}
|
||||
|
||||
public <T> List<T> getList(String path, Class<T> type)
|
||||
{
|
||||
List<T> list = new ArrayList<>();
|
||||
List<Object> rawList = get(path, List.class);
|
||||
if (rawList != null)
|
||||
{
|
||||
for (Object element : rawList)
|
||||
{
|
||||
if (type.isInstance(element))
|
||||
{
|
||||
list.add(type.cast(element));
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Map<String, Object> getMap(String path, Class<?> valueType)
|
||||
{
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Map<?, ?> rawMap = get(path, Map.class);
|
||||
if (rawMap != null)
|
||||
{
|
||||
for (Map.Entry<?, ?> entry : rawMap.entrySet())
|
||||
{
|
||||
Object rawKey = entry.getKey();
|
||||
Object rawValue = entry.getValue();
|
||||
if (rawKey instanceof String key && valueType.isInstance(rawValue))
|
||||
{
|
||||
Object value = valueType.cast(rawValue);
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public <T> T get(String path, Class<T> type)
|
||||
{
|
||||
Object value = get(path);
|
||||
if (type.isInstance(value))
|
||||
{
|
||||
return type.cast(value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void set(String path, Object value)
|
||||
{
|
||||
put(path, value);
|
||||
}
|
||||
|
||||
public Object get(String path)
|
||||
{
|
||||
String[] parts = path.split("\\.");
|
||||
Object current = data;
|
||||
for (String part : parts)
|
||||
{
|
||||
if (current instanceof Map)
|
||||
{
|
||||
current = ((Map<?, ?>) current).get(part);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void put(String path, Object value)
|
||||
{
|
||||
String[] parts = path.split("\\.");
|
||||
Map<String, Object> current = data;
|
||||
for (int i = 0; i < parts.length - 1; i++)
|
||||
{
|
||||
String part = parts[i];
|
||||
current = (Map<String, Object>) current.computeIfAbsent(part, k -> new HashMap<>());
|
||||
}
|
||||
current.put(parts[parts.length - 1], value);
|
||||
}
|
||||
}
|
56
src/main/java/app/simplexdev/data/Byter.java
Normal file
56
src/main/java/app/simplexdev/data/Byter.java
Normal file
@ -0,0 +1,56 @@
|
||||
package app.simplexdev.data;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Base64;
|
||||
|
||||
public final class Byter
|
||||
{
|
||||
private Byter()
|
||||
{
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
public static byte[] decode(final String s)
|
||||
{
|
||||
return Base64.getDecoder().decode(s);
|
||||
}
|
||||
|
||||
public static String encode(final byte[] bytes)
|
||||
{
|
||||
return Base64.getEncoder().encodeToString(bytes);
|
||||
}
|
||||
|
||||
public static String read(final byte[] bytes, final Charset charset)
|
||||
{
|
||||
return new String(bytes, charset);
|
||||
}
|
||||
|
||||
public static byte[] write(final String s, final Charset charset)
|
||||
{
|
||||
return s.getBytes(charset);
|
||||
}
|
||||
|
||||
public static byte shift(final byte b, final int shift)
|
||||
{
|
||||
return (byte) (b >> shift);
|
||||
}
|
||||
|
||||
public static byte unsignedShift(final byte b, final int shift)
|
||||
{
|
||||
return (byte) (b >>> shift);
|
||||
}
|
||||
|
||||
public static byte preservedShift(final byte b, final int shift)
|
||||
{
|
||||
final int preserved = (b & 0x010000FF) & 0x0700FF;
|
||||
final int shifted = b >> shift;
|
||||
return (byte) (shifted | preserved);
|
||||
}
|
||||
|
||||
public static byte preservedUnsignedShift(final byte b, final int shift)
|
||||
{
|
||||
final int preserved = (b & 0x010000FF) & 0x0700FF;
|
||||
final int shifted = b >>> shift;
|
||||
return (byte) (shifted | preserved);
|
||||
}
|
||||
}
|
19
src/main/java/app/simplexdev/data/ConfigType.java
Normal file
19
src/main/java/app/simplexdev/data/ConfigType.java
Normal file
@ -0,0 +1,19 @@
|
||||
package app.simplexdev.data;
|
||||
|
||||
public enum ConfigType
|
||||
{
|
||||
YAML(".yml"),
|
||||
JSON(".json"),
|
||||
PROPERTIES(".properties"),
|
||||
TOML(".toml");
|
||||
|
||||
private final String extension;
|
||||
|
||||
ConfigType(final String extension) {
|
||||
this.extension = extension;
|
||||
}
|
||||
|
||||
public final String getExtension() {
|
||||
return this.extension;
|
||||
}
|
||||
}
|
8
src/main/java/app/simplexdev/data/Resource.java
Normal file
8
src/main/java/app/simplexdev/data/Resource.java
Normal file
@ -0,0 +1,8 @@
|
||||
package app.simplexdev.data;
|
||||
|
||||
public interface Resource
|
||||
{
|
||||
String getName();
|
||||
|
||||
byte[] getContent();
|
||||
}
|
67
src/main/java/app/simplexdev/data/StorableObject.java
Normal file
67
src/main/java/app/simplexdev/data/StorableObject.java
Normal file
@ -0,0 +1,67 @@
|
||||
package app.simplexdev.data;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.Base64;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class StorableObject<T>
|
||||
{
|
||||
private final T object;
|
||||
|
||||
public StorableObject(T object)
|
||||
{
|
||||
this.object = object;
|
||||
}
|
||||
|
||||
public static <T> T fromBase64(String base64, Class<T> type)
|
||||
{
|
||||
byte[] data = Base64.getDecoder().decode(base64);
|
||||
return fromBytes(data, type);
|
||||
}
|
||||
|
||||
static <T> T fromBytes(byte[] bytes, Class<T> type)
|
||||
{
|
||||
try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais))
|
||||
{
|
||||
return type.cast(ois.readObject());
|
||||
}
|
||||
catch (IOException | ClassNotFoundException | ClassCastException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte @Nullable [] toBytes()
|
||||
{
|
||||
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos))
|
||||
{
|
||||
oos.writeObject(this.object);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String toBase64()
|
||||
{
|
||||
final byte[] data = toBytes();
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
return Base64.getEncoder().encodeToString(data);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
61
src/main/java/app/simplexdev/parser/JSONParser.java
Normal file
61
src/main/java/app/simplexdev/parser/JSONParser.java
Normal file
@ -0,0 +1,61 @@
|
||||
package app.simplexdev.parser;
|
||||
|
||||
import app.simplexdev.config.json.JsonConfig;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JSONParser implements Parser<JsonConfig> {
|
||||
@Override
|
||||
public JsonConfig parse(FileInputStream fileInputStream) throws IOException {
|
||||
try (InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) {
|
||||
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
|
||||
return new JsonConfig(jsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(JsonConfig jsonConfig, String filePath) throws IOException {
|
||||
JsonObject jsonObject = jsonConfig.getJsonObject();
|
||||
String jsonString = jsonObject.toString();
|
||||
byte[] bytes = jsonString.getBytes(StandardCharsets.UTF_8);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
|
||||
fileOutputStream.write(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return ".json";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return "config";
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> S read(String filePath, Class<S> type) throws IOException {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(filePath);
|
||||
InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) {
|
||||
Gson gson = new Gson();
|
||||
return gson.fromJson(reader, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void write(String filePath, S object) throws IOException {
|
||||
Gson gson = new Gson();
|
||||
String jsonString = gson.toJson(object);
|
||||
byte[] bytes = jsonString.getBytes(StandardCharsets.UTF_8);
|
||||
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath)) {
|
||||
fileOutputStream.write(bytes);
|
||||
}
|
||||
}
|
||||
}
|
20
src/main/java/app/simplexdev/parser/Parser.java
Normal file
20
src/main/java/app/simplexdev/parser/Parser.java
Normal file
@ -0,0 +1,20 @@
|
||||
package app.simplexdev.parser;
|
||||
|
||||
import app.simplexdev.config.Section;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface Parser<T extends Section>
|
||||
{
|
||||
T parse(final FileInputStream p0) throws IOException;
|
||||
|
||||
void save(final T p0, final String p1) throws IOException;
|
||||
|
||||
String getFileExtension();
|
||||
|
||||
String getFileName();
|
||||
|
||||
<S> S read(final String p0, final Class<S> p1) throws IOException;
|
||||
|
||||
<S> void write(final String p0, final S p1) throws IOException;
|
||||
}
|
44
src/main/java/app/simplexdev/parser/PropertiesParser.java
Normal file
44
src/main/java/app/simplexdev/parser/PropertiesParser.java
Normal file
@ -0,0 +1,44 @@
|
||||
package app.simplexdev.parser;
|
||||
|
||||
import app.simplexdev.config.Configuration;
|
||||
import app.simplexdev.config.properties.PropertiesConfig;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PropertiesParser implements Parser<PropertiesConfig>
|
||||
{
|
||||
@Override
|
||||
public PropertiesConfig parse(final FileInputStream p0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(final PropertiesConfig p0, final String p1)
|
||||
{
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> S read(String p0, Class<S> p1) throws IOException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void write(String p0, S p1) throws IOException
|
||||
{
|
||||
|
||||
}
|
||||
}
|
62
src/main/java/app/simplexdev/parser/TomlParser.java
Normal file
62
src/main/java/app/simplexdev/parser/TomlParser.java
Normal file
@ -0,0 +1,62 @@
|
||||
package app.simplexdev.parser;
|
||||
|
||||
import app.simplexdev.config.toml.TomlConfig;
|
||||
import app.simplexdev.config.toml.TomlConfiguration;
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class TomlParser implements Parser<TomlConfig> {
|
||||
|
||||
@Override
|
||||
public TomlConfig parse(FileInputStream input) throws IOException {
|
||||
try (InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
|
||||
Toml toml = new Toml().read(reader);
|
||||
TomlConfiguration configuration = new TomlConfiguration(toml);
|
||||
return new TomlConfig(configuration);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(TomlConfig config, String filePath) throws IOException {
|
||||
try (FileOutputStream output = new FileOutputStream(filePath);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
|
||||
TomlWriter tomlWriter = new TomlWriter.Builder().build();
|
||||
String tomlString = tomlWriter.write(config.getToml());
|
||||
writer.write(tomlString);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return ".toml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return "Toml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> S read(String filePath, Class<S> type) throws IOException {
|
||||
try (FileInputStream input = new FileInputStream(filePath);
|
||||
InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8)) {
|
||||
Toml toml = new Toml().read(reader);
|
||||
return toml.to(type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void write(String filePath, S object) throws IOException {
|
||||
try (FileOutputStream output = new FileOutputStream(filePath);
|
||||
OutputStreamWriter writer = new OutputStreamWriter(output, StandardCharsets.UTF_8)) {
|
||||
TomlWriter tomlWriter = new TomlWriter.Builder().build();
|
||||
tomlWriter.write(object, writer);
|
||||
}
|
||||
}
|
||||
}
|
63
src/main/java/app/simplexdev/parser/YamlParser.java
Normal file
63
src/main/java/app/simplexdev/parser/YamlParser.java
Normal file
@ -0,0 +1,63 @@
|
||||
package app.simplexdev.parser;
|
||||
|
||||
import app.simplexdev.config.yaml.YamlConfig;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class YamlParser implements Parser<YamlConfig> {
|
||||
|
||||
@Override
|
||||
public YamlConfig parse(FileInputStream fileInputStream) throws IOException {
|
||||
Yaml yaml = new Yaml();
|
||||
try (InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) {
|
||||
Object parsedData = yaml.load(reader);
|
||||
if (parsedData instanceof YamlConfig) {
|
||||
return (YamlConfig) parsedData;
|
||||
} else {
|
||||
throw new IOException("Invalid YAML data. Unable to parse into YamlConfig.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(YamlConfig yamlConfig, String filePath) throws IOException {
|
||||
Yaml yaml = new Yaml();
|
||||
String yamlString = yaml.dump(yamlConfig);
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8)) {
|
||||
writer.write(yamlString);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileExtension() {
|
||||
return ".yaml";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return "config";
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> S read(String filePath, Class<S> type) throws IOException {
|
||||
try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
|
||||
Yaml yaml = new Yaml();
|
||||
return yaml.loadAs(fileInputStream, type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> void write(String filePath, S object) throws IOException {
|
||||
Yaml yaml = new Yaml();
|
||||
String yamlString = yaml.dump(object);
|
||||
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8)) {
|
||||
writer.write(yamlString);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user