org.supercsv.cellprocessor.constraint.NotNull Java Examples

The following examples show how to use org.supercsv.cellprocessor.constraint.NotNull. 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: ReadWriteCSV.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
private static CellProcessor[] getProcessors4ClassifiedTweetIDSCCSV() {

		final CellProcessor[] processors = new CellProcessor[]{
				//new UniqueHashCode(), // tweetID (must be unique)
				new NotNull(),	// tweetID (must be unique): sometimes CAN be null!
				new NotNull(), 	// crisis name
				new Optional(),	// attribute name
				new Optional(), // attribute code
				new Optional(), // label name
				new Optional(), // label description
				new Optional(),	// label code
				new Optional(), // confidence
				new Optional(), // humanLabeled
		};
		return processors;
	}
 
Example #2
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #3
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testConverterSupport() throws Exception {
	String csv = "Connor|John|16|1999-07-12|6" + decimalFormatSymbols.getDecimalSeparator() + "65\r\n";
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt(), new ParseDate("yyyy-MM-dd"),
		new ParseBigDecimal(decimalFormatSymbols) };
	
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), customPreference);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
	Assert.assertEquals(new SimpleDateFormat("yyyy-MM-dd").parse("1999-07-12"), character.getBirthDate());
	Assert.assertEquals(new BigDecimal(6.65, new MathContext(3)), character.getSavings());
}
 
Example #4
Source File: CsvTest.java    From haxademic with MIT License 6 votes vote down vote up
/**
 * Sets up the CSV processors for loading and saving data
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {

	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(), 	// score id (must be unique)
			new NotNull(), 			// initials
			new NotNull(), 			// emails
			new ParseInt(), 		// completeTime
			new ParseInt() 			// regionId
	};

	return processors;
}
 
Example #5
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 #6
Source File: Writing.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. All values
 * are converted to Strings before writing (there's no need to convert them), and null values will be written as
 * empty columns (no need to convert them to "").
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {
	
	final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
		new NotNull(), // firstName
		new NotNull(), // lastName
		new FmtDate("dd/MM/yyyy"), // birthDate
		new NotNull(), // mailingAddress
		new Optional(new FmtBool("Y", "N")), // married
		new Optional(), // numberOfKids
		new NotNull(), // favouriteQuote
		new NotNull(), // email
		new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
	};
	
	return processors;
}
 
Example #7
Source File: Reading.java    From super-csv with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the processors used for the examples. There are 10 CSV columns, so 10 processors are defined. Empty
 * columns are read as null (hence the NotNull() for mandatory columns).
 * 
 * @return the cell processors
 */
private static CellProcessor[] getProcessors() {
	
	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"),
		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
	};
	
	return processors;
}
 
Example #8
Source File: CacheLoader.java    From geode-demo-application with Apache License 2.0 6 votes vote down vote up
public void loadData() {
	System.out.println("Loading the Data");
	// load the current transactions
	String[] nameMapping = new String[] { "customerId", "productId",
			"quantity", "retailPrice", "id", "markUp", "orderStatus" };
	CellProcessor[] processors = new CellProcessor[] { new NotNull(),// customerId
			new NotNull(),// productId
			new ParseInt(),// quantity
			new ParseDouble(),// retailsPrice
			new NotNull(),// transactionId
			new ParseDouble(),// markUp
			new NotNull() // order status
	};
	loadCurrentTransactions("current_transactions.csv", nameMapping,
			processors);
	System.out
			.println("*************************************************************");
	System.out
			.println("********************PLAYING TRANSACTIONS*********************");
	System.out
			.println("*************************************************************");
	start();
}
 
Example #9
Source File: KiteConnect.java    From javakiteconnect with MIT License 6 votes vote down vote up
/** This method returns array of cellprocessor for parsing csv.
 * @return CellProcessor[] array
 * */
private CellProcessor[] getProcessors(){
    CellProcessor[] processors = new CellProcessor[]{
            new NotNull(new ParseLong()),   //instrument_token
            new NotNull(new ParseLong()),   //exchange_token
            new NotNull(),                  //trading_symbol
            new org.supercsv.cellprocessor.Optional(),                 //company name
            new NotNull(new ParseDouble()), //last_price
            new org.supercsv.cellprocessor.Optional(new ParseDate("yyyy-MM-dd")),                 //expiry
            new org.supercsv.cellprocessor.Optional(),                 //strike
            new NotNull(new ParseDouble()), //tick_size
            new NotNull(new ParseInt()),    //lot_size
            new NotNull(),                  //instrument_type
            new NotNull(),                  //segment
            new NotNull()                   //exchange
    };
    return processors;
}
 
