Let system decide about line endings in config files

This commit is contained in:
Evenprime 2011-11-27 14:42:06 +01:00
parent f3663d07aa
commit 1f9a2643e8
3 changed files with 39 additions and 19 deletions

View File

@ -105,7 +105,7 @@ public class NoCheat extends JavaPlugin {
this.players = new PlayerManager(this); this.players = new PlayerManager(this);
// Then read the configuration files // Then read the configuration files
this.conf = new ConfigurationManager(this.getDataFolder().getPath()); this.conf = new ConfigurationManager(this.getDataFolder());
// Then set up the performance counters // Then set up the performance counters
this.performance = new PerformanceManager(); this.performance = new PerformanceManager();
@ -187,7 +187,7 @@ public class NoCheat extends JavaPlugin {
public void reloadConfiguration() { public void reloadConfiguration() {
conf.cleanup(); conf.cleanup();
this.conf = new ConfigurationManager(this.getDataFolder().getPath()); this.conf = new ConfigurationManager(this.getDataFolder());
players.cleanDataMap(); players.cleanDataMap();
players.clearCriticalData(); players.clearCriticalData();
} }

View File

@ -70,7 +70,7 @@ public class ConfigurationManager {
// private final static String loggerName = "cc.co.evenprime.nocheat"; // private final static String loggerName = "cc.co.evenprime.nocheat";
// public final Logger logger = Logger.getLogger(loggerName); // public final Logger logger = Logger.getLogger(loggerName);
public ConfigurationManager(String rootConfigFolder) { public ConfigurationManager(File rootConfigFolder) {
ActionMapper actionMapper = new ActionMapper(); ActionMapper actionMapper = new ActionMapper();
@ -85,7 +85,7 @@ public class ConfigurationManager {
} }
private void initializeActions(String rootConfigFolder, ActionMapper actionManager) { private void initializeActions(File rootConfigFolder, ActionMapper actionManager) {
File defaultActionsFile = new File(rootConfigFolder, defaultActionFileName); File defaultActionsFile = new File(rootConfigFolder, defaultActionFileName);
@ -112,7 +112,7 @@ public class ConfigurationManager {
* *
* @param configurationFile * @param configurationFile
*/ */
private void initializeConfig(String rootConfigFolder, ActionMapper action) { private void initializeConfig(File rootConfigFolder, ActionMapper action) {
// First try to obtain and parse the global config file // First try to obtain and parse the global config file
FlatFileConfiguration root; FlatFileConfiguration root;
@ -162,24 +162,23 @@ public class ConfigurationManager {
} }
} }
private ConfigurationCache createConfigurationCache(String rootConfigFolder, Configuration configProvider) { private ConfigurationCache createConfigurationCache(File rootConfigFolder, Configuration configProvider) {
return new ConfigurationCache(configProvider, setupFileLogger(new File(rootConfigFolder, configProvider.getString(DefaultConfiguration.LOGGING_FILENAME)))); return new ConfigurationCache(configProvider, setupFileLogger(new File(rootConfigFolder, configProvider.getString(DefaultConfiguration.LOGGING_FILENAME))));
} }
private static File getGlobalConfigFile(String rootFolder) { private static File getGlobalConfigFile(File rootFolder) {
File globalConfig = new File(rootFolder, configFileName); File globalConfig = new File(rootFolder, configFileName);
return globalConfig; return globalConfig;
} }
private static Map<String, File> getWorldSpecificConfigFiles(String rootConfigFolder) { private static Map<String, File> getWorldSpecificConfigFiles(File rootFolder) {
HashMap<String, File> files = new HashMap<String, File>(); HashMap<String, File> files = new HashMap<String, File>();
File rootFolder = new File(rootConfigFolder);
if(rootFolder.isDirectory()) { if(rootFolder.isDirectory()) {
for(File f : rootFolder.listFiles()) { for(File f : rootFolder.listFiles()) {
if(f.isFile()) { if(f.isFile()) {

View File

@ -26,6 +26,14 @@ public class FlatFileConfiguration extends Configuration {
public void load(ActionMapper action) throws IOException { public void load(ActionMapper action) throws IOException {
if(!file.exists()) {
if(file.getParentFile() != null)
file.getParentFile().mkdirs();
if(file.createNewFile())
save();
else
throw new IOException("Cannot load \"" + file.getPath() + "\": File can not be created!");
}
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file))); BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line = null; String line = null;
@ -117,17 +125,22 @@ public class FlatFileConfiguration extends Configuration {
public void save() { public void save() {
try { try {
if(!file.exists()) {
if(file.getParentFile() != null) if(file.getParentFile() != null)
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
if(!file.createNewFile())
throw new IOException("Cannot save to \"" + file.getPath() + "\" : File can not be created!");
}
file.createNewFile();
BufferedWriter w = new BufferedWriter(new FileWriter(file)); BufferedWriter w = new BufferedWriter(new FileWriter(file));
w.write("# Want to know what these options do? Read at the end of this file.\r\n"); w.write("# Want to know what these options do? Read at the end of this file.");
w.newLine();
saveRecursive(w, ROOT); saveRecursive(w, ROOT);
w.write("\r\n\r\n"); w.newLine();
w.newLine();
saveDescriptionsRecursive(w, ROOT); saveDescriptionsRecursive(w, ROOT);
@ -160,11 +173,18 @@ public class FlatFileConfiguration extends Configuration {
id = i.getName() + "." + id; id = i.getName() + "." + id;
} }
w.write("\r\n\r\n# " + id + ":\r\n#\r\n"); w.newLine();
w.newLine();
w.write("# " + id + ":");
w.newLine();
w.write("#");
w.newLine();
String explaination = Explainations.get(node); String[] explainationLines = Explainations.get(node).split("\n");
w.write("# " + explaination.replaceAll("\n", "\r\n# ")); for(String line : explainationLines) {
w.write("# " + line);
}
} }
private void saveRecursive(BufferedWriter w, OptionNode node) throws IOException { private void saveRecursive(BufferedWriter w, OptionNode node) throws IOException {
@ -174,7 +194,7 @@ public class FlatFileConfiguration extends Configuration {
for(OptionNode o : node.getChildren()) { for(OptionNode o : node.getChildren()) {
if(node == ROOT) { if(node == ROOT) {
w.write("\r\n"); w.newLine();
} }
saveRecursive(w, o); saveRecursive(w, o);
@ -239,7 +259,8 @@ public class FlatFileConfiguration extends Configuration {
} }
private void saveValue(BufferedWriter w, String id, String value) throws IOException { private void saveValue(BufferedWriter w, String id, String value) throws IOException {
w.write(id + " = " + value + "\r\n"); w.write(id + " = " + value);
w.newLine();
} }
private String removeQuotationMarks(String s) { private String removeQuotationMarks(String s) {