org.supercsv.prefs.CsvPreference Java Examples

The following examples show how to use org.supercsv.prefs.CsvPreference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AirlineCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
public List<Airline> deserialize(InputStream in) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    ICsvBeanReader csvBeanReader = new CsvBeanReader(inputStreamReader, CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {null, "name", "alias", "iataCode", "icaoCode", "callsign", "country", null};
    Airline airline = null;
    List<Airline> airlines = new ArrayList<>();
    try {

        while ((airline = csvBeanReader.read(Airline.class, header, getCellProcessors())) != null) {
            System.out.println("deserialized " + airline);
            airlines.add(airline);
        }
    } finally {
        if (csvBeanReader != null) {
            csvBeanReader.close();
        }
    }
    return airlines;
}
 
Example #2
Source File: CsvUtil.java    From openscoring with GNU Affero General Public License v3.0 6 votes vote down vote up
static
public CsvPreference getFormat(BufferedReader reader) throws IOException {
	reader.mark(10 * 1024);

	for(int i = 0; i < CsvUtil.DELIMITERS.length; i++){
		char delimiter = CsvUtil.DELIMITERS[i];

		try {
			CsvPreference format = createFormat(delimiter, '\"');

			if(checkFormat(reader, format)){
				return format;
			}
		} finally {
			reader.reset();
		}
	}

	throw new IOException("Unrecognized CSV format");
}
 
Example #3
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
/**
 * 全件読み込み(正常系のテスト)
 */
@Test
public void testReadAll_normal() throws IOException {
    
    File file = new File("src/test/data/test_read_normal.csv");
    
    CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
            SampleNormalBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE,
            DefaultGroup.class, SampleNormalBean.ReadGroup.class);
    csvReader.setExceptionConverter(exceptionConverter);
    
    List<SampleNormalBean> list = csvReader.readAll();
    assertThat(list).hasSize(2);
    
    for(SampleNormalBean bean : list) {
        assertBean(bean);
    }
    
    assertThat(csvReader.getErrorMessages()).hasSize(0);
    
    csvReader.close();
    
}
 
Example #4
Source File: FlowSummaryWithTypesAndPhysicalsExport.java    From waltz with Apache License 2.0 6 votes vote down vote up
private static CsvListWriter setupCSVWriter() throws IOException {
    CsvListWriter csvWriter = new CsvListWriter(new OutputStreamWriter(new FileOutputStream("/temp/flows.csv")), CsvPreference.EXCEL_PREFERENCE);
    csvWriter.write(
            "Source App",
            "Source Asset Code",
            "Source App Kind",
            "Source App Status",
            "Source App Org Unit", // src OU
            "Source In Scope",
            "Target App",
            "Target Asset Code",
            "Target App Kind",
            "Target App Status",
            "Target App Org Unit", // src OU
            "Target In Scope",
            "Data Types",
            "Physical Name",
            "Physical ExtId",
            "Physical Transport",
            "Physical Format",
            "Physical Frequency",
            "Criticality");
    return csvWriter;
}
 
Example #5
Source File: AbstractCsvWritingBenchmark.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Times CsvDozerBeanWriter.
 * 
 * @param writer
 *            the writer
 * @param preference
 *            the preferences
 * @param header
 *            the header
 * @param beanClass
 *            the type of the bean being written
 * @param data
 *            the data to write
 * @param rows
 *            the expected number of rows
 * @throws Exception
 */
public void timeCsvDozerBeanWriter(final Writer writer,
		final CsvPreference preference, final String[] header,
		final Class<?> beanClass, final List<?> data, final int rows)
		throws Exception {

	ICsvDozerBeanWriter dozerBeanWriter = null;
	try {
		dozerBeanWriter = new CsvDozerBeanWriter(writer, preference);
		dozerBeanWriter.writeHeader(header);
		dozerBeanWriter.configureBeanMapping(beanClass, header);

		for (Object o : data) {
			dozerBeanWriter.write(o);
		}

		assertEquals(rows + 1, dozerBeanWriter.getRowNumber());

	} finally {
		dozerBeanWriter.close();
	}
}
 
