Add config saving

This commit is contained in:
md_5 2013-09-24 12:14:47 +10:00
parent 103a509f26
commit 0a5f8556fe
3 changed files with 26 additions and 6 deletions

View File

@ -13,7 +13,7 @@ public final class Configuration
{ {
private static final char SEPARATOR = '.'; private static final char SEPARATOR = '.';
private final Map<String, Object> self; final Map<String, Object> self;
private final Configuration defaults; private final Configuration defaults;
private Configuration getSectionFor(String path) private Configuration getSectionFor(String path)

View File

@ -1,7 +1,9 @@
package net.md_5.bungee.config; package net.md_5.bungee.config;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.Writer;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -21,7 +23,11 @@ public abstract class ConfigurationProvider
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
public abstract Configuration load(File file); public abstract void save(Configuration config, File file) throws IOException;
public abstract void save(Configuration config, Writer writer);
public abstract Configuration load(File file) throws IOException;
public abstract Configuration load(Reader reader); public abstract Configuration load(Reader reader);

View File

@ -2,8 +2,10 @@ package net.md_5.bungee.config;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.Writer;
import java.util.Map; import java.util.Map;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -26,14 +28,26 @@ public class YamlConfiguration extends ConfigurationProvider
}; };
@Override @Override
public Configuration load(File file) public void save(Configuration config, File file) throws IOException
{
try ( FileWriter writer = new FileWriter( file ) )
{
save( config, writer );
}
}
@Override
public void save(Configuration config, Writer writer)
{
yaml.get().dump( config.self, writer );
}
@Override
public Configuration load(File file) throws IOException
{ {
try ( FileReader reader = new FileReader( file ) ) try ( FileReader reader = new FileReader( file ) )
{ {
return load( reader ); return load( reader );
} catch ( IOException ex )
{
return null;
} }
} }