Skip to content

Commit 1166ca6

Browse files
committed
No functional changes are contained in this commit: reformatted Java code to fix several formatting inconsistencies (between classes and within the same class); sorry for the big commit, but I have preferred to isolate into one commit all the formatting changes.
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1065950 13f79535-47bb-0310-9956-ffa450edef68
1 parent fe5bd51 commit 1166ca6

20 files changed

Lines changed: 2366 additions & 2192 deletions

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

Lines changed: 556 additions & 525 deletions
Large diffs are not rendered by default.

src/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 255 additions & 248 deletions
Large diffs are not rendered by default.

src/java/org/apache/commons/csv/CSVStrategy.java

Lines changed: 105 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
/**
2222
* CSVStrategy
23-
*
23+
*
2424
* Represents the strategy for a CSV.
2525
*/
2626
public class CSVStrategy implements Cloneable, Serializable {
@@ -41,47 +41,46 @@ public class CSVStrategy implements Cloneable, Serializable {
4141
// an EOF signal (-1), and because \ufffe in UTF-16 would be
4242
// encoded as two chars (using surrogates) and thus there should never
4343
// be a collision with a real text char.
44-
public static char COMMENTS_DISABLED = (char)-2;
45-
public static char ESCAPE_DISABLED = (char)-2;
46-
public static char ENCAPSULATOR_DISABLED = (char)-2;
44+
public static char COMMENTS_DISABLED = (char) -2;
45+
public static char ESCAPE_DISABLED = (char) -2;
46+
public static char ENCAPSULATOR_DISABLED = (char) -2;
4747

48-
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
49-
true, false, true);
50-
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
51-
false, false, false);
52-
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
53-
true, false, true);
48+
public static CSVStrategy DEFAULT_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
49+
true, false, true);
50+
public static CSVStrategy EXCEL_STRATEGY = new CSVStrategy(',', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, false,
51+
false, false, false);
52+
public static CSVStrategy TDF_STRATEGY = new CSVStrategy('\t', '"', COMMENTS_DISABLED, ESCAPE_DISABLED, true,
53+
true, false, true);
5454

5555

5656
public CSVStrategy(char delimiter, char encapsulator, char commentStart) {
5757
this(delimiter, encapsulator, commentStart, true, false, true);
5858
}
59-
59+
6060
/**
6161
* Customized CSV strategy setter.
6262
*
63-
* @param delimiter a Char used for value separation
64-
* @param encapsulator a Char used as value encapsulation marker
65-
* @param commentStart a Char used for comment identification
66-
* @param escape a Char used to escape special characters in values
67-
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
68-
* ignored
63+
* @param delimiter a Char used for value separation
64+
* @param encapsulator a Char used as value encapsulation marker
65+
* @param commentStart a Char used for comment identification
66+
* @param escape a Char used to escape special characters in values
67+
* @param ignoreLeadingWhitespace TRUE when leading whitespaces should be
68+
* ignored
6969
* @param ignoreTrailingWhitespace TRUE when trailing whitespaces should be
70-
* ignored
71-
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
72-
* interpreted
73-
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
70+
* ignored
71+
* @param interpretUnicodeEscapes TRUE when unicode escapes should be
72+
* interpreted
73+
* @param ignoreEmptyLines TRUE when the parser should skip emtpy lines
7474
*/
7575
public CSVStrategy(
76-
char delimiter,
77-
char encapsulator,
78-
char commentStart,
79-
char escape,
80-
boolean ignoreLeadingWhitespace,
81-
boolean ignoreTrailingWhitespace,
82-
boolean interpretUnicodeEscapes,
83-
boolean ignoreEmptyLines)
84-
{
76+
char delimiter,
77+
char encapsulator,
78+
char commentStart,
79+
char escape,
80+
boolean ignoreLeadingWhitespace,
81+
boolean ignoreTrailingWhitespace,
82+
boolean interpretUnicodeEscapes,
83+
boolean ignoreEmptyLines) {
8584
setDelimiter(delimiter);
8685
setEncapsulator(encapsulator);
8786
setCommentStart(commentStart);
@@ -92,62 +91,101 @@ public CSVStrategy(
9291
setIgnoreEmptyLines(ignoreEmptyLines);
9392
}
9493

95-
/** @deprecated */
94+
/**
95+
* @deprecated
96+
*/
9697
public CSVStrategy(
97-
char delimiter,
98-
char encapsulator,
99-
char commentStart,
100-
boolean ignoreLeadingWhitespace,
101-
boolean interpretUnicodeEscapes,
102-
boolean ignoreEmptyLines)
103-
{
104-
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
105-
true, interpretUnicodeEscapes, ignoreEmptyLines);
98+
char delimiter,
99+
char encapsulator,
100+
char commentStart,
101+
boolean ignoreLeadingWhitespace,
102+
boolean interpretUnicodeEscapes,
103+
boolean ignoreEmptyLines) {
104+
this(delimiter, encapsulator, commentStart, CSVStrategy.ESCAPE_DISABLED, ignoreLeadingWhitespace,
105+
true, interpretUnicodeEscapes, ignoreEmptyLines);
106+
}
107+
108+
public void setDelimiter(char delimiter) {
109+
this.delimiter = delimiter;
110+
}
111+
112+
public char getDelimiter() {
113+
return this.delimiter;
114+
}
115+
116+
public void setEncapsulator(char encapsulator) {
117+
this.encapsulator = encapsulator;
118+
}
119+
120+
public char getEncapsulator() {
121+
return this.encapsulator;
122+
}
123+
124+
public void setCommentStart(char commentStart) {
125+
this.commentStart = commentStart;
106126
}
107127

