Skip to content

Commit

Permalink
Fix #1812 CSV files are no longer imported correctly when double quot…
Browse files Browse the repository at this point in the history
…es inside strings are delimited with backslashes
  • Loading branch information
eduramiba committed Oct 8, 2017
1 parent af3c299 commit 8ee2579
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static CSVParser configureCSVParser(File file, Character fieldSeparator,

CSVFormat csvFormat = CSVFormat.DEFAULT
.withDelimiter(fieldSeparator)
.withEscape('\\')
.withIgnoreEmptyLines(true)
.withNullString("")
.withIgnoreSurroundingSpaces(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,68 @@ public void close() throws IOException {
private class CSVIterator implements Iterator<SheetRow> {

private final Iterator<CSVRecord> iterator;
private ErrorRow errorFound = null;

public CSVIterator() {
iterator = parser.iterator();
}

@Override
public boolean hasNext() {
if (errorFound != null) {
return false;
}

try {
return iterator.hasNext();
} catch (Exception e) {
//In case of malformed CSV or bad delimiter
errorFound = new ErrorRow(e.getMessage());
Logger.getLogger("").severe(e.getMessage());
return false;//In case of malformed CSV or bad delimiter
return true;
}
}

@Override
public SheetRow next() {
return new CSVSheetRow(iterator.next());
if (errorFound != null) {
return errorFound;
} else {
return new CSVSheetRow(iterator.next());
}
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

private class ErrorRow implements SheetRow {

private final String errorMessage;

public ErrorRow(String errorMessage) {
this.errorMessage = errorMessage;
}

@Override
public boolean isConsistent() {
return false;
}

@Override
public int size() {
return 1;
}

@Override
public String get(int index) {
if (index == 0) {
return errorMessage;
} else {
return null;
}
}
}
}
2 changes: 1 addition & 1 deletion modules/ImportPlugin/src/main/nbm/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Essential-Module: true
OpenIDE-Module-Localizing-Bundle: org/gephi/io/importer/plugin/Bundle.properties
OpenIDE-Module-Specification-Version: ${gephi.modules.specification.version}
OpenIDE-Module-Specification-Version: 0.9.2.1
OpenIDE-Module-Display-Category: Plugin
OpenIDE-Module-Name: Import Plugin
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,19 @@ public void refreshPreviewTable() {
while (iterator.hasNext() && count < MAX_ROWS_PREVIEW) {
count++;

SheetRow row = iterator.next();
final SheetRow row = iterator.next();
final int rowSize = row.size();

maxRowSize = Math.max(maxRowSize, row.size());

currentRecord = new String[row.size()];
for (int i = 0; i < row.size(); i++) {
currentRecord = new String[rowSize];
for (int i = 0; i < rowSize; i++) {
currentRecord[i] = row.get(i);
}

// Search for missing source or target columns for edges table
if (mode == SpreadsheetGeneralConfiguration.Mode.EDGES_TABLE) {
if (currentRecord[sourceColumnIndex] == null || currentRecord[targetColumnIndex] == null) {
if (rowSize <= sourceColumnIndex || rowSize <= targetColumnIndex || currentRecord[sourceColumnIndex] == null || currentRecord[targetColumnIndex] == null) {
hasRowsMissingSourcesOrTargets = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion modules/ImportPluginUI/src/main/nbm/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
AutoUpdate-Essential-Module: true
OpenIDE-Module-Localizing-Bundle: org/gephi/ui/importer/plugin/Bundle.properties
OpenIDE-Module-Specification-Version: ${gephi.modules.specification.version}
OpenIDE-Module-Specification-Version: 0.9.2.1
OpenIDE-Module-Display-Category: Gephi UI
OpenIDE-Module-Name: Import Plugin UI

0 comments on commit 8ee2579

Please sign in to comment.