Example #6
Source File: CsvTypedBeanWriterTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void writesTypedBeanToCsv() throws Exception {
    try (
        final StringWriter writer = new StringWriter();
        final ICsvTypedBeanWriter<CsvTypedBeanWriterTest.FakeBean> beanWriter =
            new CsvTypedBeanWriter<>(writer, CsvPreference.STANDARD_PREFERENCE)
    ) {
        final Collection<Function<FakeBean, ?>> extractors =
            Arrays.asList(
                FakeBean::bool,
                FakeBean::stringField,
                FakeBean::collection,
                FakeBean::decimal
            );
        final FakeBean bean = new FakeBean("Oh, String!", false,
            BigDecimal.TEN, Arrays.asList("Foo", "Bar", "Bazz"));
        beanWriter.writeHeader(CsvTypedBeanWriterTest.headers());
        beanWriter.write(Collections.singleton(bean), extractors);
        beanWriter.flush();
        Assert.assertEquals(
            CsvTypedBeanWriterTest.csv(CsvTypedBeanWriterTest.headers(), bean),
            writer.toString()
        );
    }
}
 
Example #7
Source File: MainController.java    From video-streaming-service with MIT License 6 votes vote down vote up
@RequestMapping("download-csv/{id}")
public void downloadCSV(HttpServletResponse response, @PathVariable("id") Long id) throws IOException {
    final Material material = materialRepository.findOne(id);
    if (material == null)
        throw new IllegalArgumentException("[" + id + "] data is not exist.");

    String videoName = material.getVideoName();
    int lastIndexOf = videoName.lastIndexOf("/");
    videoName = lastIndexOf >= 0 ? videoName.substring(lastIndexOf + 1, videoName.length()) : videoName;
    final String csvFileName = videoName + "_" + (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(material.getCreatedAt())) + ".csv";

    response.setContentType("text/csv");

    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", csvFileName);
    response.setHeader(headerKey, headerValue);

    final ICsvBeanWriter csvWriter = new CsvBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {"timestamp", "key"};
    csvWriter.writeHeader(header);

    for (MaterialData data : material.getMaterialDataList())
        csvWriter.write(data, header);

    csvWriter.close();
}
 
Example #8
Source File: ConnectionCsvDeserializer.java    From Showcase with Apache License 2.0 6 votes vote down vote up
public List<FlightConnection> deserialize(InputStream in) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    ICsvBeanReader csvBeanReader = new CsvBeanReader(inputStreamReader, CsvPreference.STANDARD_PREFERENCE);
    final String[] header = {"airlineIataCode", null, "sourceAirportIataCode", null, "destinationAirportIataCode", null, "codeshare", "stops", null};
    FlightConnection flightConnection = null;
    List<FlightConnection> flightConnections = new ArrayList<>();
    try {

        while ((flightConnection = csvBeanReader.read(FlightConnection.class, header, getCellProcessors())) != null) {
            System.out.println("deserialized " + flightConnection);
            flightConnections.add(flightConnection);
        }
    } finally {
        if (csvBeanReader != null) {
            csvBeanReader.close();
        }
    }
    return flightConnections;
}
 
Example #9
Source File: TokenizerTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the CommentMatches comment matcher works (comments are skipped).
 */
@Test
public void testSkipCommentsMatches() throws IOException {
	
	final CsvPreference commentsMatchesPrefs = new CsvPreference.Builder(EXCEL_PREFERENCE).skipComments(
		new CommentMatches("<!--.*-->")).build();
	
	final String input = "<!--comment-->\nnot,a,comment\n<!-- another comment-->\nalso,not,comment";
	final Tokenizer tokenizer = createTokenizer(input, commentsMatchesPrefs);
	tokenizer.readColumns(columns);
	assertTrue(columns.size() == 3);
	assertEquals("not", columns.get(0));
	assertEquals("a", columns.get(1));
	assertEquals("comment", columns.get(2));
	
	tokenizer.readColumns(columns);
	assertTrue(columns.size() == 3);
	assertEquals("also", columns.get(0));
	assertEquals("not", columns.get(1));
	assertEquals("comment", columns.get(2));
	
	assertFalse(tokenizer.readColumns(columns));
	
}
 
