-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] Rename custom tokenizer, change invalid quoting property
- Loading branch information
1 parent
520e7a8
commit 2dd221c
Showing
3 changed files
with
103 additions
and
53 deletions.
There are no files selected for viewing
41 changes: 0 additions & 41 deletions
41
s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/CustomTokenizer.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
s-pipes-modules/module-tabular/src/main/java/cz/cvut/spipes/InvalidQuotingTokenizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cz.cvut.spipes; | ||
|
||
import org.supercsv.io.Tokenizer; | ||
import org.supercsv.prefs.CsvPreference; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
|
||
/** | ||
* <p> This class is a custom implementation of the Tokenizer interface | ||
* that allows to parse CSV and TSV files with invalid quoting. | ||
* Any quotes not next to a delimiter or at the start/end of the line should be escaped.</p> | ||
* <p>Example of invalid quoting in the CSV: | ||
* <table> | ||
* <thead> | ||
* <td>Company Name</td> | ||
* <td>Product</td> | ||
* </thead> | ||
* <tr> | ||
* <td>"Albanese Confectionery", </td> | ||
* <td>"FRUIT WORMS 2" 4/5LB"</td> | ||
* <td> <--- Invalid quoting</td> | ||
* </tr> | ||
* <tr> | ||
* <td>"Albanese Confectionery", </td> | ||
* <td>"FRUIT WORMS 2"" 4/5LB"</td> | ||
* <td><--- Valid quoting</td> | ||
* </tr> | ||
* </table> | ||
* </p> | ||
* <p> The tokenizer is compliant with following formats: CSV, TSV </p> | ||
* <p>Notes: | ||
* In the TSV standard, there is no mention of quotes, but in this implementation, we process | ||
* the TSV quotes the same way as the CSV quotes. | ||
* </p> | ||
* @see <a href="https://www.rfc-editor.org/rfc/rfc4180">CSV</a> | ||
* @see <a href="https://www.iana.org/assignments/media-types/text/tab-separated-values">TSV</a> | ||
*/ | ||
public class InvalidQuotingTokenizer extends Tokenizer { | ||
|
||
public InvalidQuotingTokenizer(Reader reader, CsvPreference preferences) { | ||
super(reader, preferences); | ||
} | ||
|
||
@Override | ||
protected String readLine() throws IOException { | ||
final String line = super.readLine(); | ||
if (line == null) { | ||
return null; | ||
} | ||
|
||
final char quote = getPreferences().getQuoteChar(); | ||
final char delimiter = (char) getPreferences().getDelimiterChar(); | ||
|
||
// escape all quotes not next to a delimiter (or start/end of line) | ||
final StringBuilder b = new StringBuilder(line); | ||
for (int i = b.length() - 1; i >= 0; i--) { | ||
if (quote == b.charAt(i)) { | ||
final boolean validCharBefore = i - 1 < 0 | ||
|| b.charAt(i - 1) == delimiter; | ||
final boolean validCharAfter = i + 1 == b.length() | ||
|| b.charAt(i + 1) == delimiter; | ||
if (!(validCharBefore || validCharAfter)) { | ||
// escape that quote! | ||
b.insert(i, quote); | ||
} | ||
} | ||
} | ||
return b.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters