forked from public-mirrors/ConfigLib
added loadAndSave method
parent
174fd25fe5
commit
720ca8f47f
@ -1,44 +1,84 @@
|
|||||||
package de.exlll.configlib;
|
package de.exlll.configlib;
|
||||||
|
|
||||||
|
import org.yaml.snakeyaml.parser.ParserException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public abstract class Configuration {
|
public abstract class Configuration {
|
||||||
private final Path configPath;
|
private final Path configPath;
|
||||||
private final Comments comments;
|
|
||||||
private final FieldMapper fieldMapper;
|
private final FieldMapper fieldMapper;
|
||||||
|
private final ConfigurationWriter writer;
|
||||||
|
|
||||||
public Configuration(Path configPath) {
|
public Configuration(Path configPath) {
|
||||||
Objects.requireNonNull(configPath);
|
Objects.requireNonNull(configPath);
|
||||||
this.configPath = configPath;
|
|
||||||
FilteredFieldStreamSupplier ffss = new FilteredFieldStreamSupplier(
|
FilteredFieldStreamSupplier ffss = new FilteredFieldStreamSupplier(
|
||||||
getClass(), ConfigurationFieldFilter.INSTANCE);
|
getClass(), ConfigurationFieldFilter.INSTANCE);
|
||||||
this.comments = Comments.from(ffss);
|
Comments comments = Comments.from(ffss);
|
||||||
|
|
||||||
|
this.configPath = configPath;
|
||||||
this.fieldMapper = new FieldMapper(ffss);
|
this.fieldMapper = new FieldMapper(ffss);
|
||||||
|
this.writer = new ConfigurationWriter(configPath, comments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the configuration file from the specified {@code Path} and sets {@code this} fields.
|
||||||
|
*
|
||||||
|
* @throws ClassCastException if parsed Object is not a {@code Map}
|
||||||
|
* @throws IOException if an I/O error occurs when loading the configuration file.
|
||||||
|
* @throws ParserException if invalid YAML
|
||||||
|
*/
|
||||||
|
public final void load() throws IOException {
|
||||||
|
String dump = new ConfigurationReader(configPath).read();
|
||||||
|
Map<String, Object> valuesByFieldNames = YamlSerializer.deserialize(dump);
|
||||||
|
fieldMapper.mapValuesToFields(valuesByFieldNames, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves {@code this} fields and {@code @Comment} annotations to a configuration file at the specified {@code Path}.
|
||||||
|
* <p>
|
||||||
|
* Fields which are {@code final}, {@code static} or {@code transient} are ignored.
|
||||||
|
* <p>
|
||||||
|
* If the file exists, it is overriden; otherwise, it is created.
|
||||||
|
*
|
||||||
|
* @throws IOException if an I/O error occurs when saving the configuration file.
|
||||||
|
* @throws ParserException if invalid YAML
|
||||||
|
*/
|
||||||
public final void save() throws IOException {
|
public final void save() throws IOException {
|
||||||
createParentDirectories();
|
createParentDirectories();
|
||||||
|
|
||||||
Map<String, Object> valuesByFieldNames = fieldMapper
|
Map<String, Object> valuesByFieldNames = fieldMapper.mapFieldNamesToValues(this);
|
||||||
.mapFieldNamesToValues(this);
|
|
||||||
String dump = YamlSerializer.serialize(valuesByFieldNames);
|
String dump = YamlSerializer.serialize(valuesByFieldNames);
|
||||||
ConfigurationWriter writer = new ConfigurationWriter(configPath, comments);
|
|
||||||
writer.write(dump);
|
writer.write(dump);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createParentDirectories() throws IOException {
|
/**
|
||||||
Path parentDirectory = configPath.getParent();
|
* Loads and saves the configuration file.
|
||||||
Files.createDirectories(parentDirectory);
|
*
|
||||||
|
* @throws ClassCastException if parsed Object is not a {@code Map}
|
||||||
|
* @throws IOException if an I/O error occurs when loading or saving the configuration file.
|
||||||
|
* @throws ParserException if invalid YAML
|
||||||
|
* @see #load()
|
||||||
|
* @see #save()
|
||||||
|
*/
|
||||||
|
public final void loadAndSave() throws IOException {
|
||||||
|
try {
|
||||||
|
load();
|
||||||
|
save();
|
||||||
|
} catch (NoSuchFileException e) {
|
||||||
|
save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public final void load() throws IOException {
|
private void createParentDirectories() throws IOException {
|
||||||
// TODO save if not existent
|
Path parentDirectory = configPath.getParent();
|
||||||
String dump = new ConfigurationReader(configPath).read();
|
if (Files.notExists(parentDirectory)) {
|
||||||
Map<String, Object> valuesByFieldNames = YamlSerializer.deserialize(dump);
|
Files.createDirectories(parentDirectory);
|
||||||
fieldMapper.mapValuesToFields(valuesByFieldNames, this);
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue