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:
Paul Reilly
2023-06-27 18:00:01 -05:00
commit cc3781226b
27 changed files with 1937 additions and 0 deletions

View 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);
}
}

View 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);
}
}