1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.commons.csv.writer;
20
21 import java.io.InputStream;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26
27 /**
28 * The CSVConfig is used to configure the CSV writer
29 *
30 * @author Martin van den Bemt
31 * @version $Id: $
32 */
33 public class CSVConfig {
34
35 /** specifies if it is a fixed width csv file **/
36 private boolean fixedWidth;
37 /** list of fields **/
38 private List fields;
39
40 /** Do no do any filling **/
41 public static final int FILLNONE = 0;
42 /** Fill content the the left. Mainly usable together with fixedWidth **/
43 public static final int FILLLEFT = 1;
44 /** Fill content to the right. Mainly usable together with fixedWidth **/
45 public static final int FILLRIGHT = 2;
46
47 /** The fill pattern */
48 private int fill;
49 /** The fill char. Defaults to a space */
50 private char fillChar = ' ';
51 /** The seperator character. Defaults to , */
52 private char delimiter = ',';
53 /** Should we ignore the delimiter. Defaults to false */
54 private boolean ignoreDelimiter = false;
55 /** the value delimiter. Defaults to " */
56 private char valueDelimiter = '"';
57 /** Should we ignore the value delimiter. Defaults to true */
58 private boolean ignoreValueDelimiter = true;
59 /** Specifies if we want to use a field header */
60 private boolean fieldHeader = false;
61 /** Specifies if the end of the line needs to be trimmed */
62 private boolean endTrimmed = false;
63 /**
64 *
65 */
66 public CSVConfig() {
67 super();
68 }
69
70 /**
71 * @return if the CSV file is fixedWidth
72 */
73 public boolean isFixedWidth() {
74 return fixedWidth;
75 }
76
77 /**
78 * Specify if the CSV file is fixed width.
79 * Defaults to false
80 * @param fixedWidth the fixedwidth
81 */
82 public void setFixedWidth(boolean fixedWidth) {
83 this.fixedWidth = fixedWidth;
84 }
85
86 public void addField(CSVField field) {
87 if (fields == null) {
88 fields = new ArrayList();
89 }
90 fields.add(field);
91 }
92
93 /**
94 * Set the fields that should be used by the writer.
95 * This will overwrite currently added fields completely!
96 * @param csvFields the csvfields array. If null it will do nothing
97 */
98 public void setFields(CSVField[] csvFields) {
99 if (csvFields == null) {
100 return;
101 }
102 fields = new ArrayList(Arrays.asList(csvFields));
103 }
104
105 /**
106 * Set the fields that should be used by the writer
107 * @param csvField a collection with fields. If null it will do nothing
108 */
109 public void setFields(Collection csvField) {
110 if (csvField == null) {
111 return;
112 }
113 fields = new ArrayList(csvField);
114 }
115
116 /**
117 * @return an array with the known fields (even if no fields are specified)
118 */
119 public CSVField[] getFields() {
120 CSVField[] csvFields = new CSVField[0];
121 if (fields != null) {
122 return (CSVField[]) fields.toArray(csvFields);
123 }
124 return csvFields;
125 }
126
127 public CSVField getField(String name) {
128 if (fields == null || name == null) {
129 return null;
130 }
131 for(int i = 0; i < fields.size(); i++) {
132 CSVField field = (CSVField) fields.get(i);
133 if (name.equals(field.getName())) {
134 return field;
135 }
136 }
137 return null;
138 }
139
140 /**
141 * @return the fill pattern.
142 */
143 public int getFill() {
144 return fill;
145 }
146
147 /**
148 * Set the fill pattern. Defaults to {@link #FILLNONE}
149 * <br/>Other options are : {@link #FILLLEFT} and {@link #FILLRIGHT}
150 * @param fill the fill pattern.
151 */
152 public void setFill(int fill) {
153 this.fill = fill;
154 }
155
156 /**
157 *
158 * @return the fillchar. Defaults to a space.
159 */
160 public char getFillChar() {
161 return fillChar;
162 }
163
164 /**
165 * Set the fill char
166 * @param fillChar the fill char
167 */
168 public void setFillChar(char fillChar) {
169 this.fillChar = fillChar;
170 }
171
172 /**
173 * @return the delimeter used.
174 */
175 public char getDelimiter() {
176 return delimiter;
177 }
178
179 /**
180 * Set the delimiter to use
181 * @param delimiter the delimiter character.
182 */
183 public void setDelimiter(char delimiter) {
184 this.delimiter = delimiter;
185 }
186
187 /**
188 * @return if the writer should ignore the delimiter character.
189 */
190 public boolean isDelimiterIgnored() {
191 return ignoreDelimiter;
192 }
193
194 /**
195 * Specify if the writer should ignore the delimiter.
196 * @param ignoreDelimiter defaults to false.
197 */
198 public void setIgnoreDelimiter(boolean ignoreDelimiter) {
199 this.ignoreDelimiter = ignoreDelimiter;
200 }
201
202 /**
203 * @return the value delimeter used. Defaults to "
204 */
205 public char getValueDelimiter() {
206 return valueDelimiter;
207 }
208
209 /**
210 * Set the value delimiter to use
211 * @param valueDelimiter the value delimiter character.
212 */
213 public void setValueDelimiter(char valueDelimiter) {
214 this.valueDelimiter = valueDelimiter;
215 }
216
217 /**
218 * @return if the writer should ignore the value delimiter character.
219 * Defaults to true.
220 */
221 public boolean isValueDelimiterIgnored() {
222 return ignoreValueDelimiter;
223 }
224
225 /**
226 * Specify if the writer should ignore the value delimiter.
227 * @param ignoreValueDelimiter defaults to false.
228 */
229 public void setIgnoreValueDelimiter(boolean ignoreValueDelimiter) {
230 this.ignoreValueDelimiter = ignoreValueDelimiter;
231 }
232
233 /**
234 * @return if a field header is used. Defaults to false
235 */
236 public boolean isFieldHeader() {
237 return fieldHeader;
238 }
239 /**
240 * Specify if you want to use a field header.
241 * @param fieldHeader true or false.
242 */
243 public void setFieldHeader(boolean fieldHeader) {
244 this.fieldHeader = fieldHeader;
245 }
246
247 /**
248 * TODO..
249 * @see java.lang.Object#equals(java.lang.Object)
250 */
251 public boolean equals(Object obj) {
252 if (obj == null && !(obj instanceof CSVConfig)) {
253 return false;
254 }
255 return super.equals(obj);
256 // CSVConfig config = (CSVConfig) obj;
257 // getFill() == config.getFill()
258 // getFields().equals(config.getFields())
259 }
260
261 /**
262 * Creates a config based on a stream. It tries to guess<br/>
263 * NOTE : The stream will be closed.
264 * @param inputStream the inputstream.
265 * @return the guessed config.
266 */
267 public static CSVConfig guessConfig(InputStream inputStream) {
268 return null;
269 }
270
271 /**
272 * @return if the end of the line should be trimmed. Default is false.
273 */
274 public boolean isEndTrimmed() {
275 return endTrimmed;
276 }
277
278 /**
279 * Specify if the end of the line needs to be trimmed. Defaults to false.
280 * @param endTrimmed
281 */
282 public void setEndTrimmed(boolean endTrimmed) {
283 this.endTrimmed = endTrimmed;
284 }
285
286
287 }