13
0
geforkt von Mirrors/Paper

Added new FileConfigurationOptions.copyHeader, defaulting to true. Copies the header from default config, if there is one.

By: Nathan Adams <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2011-10-12 13:24:57 +01:00
Ursprung 69a2349368
Commit f5eee9b341
6 geänderte Dateien mit 153 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -178,6 +178,16 @@ public abstract class FileConfiguration extends MemoryConfiguration {
* @throws IllegalArgumentException Thrown if contents is null.
*/
public abstract void loadFromString(String contents) throws InvalidConfigurationException;
/**
* Compiles the header for this {@link FileConfiguration} and returns the result.
* <p>
* This will use the header from {@link #options()} -> {@link FileConfigurationOptions#header()},
* respecting the rules of {@link FileConfigurationOptions#copyHeader()} if set.
*
* @return Compiled header
*/
protected abstract String buildHeader();
@Override
public FileConfigurationOptions options() {

Datei anzeigen

@ -7,6 +7,7 @@ import org.bukkit.configuration.*;
*/
public class FileConfigurationOptions extends MemoryConfigurationOptions {
private String header = null;
private boolean copyHeader = true;
protected FileConfigurationOptions(MemoryConfiguration configuration) {
super(configuration);
@ -64,4 +65,45 @@ public class FileConfigurationOptions extends MemoryConfigurationOptions {
this.header = value;
return this;
}
/**
* Gets whether or not the header should be copied from a default source.
* <p>
* If this is true, if a default {@link FileConfiguration} is passed to
* {@link FileConfiguration#setDefaults(org.bukkit.configuration.Configuration)}
* then upon saving it will use the header from that config, instead of the one provided here.
* <p>
* If no default is set on the configuration, or the default is not of type FileConfiguration,
* or that config has no header ({@link #header()} returns null) then the header
* specified in this configuration will be used.
* <p>
* Defaults to true.
*
* @return Whether or not to copy the header
*/
public boolean copyHeader() {
return copyHeader;
}
/**
* Sets whether or not the header should be copied from a default source.
* <p>
* If this is true, if a default {@link FileConfiguration} is passed to
* {@link FileConfiguration#setDefaults(org.bukkit.configuration.Configuration)}
* then upon saving it will use the header from that config, instead of the one provided here.
* <p>
* If no default is set on the configuration, or the default is not of type FileConfiguration,
* or that config has no header ({@link #header()} returns null) then the header
* specified in this configuration will be used.
* <p>
* Defaults to true.
*
* @param value Whether or not to copy the header
* @return This object, for chaining
*/
public FileConfigurationOptions copyHeader(boolean value) {
copyHeader = value;
return this;
}
}

Datei anzeigen

@ -39,13 +39,14 @@ public class YamlConfiguration extends FileConfiguration {
serializeValues(output, getValues(false));
String header = buildHeader();
String dump = yaml.dump(output);
if (dump.equals(BLANK_CONFIG)) {
dump = "";
}
return buildHeader() + dump;
return header + dump;
}
@Override
@ -61,7 +62,12 @@ public class YamlConfiguration extends FileConfiguration {
throw new InvalidConfigurationException("Specified contents is not a valid Configuration", ex);
}
options().header(parseHeader(contents));
String header = parseHeader(contents);
if (header.length() > 0) {
options().header(header);
}
deserializeValues(input, this);
}
@ -159,6 +165,19 @@ public class YamlConfiguration extends FileConfiguration {
protected String buildHeader() {
String header = options().header();
if (options().copyHeader()) {
Configuration def = getDefaults();
if ((def != null) && (def instanceof FileConfiguration)) {
FileConfiguration filedefaults = (FileConfiguration)def;
String defaultsHeader = filedefaults.buildHeader();
if ((defaultsHeader != null) && (defaultsHeader.length() > 0)) {
return defaultsHeader;
}
}
}
if (header == null) {
return "";
}

Datei anzeigen

@ -32,6 +32,12 @@ public class YamlConfigurationOptions extends FileConfigurationOptions {
super.header(value);
return this;
}
@Override
public YamlConfigurationOptions copyHeader(boolean value) {
super.copyHeader(value);
return this;
}
/**
* Gets how much spaces should be used to indent each line.

Datei anzeigen

@ -1,9 +1,7 @@
package org.bukkit.configuration.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Map;
import org.bukkit.configuration.MemoryConfigurationTest;
@ -21,6 +19,10 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
public abstract String getTestValuesString();
public abstract String getTestHeaderInput();
public abstract String getTestHeaderResult();
@Test
public void testSave_File() throws Exception {
FileConfiguration config = getConfig();
@ -122,5 +124,65 @@ public abstract class FileConfigurationTest extends MemoryConfigurationTest {
}
assertEquals(values.keySet(), config.getKeys(true));
assertEquals(saved, config.saveToString());
}
@Test
public void testSaveToStringWithHeader() {
FileConfiguration config = getConfig();
config.options().header(getTestHeaderInput());
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
String result = config.saveToString();
String expected = getTestHeaderResult() + "\n" + getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testParseHeader() throws Exception {
FileConfiguration config = getConfig();
Map<String, Object> values = getTestValues();
String saved = getTestValuesString();
String header = getTestHeaderResult();
String expected = getTestHeaderInput();
config.loadFromString(header + "\n" + saved);
assertEquals(expected, config.options().header());
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
assertEquals(header + "\n" + saved, config.saveToString());
}
@Test
public void testCopyHeader() throws Exception {
FileConfiguration config = getConfig();
FileConfiguration defaults = getConfig();
Map<String, Object> values = getTestValues();
String saved = getTestValuesString();
String header = getTestHeaderResult();
String expected = getTestHeaderInput();
defaults.loadFromString(header);
config.loadFromString(saved);
config.setDefaults(defaults);
assertNull(config.options().header());
assertEquals(expected, defaults.options().header());
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
assertEquals(header + "\n" + saved, config.saveToString());
}
}

Datei anzeigen

@ -1,6 +1,5 @@
package org.bukkit.configuration.file;
import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.*;
@ -9,6 +8,16 @@ public class YamlConfigurationTest extends FileConfigurationTest {
public YamlConfiguration getConfig() {
return new YamlConfiguration();
}
@Override
public String getTestHeaderInput() {
return "This is a sample\nheader.\n\nNewline above should be commented.\n\n";
}
@Override
public String getTestHeaderResult() {
return "# This is a sample\n# header.\n# \n# Newline above should be commented.\n\n";
}
@Override
public String getTestValuesString() {
@ -30,55 +39,6 @@ public class YamlConfigurationTest extends FileConfigurationTest {
"- 5\n";
}
@Test
public void testSaveToStringWithHeader() {
YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader.");
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
String result = config.saveToString();
String expected = "# This is a sample\n# header.\n" + getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testSaveToStringWithLongHeader() {
YamlConfiguration config = getConfig();
config.options().header("This is a sample\nheader.\n\nNewline above should be commented.\n\n");
for (Map.Entry<String, Object> entry : getTestValues().entrySet()) {
config.set(entry.getKey(), entry.getValue());
}
String result = config.saveToString();
String expected = "# This is a sample\n# header.\n# \n# Newline above should be commented.\n\n\n" + getTestValuesString();
assertEquals(expected, result);
}
@Test
public void testParseHeader() throws Exception {
YamlConfiguration config = getConfig();
Map<String, Object> values = getTestValues();
String saved = getTestValuesString();
String header = "# This is a sample\n# header.\n# \n# Newline above should be commented.\n\n\n";
String expected = "This is a sample\nheader.\n\nNewline above should be commented.\n\n";
config.loadFromString(header + saved);
assertEquals(expected, config.options().header());
for (Map.Entry<String, Object> entry : values.entrySet()) {
assertEquals(entry.getValue(), config.get(entry.getKey()));
}
assertEquals(values.keySet(), config.getKeys(true));
}
@Test
public void testSaveToStringWithIndent() {
YamlConfiguration config = getConfig();