public class IQuestionCsvParser extends Object
| Modifier and Type | Class and Description |
|---|---|
(package private) static class |
IQuestionCsvParser.Column
Wrapper for enum to make different enum states possible;
|
| Modifier and Type | Field and Description |
|---|---|
static String |
ARRAY_SPLIT
Some Columns can hold multiple values.
|
static String |
NULL_DEFAULT
The value that will be written if a column is declared for a csv but corresponding field in IQuestion is null
|
| Constructor and Description |
|---|
IQuestionCsvParser() |
| Modifier and Type | Method and Description |
|---|---|
private static IQuestion |
csvRowToQuestion(String[] csvRow,
IQuestionCsvParser.Column... columns)
Works as described in
csvToQuestionList(CSVReader, Column...) |
static List<IQuestion> |
csvToQuestionList(au.com.bytecode.opencsv.CSVReader reader,
IQuestionCsvParser.Column... columns)
Reads a CSV which source is defined by given
CSVReader, which also defines the separation and quotation chars. |
private static Boolean |
parseBoolean(String boolStr)
Parses a boolean.
|
private static List<String> |
parseStringArray(String arrayStr)
Splits a string on
ARRAY_SPLIT. |
static void |
questionListToCsv(au.com.bytecode.opencsv.CSVWriter writer,
boolean columnDescriptorgRow,
List<IQuestion> questions,
IQuestionCsvParser.Column... columns)
Writes all data specified by the columns from all given IQuestions to the csvwriter.
|
static au.com.bytecode.opencsv.CSVReader |
readerForGoogleDocsCsvExports(Reader reader,
int skipLines)
Returns reader which uses comma(,) as separator and (") as quote.
|
public static String ARRAY_SPLIT
public static String NULL_DEFAULT
private static IQuestion csvRowToQuestion(String[] csvRow, IQuestionCsvParser.Column... columns) throws IOException
csvToQuestionList(CSVReader, Column...)csvRow - A row of a csv, with decoded terminators and split upcolumns - IOExceptionpublic static List<IQuestion> csvToQuestionList(au.com.bytecode.opencsv.CSVReader reader, IQuestionCsvParser.Column... columns) throws IOException
CSVReader, which also defines the separation and quotation chars. CSVReader can also be set up to skip the first n lines.
Define the Column structure if the CSV by passig IQuestionCsvParser.Column Objects. The order has to fit the column order of the csv.
For example, following call:
csvToQuestionList(reader,Column.ID(), Column.ignore(), Column.question("en") )
Will parse the first columnn of the csv to IQuestion#setId() , ignore the second column, and will parse column 3 as question in english.
Trailing csv columns (no IQuestionCsvParser.Column present) will be ignored, also, additionally defined IQuestionCsvParser.Columns will be ignored.
IQuestionCsvParser.Columns can appear multiple times. For a column with one value(e.g. a boolean flag, sparql query) the rightmost column is used. For Columns holding possible array contents (e.g. golden
answers, keywords), all defined columns will be parsed.
Some columns can hold arrays, e.g. IQuestionCsvParser.Column.goldenAnswers(), IQuestionCsvParser.Column.keywords(String). The array separator string can be set via ARRAY_SPLIT Arrays can be preceeded and
concluded by square and curly brackets. So, a toString on most Collections would be a valid input format.
reader - A CSVReader fitting to your needs. For GoogleDocs CSV export, you can use readerForGoogleDocsCsvExports(Reader, int)columns - as described aboveIOException - if something bad happenspublic static void questionListToCsv(au.com.bytecode.opencsv.CSVWriter writer,
boolean columnDescriptorgRow,
List<IQuestion> questions,
IQuestionCsvParser.Column... columns)
throws IOException
csvToQuestionList(CSVReader, Column...)
If or a declared IQuestionCsvParser.Column no information in IQuestion is present(e.g. field is null), NULL_DEFAULT will be written.
writer - columnDescriptorgRow - - the first row will contain the names of the columnsquestions - columns - IOExceptionprivate static Boolean parseBoolean(String boolStr) throws IOException
Boolean.parseBoolean(String) is that no other values than {"true","false"} are accepted, ignoring case.boolStr - IOException - if boolStr not in {"true","false"}, ignoring caseprivate static List<String> parseStringArray(String arrayStr)
ARRAY_SPLIT. First and last char can be square or curly brackets, they will be removedarrayStr - public static au.com.bytecode.opencsv.CSVReader readerForGoogleDocsCsvExports(Reader reader, int skipLines)
reader - skipLines - skip the first n linesCopyright © 2016–2020. All rights reserved.