Example #10
Source File: CsvDownload.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
public void download(List<T> data, String fileName, HttpServletResponse response, Class<T> clazz) throws IOException, SecurityException, NoSuchMethodException {
	String headerKey = "Content-Disposition";
	String headerValue = String.format("attachment; filename=\"%s\"", fileName);

	response.setContentType("text/csv");
	response.setHeader(headerKey, headerValue);
	
	String[] header = createHeader(clazz);
	
	ICsvDozerBeanWriter csvWriter = new CsvDozerBeanWriter(response.getWriter(), CsvPreference.STANDARD_PREFERENCE);
	csvWriter.configureBeanMapping(clazz, header);
	csvWriter.writeHeader(header);
	
	for (T t : data) {
		csvWriter.write(t);
	}
	
	csvWriter.close();
}
 
Example #11
Source File: CallbackMethodTest.java    From super-csv-annotation with Apache License 2.0 6 votes vote down vote up
@Test
public void testRead() throws Exception {
    
    CsvAnnotationBeanReader<TestCsv> csvReader = new CsvAnnotationBeanReader<>(TestCsv.class, new StringReader(TEST_CSV),
            CsvPreference.STANDARD_PREFERENCE, Group1.class, Group2.class);
    
    List<TestCsv> list = csvReader.readAll(false);
    for(TestCsv record : list) {
        assertThat(record.messages).containsExactly(
                "method::handlePreRead-null",
                "listener::handlePreRead-null",
                "validate::validate-" + record.getId(),
                "method::handlePostRead-" + record.getId(),
                "listener::handlePostRead-" + record.getId());
    }
    
    csvReader.close();
    
}
 
Example #12
Source File: AbstractCsvReadingBenchmark.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Times CsvDozerBeanReader.
 * 
 * @param reader
 *            the CSV reader
 * @param preference
 *            the preferences
 * @param beanClass
 *            the type of the bean class
 * @param rows
 *            the number of rows to read
 * @throws Exception
 */
public void timeCsvDozerBeanReader(final Reader reader,
		final CsvPreference preference, final Class<?> beanClass,
		final int rows) throws Exception {

	ICsvDozerBeanReader dozerBeanReader = null;
	try {
		dozerBeanReader = new CsvDozerBeanReader(reader, preference);

		final String[] header = dozerBeanReader.getHeader(true);
		assertNotNull(header);

		dozerBeanReader.configureBeanMapping(beanClass, header);

		for (int j = 0; j < rows; j++) {
			final Object bean = dozerBeanReader.read(beanClass);
			assertNotNull(bean);
		}

		assertEquals(rows + 1, dozerBeanReader.getRowNumber());

	} finally {
		dozerBeanReader.close();
	}
}
 
