Skip to content

Commit

Permalink
Fixed CSV files with umlaute not loaded under Linux
Browse files Browse the repository at this point in the history
Issue: #1048
Signed-off-by: Colinas Maoling <[email protected]>
[rebased to master; formatted; minor code changes]
Signed-off-by: Andreas Buchen <[email protected]>
  • Loading branch information
cmaoling authored and buchen committed Oct 27, 2018
1 parent 2ec848d commit 58f0945
Showing 1 changed file with 56 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
Expand Down Expand Up @@ -457,45 +463,66 @@ public Column[] getColumns()
return columns;
}

public void processFile() throws IOException
private void processStream(InputStream stream) throws IOException
{
try (FileInputStream stream = new FileInputStream(inputFile))
Reader reader = new InputStreamReader(stream, encoding);

CSVStrategy strategy = new CSVStrategy(delimiter, '"', CSVStrategy.COMMENTS_DISABLED,
CSVStrategy.ESCAPE_DISABLED, false, false, false, false);

CSVParser parser = new CSVParser(reader, strategy);

for (int ii = 0; ii < skipLines; ii++)
parser.getLine();

List<String[]> input = new ArrayList<>();
String[] header = null;
String[] line = parser.getLine();
if (isFirstLineHeader)
{
Reader reader = new InputStreamReader(stream, encoding);
header = line;
}
else
{
header = new String[line.length];
for (int ii = 0; ii < header.length; ii++)
header[ii] = MessageFormat.format(Messages.CSVImportGenericColumnLabel, ii + 1);
input.add(line);
}

CSVStrategy strategy = new CSVStrategy(delimiter, '"', CSVStrategy.COMMENTS_DISABLED,
CSVStrategy.ESCAPE_DISABLED, false, false, false, false);
while ((line = parser.getLine()) != null)
input.add(line);

CSVParser parser = new CSVParser(reader, strategy);
this.columns = new CSVImporter.Column[header.length];
for (int ii = 0; ii < header.length; ii++)
this.columns[ii] = new Column(ii, header[ii]);

for (int ii = 0; ii < skipLines; ii++)
parser.getLine();
this.values = input;

List<String[]> input = new ArrayList<>();
String[] header = null;
String[] line = parser.getLine();
if (isFirstLineHeader)
mapToImportDefinition();
}

public void processFile() throws IOException
{
try (FileInputStream stream = new FileInputStream(inputFile))
{
processStream(stream);
}
catch (IOException e)
{
// fallback for file names with umlaute on Linux
byte[] ptext = inputFile.toString().getBytes(StandardCharsets.UTF_8);
String str = new String(ptext, StandardCharsets.ISO_8859_1);
Path path = Paths.get(URI.create("file://" + str)); //$NON-NLS-1$

try (InputStream stream = Files.newInputStream(path))
{
header = line;
processStream(stream);
}
else
catch (IOException e2)
{
header = new String[line.length];
for (int ii = 0; ii < header.length; ii++)
header[ii] = MessageFormat.format(Messages.CSVImportGenericColumnLabel, ii + 1);
input.add(line);
throw e;
}

while ((line = parser.getLine()) != null)
input.add(line);

this.columns = new CSVImporter.Column[header.length];
for (int ii = 0; ii < header.length; ii++)
this.columns[ii] = new Column(ii, header[ii]);

this.values = input;

mapToImportDefinition();
}
}

Expand Down

0 comments on commit 58f0945

Please sign in to comment.