Skip to content

Commit cb99634

Browse files
committed
Renamed CSVStrategy to CSVFormat
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1199842 13f79535-47bb-0310-9956-ffa450edef68
1 parent 42476f4 commit cb99634

8 files changed

Lines changed: 161 additions & 162 deletions

File tree

src/main/java/org/apache/commons/csv/CSVStrategy.java renamed to src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
import java.io.Serializable;
2121

2222
/**
23-
* CSVStrategy
23+
* The format specification of a CSV file.
2424
*
25-
* Represents the strategy for a CSV.
25+
* This class is immutable.
2626
*/
27-
public class CSVStrategy implements Cloneable, Serializable {
27+
public class CSVFormat implements Cloneable, Serializable {
2828

2929
private char delimiter = ',';
3030
private char encapsulator = '"';
@@ -45,27 +45,27 @@ public class CSVStrategy implements Cloneable, Serializable {
4545
public static final char ENCAPSULATOR_DISABLED = (char) -2;
4646

4747
/** Standard comma separated format. */
48-
public static final CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
48+
public static final CSVFormat DEFAULT = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
4949

5050
/** Excel file format (using a comma as the value delimiter). */
51-
public static final CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false);
51+
public static final CSVFormat EXCEL = new CSVFormat(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false, false, false, false);
5252

5353
/** Tabulation delimited format. */
54-
public static final CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
54+
public static final CSVFormat TDF = new CSVFormat('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true, true, false, true);
5555

5656

5757
/**
58-
* Creates a CSVStrategy with the default parameters.
58+
* Creates a CSVFormat with the default parameters.
5959
*/
60-
public CSVStrategy() {
60+
public CSVFormat() {
6161
}
6262

63-
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
63+
public CSVFormat(char delimiter, char encapsulator, char commentStart) {
6464
this(delimiter, encapsulator, commentStart, ESCAPE_DISABLED, true, true, false, true);
6565
}
6666

6767
/**
68-
* Customized CSV strategy constructor.
68+
* Customized CSV format constructor.
6969
*
7070
* @param delimiter a char used for value separation
7171
* @param encapsulator a char used as value encapsulation marker
@@ -76,7 +76,7 @@ public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
7676
* @param unicodeEscapesInterpreted TRUE when unicode escapes should be interpreted
7777
* @param emptyLinesIgnored TRUE when the parser should skip emtpy lines
7878
*/
79-
public CSVStrategy(
79+
public CSVFormat(
8080
char delimiter,
8181
char encapsulator,
8282
char commentStart,
@@ -99,30 +99,30 @@ public char getDelimiter() {
9999
return delimiter;
100100
}
101101

102-
public CSVStrategy withDelimiter(char delimiter) {
103-
CSVStrategy strategy = (CSVStrategy) clone();
102+
public CSVFormat withDelimiter(char delimiter) {
103+
CSVFormat format = (CSVFormat) clone();
104104
this.delimiter = delimiter;
105-
return strategy;
105+
return format;
106106
}
107107

108108
public char getEncapsulator() {
109109
return encapsulator;
110110
}
111111

112-
public CSVStrategy withEncapsulator(char encapsulator) {
113-
CSVStrategy strategy = (CSVStrategy) clone();
114-
strategy.encapsulator = encapsulator;
115-
return strategy;
112+
public CSVFormat withEncapsulator(char encapsulator) {
113+
CSVFormat format = (CSVFormat) clone();
114+
format.encapsulator = encapsulator;
115+
return format;
116116
}
117117

118118
public char getCommentStart() {
119119
return commentStart;
120120
}
121121

122-
public CSVStrategy withCommentStart(char commentStart) {
123-
CSVStrategy strategy = (CSVStrategy) clone();
124-
strategy.commentStart = commentStart;
125-
return strategy;
122+
public CSVFormat withCommentStart(char commentStart) {
123+
CSVFormat format = (CSVFormat) clone();
124+
format.commentStart = commentStart;
125+
return format;
126126
}
127127

128128
public boolean isCommentingDisabled() {
@@ -133,60 +133,60 @@ public char getEscape() {
133133
return escape;
134134
}
135135

136-
public CSVStrategy withEscape(char escape) {
137-
CSVStrategy strategy = (CSVStrategy) clone();
138-
strategy.escape = escape;
139-
return strategy;
136+
public CSVFormat withEscape(char escape) {
137+
CSVFormat format = (CSVFormat) clone();
138+
format.escape = escape;
139+
return format;
140140
}
141141

142142
public boolean isLeadingSpacesIgnored() {
143143
return leadingSpacesIgnored;
144144
}
145145

146-
public CSVStrategy withLeadingSpacesIgnored(boolean leadingSpacesIgnored) {
147-
CSVStrategy strategy = (CSVStrategy) clone();
148-
strategy.leadingSpacesIgnored = leadingSpacesIgnored;
149-
return strategy;
146+
public CSVFormat withLeadingSpacesIgnored(boolean leadingSpacesIgnored) {
147+
CSVFormat format = (CSVFormat) clone();
148+
format.leadingSpacesIgnored = leadingSpacesIgnored;
149+
return format;
150150
}
151151

152152
public boolean isTrailingSpacesIgnored() {
153153
return trailingSpacesIgnored;
154154
}
155155

156-
public CSVStrategy withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
157-
CSVStrategy strategy = (CSVStrategy) clone();
158-
strategy.trailingSpacesIgnored = trailingSpacesIgnored;
159-
return strategy;
156+
public CSVFormat withTrailingSpacesIgnored(boolean trailingSpacesIgnored) {
157+
CSVFormat format = (CSVFormat) clone();
158+
format.trailingSpacesIgnored = trailingSpacesIgnored;
159+
return format;
160160
}
161161

162162
public boolean isUnicodeEscapesInterpreted() {
163163
return unicodeEscapesInterpreted;
164164
}
165165

166-
public CSVStrategy withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) {
167-
CSVStrategy strategy = (CSVStrategy) clone();
168-
strategy.unicodeEscapesInterpreted = unicodeEscapesInterpreted;
169-
return strategy;
166+
public CSVFormat withUnicodeEscapesInterpreted(boolean unicodeEscapesInterpreted) {
167+
CSVFormat format = (CSVFormat) clone();
168+
format.unicodeEscapesInterpreted = unicodeEscapesInterpreted;
169+
return format;
170170
}
171171

172172
public boolean isEmptyLinesIgnored() {
173173
return emptyLinesIgnored;
174174
}
175175

176-
public CSVStrategy withEmptyLinesIgnored(boolean emptyLinesIgnored) {
177-
CSVStrategy strategy = (CSVStrategy) clone();
178-
strategy.emptyLinesIgnored = emptyLinesIgnored;
179-
return strategy;
176+
public CSVFormat withEmptyLinesIgnored(boolean emptyLinesIgnored) {
177+
CSVFormat format = (CSVFormat) clone();
178+
format.emptyLinesIgnored = emptyLinesIgnored;
179+
return format;
180180
}
181181

182182
public String getLineSeparator() {
183183
return lineSeparator;
184184
}
185185

186-
public CSVStrategy withLineSeparator(String lineSeparator) {
187-
CSVStrategy strategy = (CSVStrategy) clone();
188-
strategy.lineSeparator = lineSeparator;
189-
return strategy;
186+
public CSVFormat withLineSeparator(String lineSeparator) {
187+
CSVFormat format = (CSVFormat) clone();
188+
format.lineSeparator = lineSeparator;
189+
return format;
190190
}
191191

192192
protected Object clone() {

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@
2727
* Parses CSV files according to the specified configuration.
2828
*
2929
* Because CSV appears in many different dialects, the parser supports many
30-
* configuration settings by allowing the specification of a {@link CSVStrategy}.
30+
* configuration settings by allowing the specification of a {@link CSVFormat}.
3131
*
3232
* <p>Parsing of a csv-string having tabs as separators,
3333
* '"' as an optional value encapsulator, and comments starting with '#':</p>
3434
* <pre>
3535
* String[][] data =
36-
* (new CSVParser(new StringReader("a\tb\nc\td"), new CSVStrategy('\t','"','#'))).getAllValues();
36+
* (new CSVParser(new StringReader("a\tb\nc\td"), new CSVFormat('\t','"','#'))).getAllValues();
3737
* </pre>
3838
*
3939
* <p>Parsing of a csv-string in Excel CSV format</p>
4040
* <pre>
4141
* String[][] data =
42-
* (new CSVParser(new StringReader("a;b\nc;d"), CSVStrategy.EXCEL_STRATEGY)).getAllValues();
42+
* (new CSVParser(new StringReader("a;b\nc;d"), CSVFormat.EXCEL)).getAllValues();
4343
* </pre>
4444
*
4545
* <p>
46-
* Internal parser state is completely covered by the strategy
46+
* Internal parser state is completely covered by the format
4747
* and the reader-state.</p>
4848
*
4949
* <p>see <a href="package-summary.html">package documentation</a>
@@ -73,7 +73,7 @@ public class CSVParser {
7373
// the input stream
7474
private final ExtendedBufferedReader in;
7575

76-
private final CSVStrategy strategy;
76+
private final CSVFormat format;
7777

7878
// the following objects are shared to reduce garbage
7979
/**
@@ -117,31 +117,31 @@ Token reset() {
117117
// ======================================================
118118

119119
/**
120-
* CSV parser using the default {@link CSVStrategy}.
120+
* CSV parser using the default {@link CSVFormat}.
121121
*
122122
* @param input a Reader containing "csv-formatted" input
123123
*/
124124
public CSVParser(Reader input) {
125-
this(input, CSVStrategy.DEFAULT_STRATEGY);
125+
this(input, CSVFormat.DEFAULT);
126126
}
127127

128128
/**
129-
* Customized CSV parser using the given {@link CSVStrategy}
129+
* Customized CSV parser using the given {@link CSVFormat}
130130
*
131131
* @param input a Reader containing "csv-formatted" input
132-
* @param strategy the CSVStrategy used for CSV parsing
132+
* @param format the CSVFormat used for CSV parsing
133133
*/
134-
public CSVParser(Reader input, CSVStrategy strategy) {
134+
public CSVParser(Reader input, CSVFormat format) {
135135
this.in = new ExtendedBufferedReader(input);
136-
this.strategy = strategy;
136+
this.format = format;
137137
}
138138

139139
// ======================================================
140140
// the parser
141141
// ======================================================
142142

143143
/**
144-
* Parses the CSV according to the given strategy
144+
* Parses the CSV according to the given format
145145
* and returns the content as an array of records
146146
* (whereas records are arrays of single values).
147147
* <p/>
@@ -260,7 +260,7 @@ Token nextToken(Token tkn) throws IOException {
260260
c = in.readAgain();
261261

262262
// empty line detection: eol AND (last char was EOL or beginning)
263-
while (strategy.isEmptyLinesIgnored() && eol
263+
while (format.isEmptyLinesIgnored() && eol
264264
&& (lastChar == '\n'
265265
|| lastChar == '\r'
266266
|| lastChar == ExtendedBufferedReader.UNDEFINED)
@@ -278,25 +278,25 @@ Token nextToken(Token tkn) throws IOException {
278278
}
279279

280280
// did we reach eof during the last iteration already ? TT_EOF
281-
if (isEndOfFile(lastChar) || (lastChar != strategy.getDelimiter() && isEndOfFile(c))) {
281+
if (isEndOfFile(lastChar) || (lastChar != format.getDelimiter() && isEndOfFile(c))) {
282282
tkn.type = TT_EOF;
283283
return tkn;
284284
}
285285

286286
// important: make sure a new char gets consumed in each iteration
287287
while (!tkn.isReady && tkn.type != TT_EOF) {
288288
// ignore whitespaces at beginning of a token
289-
while (strategy.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) {
289+
while (format.isLeadingSpacesIgnored() && isWhitespace(c) && !eol) {
290290
wsBuf.append((char) c);
291291
c = in.read();
292292
eol = isEndOfLine(c);
293293
}
294294
// ok, start of token reached: comment, encapsulated, or token
295-
if (c == strategy.getCommentStart()) {
295+
if (c == format.getCommentStart()) {
296296
// ignore everything till end of line and continue (incr linecount)
297297
in.readLine();
298298
tkn = nextToken(tkn.reset());
299-
} else if (c == strategy.getDelimiter()) {
299+
} else if (c == format.getDelimiter()) {
300300
// empty token return TT_TOKEN("")
301301
tkn.type = TT_TOKEN;
302302
tkn.isReady = true;
@@ -305,7 +305,7 @@ Token nextToken(Token tkn) throws IOException {
305305
//noop: tkn.content.append("");
306306
tkn.type = TT_EORECORD;
307307
tkn.isReady = true;
308-
} else if (c == strategy.getEncapsulator()) {
308+
} else if (c == format.getEncapsulator()) {
309309
// consume encapsulated token
310310
encapsulatedTokenLexer(tkn, c);
311311
} else if (isEndOfFile(c)) {
@@ -316,7 +316,7 @@ Token nextToken(Token tkn) throws IOException {
316316
} else {
317317
// next token must be a simple token
318318
// add removed blanks when not ignoring whitespace chars...
319-
if (!strategy.isLeadingSpacesIgnored()) {
319+
if (!format.isLeadingSpacesIgnored()) {
320320
tkn.content.append(wsBuf);
321321
}
322322
simpleTokenLexer(tkn, c);
@@ -354,15 +354,15 @@ private Token simpleTokenLexer(Token tkn, int c) throws IOException {
354354
tkn.type = TT_EOF;
355355
tkn.isReady = true;
356356
break;
357-
} else if (c == strategy.getDelimiter()) {
357+
} else if (c == format.getDelimiter()) {
358358
// end of token
359359
tkn.type = TT_TOKEN;
360360
tkn.isReady = true;
361361
break;
362-
} else if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
362+
} else if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
363363
// interpret unicode escaped chars (like \u0070 -> p)
364364
tkn.content.append((char) unicodeEscapeLexer(c));
365-
} else if (c == strategy.getEscape()) {
365+
} else if (c == format.getEscape()) {
366366
tkn.content.append((char) readEscape(c));
367367
} else {
368368
tkn.content.append((char) c);
@@ -371,7 +371,7 @@ private Token simpleTokenLexer(Token tkn, int c) throws IOException {
371371
c = in.read();
372372
}
373373

374-
if (strategy.isTrailingSpacesIgnored()) {
374+
if (format.isTrailingSpacesIgnored()) {
375375
tkn.content.trimTrailingWhitespace();
376376
}
377377

@@ -400,20 +400,20 @@ private Token encapsulatedTokenLexer(Token tkn, int c) throws IOException {
400400
for (; ;) {
401401
c = in.read();
402402

403-
if (c == '\\' && strategy.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
403+
if (c == '\\' && format.isUnicodeEscapesInterpreted() && in.lookAhead() == 'u') {
404404
tkn.content.append((char) unicodeEscapeLexer(c));
405-
} else if (c == strategy.getEscape()) {
405+
} else if (c == format.getEscape()) {
406406
tkn.content.append((char) readEscape(c));
407-
} else if (c == strategy.getEncapsulator()) {
408-
if (in.lookAhead() == strategy.getEncapsulator()) {
407+
} else if (c == format.getEncapsulator()) {
408+
if (in.lookAhead() == format.getEncapsulator()) {
409409
// double or escaped encapsulator -> add single encapsulator to token
410410
c = in.read();
411411
tkn.content.append((char) c);
412412
} else {
413413
// token finish mark (encapsulator) reached: ignore whitespace till delimiter
414414
for (; ;) {
415415
c = in.read();
416-
if (c == strategy.getDelimiter()) {
416+
if (c == format.getDelimiter()) {
417417
tkn.type = TT_TOKEN;
418418
tkn.isReady = true;
419419
return tkn;
@@ -512,12 +512,12 @@ private int readEscape(int c) throws IOException {
512512
// ======================================================
513513

514514
/**
515-
* Obtain the specified CSV Strategy. This should not be modified.
515+
* Obtain the specified CSV format.
516516
*
517-
* @return strategy currently being used
517+
* @return format currently being used
518518
*/
519-
public CSVStrategy getStrategy() {
520-
return this.strategy;
519+
public CSVFormat getFormat() {
520+
return this.format;
521521
}
522522

523523
// ======================================================
@@ -528,7 +528,7 @@ public CSVStrategy getStrategy() {
528528
* @return true if the given char is a whitespace character
529529
*/
530530
private boolean isWhitespace(int c) {
531-
return Character.isWhitespace((char) c) && (c != strategy.getDelimiter());
531+
return Character.isWhitespace((char) c) && (c != format.getDelimiter());
532532
}
533533

534534
/**

0 commit comments

Comments
 (0)