mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-26 19:17:43 +01:00
Cleaned up ConfigWriter code
This commit is contained in:
parent
fdaefd7e64
commit
2ef83ad99e
@ -16,6 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
package com.djrapitops.plan.system.settings.config;
|
package com.djrapitops.plan.system.settings.config;
|
||||||
|
|
||||||
|
import com.djrapitops.plugin.utilities.Verify;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -37,14 +39,31 @@ public class ConfigWriter {
|
|||||||
private Path outputPath;
|
private Path outputPath;
|
||||||
private int indent;
|
private int indent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ConfigWriter that doesn't write anywhere.
|
||||||
|
*/
|
||||||
public ConfigWriter() {
|
public ConfigWriter() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new ConfigWriter that writes to a Path.
|
||||||
|
*
|
||||||
|
* @param outputPath Path to write to.
|
||||||
|
*/
|
||||||
public ConfigWriter(Path outputPath) {
|
public ConfigWriter(Path outputPath) {
|
||||||
this.outputPath = outputPath;
|
this.outputPath = outputPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a {@link ConfigNode} into the given resource.
|
||||||
|
*
|
||||||
|
* @param writing ConfigNode to write.
|
||||||
|
* @throws IOException If the Path given to constructor can not be written to.
|
||||||
|
* @throws IllegalStateException If the Path is null
|
||||||
|
*/
|
||||||
public void write(ConfigNode writing) throws IOException {
|
public void write(ConfigNode writing) throws IOException {
|
||||||
|
Verify.nullCheck(outputPath, () -> new IllegalStateException("Output path was null."));
|
||||||
|
|
||||||
ConfigNode storedParent = writing.parent;
|
ConfigNode storedParent = writing.parent;
|
||||||
writing.updateParent(null);
|
writing.updateParent(null);
|
||||||
|
|
||||||
@ -53,6 +72,14 @@ public class ConfigWriter {
|
|||||||
writing.updateParent(storedParent);
|
writing.updateParent(storedParent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the lines of a {@link ConfigNode}.
|
||||||
|
* <p>
|
||||||
|
* "Write" the lines into a List.
|
||||||
|
*
|
||||||
|
* @param writing ConfigNode to "write"
|
||||||
|
* @return List of lines that would be written.
|
||||||
|
*/
|
||||||
public List<String> parseLines(ConfigNode writing) {
|
public List<String> parseLines(ConfigNode writing) {
|
||||||
List<String> lines = new ArrayList<>();
|
List<String> lines = new ArrayList<>();
|
||||||
|
|
||||||
@ -84,6 +111,8 @@ public class ConfigWriter {
|
|||||||
if (value == null || value.isEmpty()) {
|
if (value == null || value.isEmpty()) {
|
||||||
addKey(key, lines);
|
addKey(key, lines);
|
||||||
} else if (value.contains("\n")) {
|
} else if (value.contains("\n")) {
|
||||||
|
// List values include newline characters,
|
||||||
|
// see ConfigValueParser.StringListParser
|
||||||
addListValue(key, value.split("\\n"), lines);
|
addListValue(key, value.split("\\n"), lines);
|
||||||
} else {
|
} else {
|
||||||
addNormalValue(key, value, lines);
|
addNormalValue(key, value, lines);
|
||||||
@ -91,29 +120,38 @@ public class ConfigWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void addKey(String key, Collection<String> lines) {
|
private void addKey(String key, Collection<String> lines) {
|
||||||
|
// Key:
|
||||||
lines.add(indentedBuilder().append(key).append(":").toString());
|
lines.add(indentedBuilder().append(key).append(":").toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addNormalValue(String key, String value, Collection<String> lines) {
|
private void addNormalValue(String key, String value, Collection<String> lines) {
|
||||||
|
// Key: value
|
||||||
StringBuilder lineBuilder = indentedBuilder().append(key).append(": ").append(value);
|
StringBuilder lineBuilder = indentedBuilder().append(key).append(": ").append(value);
|
||||||
lines.add(lineBuilder.toString());
|
lines.add(lineBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addListValue(String key, String[] listItems, Collection<String> lines) {
|
private void addListValue(String key, String[] listItems, Collection<String> lines) {
|
||||||
|
// Key:
|
||||||
|
// - List item
|
||||||
addKey(key, lines);
|
addKey(key, lines);
|
||||||
for (String listItem : listItems) {
|
for (String listItem : listItems) {
|
||||||
listItem = listItem.trim();
|
listItem = listItem.trim();
|
||||||
if (listItem.isEmpty()) {
|
if (listItem.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
StringBuilder lineBuilder = indentedBuilder()
|
addListItem(listItem, lines);
|
||||||
.append(" ") // Append spaces to adhere to yml format for lists
|
|
||||||
.append(listItem);
|
|
||||||
lines.add(lineBuilder.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addListItem(String listItem, Collection<String> lines) {
|
||||||
|
StringBuilder lineBuilder = indentedBuilder()
|
||||||
|
.append(" ") // Append 2 spaces to adhere to yml format for lists
|
||||||
|
.append(listItem);
|
||||||
|
lines.add(lineBuilder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
private void addComment(Iterable<String> comments, Collection<String> lines) {
|
private void addComment(Iterable<String> comments, Collection<String> lines) {
|
||||||
|
// # Comment line
|
||||||
for (String comment : comments) {
|
for (String comment : comments) {
|
||||||
StringBuilder lineBuilder = indentedBuilder().append("# ").append(comment);
|
StringBuilder lineBuilder = indentedBuilder().append("# ").append(comment);
|
||||||
lines.add(lineBuilder.toString());
|
lines.add(lineBuilder.toString());
|
||||||
|
Loading…
Reference in New Issue
Block a user