Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Fixed Config Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
AuroraLS3 committed Nov 3, 2017
1 parent 00b13da commit bee6baf
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package com.djrapitops.plugin.api.config;

import com.djrapitops.plugin.api.utility.log.FileLogger;
import com.djrapitops.plugin.utilities.Format;

import java.io.File;
import java.io.IOException;
Expand All @@ -26,7 +25,7 @@ public class Config extends ConfigNode {
private final File file;

public Config(File file) {
super(null, "");
super("", null, "");
this.file = file;
}

Expand Down Expand Up @@ -56,42 +55,57 @@ private void processLines(List<String> fileLines) {
ConfigNode parent = this;
ConfigNode lastNode = this;
for (String line : fileLines) {
int depth = FileLogger.getIndentation(line) / 4;
System.out.print(fileLines.indexOf(line) + ": ");
try {
int depth = FileLogger.getIndentation(line);

String trimmed = line.trim();
if (trimmed.startsWith("#")) {
comments.add(trimmed);
System.out.println("Comment");
continue;
}

String trimmed = line.trim();
if (trimmed.startsWith("#")) {
comments.add(trimmed);
continue;
}
System.out.print("Depth:" + depth + " | ");
if (depth > lastDepth) {
System.out.print("+");
parent = lastNode;
} else if (depth < lastDepth) {
// Prevents incorrect indent in the case:
// 1:
// 2:
// 3:
// 1:
int nDepth = lastDepth;
while (nDepth > depth) {
System.out.print("-" + nDepth);
nDepth = parent.depth;
parent = parent.parent;
}
}
System.out.print(" | ");

String[] keyAndValue = trimmed.split(":", 2);
String configKey = keyAndValue[0];

String value = keyAndValue[1];
int indexOfHashtag = value.lastIndexOf("#");
String valueWithoutComment = indexOfHashtag < 0 ? value : value.substring(0, indexOfHashtag - 1);

if (depth > lastDepth) {
parent = lastNode;
} else if (depth < lastDepth) {
int nDepth = depth;
// Prevents incorrect indent in the case:
// 1:
// 2:
// 3:
// 1:
while (lastDepth - nDepth < 0) {
nDepth++;
parent = parent.parent;
String[] keyAndValue = trimmed.split(":", 2);
String configKey = keyAndValue[0];

String value = keyAndValue[1];
int indexOfHashtag = value.lastIndexOf(" #");
String valueWithoutComment = indexOfHashtag < 0 ? value : value.substring(0, indexOfHashtag);

ConfigNode node = new ConfigNode(configKey, parent, valueWithoutComment);
node.depth = depth;
node.setComment(new ArrayList<>(comments));
if (!comments.isEmpty()) {
System.out.print("AddComments | ");
}
comments.clear();
lastNode = node;
lastDepth = depth;
System.out.println(node.getKey(true));
parent.addChild(configKey, node);
} catch (Exception e) {
throw new IllegalStateException("Malformed File (" + file.getName() + "), Error on line " + fileLines.indexOf(line) + ": " + line, e);
}

ConfigNode node = new ConfigNode(parent, valueWithoutComment);
node.setComment(new ArrayList<>(comments));
comments.clear();
lastNode = node;
lastDepth = depth;
parent.children.put(configKey, node);
}
}

Expand All @@ -106,16 +120,17 @@ private List<String> processTree() {

private List<String> getLines(ConfigNode root, int depth) {
List<String> lines = new ArrayList<>();
for (Map.Entry<String, ConfigNode> entry : root.children.entrySet()) {
String key = entry.getKey();
ConfigNode node = entry.getValue();
Map<String, ConfigNode> children = root.getChildren();

for (String key : root.childOrder) {
ConfigNode node = children.get(key);
String value = node.getValue();

for (String commentLine : node.getComment()) {
StringBuilder comment = new StringBuilder();
addIndentation(depth, comment);
comment.append("#").append(comment);
lines.add(commentLine);
comment.append(commentLine);
lines.add(comment.toString());
}

StringBuilder b = new StringBuilder();
Expand All @@ -136,7 +151,7 @@ private List<String> getLines(ConfigNode root, int depth) {
lines.add(listBuilder.toString());
}
} else {
b.append(key).append(": ").append(value);
b.append(key).append(":").append(value);
lines.add(b.toString());
}
lines.addAll(getLines(node, depth + 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@
*/
public class ConfigNode {

protected ConfigNode parent;
protected Map<String, ConfigNode> children;
private final String key;
ConfigNode parent;
List<String> childOrder;
int depth;
private Map<String, ConfigNode> children;
private List<String> comment;

private String value;

public ConfigNode(ConfigNode parent, String value) {
public ConfigNode(String key, ConfigNode parent, String value) {
this.key = key;
this.parent = parent;
this.value = value;
childOrder = new ArrayList<>();
children = new HashMap<>();
comment = new ArrayList<>();
}
Expand All @@ -36,6 +41,10 @@ public Map<String, ConfigNode> getChildren() {
return children;
}

public List<String> getKeysInOrder() {
return childOrder;
}

public String getString() {
// boolean surroundedWithSingleQuotes = value.startsWith("'") && value.endsWith("'");
// boolean surroundedWithDoubleQuotes = value.startsWith("\"") && value.endsWith("\"");
Expand Down Expand Up @@ -121,4 +130,26 @@ public void save() throws IOException {
public String getValue() {
return value;
}

public void addChild(String name, ConfigNode node) {
children.put(name, node);
childOrder.add(name);
}

public String getKey(boolean deep) {
if (deep) {
if (parent != null) {
String s = parent.getKey(true) + "." + key;
if (s.startsWith(".")) {
return s.substring(1);
}
return s;
}
}
return key;
}

public void sort() {
Collections.sort(childOrder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -42,9 +44,25 @@ public void save() throws Exception {
Config config = new Config(testFile);
File copyFromFile = new File("testconfig.yml");
config.copyDefaults(copyFromFile);
System.out.println("");
config.save();

assertEquals(Files.lines(testFile.toPath()), Files.lines(copyFromFile.toPath()));
List<String> original = Files.lines(copyFromFile.toPath()).collect(Collectors.toList());
List<String> test = Files.lines(testFile.toPath()).collect(Collectors.toList());
boolean different = false;
for (int i = 0; i < original.size(); i++) {
String origLine = original.get(i);
String testLine = test.get(i).replace(" ", " ");
if (!origLine.equals(testLine)) {
System.out.println((i + 1) + "! " + origLine);
System.out.println((i + 1) + "! " + testLine);
different = true;
} else {
System.out.println((i + 1) + ": " + origLine);
System.out.println((i + 1) + ": " + testLine);
}
}
assertFalse(different);
}

}
9 changes: 4 additions & 5 deletions AbstractPluginFramework/testconfig.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Plan Config | More info at https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/documentation/Configuration.md
# ---------------------------------------------------------------------------------------------------------------------
Server:
ServerName: Plan
# TestComment
Plugin:
Debug: true
Locale: default
WriteNewLocaleFileOnEnable: false
# AmazingComment
WriteNewLocaleFileOnEnable: false
Bungee-Override:
StandaloneMode: false
CopyBungeeConfig: true
Expand Down Expand Up @@ -92,9 +93,7 @@ Theme:
RAM: Default
Chunks: Default
Entities: Default'
WorldPie: '"#0099C6", "#66AA00", "#316395", "#994499", "#22AA99", "#AAAA11", "#6633CC",
"#E67300", "#329262", "#5574A6"'
WorldPie: '"#0099C6", "#66AA00", "#316395", "#994499", "#22AA99", "#AAAA11", "#6633CC", "#E67300", "#329262", "#5574A6"'
GMDrilldown: '"#438c99", "#639A67", "#D8EBB5", "#D9BF77"'
ActivityPie: '"#228B22", "#A9A9A9", "#808080", "#951800"'
ServerPreferencePie: '"#0099C6", "#66AA00", "#316395", "#994499", "#22AA99", "#AAAA11",
"#6633CC", "#E67300", "#329262", "#5574A6"'
ServerPreferencePie: '"#0099C6", "#66AA00", "#316395", "#994499", "#22AA99", "#AAAA11", "#6633CC", "#E67300", "#329262", "#5574A6"'

0 comments on commit bee6baf

Please sign in to comment.