108-
public void setDelimiter(char delimiter) { this.delimiter = delimiter; }
109-
public char getDelimiter() { return this.delimiter; }
128+
public char getCommentStart() {
129+
return this.commentStart;
130+
}
131+
132+
public boolean isCommentingDisabled() {
133+
return this.commentStart == COMMENTS_DISABLED;
134+
}
135+
136+
public void setEscape(char escape) {
137+
this.escape = escape;
138+
}
139+
140+
public char getEscape() {
141+
return this.escape;
142+
}
110143

111-
public void setEncapsulator(char encapsulator) { this.encapsulator = encapsulator; }
112-
public char getEncapsulator() { return this.encapsulator; }
144+
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
145+
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
146+
}
113147

114-
public void setCommentStart(char commentStart) { this.commentStart = commentStart; }
115-
public char getCommentStart() { return this.commentStart; }
116-
public boolean isCommentingDisabled() { return this.commentStart == COMMENTS_DISABLED; }
148+
public boolean getIgnoreLeadingWhitespaces() {
149+
return this.ignoreLeadingWhitespaces;
150+
}
117151

118-
public void setEscape(char escape) { this.escape = escape; }
119-
public char getEscape() { return this.escape; }
152+
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
153+
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
154+
}
120155

121-
public void setIgnoreLeadingWhitespaces(boolean ignoreLeadingWhitespaces) {
122-
this.ignoreLeadingWhitespaces = ignoreLeadingWhitespaces;
156+
public boolean getIgnoreTrailingWhitespaces() {
157+
return this.ignoreTrailingWhitespaces;
123158
}
124-
public boolean getIgnoreLeadingWhitespaces() { return this.ignoreLeadingWhitespaces; }
125159

126-
public void setIgnoreTrailingWhitespaces(boolean ignoreTrailingWhitespaces) {
127-
this.ignoreTrailingWhitespaces = ignoreTrailingWhitespaces;
160+
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
161+
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
128162
}
129-
public boolean getIgnoreTrailingWhitespaces() { return this.ignoreTrailingWhitespaces; }
130163

131-
public void setUnicodeEscapeInterpretation(boolean interpretUnicodeEscapes) {
132-
this.interpretUnicodeEscapes = interpretUnicodeEscapes;
164+
public boolean getUnicodeEscapeInterpretation() {
165+
return this.interpretUnicodeEscapes;
133166
}
134-
public boolean getUnicodeEscapeInterpretation() { return this.interpretUnicodeEscapes; }
135167

136-
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) { this.ignoreEmptyLines = ignoreEmptyLines; }
137-
public boolean getIgnoreEmptyLines() { return this.ignoreEmptyLines; }
168+
public void setIgnoreEmptyLines(boolean ignoreEmptyLines) {
169+
this.ignoreEmptyLines = ignoreEmptyLines;
170+
}
171+
172+
public boolean getIgnoreEmptyLines() {
173+
return this.ignoreEmptyLines;
174+
}
138175

139176
public void setPrinterNewline(String newline) {
140-
this.printerNewline = newline;
177+
this.printerNewline = newline;
141178
}
179+
142180
public String getPrinterNewline() {
143-
return this.printerNewline;
181+
return this.printerNewline;
144182
}
145183

146184
public Object clone() {
147-
try {
148-
return super.clone();
149-
} catch (CloneNotSupportedException e) {
150-
throw new RuntimeException(e); // impossible
151-
}
185+
try {
186+
return super.clone();
187+
} catch (CloneNotSupportedException e) {
188+
throw new RuntimeException(e); // impossible
189+
}
152190
}
153191
}