Example #13
Source File: SuperCsvBOMTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
public void ReadTestCSVFile(Reader reader) throws IOException {
	ICsvBeanReader beanReader = new CsvBeanReader(reader, CsvPreference.STANDARD_PREFERENCE);
	final String[] header = beanReader.getHeader(true);
	assertEquals("customerNo", header[0]);
	CustomerBean customer = null;
	final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust!
	StrRegEx.registerMessage(emailRegex, "must be a valid email address");
	final CellProcessor[] processors = new CellProcessor[]{new UniqueHashCode(), // customerNo (must be unique)
			new NotNull(), // firstName
			new NotNull(), // lastName
			new ParseDate("dd/MM/yyyy"), // birthDate
			new ParseSqlTime("HH:mm:ss"), // birthTime
			new NotNull(), // mailingAddress
			new Optional(new ParseBool()), // married
			new Optional(new ParseInt()), // numberOfKids
			new NotNull(), // favouriteQuote
			new StrRegEx(emailRegex), // email
			new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	customer = beanReader.read(CustomerBean.class, header, processors);
	assertEquals("1", customer.getCustomerNo());
	assertEquals("John", customer.getFirstName());
	assertEquals("[email protected]", customer.getEmail());
	assertEquals(0, customer.getLoyaltyPoints());
	beanReader.close();
}
 
Example #14
Source File: HistoDbClientImpl.java    From ipst with Mozilla Public License 2.0 6 votes vote down vote up
String[] getColumns(Map<String, String> query) throws IOException, InterruptedException {
    Objects.requireNonNull(query, "query is null");

    Map<String, String> queryMap = new LinkedHashMap<>();
    queryMap.put("headers", "true");
    queryMap.put("count", "1");
    if (query != null) {
        queryMap.putAll(query);
    }

    try (InputStream is =  httpClient.getHttpRequest(new HistoDbUrl(config, "itesla/" + HistoQueryType.data + ".csv", queryMap));
         CsvListReader reader = new CsvListReader(new InputStreamReader(is), CsvPreference.STANDARD_PREFERENCE)) {
        return reader.getHeader(true);
    }

}
 
Example #15
Source File: MathUtil.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
public static Table<String, String, Float> parseMatrix(Reader reader) throws IOException {
    Table<String, String, Float> table = HashBasedTable.create();
    try (ICsvListReader csvReader = new CsvListReader(reader, CsvPreference.STANDARD_PREFERENCE)) {
        List<String> columnHeaders = csvReader.read();
        List<String> row;
        while ((row = csvReader.read()) != null) {
            String rowHeader = row.get(0);
            for (int i = 1; i < row.size(); i++) {
                String columnHeader = columnHeaders.get(i);
                String value = row.get(i);
                table.put(rowHeader, columnHeader, value == null ? Float.NaN : Float.parseFloat(value));
            }
        }
    }
    return table;
}
 
Example #16
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipCommentLines() throws IOException {
	String csv = "<!--Sarah,Connor-->\r\nJohn,Connor\r\n<!--Kyle,Reese-->";
	
	CsvPreference customPreference = new Builder(STANDARD_PREFERENCE).skipComments(new CommentMatches("<!--.*-->"))
		.build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<String> line = listReader.read();
	List<String> emptyLine = listReader.read();
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get(0));
	Assert.assertEquals("Connor", line.get(1));
	Assert.assertNull(emptyLine);
}
 
Example #17
Source File: CsvUtilTest.java    From openscoring with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void getFormat() throws IOException {
	CsvPreference first;
	CsvPreference second;

	String csv = "1\tone\n" +
		"2\ttwo\n" +
		"3\tthree";

	try(BufferedReader reader = new BufferedReader(new StringReader(csv))){
		first = CsvUtil.getFormat(reader);
		second = CsvUtil.getFormat(reader);
	}

	assertNotSame(first.getEncoder(), second.getEncoder());
}
 
Example #18
Source File: LazyCsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 初期化に失敗する場合 - Beanに定義してある情報とCSVの列数が一致しない場合
 */
@Test
public void testInit_noMatchColumnSize() throws Exception {
    
    
    File file = new File("src/test/data/test_read_lazy.csv");
    
    LazyCsvAnnotationBeanReader<SampleLazyBean> csvReader = new LazyCsvAnnotationBeanReader<>(
            SampleLazyBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE);
    csvReader.setExceptionConverter(exceptionConverter);
    
    final String[] headers = new String[]{
            "no",
            "name",
            "生年月日",
            "備考",
            "あああ"
        };
    
    assertThatThrownBy(() -> csvReader.init(headers))
        .isInstanceOf(SuperCsvNoMatchColumnSizeException.class);
    
    assertThat(csvReader.getErrorMessages())
        .hasSize(1)
        .containsExactly("[1行] : 列数が不正です。 4列で設定すべきですが、実際には5列になっています。");
}
 
Example #19
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 全件読み込み - カラムにエラーがある
 */
@Test
public void testReadAll_error_column() throws IOException {
    
    File file = new File("src/test/data/test_read_error_wrong_pattern.csv");
    
    CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
            SampleNormalBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE,
            DefaultGroup.class, SampleNormalBean.ReadGroup.class);
    csvReader.setExceptionConverter(exceptionConverter);
    
    try {
        List<SampleNormalBean> list = csvReader.readAll();
    
    } catch(Exception e) {
        assertThat(e).isInstanceOf(SuperCsvBindingException.class);
    }
    
    // convert error messages.
    List<String> messages = csvReader.getErrorMessages();
    assertThat(messages).hasSize(1)
        .contains("[2行, 6列] : 項目「date1」の値(2000/01/01 00:01:02)の書式は不正です。");
    messages.forEach(System.out::println);
    
    csvReader.close();
    
}
 
