/** * Returns this CSV format configuration. * @param head whether configure for head of file or not * @return CSV format configuration */ protected CsvConfiguration getConfiguration(boolean head) { List<String> headers = new ArrayList<>(); if(head) { headers.add("店舗コード"); headers.add("名称"); } CsvConfiguration config = new CsvConfiguration(Charset.forName("UTF-8"), headers, "true", "false", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); config.setLineBreakInValue(false); return config; } @Override
/** * Creates a new instance. * @param stream the source stream * @param path the source path * @param config current configuration * @throws IllegalArgumentException if some parameters were {@code null} */ public CsvParser(InputStream stream, String path, CsvConfiguration config) { if (stream == null) { throw new IllegalArgumentException("stream must not be null"); //$NON-NLS-1$ } if (config == null) { throw new IllegalArgumentException("config must not be null"); //$NON-NLS-1$ } this.reader = new InputStreamReader(stream, config.getCharset()); this.path = path; this.separator = config.getSeparatorChar(); this.trueFormat = config.getTrueFormat(); this.dateFormat = DateFormatter.newInstance(config.getDateFormat()); this.dateTimeFormat = DateTimeFormatter.newInstance(config.getDateTimeFormat()); this.headerCellsFormat = config.getHeaderCells(); this.forceConsumeHeader = config.isForceConsumeHeader(); this.allowLineBreakInValue = config.isLineBreakInValue(); readerBuffer.clear(); readerBuffer.flip(); }
private Serializer getSerializer() { CsvConfiguration config = new CsvConfiguration(); config.setFieldDelimiter(';'); config.setDefaultQuoteMode(QuoteMode.NEVER); //!!!! return CsvIOFactory.createFactory(config, MyCsvDto.class).createSerializer(); }
/** * Creates a new instance. * @param stream the target stream * @param path the destination path * @param config current configuration * @throws IllegalArgumentException if some parameters were {@code null} */ public CsvEmitter(OutputStream stream, String path, CsvConfiguration config) { if (stream == null) { throw new IllegalArgumentException("stream must not be null"); //$NON-NLS-1$ } if (config == null) { throw new IllegalArgumentException("config must not be null"); //$NON-NLS-1$ } this.writer = new OutputStreamWriter(stream, config.getCharset()); this.separator = config.getSeparatorChar(); this.escapePattern = Pattern.compile( "[" + ESCAPE + separator + LINE_DELIMITER + "]"); //$NON-NLS-1$ //$NON-NLS-2$ this.trueFormat = escape(config.getTrueFormat()); this.escapeTrue = hasEscapeTarget(config.getTrueFormat()); this.falseFormat = escape(config.getFalseFormat()); this.escapeFalse = hasEscapeTarget(config.getFalseFormat()); this.dateFormat = DateFormatter.newInstance(config.getDateFormat()); this.escapeDate = hasMetaCharacter(dateFormat.getPattern()); this.dateTimeFormat = DateTimeFormatter.newInstance(config.getDateTimeFormat()); this.escapeDateTime = hasMetaCharacter(dateTimeFormat.getPattern()); this.headerCellsFormat = config.getHeaderCells(); this.forceEscapeMask = buildQuoteMask(config.getForceQuoteColumns()); this.columnIndex = 0; }
public static Object[][] getCsvData( File csvFile ) { CsvConfiguration conf = new CsvConfiguration( 1 ); DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf ); Schema schema = csvContext.getDefaultSchema(); Table[] tables = schema.getTables(); Table table = tables[0]; // a representation of the csv file name including extension DataSet dataSet = csvContext.query() .from( table ) .selectAll() .where("run").eq("Y") .execute(); List<Row> rows = dataSet.toRows(); Object[][] myArray = get2ArgArrayFromRows( rows ); return myArray; }
private String[][] parse(int columns, String string) { CsvConfiguration conf = new CsvConfiguration( CsvConfiguration.DEFAULT_CHARSET, CsvConfiguration.DEFAULT_HEADER_CELLS, CsvConfiguration.DEFAULT_TRUE_FORMAT, CsvConfiguration.DEFAULT_FALSE_FORMAT, CsvConfiguration.DEFAULT_DATE_FORMAT, CsvConfiguration.DEFAULT_DATE_TIME_FORMAT); List<String[]> results = new ArrayList<>(); ByteArrayInputStream input = new ByteArrayInputStream(string.getBytes(conf.getCharset())); try (CsvParser parser = new CsvParser(input, string, conf)) { StringOption buffer = new StringOption(); while (parser.next()) { String[] line = new String[columns]; for (int i = 0; i < columns; i++) { parser.fill(buffer); line[i] = buffer.or((String) null); } parser.endRecord(); results.add(line); } } catch (Exception e) { throw new AssertionError(e); } return results.toArray(new String[results.size()][]); }
private CsvParser create(String content) { CsvConfiguration conf = createConfiguration(); return new CsvParser( new ByteArrayInputStream(content.getBytes(conf.getCharset())), testName.getMethodName(), conf); }
private CsvConfiguration createConfiguration() { CsvConfiguration conf = new CsvConfiguration( CsvConfiguration.DEFAULT_CHARSET, headers, trueFormat, falseFormat, dateFormat, dateTimeFormat); conf.setForceQuoteColumns(quote.stream().mapToInt(i -> i).toArray()); return conf; }
CsvConfiguration config = new CsvConfiguration(); config.setFieldDelimiter(','); return config;
private CsvConfiguration createConfiguration() { CsvConfiguration conf = new CsvConfiguration( CsvConfiguration.DEFAULT_CHARSET, headers, trueFormat, falseFormat, dateFormat, dateTimeFormat); return conf; }
/** * Simple stress test for {@link DecimalOption} type. * @throws Exception if failed */ @Test public void stress_decimal() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("3.141592\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DecimalOption date = new DecimalOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DecimalOption(new BigDecimal("3.141592")))); } rows++; } assertThat(rows, is(count)); } }
/** * Returns this CSV format configuration. * @param head whether configure for head of file or not * @return CSV format configuration */ protected CsvConfiguration getConfiguration(boolean head) { List<String> headers = new ArrayList<>(); if(head) { headers.add("カテゴリコード"); headers.add("販売数量"); headers.add("売上合計"); } CsvConfiguration config = new CsvConfiguration(Charset.forName("UTF-8"), headers, "true", "false", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); config.setLineBreakInValue(false); return config; } @Override
public static Object[][] getCsvData(File csvFile) { CsvConfiguration conf = new CsvConfiguration(1); DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf); Schema schema = csvContext.getDefaultSchema(); Table[] tables = schema.getTables(); Table table = tables[0]; DataSet dataSet = csvContext.query().from(table).selectAll().where("run").eq("Y").execute(); List<Row> rows = dataSet.toRows(); Object[][] myArray = new Object[rows.size()][2]; int i = 0; SelectItem[] cols = rows.get(0).getSelectItems(); for (Row r : rows) { Object[] data = r.getValues(); for (int j = 0; j < cols.length; j++) { if (data[j] == null) data[j] = ""; // force empty string where there are NULL // values } myArray[i][0] = cols; myArray[i][1] = data; i++; } logger.info("Row count: " + rows.size()); logger.info("Column names: " + Arrays.toString(cols)); return myArray; }
/** * Simple stress test for {@link Date} type. * @throws Exception if failed */ @Test public void stress_date() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("1999-12-31\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DateOption date = new DateOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DateOption(new Date(1999, 12, 31)))); } rows++; } parser.close(); assertThat(rows, is(count)); } }
/** * Returns this CSV format configuration. * @param head whether configure for head of file or not * @return CSV format configuration */ protected CsvConfiguration getConfiguration(boolean head) { List<String> headers = new ArrayList<>(); if(head) { headers.add("ファイル名"); headers.add("日時"); headers.add("店舗コード"); headers.add("商品コード"); headers.add("メッセージ"); } CsvConfiguration config = new CsvConfiguration(Charset.forName("UTF-8"), headers, "true", "false", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); config.setLineBreakInValue(false); return config; } @Override
CsvConfiguration conf = new CsvConfiguration( 1 ); DataContext csvContext = DataContextFactory.createCsvDataContext( csvFile, conf ); Schema schema = csvContext.getDefaultSchema();
/** * Simple stress test for {@link DateTime} type. * @throws Exception if failed */ @Test public void stress_datetime() throws Exception { int count = 5000000; CsvConfiguration conf = createConfiguration(); try (RCReader reader = new RCReader("1999-12-31 01:23:45\r\n".getBytes(conf.getCharset()), count); CsvParser parser = new CsvParser(reader, "testing", conf)) { int rows = 0; DateTimeOption date = new DateTimeOption(); while (parser.next()) { parser.fill(date); parser.endRecord(); if (rows == 0) { assertThat(date, is(new DateTimeOption(new DateTime(1999, 12, 31, 1, 23, 45)))); } rows++; } parser.close(); assertThat(rows, is(count)); } }
/** * Returns this CSV format configuration. * @param head whether configure for head of file or not * @return CSV format configuration */ protected CsvConfiguration getConfiguration(boolean head) { List<String> headers = new ArrayList<>(); if(head) { headers.add("日時"); headers.add("店舗コード"); headers.add("商品コード"); headers.add("数量"); headers.add("販売単価"); headers.add("販売金額"); } CsvConfiguration config = new CsvConfiguration(Charset.forName("UTF-8"), headers, "true", "false", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); config.setLineBreakInValue(false); return config; } @Override
/** * Returns this CSV format configuration. * @param head whether configure for head of file or not * @return CSV format configuration */ protected CsvConfiguration getConfiguration(boolean head) { List<String> headers = new ArrayList<>(); if(head) { headers.add("商品コード"); headers.add("商品名"); headers.add("部門コード"); headers.add("部門名"); headers.add("カテゴリコード"); headers.add("カテゴリ名"); headers.add("単価"); headers.add("登録日"); headers.add("適用開始日"); headers.add("適用終了日"); } CsvConfiguration config = new CsvConfiguration(Charset.forName("UTF-8"), headers, "true", "false", "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss"); config.setLineBreakInValue(false); return config; } @Override