/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. Default values for other parameters: * <ul> * <li><code>recordSeparator</code> - <code>"\n"</code></li> * <li><code>trim</code> - <code>true</code></li> * <li><code>quote</code> - <code>null</code> (disabled)</li> * <li><code>commentMarker</code> - <code>'#'</code></li> * <li><code>ignoreEmptyLines</code> - <code>true</code></li> * <li><code>nullString</code> - <code>null</code> (disabled)</li> * </ul> * * @param path Path to the CSV file. * @param delimiter Delimiter of columns within CSV file. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path, char delimiter) { return wrap(new CsvReaderValue(new CSVParserSettings(path, delimiter))); }
/** * Constructs composite value with specified initial child values. * * @param parserSettings Settings for the CSV parser. */ public CsvReaderValue(CSVParserSettings parserSettings) { if (parserSettings == null) { throw new IllegalArgumentException("parserSettings cannot be null."); } this.evaluatedValues = new HashMap<>(); this.csvParser = createCSVParser(parserSettings); this.iterator = csvParser.iterator(); }
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }
private CSVParser createCSVParser(CSVParserSettings parserSettings) { CSVFormat csvFormat = CSVFormat.DEFAULT.withDelimiter(parserSettings.getDelimiter()) .withRecordSeparator(parserSettings.getRecordSeparator()) .withTrim(parserSettings.isTrim()) .withQuote(parserSettings.getQuote()) .withCommentMarker(parserSettings.getCommentMarker()) .withIgnoreEmptyLines(parserSettings.isIgnoreEmptyLines()) .withNullString(parserSettings.getNullString()); try { File file = new File(parserSettings.getPath()); return new CSVParser(new FileReader(file), csvFormat); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e); } } }
/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. Default values for other parameters: * <ul> * <li><code>delimiter</code> - <code>','</code></li> * <li><code>recordSeparator</code> - <code>"\n"</code></li> * <li><code>trim</code> - <code>true</code></li> * <li><code>quote</code> - <code>null</code> (disabled)</li> * <li><code>commentMarker</code> - <code>'#'</code></li> * <li><code>ignoreEmptyLines</code> - <code>true</code></li> * <li><code>nullString</code> - <code>null</code> (disabled)</li> * </ul> * * @param path Path to the CSV file. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path) { return wrap(new CsvReaderValue(new CSVParserSettings(path))); }
/** * Constructs composite value with specified initial child values. * * @param parserSettings Settings for the CSV parser. */ public CsvReaderValue(CSVParserSettings parserSettings) { if (parserSettings == null) { throw new IllegalArgumentException("parserSettings cannot be null."); } this.evaluatedValues = new HashMap<>(); this.csvParser = createCSVParser(parserSettings); this.iterator = csvParser.iterator(); }
/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. Default values for other parameters: * <ul> * <li><code>recordSeparator</code> - <code>"\n"</code></li> * <li><code>trim</code> - <code>true</code></li> * <li><code>quote</code> - <code>null</code> (disabled)</li> * <li><code>commentMarker</code> - <code>'#'</code></li> * <li><code>ignoreEmptyLines</code> - <code>true</code></li> * <li><code>nullString</code> - <code>null</code> (disabled)</li> * </ul> * * @param path Path to the CSV file. * @param delimiter Delimiter of columns within CSV file. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path, char delimiter) { return wrap(new CsvReaderValue(new CSVParserSettings(path, delimiter))); }
/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. Default values for other parameters: * <ul> * <li><code>delimiter</code> - <code>','</code></li> * <li><code>recordSeparator</code> - <code>"\n"</code></li> * <li><code>trim</code> - <code>true</code></li> * <li><code>quote</code> - <code>null</code> (disabled)</li> * <li><code>commentMarker</code> - <code>'#'</code></li> * <li><code>ignoreEmptyLines</code> - <code>true</code></li> * <li><code>nullString</code> - <code>null</code> (disabled)</li> * </ul> * * @param path Path to the CSV file. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path) { return wrap(new CsvReaderValue(new CSVParserSettings(path))); }
/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. * * @param path Path to the CSV file. * @param delimiter Delimiter of columns within CSV file. * @param recordSeparator Delimiter of records within CSV file. * @param trim True if each column value is to be trimmed for leading and trailing whitespace, otherwise * <code>false</code>. * @param quote Character that will be stripped from beginning and end of each column if present. If set to * <code>null</code>, no characters will be stripped (nothing will be used as quote character). * @param commentMarker Character to use as a comment marker, everything after it is considered comment. * @param ignoreEmptyLines True if empty lines are to be ignored, otherwise <code>false</code>. * @param nullString Converts string with given value to <code>null</code>. If set to <code>null</code>, no * conversion will be done. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path, char delimiter, String recordSeparator, boolean trim, Character quote, char commentMarker, boolean ignoreEmptyLines, String nullString) { return wrap(new CsvReaderValue(new CSVParserSettings(path, delimiter, recordSeparator, trim, quote, commentMarker, ignoreEmptyLines, nullString))); }
/** * Creates an instance of {@link ObjectGenerator} which reads CSV file and returns new record on each iteration * until CSV file is depleted. * * @param path Path to the CSV file. * @param delimiter Delimiter of columns within CSV file. * @param recordSeparator Delimiter of records within CSV file. * @param trim True if each column value is to be trimmed for leading and trailing whitespace, otherwise * <code>false</code>. * @param quote Character that will be stripped from beginning and end of each column if present. If set to * <code>null</code>, no characters will be stripped (nothing will be used as quote character). * @param commentMarker Character to use as a comment marker, everything after it is considered comment. * @param ignoreEmptyLines True if empty lines are to be ignored, otherwise <code>false</code>. * @param nullString Converts string with given value to <code>null</code>. If set to <code>null</code>, no * conversion will be done. * @return An instance of {@link ObjectGenerator} which reads CSV file. */ public static ObjectGenerator<Map<String, String>> csv(String path, char delimiter, String recordSeparator, boolean trim, Character quote, char commentMarker, boolean ignoreEmptyLines, String nullString) { return wrap(new CsvReaderValue(new CSVParserSettings(path, delimiter, recordSeparator, trim, quote, commentMarker, ignoreEmptyLines, nullString))); }
/** * Creates CSV value. * * @return An CSV value. */ protected CsvReaderValue createCsvReaderValue() { CSVParserSettings parserSettings = null; switch (getContext().getValueStack().size()) { case 1: parserSettings = new CSVParserSettings((String) pop()); break; case 2: parserSettings = new CSVParserSettings((String) pop(1), (char) pop()); break; case 8: parserSettings = new CSVParserSettings((String) pop(7), (char) pop(6), (String) pop(5), (boolean) pop(4), peek(3) instanceof NullValue ? null : (Character) pop(3), (char) pop(2), (boolean) pop(1), peek() instanceof NullValue ? null : (String) pop()); break; default: throw new RuntimeException("Unsupported number of parameters, should not happen ever."); } return new CsvReaderValue(parserSettings); }
/** * Creates CSV value. * * @return An CSV value. */ protected CsvReaderValue createCsvReaderValue() { CSVParserSettings parserSettings = null; switch (getContext().getValueStack().size()) { case 1: parserSettings = new CSVParserSettings((String) pop()); break; case 2: parserSettings = new CSVParserSettings((String) pop(1), (char) pop()); break; case 8: parserSettings = new CSVParserSettings((String) pop(7), (char) pop(6), (String) pop(5), (boolean) pop(4), peek(3) instanceof NullValue ? null : (Character) pop(3), (char) pop(2), (boolean) pop(1), peek() instanceof NullValue ? null : (String) pop()); break; default: throw new RuntimeException("Unsupported number of parameters, should not happen ever."); } return new CsvReaderValue(parserSettings); }