switch to fastcsv as supercsv is not maintaned anymore

This commit is contained in:
2021-12-10 00:30:59 +01:00
parent 8cecca2172
commit 43856dffab
69 changed files with 173 additions and 220 deletions

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -26,22 +26,24 @@
package ninja.javafx.smartcsv.csv;
import org.supercsv.prefs.CsvPreference;
import ninja.javafx.smartcsv.preferences.Preferences;
import static ninja.javafx.smartcsv.preferences.Preferences.defaultPreferences;
/**
*
*/
public class CSVConfigurable {
protected CsvPreference csvPreference;
protected Preferences csvPreference;
protected String fileEncoding;
public CSVConfigurable() {
csvPreference = CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE;
csvPreference = defaultPreferences();
}
public void setCsvPreference(CsvPreference csvPreference) {
public void setCsvPreference(Preferences csvPreference) {
this.csvPreference = csvPreference;
}

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -26,17 +26,13 @@
package ninja.javafx.smartcsv.csv;
import de.siegmar.fastcsv.reader.NamedCsvReader;
import ninja.javafx.smartcsv.FileReader;
import ninja.javafx.smartcsv.fx.table.model.CSVModel;
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import org.supercsv.exception.SuperCsvException;
import org.supercsv.io.CsvMapReader;
import org.supercsv.io.ICsvMapReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
/**
* reads the csv file and stores the values in csv model
@@ -48,28 +44,38 @@ public class CSVFileReader extends CSVConfigurable implements FileReader<CSVMode
@Override
public void read(File file) throws IOException {
try (ICsvMapReader mapReader = new CsvMapReader(new java.io.FileReader(file.getAbsoluteFile(), Charset.forName(fileEncoding)),
csvPreference)) {
System.out.println(csvPreference);
try (var csv = getNamedCsvReader(file)) {
model = new CSVModel();
// the header columns are used as the keys to the Map
String[] header = mapReader.getHeader(true);
var header = csv.getHeader().toArray(new String[csv.getHeader().size()]);
model.setHeader(header);
Map<String, String> customerMap;
while ((customerMap = mapReader.read(header)) != null) {
CSVRow row = model.addRow();
csv.forEach(csvRow -> {
var row = model.addRow();
for (String column : header) {
model.addValue(row, column, customerMap.get(column));
model.addValue(row, column, csvRow.getField(column));
}
}
} catch (IOException | SuperCsvException ex) {
});
} catch (IOException ex) {
// TODO perhaps a custom NinjaException that can properly identify and localize the exception message
// is this a file not found? is this a corrupt csv? etc
throw new IOException("Failed to read " + file + ": " + ex.getMessage(), ex);
}
}
private NamedCsvReader getNamedCsvReader(File file) throws IOException {
var builder = NamedCsvReader.builder()
.fieldSeparator(csvPreference.delimiterChar());
if (csvPreference.quoteChar() != null) {
builder.quoteCharacter(csvPreference.quoteChar());
}
return builder.build(file.toPath(), Charset.forName(fileEncoding));
}
public CSVModel getContent() {
return model;
}

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -26,18 +26,17 @@
package ninja.javafx.smartcsv.csv;
import de.siegmar.fastcsv.writer.CsvWriter;
import de.siegmar.fastcsv.writer.QuoteStrategy;
import ninja.javafx.smartcsv.fx.table.model.CSVModel;
import ninja.javafx.smartcsv.fx.table.model.CSVRow;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.List;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toList;
/**
* filewriter for the csv
@@ -52,22 +51,22 @@ public class CSVFileWriter extends CSVConfigurable implements ninja.javafx.smart
@Override
public void write(File filename) throws IOException {
ICsvMapWriter mapWriter = null;
try {
mapWriter = new CsvMapWriter(new FileWriter(filename.getAbsolutePath(), Charset.forName(fileEncoding)),
csvPreference);
mapWriter.writeHeader(model.getHeader());
try (var writer = getCsvWriter(filename)){
writer.writeRow(model.getHeader());
for(CSVRow row: model.getRows()) {
Map<String, String> columns = convertMapFromModel(row);
mapWriter.write(columns, model.getHeader());
writer.writeRow(convertMapFromModel(row));
}
}
finally {
if( mapWriter != null ) {
mapWriter.close();
}
}
private CsvWriter getCsvWriter(File filename) throws IOException {
var writer = CsvWriter.builder().fieldSeparator(csvPreference.delimiterChar());
if (csvPreference.quoteChar() != null) {
writer.quoteCharacter(csvPreference.quoteChar());
writer.quoteStrategy(QuoteStrategy.ALWAYS);
}
return writer.build(filename.toPath(), Charset.forName(fileEncoding));
}
/**
@@ -75,13 +74,8 @@ public class CSVFileWriter extends CSVConfigurable implements ninja.javafx.smart
* @param row the row to convert
* @return a simple map for the supercvs writer
*/
private Map<String, String> convertMapFromModel(CSVRow row) {
return row.getColumns().entrySet().stream()
.collect(
toMap(
Map.Entry::getKey,
e -> e.getValue().getValue().getValue() != null ? e.getValue().getValue().getValue() : ""
)
);
private List<String> convertMapFromModel(CSVRow row) {
return row.getColumns().values().stream().map(v -> v.get().getValue())
.collect(toList());
}
}

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -85,6 +85,8 @@ public class SmartCSV extends Application {
private void showUI(Stage primaryStage, String name, String version) {
SmartCSVController smartCVSController = appContext.getBean(SmartCSVController.class);
Scene scene = new Scene((Parent) smartCVSController.getView());
var defaultThemeCss = getClass().getResource("/ninja/javafx/smartcsv/fx/smartcsv.css").toExternalForm();
scene.getRoot().getStylesheets().add(defaultThemeCss);
primaryStage.setScene(scene);
primaryStage.setTitle(String.format("%s %s", name, version));

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -64,7 +64,6 @@ import ninja.javafx.smartcsv.validation.configuration.ValidationConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.supercsv.prefs.CsvPreference;
import java.io.File;
import java.io.IOException;
@@ -79,6 +78,7 @@ import static javafx.application.Platform.exit;
import static javafx.application.Platform.runLater;
import static javafx.beans.binding.Bindings.*;
import static javafx.scene.layout.AnchorPane.*;
import static ninja.javafx.smartcsv.preferences.Preferences.defaultPreferences;
/**
* main controller of the application
@@ -222,7 +222,7 @@ public class SmartCSVController extends FXMLController {
private FileStorage<CSVModel> currentCsvFile = new FileStorage<>(csvFileReader, csvFileWriter);
private FileStorage<ValidationConfiguration> currentConfigFile = new FileStorage<>(new ValidationFileReader(), new ValidationFileWriter());
private FileStorage<CsvPreference> csvPreferenceFile = new FileStorage<>(new PreferencesFileReader(), new PreferencesFileWriter());
private FileStorage<Preferences> csvPreferenceFile = new FileStorage<>(new PreferencesFileReader(), new PreferencesFileWriter());
private FileStorage<String> fileEncodingFile = new FileStorage<>(new EncodingFileReader(), new EncodingFileWriter());
private ListChangeListener<ValidationError> errorListListener = c -> tableView.refresh();
@@ -368,7 +368,7 @@ public class SmartCSVController extends FXMLController {
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
CsvPreference csvPreference = preferencesController.getCsvPreference();
Preferences csvPreference = preferencesController.getCsvPreference();
setCsvPreference(csvPreference);
saveCsvPreferences(csvPreference);
String fileEncoding = CharsetHelper.getCharsetName(preferencesController.getFileEncoding());
@@ -544,12 +544,12 @@ public class SmartCSVController extends FXMLController {
loadEncodingFromFile();
});
} else {
setCsvPreference(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE);
setCsvPreference(defaultPreferences());
loadEncodingFromFile();
}
}
private void saveCsvPreferences(CsvPreference csvPreference) {
private void saveCsvPreferences(Preferences csvPreference) {
try {
createPreferenceFile();
csvPreferenceFile.setContent(csvPreference);
@@ -572,7 +572,7 @@ public class SmartCSVController extends FXMLController {
}
}
private void setCsvPreference(CsvPreference csvPreference) {
private void setCsvPreference(Preferences csvPreference) {
preferencesController.setCsvPreference(csvPreference);
csvFileReader.setCsvPreference(csvPreference);
csvFileWriter.setCsvPreference(csvPreference);

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -34,18 +34,15 @@ import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import ninja.javafx.smartcsv.fx.FXMLController;
import ninja.javafx.smartcsv.preferences.Preferences;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.supercsv.prefs.CsvPreference;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteModeName;
/**
* controller for preferences
*/
@@ -58,15 +55,9 @@ public class PreferencesController extends FXMLController {
@FXML
private TextField delimiterChar;
@FXML
private CheckBox surroundingSpacesNeedQuotes;
@FXML
private CheckBox ignoreEmptyLines;
@FXML
private ComboBox<String> quoteMode;
@FXML
private ComboBox<String> fileEncoding;
@@ -83,7 +74,6 @@ public class PreferencesController extends FXMLController {
@Override
public void initialize(URL location, ResourceBundle resources) {
quoteMode.getItems().addAll("normal", "always", "column");
fileEncoding.getItems().addAll(Charset.availableCharsets().keySet());
UnaryOperator<TextFormatter.Change> allowOnlyOneCharacter = change -> {
@@ -102,24 +92,23 @@ public class PreferencesController extends FXMLController {
}
private void revalidate() {
valid.setValue(quoteChar.getText().length() == 1 && delimiterChar.getText().length() == 1);
valid.setValue(quoteChar.getText().length() <= 1 && delimiterChar.getText().length() == 1);
}
public void setCsvPreference(CsvPreference csvPreference) {
quoteChar.setText(Character.toString(csvPreference.getQuoteChar()));
delimiterChar.setText(Character.toString((char)csvPreference.getDelimiterChar()));
surroundingSpacesNeedQuotes.setSelected(csvPreference.isSurroundingSpacesNeedQuotes());
ignoreEmptyLines.setSelected(csvPreference.isIgnoreEmptyLines());
quoteMode.getSelectionModel().select(getQuoteModeName(csvPreference.getQuoteMode()));
endOfLineSymbols = csvPreference.getEndOfLineSymbols();
public void setCsvPreference(Preferences csvPreference) {
if (csvPreference.quoteChar() != null) {
quoteChar.setText(csvPreference.quoteChar().toString());
} else {
quoteChar.setText("");
}
delimiterChar.setText(Character.toString(csvPreference.delimiterChar()));
ignoreEmptyLines.setSelected(csvPreference.ignoreEmptyLines());
endOfLineSymbols = csvPreference.endOfLineSymbols();
}
public CsvPreference getCsvPreference() {
return new CsvPreference.Builder(quoteChar.getText().charAt(0), delimiterChar.getText().charAt(0), endOfLineSymbols)
.useQuoteMode(getQuoteMode(quoteMode.getSelectionModel().getSelectedItem()))
.surroundingSpacesNeedQuotes(surroundingSpacesNeedQuotes.isSelected())
.ignoreEmptyLines(ignoreEmptyLines.isSelected())
.build();
public Preferences getCsvPreference() {
var quote = quoteChar.getText().length() == 0 ? null : quoteChar.getText().charAt(0);
return new Preferences(quote, delimiterChar.getText().charAt(0), endOfLineSymbols, ignoreEmptyLines.isSelected());
}
public void setFileEncoding(String fileEncoding) {

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,8 +2,8 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
@@ -21,36 +21,18 @@
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package ninja.javafx.smartcsv.preferences;
import org.supercsv.quote.AlwaysQuoteMode;
import org.supercsv.quote.ColumnQuoteMode;
import org.supercsv.quote.NormalQuoteMode;
import org.supercsv.quote.QuoteMode;
public record Preferences(
Character quoteChar,
char delimiterChar,
String endOfLineSymbols,
boolean ignoreEmptyLines) {
/**
* Helper class for mapping quote quoteModeName
*/
public class QuoteModeHelper {
public static QuoteMode getQuoteMode(String quoteModeName) {
switch (quoteModeName) {
case "always": return new AlwaysQuoteMode();
case "column": return new ColumnQuoteMode();
default: return new NormalQuoteMode();
}
}
public static String getQuoteModeName(QuoteMode quoteMode) {
if (quoteMode instanceof AlwaysQuoteMode) {
return "always";
} else if (quoteMode instanceof ColumnQuoteMode) {
return "column";
} else {
return "normal";
}
public static Preferences defaultPreferences() {
return new Preferences('\"',',', "\n", true);
}
}

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -28,27 +28,23 @@ package ninja.javafx.smartcsv.preferences;
import com.google.gson.GsonBuilder;
import ninja.javafx.smartcsv.FileReader;
import org.supercsv.prefs.CsvPreference;
import org.supercsv.quote.AlwaysQuoteMode;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static ninja.javafx.smartcsv.preferences.QuoteModeHelper.getQuoteMode;
import static ninja.javafx.smartcsv.preferences.Preferences.defaultPreferences;
/**
* file reader for the preferences
*/
public class PreferencesFileReader implements FileReader<CsvPreference> {
public class PreferencesFileReader implements FileReader<Preferences> {
private CsvPreference csvPreference;
private Preferences csvPreference;
public PreferencesFileReader() {
csvPreference = new CsvPreference.
Builder(CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE).
useQuoteMode(new AlwaysQuoteMode()).build();
csvPreference = defaultPreferences();
}
@Override
@@ -56,22 +52,15 @@ public class PreferencesFileReader implements FileReader<CsvPreference> {
Map config = new GsonBuilder().create().fromJson(new java.io.FileReader(filename), HashMap.class);
if (config != null) {
char quoteChar = config.get("quoteChar").toString().charAt(0);
Character quoteChar = config.get("quoteChar") == null ? null : config.get("quoteChar").toString().charAt(0);
char delimiterChar = config.get("delimiterChar").toString().charAt(0);
String endOfLineSymbols = config.get("endOfLineSymbols").toString();
boolean surroundingSpacesNeedQuotes = (Boolean) config.get("surroundingSpacesNeedQuotes");
boolean ignoreEmptyLines = (Boolean) config.get("ignoreEmptyLines");
String quoteMode = config.get("quoteMode").toString();
csvPreference = new CsvPreference.Builder(quoteChar, delimiterChar, endOfLineSymbols)
.useQuoteMode(getQuoteMode(quoteMode))
.surroundingSpacesNeedQuotes(surroundingSpacesNeedQuotes)
.ignoreEmptyLines(ignoreEmptyLines)
.build();
csvPreference = new Preferences(quoteChar, delimiterChar, endOfLineSymbols, ignoreEmptyLines);
}
}
public CsvPreference getContent() {
public Preferences getContent() {
return csvPreference;
}

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -29,7 +29,6 @@ package ninja.javafx.smartcsv.preferences;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import ninja.javafx.smartcsv.FileWriter;
import org.supercsv.prefs.CsvPreference;
import java.io.File;
import java.io.IOException;
@@ -40,22 +39,20 @@ import java.util.Map;
/**
* Save preferences to configuration file
*/
public class PreferencesFileWriter implements FileWriter<CsvPreference> {
public class PreferencesFileWriter implements FileWriter<Preferences> {
private CsvPreference csvPreference;
private Preferences csvPreference;
public void setContent(CsvPreference csvPreference) {
public void setContent(Preferences 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()));
preferences.put("endOfLineSymbols", csvPreference.getEndOfLineSymbols());
preferences.put("surroundingSpacesNeedQuotes", csvPreference.isSurroundingSpacesNeedQuotes());
preferences.put("ignoreEmptyLines", csvPreference.isIgnoreEmptyLines());
preferences.put("quoteMode", QuoteModeHelper.getQuoteModeName(csvPreference.getQuoteMode()));
preferences.put("quoteChar", csvPreference.quoteChar() == null ? null: csvPreference.quoteChar().toString());
preferences.put("delimiterChar", Character.toString((char)csvPreference.delimiterChar()));
preferences.put("endOfLineSymbols", csvPreference.endOfLineSymbols());
preferences.put("ignoreEmptyLines", csvPreference.ignoreEmptyLines());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
Files.write(file.toPath(), gson.toJson(preferences).getBytes());

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@@ -2,7 +2,7 @@
The MIT License (MIT)
-----------------------------------------------------------------------------
Copyright (c) 2015-2019 javafx.ninja <info@javafx.ninja>
Copyright (c) 2015-2021 javafx.ninja <info@javafx.ninja>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal