1
mirror of https://github.com/CarmJos/EasyConfiguration.git synced 2026-06-04 10:38:19 +08:00

feat(json): Implement json sources

This commit is contained in:
2025-02-12 04:25:29 +08:00
parent c68d2371ee
commit 05ff61a9d9
44 changed files with 656 additions and 558 deletions
@@ -0,0 +1,39 @@
package cc.carm.lib.configuration.source;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.nio.file.Path;
public abstract class FileConfigFactory<SOURCE extends FileConfigSource<?, ?, SOURCE>,
HOLDER extends ConfigurationHolder<SOURCE>, SELF extends FileConfigFactory<SOURCE, HOLDER, SELF>>
extends ConfigurationFactory<SOURCE, HOLDER, SELF> {
protected @NotNull File file;
protected @Nullable String resourcePath;
public FileConfigFactory(@NotNull File file) {
this.file = file;
}
public SELF file(@NotNull File file) {
this.file = file;
return self();
}
public SELF file(@NotNull Path path) {
return file(path.toFile());
}
public SELF file(@NotNull File parent, @NotNull String fileName) {
return file(new File(parent, fileName));
}
public SELF resourcePath(@Nullable String resourcePath) {
this.resourcePath = resourcePath;
return self();
}
}
@@ -2,7 +2,8 @@ package cc.carm.lib.configuration.source;
import cc.carm.lib.configuration.function.DataFunction;
import cc.carm.lib.configuration.option.FileConfigOptions;
import cc.carm.lib.configuration.source.section.ConfigurationSource;
import cc.carm.lib.configuration.source.section.ConfigureSection;
import cc.carm.lib.configuration.source.section.ConfigureSource;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -14,8 +15,8 @@ import java.nio.file.Files;
import java.util.Objects;
import java.util.function.Consumer;
public abstract class FileConfigSource<SELF extends FileConfigSource<SELF, ORIGINAL>, ORIGINAL>
extends ConfigurationSource<SELF, ORIGINAL> {
public abstract class FileConfigSource<SECTION extends ConfigureSection, ORIGINAL, SELF extends FileConfigSource<SECTION, ORIGINAL, SELF>>
extends ConfigureSource<SECTION, ORIGINAL, SELF> {
protected final @NotNull File file;
protected final @Nullable String resourcePath;
@@ -27,6 +28,7 @@ public abstract class FileConfigSource<SELF extends FileConfigSource<SELF, ORIGI
this.resourcePath = resourcePath;
}
public Charset charset() {
return holder().options().get(FileConfigOptions.CHARSET);
}
@@ -56,28 +58,23 @@ public abstract class FileConfigSource<SELF extends FileConfigSource<SELF, ORIGI
}
}
protected <R> R fileInputStream(@NotNull DataFunction<InputStream, R> loader) {
try (FileInputStream is = new FileInputStream(file)) {
protected <R> R fileInputStream(@NotNull DataFunction<InputStream, R> loader) throws Exception {
try (InputStream is = Files.newInputStream(file.toPath())) {
return loader.handle(is);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected <R> R fileReader(@NotNull DataFunction<Reader, R> loader) {
protected <R> R fileReader(@NotNull DataFunction<Reader, R> loader) throws Exception {
return fileInputStream(is -> loader.handle(new InputStreamReader(is, charset())));
}
protected void fileOutputStream(@NotNull Consumer<FileOutputStream> stream) {
try (FileOutputStream os = new FileOutputStream(file)) {
protected void fileOutputStream(@NotNull Consumer<OutputStream> stream) throws Exception {
try (OutputStream os = Files.newOutputStream(file.toPath())) {
stream.accept(os);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void fileWriter(@NotNull Consumer<Writer> writer) {
protected void fileWriter(@NotNull Consumer<Writer> writer) throws Exception {
fileOutputStream(os -> writer.accept(new OutputStreamWriter(os, charset())));
}