extracted load and save service and generalize file writer

This commit is contained in:
Andreas Billmann
2016-01-12 21:52:08 +01:00
parent 72a8674cfc
commit 469895b27e
8 changed files with 294 additions and 142 deletions

View File

@@ -29,23 +29,29 @@ package ninja.javafx.smartcsv.preferences;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigRenderOptions;
import ninja.javafx.smartcsv.FileWriter;
import org.springframework.stereotype.Service;
import org.supercsv.prefs.CsvPreference;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import static java.nio.file.Files.write;
/**
* Save preferences to configuration file
*/
@Service
public class PreferencesFileWriter {
public class PreferencesFileWriter implements FileWriter {
public void saveFile(File file, CsvPreference csvPreference) throws IOException {
private CsvPreference csvPreference;
public void setCsvPreference(CsvPreference csvPreference) {
this.csvPreference = csvPreference;
}
public void write(File file) throws IOException {
Map<String, Object> preferences = new HashMap<>();
preferences.put("quoteChar", Character.toString(csvPreference.getQuoteChar()));
preferences.put("delimiterChar", Character.toString((char)csvPreference.getDelimiterChar()));
@@ -55,7 +61,7 @@ public class PreferencesFileWriter {
preferences.put("quoteMode", QuoteModeHelper.getQuoteModeName(csvPreference.getQuoteMode()));
Config config = ConfigFactory.parseMap(preferences);
write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes());
Files.write(file.toPath(), config.root().render(ConfigRenderOptions.concise()).getBytes());
}
}