1 package com.opencsv;
2
3 import java.io.Writer;
4
5 /**
6 * Builder for creating the CSVWriter.
7 * <p>Note: this should be the preferred method of creating the CSVWriter as
8 * we will no longer be creating constructors for new fields added to the
9 * writer. Plus there are now multiple flavors of CSVWriter and this will
10 * help build the correct one.</p>
11 * <br>
12 * <p>If a CSVWriterBuilder has a parser injected, it will create a CSVParserWriter, otherwise
13 * it will create a CSVWriter. If a parser is injected into a builder that already has a separator,
14 * quotechar, or escapechar then an IllegalArguementException is thrown. Likewise the opposite
15 * is true.</p>
16 * <br>
17 * <p>If nothing is defined then a CSVWriter will be produced with default settings.</p>
18 * <p>
19 * <code>
20 * Writer writer = new StringWriter(); // any Writer<br>
21 * CSVParser parser = new CSVParserBuilder().build();<br>
22 * ICSVWriter csvParserWriter = new CSVWriterBuilder(writer)<br>
23 * .withParser(parser)<br>
24 * .withLineEnd(ICSVWriter.RFC4180_LINE_END)<br>
25 * .build(); // will produce a CSVParserWriter<br>
26 * <br>
27 * ICSVWriter csvWriter = new CSVWriterBuilder(writer)<br>
28 * .withSeparator(ICSVParser.DEFAULT_SEPARATOR)<br>
29 * .withQuoteChar(ICSVParser.DEFAULT_QUOTE_CHARACTER)<br>
30 * .withEscapeChar(ICSVParser.DEFAULT_ESCAPE_CHARACTER)<br>
31 * .withLineEnd(ICSVWriter.DEFAULT_LINE_END)<br>
32 * .build(); // will produce a CSVWriter<br>
33 * </code>
34 *
35 * @since 4.2
36 */
37 public class CSVWriterBuilder {
38 private final Writer writer;
39 private ICSVParser parser;
40 private Character separator;
41 private Character quotechar;
42 private Character escapechar;
43 private ResultSetHelper resultSetHelper;
44 private String lineEnd = ICSVWriter.DEFAULT_LINE_END;
45
46 /**
47 * Constructor taking a writer for the resulting CSV output. This is because the Writer is required and
48 * everything else has an optional default.
49 *
50 * @param writer A writer to create the resulting CSV output for the writer.
51 */
52 public CSVWriterBuilder(Writer writer) {
53 this.writer = writer;
54 }
55
56
57 /**
58 * Sets the parser that the ICSVWriter will be using. If none is defined then a CSVWriter will be returned
59 * when the build command is executed.
60 *
61 * @param parser Parser to inject into the ICSVWriter.
62 * @return The CSVWriterBuilder with the parser set.
63 * @throws IllegalArgumentException If a separator, quote or escape character has been set.
64 */
65 public CSVWriterBuilder withParser(ICSVParser parser) {
66 if (separator != null || quotechar != null || escapechar != null) {
67 throw new IllegalArgumentException("You cannot set the parser in the builder if you have set the separator, quote, or escape character");
68 }
69 this.parser = parser;
70 return this;
71 }
72
73 /**
74 * Sets the separator that the ICSVWriter will be using.
75 *
76 * @param separator The separator character to use when creating the CSV content.
77 * @return The CSVWriterBuilder with the separator set.
78 * @throws IllegalArgumentException If a parser has been set.
79 */
80 public CSVWriterBuilder withSeparator(char separator) {
81 if (parser != null) {
82 throw new IllegalArgumentException("You cannot set the separator in the builder if you have a ICSVParser set. Set the separator in the parser instead.");
83 }
84 this.separator = separator;
85 return this;
86 }
87
88 /**
89 * Sets the quote character that the ICSVWriter will be using.
90 *
91 * @param quoteChar The quote character to use when creating the CSV content.
92 * @return The CSVWriterBuilder with the quote character set.
93 * @throws IllegalArgumentException If a parser has been set.
94 */
95 public CSVWriterBuilder withQuoteChar(char quoteChar) {
96 if (parser != null) {
97 throw new IllegalArgumentException("You cannot set the quote character in the builder if you have a ICSVParser set. Set the quote character in the parser instead.");
98 }
99 this.quotechar = quoteChar;
100 return this;
101 }
102
103 /**
104 * Sets the escape character that the ICSVWriter will be using.
105 *
106 * @param escapeChar The escape character to use when creating the CSV content.
107 * @return The CSVWriterBuilder with the escape character set.
108 * @throws IllegalArgumentException If a parser has been set.
109 */
110 public CSVWriterBuilder withEscapeChar(char escapeChar) {
111 if (parser != null) {
112 throw new IllegalArgumentException("You cannot set the escape character in the builder if you have a ICSVParser set. Set the escape character in the parser instead.");
113 }
114 this.escapechar = escapeChar;
115 return this;
116 }
117
118 /**
119 * Sets the newline character that the ICSVWriter will use. If none is defined then {@code \n} will be used
120 *
121 * @param lineEnd Newline string to inject into the ICSVWriter.
122 * @return The CSVWriterBuilder with the lineEnd set.
123 */
124 public CSVWriterBuilder withLineEnd(String lineEnd) {
125 this.lineEnd = lineEnd;
126 return this;
127 }
128
129 /**
130 * Creates the CSVWriter.
131 *
132 * @return A CSVWriter based on the set criteria.
133 */
134 public ICSVWriter build() {
135 if (parser != null) {
136 return createCSVParserWriter();
137 }
138 return createCSVWriter();
139 }
140
141 private ICSVWriter createCSVParserWriter() {
142 final CSVParserWriter icsvWriter = new CSVParserWriter(writer, parser, lineEnd);
143
144 if (resultSetHelper != null) {
145 icsvWriter.setResultService(resultSetHelper);
146 }
147
148 return icsvWriter;
149 }
150
151 private ICSVWriter createCSVWriter() {
152 if (separator == null) {
153 separator = ICSVWriter.DEFAULT_SEPARATOR;
154 }
155 if (quotechar == null) {
156 quotechar = ICSVWriter.DEFAULT_QUOTE_CHARACTER;
157 }
158 if (escapechar == null) {
159 escapechar = ICSVWriter.DEFAULT_ESCAPE_CHARACTER;
160 }
161 ICSVWriter icsvWriter = new CSVWriter(writer, separator, quotechar, escapechar, lineEnd);
162
163 if (resultSetHelper != null) {
164 icsvWriter.setResultService(resultSetHelper);
165 }
166
167 return icsvWriter;
168 }
169
170 /**
171 * Sets the ResultSetHelper that the ICSVWriter will use. If it is not defined then it will not be set and will
172 * be up to the ICSVWriter to handle - CSVWriter will create one by default.
173 *
174 * @param helper ResultSetHelper to be injected into the ICSVWriter.
175 * @return The CSVWriterBuiilder with the ResultSetHelper set.
176 */
177 public CSVWriterBuilder withResultSetHelper(ResultSetHelper helper) {
178 this.resultSetHelper = helper;
179 return this;
180 }
181 }