Example #10
Source File: ReadWriteCSV.java    From AIDR with GNU Affero General Public License v3.0 6 votes vote down vote up
private static CellProcessor[] getProcessors4ClassifiedCCSV() {

		final CellProcessor[] processors = new CellProcessor[]{
				//new UniqueHashCode(), // tweetID (must be unique)
				//new NotNull(),	// tweetID (must be unique)
				new Optional(),	// tweetID - data shows that sometimes tweetID CAN be null!
				new Optional(), // message
				new Optional(), // userID
				new Optional(), // userName
				new Optional(), // userURL
				new Optional(), // createdAt
				new NotNull(),	// crisis name
				new Optional(),	// attribute name
				new Optional(),	// attribute code
				new Optional(), // label name
				new Optional(), // label description
				new Optional(), // label code
				new Optional(), // confidence
				new Optional() // humanLabeled
		};
		return processors;
	}
 
Example #11
Source File: ProcesssorBuilderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CellProcessor> buildForWriting(final Class<Integer> type, final FieldAccessor field,
        final Configuration config, final Class<?>[] groups) {
    
    CellProcessor processor = new NotNull(new FmtNumber(new DecimalFormat("#,##0")));
    return Optional.of(processor);
}
 
Example #12
Source File: ProcesssorBuilderTest.java    From super-csv-annotation with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CellProcessor> buildForReading(final Class<Integer> type, final FieldAccessor field,
        final Configuration config, final Class<?>[] groups) {
    
    CellProcessor processor = new NotNull(new Trim(new ParseInt()));
    return Optional.of(processor);
}
 
Example #13
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSeparator() throws IOException {
	String csv = "John+Connor";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	char customSeparator = '+';
	CsvPreference customPreference = new Builder('"', customSeparator, "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example #14
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToPrimitives() throws IOException {
	String csv = "Connor,John,16\r\n";
	String[] mapping = { "lastName", "firstName", "age" };
	CellProcessor[] processors = { new NotNull(), new NotNull(), new ParseInt() };
	
	CsvBeanReader beanReader = new CsvBeanReader(new StringReader(csv), STANDARD_PREFERENCE);
	FeatureBean character = beanReader.read(FeatureBean.class, mapping, processors);
	
	Assert.assertNotNull(character);
	Assert.assertEquals("John", character.getFirstName());
	Assert.assertEquals("Connor", character.getLastName());
	Assert.assertEquals(16, character.getAge());
}
 
Example #15
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedQuoteInQuotedField() throws IOException {
	String csv = "\"Joh\"\"n\",\"Con\"\"nor\"";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Joh\"n", result.get(0));
	Assert.assertEquals("Con\"nor", result.get(1));
}
 
Example #16
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineInDelimitedField() throws IOException {
	String csv = "\"Jo\nhn\",\"Con\nnor\"\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "\n").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Jo\nhn", result.get(0));
	Assert.assertEquals("Con\nnor", result.get(1));
}
 
Example #17
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEOL() throws IOException {
	String csv = "John,Connor\r>\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	String customEndOfLine = "\r>\n";
	CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example #18
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQuote() throws IOException {
	String csv = "|John  Connor|";
	CellProcessor[] processors = { new NotNull() };
	
	char customQuote = '|';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(1, result.size());
	Assert.assertEquals("John  Connor", result.get(0));
}
 
Example #19
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConverterSupport() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}
 
