Update to ConfigMe 1.4.0

This commit is contained in:
ljacqu
2023-08-23 06:29:11 +02:00
parent 80c1c2edac
commit 79309c3c05
5 changed files with 47 additions and 30 deletions
@@ -2,8 +2,14 @@ package fr.xephi.authme.message.updater;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilder;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilderImpl;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
import java.io.File;
@@ -12,36 +18,43 @@ import java.io.File;
*/
public class MigraterYamlFileResource extends YamlFileResource {
private Yaml singleQuoteYaml;
public MigraterYamlFileResource(File file) {
super(file);
}
@Override
public PropertyReader createReader() {
return MessageMigraterPropertyReader.loadFromFile(getFile());
return MessageMigraterPropertyReader.loadFromFile(getPath());
}
@Override
protected Yaml createNewYaml() {
if (singleQuoteYaml == null) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED);
// Overridden setting: don't split lines
options.setSplitLines(false);
singleQuoteYaml = new Yaml(options);
}
return singleQuoteYaml;
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
options.setProcessComments(true);
options.setIndent(4);
// Overridden setting: don't split lines
options.setSplitLines(false);
return new Yaml(options);
}
// Because we set the YAML object to put strings in single quotes, this method by default uses that YAML object
// and also puts all paths as single quotes. Override to just always return the same string since we know those
// are only message names (so never any conflicting strings like "true" or "0").
@Override
protected String escapePathElementIfNeeded(String path) {
return path;
protected @NotNull SnakeYamlNodeBuilder createNodeBuilder() {
return new MigraterYamlNodeBuilder();
}
/** Extended to represent all strings with single quotes in the YAML. */
private static final class MigraterYamlNodeBuilder extends SnakeYamlNodeBuilderImpl {
@Override
protected @NotNull Node createStringNode(@NotNull String value) {
return new ScalarNode(Tag.STR, value, null, null, DumperOptions.ScalarStyle.SINGLE_QUOTED);
}
@Override
public @NotNull Node createKeyNode(@NotNull String key) {
return super.createStringNode(key); // no single quotes
}
}
}