1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.example.dozer;
17
18 import java.io.FileWriter;
19 import java.util.Arrays;
20 import java.util.List;
21
22 import org.supercsv.cellprocessor.ConvertNullTo;
23 import org.supercsv.cellprocessor.FmtBool;
24 import org.supercsv.cellprocessor.Optional;
25 import org.supercsv.cellprocessor.Token;
26 import org.supercsv.cellprocessor.constraint.NotNull;
27 import org.supercsv.cellprocessor.ift.CellProcessor;
28 import org.supercsv.io.dozer.CsvDozerBeanWriter;
29 import org.supercsv.io.dozer.ICsvDozerBeanWriter;
30 import org.supercsv.mock.dozer.Answer;
31 import org.supercsv.mock.dozer.SurveyResponse;
32 import org.supercsv.prefs.CsvPreference;
33
34
35
36
37 public class Writing {
38
39 private static final String[] FIELD_MAPPING = new String[] {
40 "age",
41 "consentGiven",
42 "answers[0].questionNo",
43 "answers[0].answer",
44 "answers[1].questionNo",
45 "answers[1].answer",
46 "answers[2].questionNo",
47 "answers[2].answer" };
48
49 public static void main(String[] args) throws Exception {
50 writeWithDozerCsvBeanWriter();
51 partialWriteWithCsvDozerBeanWriter();
52 }
53
54
55
56
57 private static void writeWithDozerCsvBeanWriter() throws Exception {
58
59 final CellProcessor[] processors = new CellProcessor[] {
60 new Token(0, null),
61 new FmtBool("Y", "N"),
62 new NotNull(),
63 new Optional(),
64 new NotNull(),
65 new Optional(),
66 new NotNull(),
67 new Optional() };
68
69
70 SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
71 "Albert Einstein"), new Answer(3, "Big Bang Theory")));
72 SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
73 "Nikola Tesla"), new Answer(3, "Stargate")));
74 SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
75 "Carl Sagan"), new Answer(3, "Star Wars")));
76 final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
77
78 ICsvDozerBeanWriter beanWriter = null;
79 try {
80 beanWriter = new CsvDozerBeanWriter(new FileWriter("target/writeWithCsvDozerBeanWriter.csv"),
81 CsvPreference.STANDARD_PREFERENCE);
82
83
84 beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
85
86
87 beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
88 "questionNo3", "answer3");
89
90
91 for( final SurveyResponse surveyResponse : surveyResponses ) {
92 beanWriter.write(surveyResponse, processors);
93 }
94
95 }
96 finally {
97 if( beanWriter != null ) {
98 beanWriter.close();
99 }
100 }
101 }
102
103
104
105
106 private static void partialWriteWithCsvDozerBeanWriter() throws Exception {
107
108
109 final CellProcessor[] partialProcessors = new CellProcessor[] {
110 new Token(0, "age not supplied"),
111 new FmtBool("Y", "N"),
112 new NotNull(),
113 new ConvertNullTo("not answered"),
114 new NotNull(),
115 new ConvertNullTo("not answered"),
116 new NotNull(),
117 new ConvertNullTo("not answered")};
118
119
120 SurveyResponse response1 = new SurveyResponse(18, true, Arrays.asList(new Answer(1, "Twelve"), new Answer(2,
121 "Albert Einstein"), new Answer(3, "Big Bang Theory")));
122 SurveyResponse response2 = new SurveyResponse(0, true, Arrays.asList(new Answer(1, "Thirteen"), new Answer(2,
123 "Nikola Tesla"), new Answer(3, "Stargate")));
124 SurveyResponse response3 = new SurveyResponse(42, false, Arrays.asList(new Answer(1, null), new Answer(2,
125 "Carl Sagan"), new Answer(3, "Star Wars")));
126 final List<SurveyResponse> surveyResponses = Arrays.asList(response1, response2, response3);
127
128 ICsvDozerBeanWriter beanWriter = null;
129 try {
130 beanWriter = new CsvDozerBeanWriter(new FileWriter("target/partialWriteWithCsvDozerBeanWriter.csv"),
131 CsvPreference.STANDARD_PREFERENCE);
132
133
134 beanWriter.configureBeanMapping(SurveyResponse.class, FIELD_MAPPING);
135
136
137 beanWriter.writeHeader("age", "consentGiven", "questionNo1", "answer1", "questionNo2", "answer2",
138 "questionNo3", "answer3");
139
140
141 for( final SurveyResponse surveyResponse : surveyResponses ) {
142 beanWriter.write(surveyResponse, partialProcessors);
143 }
144
145 }
146 finally {
147 if( beanWriter != null ) {
148 beanWriter.close();
149 }
150 }
151 }
152 }