Skip to content

Commit 366598c

Browse files
committed
Start with CSVWriter.. (it's kind of working already..)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/csv/trunk@373942 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7e7501b commit 366598c

4 files changed

Lines changed: 713 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)