diff --git a/Simple-Yaml-1.7.1-javadoc.jar b/Simple-Yaml-1.7.1-javadoc.jar index 3df2364e..10358630 100644 Binary files a/Simple-Yaml-1.7.1-javadoc.jar and b/Simple-Yaml-1.7.1-javadoc.jar differ diff --git a/Simple-Yaml-1.7.1.jar b/Simple-Yaml-1.7.1.jar index d1fa5ff9..f51250a7 100644 Binary files a/Simple-Yaml-1.7.1.jar and b/Simple-Yaml-1.7.1.jar differ diff --git a/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlConfiguration.java b/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlConfiguration.java index 24768814..a61c64e9 100644 --- a/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlConfiguration.java +++ b/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlConfiguration.java @@ -93,19 +93,23 @@ public static YamlConfiguration loadConfiguration(final Reader reader) { @Override public String saveToString() throws IOException { + return this.buildHeader() + this.dump(); + } + + protected String dump() { this.yamlOptions.setIndent(this.options().indent()); + this.yamlOptions.setIndicatorIndent(this.options().indent()); this.yamlOptions.setAllowUnicode(this.options().isUnicode()); this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - final String header = this.buildHeader(); String dump = this.yaml.dump(this.getValues(false)); if (dump.equals(YamlConfiguration.BLANK_CONFIG)) { dump = ""; } - return header + dump; + return dump; } @Override @@ -144,22 +148,21 @@ public void loadFromString(final String contents) throws InvalidConfigurationExc @Override protected String buildHeader() { final String header = this.options().header(); + final boolean copyHeader = this.options().copyHeader(); - if (this.options().copyHeader()) { - final Configuration def = this.getDefaults(); + if (!copyHeader || header == null) { + return ""; + } - if (def instanceof FileConfiguration) { - final FileConfiguration filedefaults = (FileConfiguration) def; - final String defaultsHeader = filedefaults.buildHeader(); + final Configuration def = this.getDefaults(); - if (defaultsHeader != null && defaultsHeader.length() > 0) { - return defaultsHeader; - } - } - } + if (def instanceof FileConfiguration) { + final FileConfiguration filedefaults = (FileConfiguration) def; + final String defaultsHeader = filedefaults.buildHeader(); - if (header == null) { - return ""; + if (defaultsHeader != null && defaultsHeader.length() > 0) { + return defaultsHeader; + } } final StringBuilder builder = new StringBuilder(); @@ -171,7 +174,6 @@ protected String buildHeader() { if (startedHeader || lines[i].length() != 0) { builder.insert(0, lines[i]); - builder.insert(0, Commentable.COMMENT_PREFIX); startedHeader = true; } } @@ -217,7 +219,7 @@ protected static String parseHeader(final String input) { } if (line.length() > commentPrefixTrimmed.length()) { - result.append(line.substring(commentPrefixTrimmed.length()).trim()); + result.append(line.trim()); } foundHeader = true; diff --git a/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlFile.java b/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlFile.java index c0becae5..ab6470fb 100644 --- a/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlFile.java +++ b/Simple-Yaml/src/main/java/org/simpleyaml/configuration/file/YamlFile.java @@ -126,11 +126,10 @@ public void save() throws IOException { * @throws IOException if it hasn't been possible to save configuration file */ public String saveToString() throws IOException { - String contents = super.saveToString(); if (this.useComments) { - contents = new CommentDumper(this.options(), this.parseComments(), new StringReader(contents)).dump(); + return new CommentDumper(this.options(), this.parseComments(), new StringReader(super.dump())).dump(); } - return contents; + return super.saveToString(); } /** diff --git a/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlConfigurationTest.java b/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlConfigurationTest.java index 546bb221..7217c17d 100644 --- a/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlConfigurationTest.java +++ b/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlConfigurationTest.java @@ -57,14 +57,14 @@ void saveToString() throws IOException { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -220,14 +220,14 @@ void parseHeader() { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -237,7 +237,7 @@ void parseHeader() { MatcherAssert.assertThat( "Couldn't parse the header of the content!", YamlConfiguration.parseHeader(content), - new IsEqual<>("test123") + new IsEqual<>("# test123") ); } diff --git a/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlFileTest.java b/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlFileTest.java index 07cde48b..da109568 100644 --- a/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlFileTest.java +++ b/Simple-Yaml/src/test/java/org/simpleyaml/configuration/file/YamlFileTest.java @@ -51,14 +51,14 @@ void load() throws Exception { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -88,23 +88,23 @@ void load() throws Exception { void loadWithFolderSpaces() throws Exception { final YamlFile yamlFile = new YamlFile(Objects.requireNonNull(getClass().getClassLoader().getResource("folder with space/test.yml"))); final String content = "test:\n" + - " number: 5\n" + - " string: Hello world\n" + - " boolean: true\n" + - " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + - "math:\n" + - " pi: 3.141592653589793\n" + - "timestamp:\n" + - " canonicalDate: 2020-07-04T13:18:04.458Z\n" + - " formattedDate: 04/07/2020 15:18:04\n"; + " number: 5\n" + + " string: Hello world\n" + + " boolean: true\n" + + " list:\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + + "math:\n" + + " pi: 3.141592653589793\n" + + "timestamp:\n" + + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + + " formattedDate: 04/07/2020 15:18:04\n"; yamlFile.load(); MatcherAssert.assertThat( "Couldn't load the file!", @@ -121,14 +121,14 @@ void loadConfiguration() throws Exception { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -159,7 +159,7 @@ void loadWithComments() throws Exception { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -168,28 +168,28 @@ void loadWithComments() throws Exception { " boolean: true\n" + " # List of words\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " # Comment on a list item\n" + - " - entry # :)\n" + - '\n' + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " # Comment on a list item\n" + + " - entry # :)\n" + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; yamlFile.loadWithComments(); MatcherAssert.assertThat( @@ -205,7 +205,7 @@ void loadConfigurationWithComments() throws Exception { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -214,28 +214,28 @@ void loadConfigurationWithComments() throws Exception { " boolean: true\n" + " # List of words\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " # Comment on a list item\n" + - " - entry # :)\n" + - '\n' + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " # Comment on a list item\n" + + " - entry # :)\n" + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; MatcherAssert.assertThat( "Couldn't load the file with comments!", @@ -259,24 +259,24 @@ void loadConfigurationWithComments() throws Exception { @Test void createOrLoad() throws Exception { final YamlFile yamlFile = new YamlFile(YamlFileTest.getResourceURI("test-comments.yml")); - final String content = "# ####################\n" + - "# # INITIAL COMMENT ##\n" + - "# ####################\n" + - "# \n" + + final String content = "#####################\n" + + "## INITIAL COMMENT ##\n" + + "#####################\n" + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -296,7 +296,7 @@ void createOrLoadWithComments() throws Exception { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -305,28 +305,28 @@ void createOrLoadWithComments() throws Exception { " boolean: true\n" + " # List of words\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " # Comment on a list item\n" + - " - entry # :)\n" + - '\n' + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " # Comment on a list item\n" + + " - entry # :)\n" + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; yamlFile.createOrLoadWithComments(); MatcherAssert.assertThat( @@ -434,14 +434,14 @@ void fileToString() throws Exception { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -467,14 +467,14 @@ void saveToString() throws Exception { " string: Hello world\n" + " boolean: true\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " - entry\n" + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " - entry\n" + "math:\n" + " pi: 3.141592653589793\n" + "timestamp:\n" + @@ -500,7 +500,7 @@ void saveToStringWithComments() throws Exception { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -509,28 +509,28 @@ void saveToStringWithComments() throws Exception { " boolean: true\n" + " # List of words\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " # Comment on a list item\n" + - " - entry # :)\n" + - '\n' + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " # Comment on a list item\n" + + " - entry # :)\n" + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; MatcherAssert.assertThat( @@ -550,20 +550,20 @@ void saveToStringWithComments2() throws Exception { " sub-section-1:\n" + " # List of numbers\n" + " list:\n" + - " - 1\n" + - " - 2\n" + - " - 3\n" + + " - 1\n" + + " - 2\n" + + " - 3\n" + " sub-section-2: # Side comment\n" + " list:\n" + - " - 1\n" + - " - 2 # Side comment on an arbitrary element\n" + - " - 3\n" + + " - 1\n" + + " - 2 # Side comment on an arbitrary element\n" + + " - 3\n" + " sub-section-3:\n" + " # List of numbers\n" + " list: # Side comment with extra space\n" + - " - 1\n" + - " - 2\n" + - " - 3\n"; + " - 1\n" + + " - 2\n" + + " - 3\n"; MatcherAssert.assertThat( "Couldn't get the content of the file (toString)!", @@ -722,7 +722,7 @@ void getSize() throws URISyntaxException { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -738,21 +738,21 @@ void getSize() throws URISyntaxException { " - in\n" + " - a\n" + " - separated\n" + - " # Comment on a list item\n" + + " # Comment on a list item\n" + " - entry # :)\n" + - '\n' + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; MatcherAssert.assertThat( @@ -808,7 +808,7 @@ void copyTo() throws Exception { final String content = "#####################\n" + "## INITIAL COMMENT ##\n" + "#####################\n" + - '\n' + + "\n" + "# Test comments\n" + "test:\n" + " number: 5\n" + @@ -817,28 +817,28 @@ void copyTo() throws Exception { " boolean: true\n" + " # List of words\n" + " list:\n" + - " - Each\n" + - " - word\n" + - " - will\n" + - " - be\n" + - " - in\n" + - " - a\n" + - " - separated\n" + - " # Comment on a list item\n" + - " - entry # :)\n" + - '\n' + + " - Each\n" + + " - word\n" + + " - will\n" + + " - be\n" + + " - in\n" + + " - a\n" + + " - separated\n" + + " # Comment on a list item\n" + + " - entry # :)\n" + + "\n" + "# Wonderful number\n" + "math:\n" + " pi: 3.141592653589793\n" + " # Comment without direct key\n" + - '\n' + + "\n" + "# Some timestamps\n" + "timestamp:\n" + " # ISO\n" + " canonicalDate: 2020-07-04T13:18:04.458Z\n" + " # Date/Time with format\n" + " formattedDate: 04/07/2020 15:18:04\n" + - '\n' + + "\n" + "# End\n"; MatcherAssert.assertThat( "Couldn't copy the file!",