From b87416d030742d413e8831a6cb1b11d8a2a1844c Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 28 Oct 2018 11:43:55 +0100 Subject: [PATCH] Removed YmlConfiguration and YmlCommentParser as they weren't used --- .../bentobox/util/yaml/YmlCommentParser.java | 217 ------------------ .../bentobox/util/yaml/YmlConfiguration.java | 44 ---- 2 files changed, 261 deletions(-) delete mode 100644 src/main/java/world/bentobox/bentobox/util/yaml/YmlCommentParser.java delete mode 100644 src/main/java/world/bentobox/bentobox/util/yaml/YmlConfiguration.java diff --git a/src/main/java/world/bentobox/bentobox/util/yaml/YmlCommentParser.java b/src/main/java/world/bentobox/bentobox/util/yaml/YmlCommentParser.java deleted file mode 100644 index 072629694..000000000 --- a/src/main/java/world/bentobox/bentobox/util/yaml/YmlCommentParser.java +++ /dev/null @@ -1,217 +0,0 @@ -package world.bentobox.bentobox.util.yaml; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; -import java.util.ArrayDeque; -import java.util.Collections; -import java.util.Deque; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Logger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.bukkit.Bukkit; - -/** - * A very simplistic yml parser, that only do the following: - *
    - *
  1. Keep track of indentation-levels and sections.
  2. - *
  3. Handle comments.
  4. - *