src/java/org/apache/commons/csv/CSVUtils.java

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class CSVUtils {
3030

3131
/**
3232
* <p><code>CSVUtils</code> instances should NOT be constructed in
33-
* standard programming.
33+
* standard programming.
3434
*
3535
* <p>This constructor is public to permit tools that require a JavaBean
3636
* instance to operate.</p>
3737
*/
3838
public CSVUtils() {
3939
}
40-
40+
4141
/**
4242
* Converts an array of string values into a single CSV line. All
4343
* <code>null</code> values are converted to the string <code>"null"</code>,
@@ -46,13 +46,13 @@ public CSVUtils() {
4646
*
4747
* @param values the value array
4848
* @return the CSV string, will be an empty string if the length of the
49-
* value array is 0
49+
* value array is 0
5050
*/
5151
public static String printLine(String[] values, CSVStrategy strategy) {
5252
// set up a CSVUtils
5353
StringWriter stringWriter = new StringWriter();
5454
CSVPrinter csvPrinter = new CSVPrinter(stringWriter, strategy);
55-
55+
5656
// check for null values an "null" as strings and convert them
5757
// into the strings "null" and "\"null\""
5858
for (int i = 0; i < values.length; i++) {
@@ -62,60 +62,60 @@ public static String printLine(String[] values, CSVStrategy strategy) {
6262
values[i] = "\"null\"";
6363
}
6464
}
65-
65+
6666
// convert to CSV
6767
try {
68-
csvPrinter.println(values);
68+
csvPrinter.println(values);
6969
} catch (IOException e) {
70-
// should not happen with StringWriter
70+
// should not happen with StringWriter
7171
}
7272
// as the resulting string has \r\n at the end, we will trim that away
7373
return stringWriter.toString().trim();
7474
}
75-
76-
// ======================================================
77-
// static parsers
78-
// ======================================================
79-
80-
/**
81-
* Parses the given String according to the default {@link CSVStrategy}.
82-
*
83-
* @param s CSV String to be parsed.
84-
* @return parsed String matrix (which is never null)
85-
* @throws IOException in case of error
86-
*/
87-
public static String[][] parse(String s) throws IOException {
88-
if (s == null) {
89-
throw new IllegalArgumentException("Null argument not allowed.");
90-
}
91-
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
92-
if (result == null) {
93-
// since CSVStrategy ignores empty lines an empty array is returned
94-
// (i.e. not "result = new String[][] {{""}};")
95-
result = EMPTY_DOUBLE_STRING_ARRAY;
96-
}
97-
return result;
98-
}
99-
100-
/**
101-
* Parses the first line only according to the default {@link CSVStrategy}.
102-
*
103-
* Parsing empty string will be handled as valid records containing zero
104-
* elements, so the following property holds: parseLine("").length == 0.
105-
*
106-
* @param s CSV String to be parsed.
107-
* @return parsed String vector (which is never null)
108-
* @throws IOException in case of error
109-
*/
110-
public static String[] parseLine(String s) throws IOException {
111-
if (s == null) {
112-
throw new IllegalArgumentException("Null argument not allowed.");
75+
76+
// ======================================================
77+
// static parsers
78+
// ======================================================
79+
80+
/**
81+
* Parses the given String according to the default {@link CSVStrategy}.
82+
*
83+
* @param s CSV String to be parsed.
84+
* @return parsed String matrix (which is never null)
85+
* @throws IOException in case of error
86+
*/
87+
public static String[][] parse(String s) throws IOException {
88+
if (s == null) {
89+
throw new IllegalArgumentException("Null argument not allowed.");
90+
}
91+
String[][] result = (new CSVParser(new StringReader(s))).getAllValues();
92+
if (result == null) {
93+
// since CSVStrategy ignores empty lines an empty array is returned
94+
// (i.e. not "result = new String[][] {{""}};")
95+
result = EMPTY_DOUBLE_STRING_ARRAY;
96+
}
97+
return result;
11398
}
114-
// uh,jh: make sure that parseLine("").length == 0
115-
if (s.length() == 0) {
116-
return EMPTY_STRING_ARRAY;
99+
100+
/**
101+
* Parses the first line only according to the default {@link CSVStrategy}.
102+
*
103+
* Parsing empty string will be handled as valid records containing zero
104+
* elements, so the following property holds: parseLine("").length == 0.
105+
*
106+
* @param s CSV String to be parsed.
107+
* @return parsed String vector (which is never null)
108+
* @throws IOException in case of error
109+
*/
110+
public static String[] parseLine(String s) throws IOException {
111+
if (s == null) {
112+
throw new IllegalArgumentException("Null argument not allowed.");
113+
}
114+
// uh,jh: make sure that parseLine("").length == 0
115+
if (s.length() == 0) {
116+
return EMPTY_STRING_ARRAY;
117+
}
118+
return (new CSVParser(new StringReader(s))).getLine();
117119
}
118-
return (new CSVParser(new StringReader(s))).getLine();
119-
}
120-
120+
121121
}

0 commit comments

Comments
 (0)