From bdd890d0ae44cc0bab1daa234543aa20a38a7a4d Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Thu, 9 Jun 2011 23:31:00 -0700 Subject: [PATCH] Added the ability to set a header when saving configuration files. By: sk89q --- .../org/bukkit/util/config/Configuration.java | 53 +++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/util/config/Configuration.java b/paper-api/src/main/java/org/bukkit/util/config/Configuration.java index ddda68cd37..95f962206b 100644 --- a/paper-api/src/main/java/org/bukkit/util/config/Configuration.java +++ b/paper-api/src/main/java/org/bukkit/util/config/Configuration.java @@ -56,6 +56,7 @@ import org.yaml.snakeyaml.representer.Representer; public class Configuration extends ConfigurationNode { private Yaml yaml; private File file; + private String header = null; public Configuration(File file) { super(new HashMap()); @@ -93,8 +94,49 @@ public class Configuration extends ConfigurationNode { } /** - * Saves the configuration to disk. All errors are clobbered. - * + * Set the header for the file as a series of lines that are terminated + * by a new line sequence. + * + * @param headerLines header lines to prepend + */ + public void setHeader(String ... headerLines) { + StringBuilder header = new StringBuilder(); + + for (String line : headerLines) { + if (header.length() > 0) { + header.append("\r\n"); + } + header.append(line); + } + + setHeader(header.toString()); + } + + /** + * Set the header for the file. A header can be provided to prepend the + * YAML data output on configuration save. The header is + * printed raw and so must be manually commented if used. A new line will + * be appended after the header, however, if a header is provided. + * + * @param header header to prepend + */ + public void setHeader(String header) { + this.header = header; + } + + /** + * Return the set header. + * + * @return + */ + public String getHeader() { + return header; + } + + /** + * Saves the configuration to disk. All errors are clobbered. + * + * @param header header to prepend * @return true if it was successful */ public boolean save() { @@ -108,7 +150,12 @@ public class Configuration extends ConfigurationNode { try { stream = new FileOutputStream(file); - yaml.dump(root, new OutputStreamWriter(stream, "UTF-8")); + OutputStreamWriter writer = new OutputStreamWriter(stream, "UTF-8"); + if (header != null) { + writer.append(header); + writer.append("\r\n"); + } + yaml.dump(root, writer); return true; } catch (IOException e) {} finally { try {