- */ -public class YmlCommentParser { - private static final Logger log = Bukkit.getLogger(); - private static final Pattern SECTION_PATTERN = Pattern.compile("^(?\\s*)(?[^ \\-][^:]*):(?[^#]*)?(?#.*)?"); - private static final Pattern COMMENT_PATTERN = Pattern.compile("^(?\\s*)(?#.*)"); - private Map commentMap = new HashMap<>(); - - public Map getCommentMap() { - return Collections.unmodifiableMap(commentMap); - } - - - public void addComment(String path, String comment) { - commentMap.put(path, comment); - } - - public void addComments(Map comments) { - commentMap.putAll(comments); - } - - private void readLines(BufferedReader rdr) throws IOException { - int indentLevel = 0; - Deque
sections = new ArrayDeque<>(); - StringBuilder comments = new StringBuilder(); - String baseKey = null; - sections.add(new Section(null, 0)); - String line; - int lineNum = 1; - boolean isFirstAfterSection = true; - while ((line = rdr.readLine()) != null) { - Matcher commentM = COMMENT_PATTERN.matcher(line); - Matcher sectionM = SECTION_PATTERN.matcher(line); - if (commentM.matches()) { - comments.append(commentM.group("comment")).append("\n"); - } else if (sectionM.matches()) { - String comment = sectionM.group("comment"); - if (comment != null && !comment.trim().isEmpty()) { - comments.append(comment).append("\n"); - } - String name = sectionM.group("name").trim(); - String value = sectionM.group("value"); - String indent = sectionM.group("indent"); - if (isFirstAfterSection && indent.length() > indentLevel) { - indentLevel = indent.length(); - sections.peek().setIndentation(indentLevel); - } else if (indent.length() < indentLevel) { - while (indent.length() < indentLevel && !sections.isEmpty()) { - sections.pop(); - baseKey = sections.peek().getPath(); - indentLevel = sections.peek().getIndentation(); - isFirstAfterSection = false; - } - } - String path = getPath(baseKey, name); - if (value != null && !value.trim().isEmpty()) { - // Scalar with value - addComments(path, comments); - if (!isFirstAfterSection && indent.length() > indentLevel) { - log.warning("line " + lineNum + ": mixed indentation, expected " + indentLevel + " but got " + indent.length()); - } - isFirstAfterSection = false; - } else if (indent.length() >= indentLevel) { - indentLevel = indent.length(); - sections.push(createSection(path, indentLevel, comments)); - baseKey = path; - isFirstAfterSection = true; - } - } else if (line.trim().isEmpty()) { - // Currently gathered comments are reset - they are "floating", decoupled from sections. - comments.setLength(0); - comments.trimToSize(); - } - lineNum++; - } - } - - private String getPath(String baseKey, String name) { - return baseKey != null ? baseKey + "." + name : name; - } - - private Section createSection(String path, int indentLevel, StringBuilder comments) { - Section section = new Section(path, indentLevel); - addComments(path, comments); - return section; - } - - private void addComments(String path, StringBuilder comments) { - if (comments.length() > 0) { - commentMap.put(path, comments.toString()); - comments.setLength(0); - comments.trimToSize(); - } - } - - public String getComment(String path) { - return commentMap.get(path); - } - - /** - * Merges the comments into the "pure" yml. - * @param ymlPure A YML data-tree, without comments. - * @return A YML data-tree including comments. - */ - public String mergeComments(String ymlPure) { - StringBuilder sb = new StringBuilder(); - boolean isFirstAfterSection = true; - Deque
sections = new ArrayDeque<>(); - sections.push(new Section(null, 0)); - int indentLevel = 0; - String baseKey = null; - int lineNum = 1; - // First section shares comments with the header - so ignore that one - boolean isHeader = true; - for (String line : ymlPure.split("\n")) { - // Skip header - Matcher commentM = COMMENT_PATTERN.matcher(line); - if (isHeader && (commentM.matches() || line.trim().isEmpty())) { - continue; // Skip header - } - isHeader = false; - Matcher sectionM = SECTION_PATTERN.matcher(line); - if (sectionM.matches()) { - String name = sectionM.group("name").trim(); - String value = sectionM.group("value"); - String indent = sectionM.group("indent"); - if (isFirstAfterSection && indent.length() > indentLevel) { - indentLevel = indent.length(); - sections.peek().setIndentation(indentLevel); - } else if (indent.length() < indentLevel) { - while (indent.length() < indentLevel && !sections.isEmpty()) { - sections.pop(); - baseKey = sections.peek().getPath(); - indentLevel = sections.peek().getIndentation(); - isFirstAfterSection = false; - } - } - String path = getPath(baseKey, name); - String comment = getComment(path); - if (comment != null) { - sb.append(lineNum > 1 ? "\n" : "").append(comment - .replaceAll("^#", Matcher.quoteReplacement(indent + "#")) - .replaceAll("\n#", Matcher.quoteReplacement("\n" + indent + "#"))); - } - if (value != null && !value.trim().isEmpty()) { - // Scalar with value - isFirstAfterSection = false; - } else if (indent.length() >= indentLevel) { - indentLevel = indent.length(); - sections.push(new Section(path, indentLevel)); - baseKey = path; - isFirstAfterSection = true; - } - } - lineNum++; - sb.append(line).append("\n"); - } - return sb.toString().replaceAll("\r\n", "\n").replaceAll("\n\r", "\n").replaceAll("\n", "\r\n"); - } - - public void load(Reader reader) throws IOException { - readLines(new BufferedReader(reader)); - } - - public void loadFromString(String contents) { - try { - readLines(new BufferedReader(new StringReader(contents))); - } catch (IOException e) { - throw new IllegalStateException("Unable to read from string", e); - } - } - - - private static class Section { - private int indentation; - private final String path; - - private Section(String name, int indentation) { - this.indentation = indentation; - path = name; - } - - public int getIndentation() { - return indentation; - } - - public String getPath() { - return path; - } - - public void setIndentation(int indentLevel) { - indentation = indentLevel; - } - } -} \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/util/yaml/YmlConfiguration.java b/src/main/java/world/bentobox/bentobox/util/yaml/YmlConfiguration.java deleted file mode 100644 index ea65b45a9..000000000 --- a/src/main/java/world/bentobox/bentobox/util/yaml/YmlConfiguration.java +++ /dev/null @@ -1,44 +0,0 @@ -package world.bentobox.bentobox.util.yaml; - -import java.util.Map; - -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; - -/** - * A YamlConfiguration that supports comments - * - * Note: This includes a VERY SIMPLISTIC Yaml-parser, which sole purpose is to detect and store comments. - */ -public class YmlConfiguration extends YamlConfiguration { - private YmlCommentParser commentParser = new YmlCommentParser(); - - public String getComment(String key) { - String comment = commentParser.getComment(key); - return comment != null ? comment.replaceAll("^# ?", "").replaceAll("\n# ?", "") : null; - } - - public Map getComments() { - return commentParser.getCommentMap(); - } - - public void addComment(String path, String comment) { - commentParser.addComment(path, comment); - } - public void addComments(Map comments) { - commentParser.addComments(comments); - } - - @Override - public void loadFromString(String contents) throws InvalidConfigurationException { - super.loadFromString(contents); - commentParser.loadFromString(contents); - } - - @Override - public String saveToString() { - String ymlPure = super.saveToString(); - return commentParser.mergeComments(ymlPure); - } - -} \ No newline at end of file