Class IQuestionCsvParser
- Author:
- jhuth
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classWrapper for enum to make different enum states possible; -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static IQuestioncsvRowToQuestion(String[] csvRow, IQuestionCsvParser.Column... columns)Works as described incsvToQuestionList(CSVReader, Column...)csvToQuestionList(au.com.bytecode.opencsv.CSVReader reader, IQuestionCsvParser.Column... columns)Reads a CSV which source is defined by givenCSVReader, which also defines the separation and quotation chars.private static BooleanparseBoolean(String boolStr)Parses a boolean.parseStringArray(String arrayStr)Splits a string onARRAY_SPLIT.static voidquestionListToCsv(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.CSVReaderreaderForGoogleDocsCsvExports(Reader reader, int skipLines)Returns reader which uses comma(,) as separator and (") as quote.
-
Field Details
-
ARRAY_SPLIT
Some Columns can hold multiple values. So, a String representing this column content will be split on this char. -
NULL_DEFAULT
The value that will be written if a column is declared for a csv but corresponding field in IQuestion is null
-
-
Constructor Details
-
IQuestionCsvParser
public IQuestionCsvParser()
-
-
Method Details
-
csvRowToQuestion
private static IQuestion csvRowToQuestion(String[] csvRow, IQuestionCsvParser.Column... columns) throws IOExceptionWorks as described incsvToQuestionList(CSVReader, Column...)- Parameters:
csvRow- A row of a csv, with decoded terminators and split upcolumns-- Returns:
- Throws:
IOException
-
csvToQuestionList
public static List<IQuestion> csvToQuestionList(au.com.bytecode.opencsv.CSVReader reader, IQuestionCsvParser.Column... columns) throws IOExceptionReads a CSV which source is defined by givenCSVReader, which also defines the separation and quotation chars.CSVReadercan also be set up to skip the first n lines.Define the Column structure if the CSV by passig
IQuestionCsvParser.ColumnObjects. The order has to fit the column order of the csv.For example, following call:
csvToQuestionList(reader,{@link 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.Columnpresent) will be ignored, also, additionally definedIQuestionCsvParser.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 viaARRAY_SPLITArrays can be preceeded and concluded by square and curly brackets. So, a toString on most Collections would be a valid input format.- Parameters:
reader- A CSVReader fitting to your needs. For GoogleDocs CSV export, you can usereaderForGoogleDocsCsvExports(Reader, int)columns- as described above- Returns:
- A list of IQuestions with the parseable information set.
- Throws:
IOException- if something bad happens
-
questionListToCsv
public static void questionListToCsv(au.com.bytecode.opencsv.CSVWriter writer, boolean columnDescriptorgRow, List<IQuestion> questions, IQuestionCsvParser.Column... columns) throws IOExceptionWrites all data specified by the columns from all given IQuestions to the csvwriter. Get more information from the doc ofcsvToQuestionList(CSVReader, Column...)If or a declared
IQuestionCsvParser.Columnno information in IQuestion is present(e.g. field is null),NULL_DEFAULTwill be written.- Parameters:
writer-columnDescriptorgRow- - the first row will contain the names of the columnsquestions-columns-- Throws:
IOException
-
parseBoolean
Parses a boolean. Difference toBoolean.parseBoolean(String)is that no other values than {"true","false"} are accepted, ignoring case.- Parameters:
boolStr-- Returns:
- a parsed boolean
- Throws:
IOException- if boolStr not in {"true","false"}, ignoring case
-
parseStringArray
Splits a string onARRAY_SPLIT. First and last char can be square or curly brackets, they will be removed- Parameters:
arrayStr-- Returns:
-
readerForGoogleDocsCsvExports
public static au.com.bytecode.opencsv.CSVReader readerForGoogleDocsCsvExports(Reader reader, int skipLines)Returns reader which uses comma(,) as separator and (") as quote. Sufficient to read data which was generated with googleDocs csv-export.- Parameters:
reader-skipLines- skip the first n lines- Returns:
-