Example #20
Source File: SelectiveCsvEncoderTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the encode method when no column numbers are supplied.
 */
@Test
public void testEncodeWithNoColumnNumbers() {
	final SelectiveCsvEncoder encoder = new SelectiveCsvEncoder();
	final CsvContext context = new CsvContext(1, 1, 1);
	assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
}
 
Example #21
Source File: SelectiveCsvEncoderTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests encoding using the supplied encoder.
 * 
 * @param encoder
 *            the encoder
 */
private void testEncode(final CsvEncoder encoder) {
	final CsvContext context = new CsvContext(1, 1, 1);
	assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
	context.setColumnNumber(2);
	assertEquals(ESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
	context.setColumnNumber(3);
	assertEquals(ESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
	context.setColumnNumber(4);
	assertEquals(UNESCAPED, encoder.encode(UNESCAPED, context, CsvPreference.STANDARD_PREFERENCE));
}
 
Example #22
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 全件読み込み - ヘッダーにエラーがある場合
 */
@Test
public void testReadAll_error_header_size() throws IOException {
    
    File file = new File("src/test/data/test_read_error_header_size.csv");
    
    CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
            SampleNormalBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE,
            DefaultGroup.class, SampleNormalBean.ReadGroup.class);
    csvReader.setExceptionConverter(exceptionConverter);
    
    try {
        List<SampleNormalBean> list = csvReader.readAll();
    
    } catch(Exception e) {
        assertThat(e).isInstanceOf(SuperCsvNoMatchColumnSizeException.class);
    }
    
    // convert error messages.
    List<String> messages = csvReader.getErrorMessages();
    assertThat(messages).hasSize(1)
        .contains("[1行] : 列数が不正です。 11列で設定すべきですが、実際には10列になっています。");
    messages.forEach(System.out::println);
    
    csvReader.close();
    
}
 
Example #23
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQuote() throws IOException {
	List<String> data = Collections.singletonList("John \n Connor");
	CellProcessor[] processors = { new NotNull() };
	
	char customQuote = '|';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("|John  Connor|", result);
}
 
Example #24
Source File: Tokenizer.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new <tt>Tokenizer</tt>, which reads the CSV file, line by line.
 * 
 * @param reader
 *            the reader
 * @param preferences
 *            the CSV preferences
 * @throws NullPointerException
 *             if reader or preferences are null
 */
public Tokenizer(final Reader reader, final CsvPreference preferences) {
	super(reader, preferences);
	this.quoteChar = preferences.getQuoteChar();
	this.delimiterChar = preferences.getDelimiterChar();
	this.surroundingSpacesNeedQuotes = preferences.isSurroundingSpacesNeedQuotes();
	this.ignoreEmptyLines = preferences.isIgnoreEmptyLines();
	this.commentMatcher = preferences.getCommentMatcher();
	this.maxLinesPerRow = preferences.getMaxLinesPerRow();
	this.emptyColumnParsing = preferences.getEmptyColumnParsing();
	this.quoteEscapeChar = preferences.getQuoteEscapeChar();
}
 
Example #25
Source File: CsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 正常系のテスト - コンストラクタのテスト
 * <p> BeanMappingの指定</p>
 * @throws IOException 
 */
@Test
public void testConstructor_beanMapping() throws IOException {
    
    File file = new File("src/test/data/test_read_normal.csv");
    
    BeanMappingFactory mappingFactory = new BeanMappingFactory();
    BeanMapping<SampleNormalBean> beanMapping = mappingFactory.create(SampleNormalBean.class,
            DefaultGroup.class, SampleNormalBean.ReadGroup.class);
    
    CsvAnnotationBeanReader<SampleNormalBean> csvReader = new CsvAnnotationBeanReader<>(
            beanMapping,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE);
    
    List<SampleNormalBean> list = new ArrayList<>();
    
    // read header
    final String[] csvHeaders = csvReader.getHeader(true);
    assertThat(csvHeaders).isNotNull();
    
    SampleNormalBean bean;
    while((bean = csvReader.read()) != null) {
        list.add(bean);
        
        assertBean(bean);
    }
    
    assertThat(csvReader.getErrorMessages()).hasSize(0);
    
    csvReader.close();
    
}
 
Example #26
Source File: Reading.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * An example of reading using CsvDozerBeanReader.
 */
private static void readWithCsvDozerBeanReader() throws Exception {
	
	final CellProcessor[] processors = new CellProcessor[] { 
		new Optional(new ParseInt()), // age
		new ParseBool(),              // consent
		new ParseInt(),               // questionNo 1
		new Optional(),               // answer 1
		new ParseInt(),               // questionNo 2
		new Optional(),               // answer 2
		new ParseInt(),               // questionNo 3
		new Optional()                // answer 3
	};
	
	ICsvDozerBeanReader beanReader = null;
	try {
		beanReader = new CsvDozerBeanReader(new FileReader(CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);
		
		beanReader.getHeader(true); // ignore the header
		beanReader.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
		
		SurveyResponse surveyResponse;
		while( (surveyResponse = beanReader.read(SurveyResponse.class, processors)) != null ) {
			System.out.println(String.format("lineNo=%s, rowNo=%s, surveyResponse=%s", beanReader.getLineNumber(),
				beanReader.getRowNumber(), surveyResponse));
		}
		
	}
	finally {
		if( beanReader != null ) {
			beanReader.close();
		}
	}
}
 
Example #27
Source File: LazyCsvAnnotationBeanReaderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
/**
 * 全件読み込み - カラムにエラーがある場合も処理を続ける
 */
@Test
public void testReadAll_error_continueOnError() throws Exception {
    
    File file = new File("src/test/data/test_read_lazy_wrong_pattern.csv");
    
    LazyCsvAnnotationBeanReader<SampleLazyBean> csvReader = new LazyCsvAnnotationBeanReader<>(
            SampleLazyBean.class,
            new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8")),
            CsvPreference.STANDARD_PREFERENCE);
    csvReader.setExceptionConverter(exceptionConverter);
    
    List<SampleLazyBean> list = csvReader.readAll(true);
    assertThat(list).hasSize(1);
    
    for(SampleLazyBean bean : list) {
        assertBean(bean);
    }
    
    // convert error messages.
    List<String> messages = csvReader.getErrorMessages();
    assertThat(messages).hasSize(1)
        .contains("[2行, 3列] : 項目「生年月日」の値(2000-10-01)は、日時の書式「uuuu/MM/dd」として不正です。");
    messages.forEach(System.out::println);
    
    csvReader.close();
    
}
 
Example #28
Source File: ExportCSVWriterBuilder.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public CsvListWriter build(OutputStream outputStream, ExportParams exportParams) throws IOException {
    OutputStreamWriter stream = new OutputStreamWriter(outputStream, exportParams.getCharacterEncoding());
    Writer writer = new BufferedWriter(stream, WRITE_BUFFER_SIZE_BYTES);
    CsvPreference preference = new CsvPreference.Builder(
            exportParams.getQuoteChar(),
            exportParams.getFieldDelimiter(),
            exportParams.getRecordDelimiter())
            .useQuoteMode(new ColumnQuoteMode())
            .build();

    return new CsvListWriter(writer, preference);
}
 
Example #29
Source File: CsvDozerBeanReaderTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadForBeanReaderWithBrokenMaxTwoLineQuotes() throws IOException {
	reader = new StringReader(BROKEN_CSV);
	final CsvDozerBeanReader brokenCsvBeanReader = new CsvDozerBeanReader(reader, new CsvPreference.Builder(PREFS).maxLinesPerRow(2).build(), beanMapper);
	final List<SurveyResponse> responses = new ArrayList<SurveyResponse>();
	testReadForBrokenCSV(brokenCsvBeanReader, responses, "max number of lines to read exceeded while reading quoted column beginning on line 2 and ending on line 3", 1);
}
 
Example #30
Source File: TokenizerEscapingTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Tests the readColumns() method with an odd number of escape characters
 * char.
 */
@Test(expected = SuperCsvException.class)
public void testOddSeriesOfEscapeChars() throws Exception {

	final CsvPreference csvPref = new CsvPreference.Builder(CsvPreference.STANDARD_PREFERENCE)
			.setQuoteEscapeChar('#')
			.build();

	final String input = "\"#####\"";
	tokenizer = createTokenizer(input, csvPref);
	tokenizer.readColumns(columns);
}