Example #20
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertsToBasicObjects() throws IOException {
	Calendar calendar = Calendar.getInstance();
	calendar.set(Calendar.YEAR, 1999);
	calendar.set(Calendar.MONTH, 6);
	calendar.set(Calendar.DAY_OF_MONTH, 12);
	
	FeatureBean character = new FeatureBean("John", "Connor", 16);
	character.setSavings(new BigDecimal(6.65));
	character.setBirthDate(calendar.getTime());
	
	String[] mapping = { "lastName", "firstName", "age", "birthDate", "savings" };
	DecimalFormat formatter = new DecimalFormat();
	formatter.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance());
	CellProcessor[] processors = { new NotNull(), new NotNull(), new NotNull(), new FmtDate("yyyy-MM-dd"),
		new FmtNumber(formatter) };
	
	StringWriter writer = new StringWriter();
	CsvPreference customPreference = new Builder('"', '|', "\r\n").build();
	CsvBeanWriter beanWriter = new CsvBeanWriter(writer, customPreference);
	beanWriter.write(character, mapping, processors);
	beanWriter.close();
	
	String csv = writer.toString();
	Assert.assertNotNull(csv);
	Assert.assertEquals("Connor|John|16|1999-07-12|" + formatter.format(character.getSavings()) + "\r\n", csv);
}
 
Example #21
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedQuoteInQuotedField() throws IOException {
	List<String> data = Arrays.asList("Joh\"n", "Con\"nor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("\"Joh\"\"n\",\"Con\"\"nor\"", result);
}
 
Example #22
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineInDelimitedField() throws IOException {
	List<String> data = Arrays.asList("Jo\nhn", "Con\nnor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "\n").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("\"Jo\nhn\",\"Con\nnor\"\n", result);
}
 
Example #23
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEOL() throws IOException {
	List<String> data = Collections.singletonList("John Connor");
	CellProcessor[] processors = { new NotNull() };
	
	String customEndOfLine = ">\r\n";
	CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("John Connor>\r\n", result);
}
 
Example #24
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 #25
Source File: WritingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSeparator() throws IOException {
	List<String> data = Arrays.asList("John", "Connor");
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	char customSeparator = '+';
	CsvPreference customPreference = new Builder('"', customSeparator, "").build();
	String result = writeToCsv(data, processors, customPreference);
	
	Assert.assertEquals("John+Connor", result);
}
 
Example #26
Source File: SuperCSVParserExample.java    From journaldev with MIT License 5 votes vote down vote up
private static CellProcessor[] getProcessors() {
	
	final CellProcessor[] processors = new CellProcessor[] { 
               new UniqueHashCode(), // ID (must be unique)
               new NotNull(), // Name
               new Optional(), // Role
               new NotNull() // Salary
       };
	return processors;
}
 
Example #27
Source File: DtaParser.java    From ipst with Mozilla Public License 2.0 5 votes vote down vote up
public static Map<String, String> readWithCsvMapReader(Path dicoFile) throws Exception {

        Map<String, String> retMap = new HashMap<>();
        try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(dicoFile, StandardCharsets.UTF_8), CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE)) {
            final String[] header = mapReader.getHeader(true);
            LOGGER.debug(" cvsheader length: " + header.length);
            final CellProcessor[] rowProcessors = new CellProcessor[header.length];
            for (int i = 0; i < rowProcessors.length; i++) {
                if (i == 0) {
                    rowProcessors[i] = new NotNull();
                } else {
                    rowProcessors[i] = null;
                }
            }

            Map<String, Object> componentMap;
            while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
                //System.out.println(String.format("lineNo=%s, rowNo=%s, mapping=%s", mapReader.getLineNumber(), mapReader.getRowNumber(), customerMap));
                String eurostagId = (String) componentMap.get(header[1]);
                String cimId = (String) componentMap.get(header[0]);
                if (eurostagId == null) {
                    LOGGER.warn("eurostagId=" + eurostagId + ", cimId=" + cimId);
                } else {
                    if (retMap.containsKey(eurostagId)) {
                        LOGGER.warn("eurostagId=" + eurostagId + " already in the map");
                    }
                    retMap.put(eurostagId, cimId);
                }
            }
        }

        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("ids map: " + retMap);
        }
        return retMap;

    }
 
