/
CsvUtils.java
181 lines (165 loc) · 6.59 KB
/
CsvUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.io.data;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import org.jumpmind.exception.IoException;
import org.jumpmind.symmetric.csv.CsvReader;
import org.jumpmind.symmetric.csv.CsvWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CsvUtils {
private static final Logger log = LoggerFactory.getLogger(CsvUtils.class);
public static final String DELIMITER = ", ";
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static CsvReader getCsvReader(Reader reader) {
CsvReader csvReader = new CsvReader(reader);
csvReader.setEscapeMode(CsvReader.ESCAPE_MODE_BACKSLASH);
csvReader.setSafetySwitch(false);
csvReader.setCaptureRawRecord(false);
return csvReader;
}
public static CsvReader getCsvReaderDquote(Reader reader) {
CsvReader csvReader = new CsvReader(reader);
csvReader.setEscapeMode(CsvWriter.ESCAPE_MODE_DOUBLED);
csvReader.setSafetySwitch(false);
csvReader.setCaptureRawRecord(false);
return csvReader;
}
public static String[] tokenizeCsvData(String csvData) {
String[] tokens = null;
if (csvData != null) {
try (StringReader sr = new StringReader(csvData); CsvReader csvReader = getCsvReader(sr)) {
if (csvReader.readRecord()) {
tokens = csvReader.getValues();
}
} catch (IOException e) {
}
}
return tokens;
}
/**
* This escapes backslashes but doesn't wrap the data in a text qualifier.
*/
public static String escapeCsvData(String data) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',')) {
writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
writer.write(data);
writer.flush();
return out.toString();
} catch (IOException e) {
return data;
}
}
public static String escapeAndQuoteCsvData(String data) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',')) {
writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
writer.setTextQualifier('"');
writer.setUseTextQualifier(true);
writer.setForceQualifier(false);
writer.write(data, true);
writer.flush();
return out.toString();
} catch (IOException e) {
return data;
}
}
public static String escapeCsvData(String[] data) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',')) {
writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
writer.setTextQualifier('\"');
writer.setUseTextQualifier(true);
writer.setForceQualifier(true);
for (String s : data) {
writer.write(s, true);
}
writer.flush();
return out.toString();
} catch (IOException e) {
throw new IoException(e);
}
}
public static String escapeCsvData(String[] data, char recordDelimiter, char textQualifier) {
return escapeCsvData(data, recordDelimiter, textQualifier, CsvWriter.ESCAPE_MODE_BACKSLASH);
}
public static String escapeCsvData(String[] data, char recordDelimiter, char textQualifier, int escapeMode) {
return escapeCsvData(data, recordDelimiter, textQualifier, escapeMode, null);
}
public static String escapeCsvData(String[] data, char recordDelimiter, char textQualifier, int escapeMode, String nullString) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',')) {
writer.setEscapeMode(escapeMode);
if (recordDelimiter != '\0') {
writer.setRecordDelimiter(recordDelimiter);
}
if (textQualifier != '\0') {
writer.setTextQualifier(textQualifier);
writer.setUseTextQualifier(true);
writer.setForceQualifier(true);
}
if (nullString != null) {
writer.setNullString(nullString);
}
writer.writeRecord(data, true);
writer.flush();
return out.toString();
} catch (IOException e) {
throw new IoException(e);
}
}
public static int write(Writer writer, String... data) {
try {
StringBuilder buffer = new StringBuilder();
for (String string : data) {
buffer.append(string);
}
writer.write(buffer.toString());
if (log.isDebugEnabled()) {
log.debug(buffer.toString());
}
return buffer.length();
} catch (IOException ex) {
throw new IoException(ex);
}
}
public static void writeSql(String sql, Writer writer) {
write(writer, CsvConstants.SQL, DELIMITER, sql);
writeLineFeed(writer);
}
public static void writeBsh(String script, Writer writer) {
write(writer, CsvConstants.BSH, DELIMITER, script);
writeLineFeed(writer);
}
public static void writeLineFeed(Writer writer) {
try {
writer.write(LINE_SEPARATOR);
} catch (IOException ex) {
throw new IoException(ex);
}
}
}