Example #28
Source File: DdbDyrLoader.java    From ipst with Mozilla Public License 2.0 4 votes vote down vote up
private Map<String, String> readWithCsvMapReader(Path mappingFile) throws IOException {
    Map<String, String> retMap = new HashMap<>();
    try (ICsvMapReader mapReader = new CsvMapReader(Files.newBufferedReader(mappingFile, StandardCharsets.UTF_8), CsvPreference.STANDARD_PREFERENCE)) {
        final String[] header = mapReader.getHeader(true);
        log.info(" cvsheader length: " + header.length);
        final CellProcessor[] rowProcessors = new CellProcessor[header.length];
        for (int i = 0; i < rowProcessors.length; i++) {
            if (i == 0) {
                rowProcessors[i] = new NotNull();
            } else {
                rowProcessors[i] = new NotNull();
            }
        }

        Map<String, Object> componentMap;
        while ((componentMap = mapReader.read(header, rowProcessors)) != null) {
            String psseId = (String) componentMap.get(header[0]);
            String rdfId = (String) componentMap.get(header[1]);
            if (psseId == null) {
                log.warn("psseId=" + psseId + ", rdfId=" + rdfId);
            } else {
                if (retMap.containsKey(psseId)) {
                    log.warn("psseId=" + psseId + " already in the map");
                }
                retMap.put(psseId, rdfId);
            }
        }
    }

    if (log.isTraceEnabled()) {
        log.trace("ids map: " + retMap);
    }
    log.info("ids map: " + retMap);
    return retMap;
}
 
Example #29
Source File: Writing.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * An example of writing using CsvDozerBeanWriter.
 */
private static void writeWithDozerCsvBeanWriter() throws Exception {
	
	final CellProcessor[] processors = new CellProcessor[] { 
		new Token(0, null),     // age
		new FmtBool("Y", "N"),  // consent
		new NotNull(),          // questionNo 1
		new Optional(),         // answer 1
		new NotNull(),          // questionNo 2
		new Optional(),         // answer 2
		new NotNull(),          // questionNo 3
		new Optional() };       // answer 3
	
	// create the survey responses to write
	SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
		"Albert Einstein"), new Answer(3, "Big Bang Theory")));
	SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
		"Nikola Tesla"), new Answer(3, "Stargate")));
	SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
		"Carl Sagan"), new Answer(3, "Star Wars")));
	final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
	
	ICsvDozerBeanWriter beanWriter = null;
	try {
		beanWriter = new CsvDozerBeanWriter(new FileWriter("target/writeWithCsvDozerBeanWriter.csv"),
			CsvPreference.STANDARD_PREFERENCE);
		
		// configure the mapping from the fields to the CSV columns
		beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
		
		// write the header
		beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
			"questionNo3", "answer3");
		
		// write the beans
		for( final SurveyResponse surveyResponse : surveyResponses ) {
			beanWriter.write(surveyResponse, processors);
		}
		
	}
	finally {
		if( beanWriter != null ) {
			beanWriter.close();
		}
	}
}
 
Example #30
Source File: Writing.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * An example of partial reading using CsvDozerBeanWriter.
 */
private static void partialWriteWithCsvDozerBeanWriter() throws Exception {
	// ignore questionNo/answer 1 columns
	final String[] partialFieldMapping = new String[] {
			"age",
			"consentGiven",
			"answers[1].questionNo",
			"answers[1].answer",
			"answers[2].questionNo",
			"answers[2].answer" };
	
	// ignore questionNo/answer 1 columns keep up with partialFieldMapping
	final CellProcessor[] partialProcessors = new CellProcessor[] { 
		new Token(0, "age not supplied"), // age
		new FmtBool("Y", "N"),                 // consent
		new NotNull(),                         // questionNo 2
		new ConvertNullTo("not answered"),     // answer 2
		new NotNull(),                         // questionNo 3
		new ConvertNullTo("not answered")};    // answer 3
	
	// create the survey responses to write
	SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
		"Albert Einstein"), new Answer(3, "Big Bang Theory")));
	SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
		"Nikola Tesla"), new Answer(3, "Stargate")));
	SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
		"Carl Sagan"), new Answer(3, "Star Wars")));
	final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
	
	ICsvDozerBeanWriter beanWriter = null;
	try {
		beanWriter = new CsvDozerBeanWriter(new FileWriter("target/partialWriteWithCsvDozerBeanWriter.csv"),
			CsvPreference.STANDARD_PREFERENCE);
		
		// configure the mapping from the fields to the CSV columns
		beanWriter.configureBeanMapping(SurveyResponse.class, partialFieldMapping);
		
		// write the header
		beanWriter.writeHeader("age", "consentGiven", "questionNo2", "answer2",
			"questionNo3", "answer3");
		
		// write the beans
		for( final SurveyResponse surveyResponse : surveyResponses ) {
			beanWriter.write(surveyResponse, partialProcessors);
		}
		
	}
	finally {
		if( beanWriter != null ) {
			beanWriter.close();
		}
	}
}