| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.tools; |
| 7 |
|
| 8 |
import java.io.BufferedOutputStream; |
| 9 |
import java.io.BufferedReader; |
| 10 |
import java.io.BufferedWriter; |
| 11 |
import java.io.IOException; |
| 12 |
import java.io.OutputStream; |
| 13 |
import java.io.OutputStreamWriter; |
| 14 |
import java.io.Reader; |
| 15 |
import java.io.Writer; |
| 16 |
import java.nio.charset.Charset; |
| 17 |
import java.nio.charset.StandardCharsets; |
| 18 |
import java.sql.Connection; |
| 19 |
import java.sql.ResultSet; |
| 20 |
import java.sql.ResultSetMetaData; |
| 21 |
import java.sql.SQLException; |
| 22 |
import java.sql.Statement; |
| 23 |
import java.sql.Types; |
| 24 |
import java.util.ArrayList; |
| 25 |
import org.h2.api.ErrorCode; |
| 26 |
import org.h2.engine.Constants; |
| 27 |
import org.h2.message.DbException; |
| 28 |
import org.h2.mvstore.DataUtils; |
| 29 |
import org.h2.store.fs.FileUtils; |
| 30 |
import org.h2.util.IOUtils; |
| 31 |
import org.h2.util.JdbcUtils; |
| 32 |
import org.h2.util.StringUtils; |
| 33 |
import org.h2.util.Utils; |
| 34 |
|
| 35 |
/** |
| 36 |
* A facility to read from and write to CSV (comma separated values) files. When |
| 37 |
* reading, the BOM (the byte-order-mark) character 0xfeff at the beginning of |
| 38 |
* the file is ignored. |
| 39 |
* |
| 40 |
* @author Thomas Mueller, Sylvain Cuaz |
| 41 |
*/ |
| 42 |
public class Csv implements SimpleRowSource { |
| 43 |
|
| 44 |
private String[] columnNames; |
| 45 |
|
| 46 |
private String characterSet; |
| 47 |
private char escapeCharacter = '\"'; |
| 48 |
private char fieldDelimiter = '\"'; |
| 49 |
private char fieldSeparatorRead = ','; |
| 50 |
private String fieldSeparatorWrite = ","; |
| 51 |
private boolean caseSensitiveColumnNames; |
| 52 |
private boolean preserveWhitespace; |
| 53 |
private boolean writeColumnHeader = true; |
| 54 |
private char lineComment; |
| 55 |
private String lineSeparator = System.lineSeparator(); |
| 56 |
private String nullString = ""; |
| 57 |
|
| 58 |
private String fileName; |
| 59 |
private BufferedReader input; |
| 60 |
private char[] inputBuffer; |
| 61 |
private int inputBufferPos; |
| 62 |
private int inputBufferStart = -1; |
| 63 |
private int inputBufferEnd; |
| 64 |
private Writer output; |
| 65 |
private boolean endOfLine, endOfFile; |
| 66 |
|
| 67 |
private int writeResultSet(ResultSet rs) throws SQLException { |
| 68 |
try { |
| 69 |
int rows = 0; |
| 70 |
ResultSetMetaData meta = rs.getMetaData(); |
| 71 |
int columnCount = meta.getColumnCount(); |
| 72 |
String[] row = new String[columnCount]; |
| 73 |
for (int i = 0; i < columnCount; i++) { |
| 74 |
row[i] = meta.getColumnLabel(i + 1); |
| 75 |
} |
| 76 |
if (writeColumnHeader) { |
| 77 |
writeRow(row); |
| 78 |
} |
| 79 |
while (rs.next()) { |
| 80 |
for (int i = 0; i < columnCount; i++) { |
| 81 |
row[i] = rs.getString(i + 1); |
| 82 |
} |
| 83 |
writeRow(row); |
| 84 |
rows++; |
| 85 |
} |
| 86 |
output.close(); |
| 87 |
return rows; |
| 88 |
} catch (IOException e) { |
| 89 |
throw DbException.convertIOException(e, null); |
| 90 |
} finally { |
| 91 |
close(); |
| 92 |
JdbcUtils.closeSilently(rs); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Writes the result set to a file in the CSV format. |
| 98 |
* |
| 99 |
* @param writer the writer |
| 100 |
* @param rs the result set |
| 101 |
* @return the number of rows written |
| 102 |
* @throws SQLException on failure |
| 103 |
*/ |
| 104 |
public int write(Writer writer, ResultSet rs) throws SQLException { |
| 105 |
this.output = writer; |
| 106 |
return writeResultSet(rs); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Writes the result set to a file in the CSV format. The result set is read |
| 111 |
* using the following loop: |
| 112 |
* |
| 113 |
* <pre> |
| 114 |
* while (rs.next()) { |
| 115 |
* writeRow(row); |
| 116 |
* } |
| 117 |
* </pre> |
| 118 |
* |
| 119 |
* @param outputFileName the name of the csv file |
| 120 |
* @param rs the result set - the result set must be positioned before the |
| 121 |
* first row. |
| 122 |
* @param charset the charset or null to use the system default charset |
| 123 |
* @return the number of rows written |
| 124 |
* @throws SQLException on failure |
| 125 |
*/ |
| 126 |
public int write(String outputFileName, ResultSet rs, String charset) |
| 127 |
throws SQLException { |
| 128 |
init(outputFileName, charset); |
| 129 |
try { |
| 130 |
initWrite(); |
| 131 |
return writeResultSet(rs); |
| 132 |
} catch (IOException e) { |
| 133 |
throw convertException("IOException writing " + outputFileName, e); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Writes the result set of a query to a file in the CSV format. |
| 139 |
* |
| 140 |
* @param conn the connection |
| 141 |
* @param outputFileName the file name |
| 142 |
* @param sql the query |
| 143 |
* @param charset the charset or null to use the system default charset |
| 144 |
* (see system property file.encoding) |
| 145 |
* @return the number of rows written |
| 146 |
* @throws SQLException on failure |
| 147 |
*/ |
| 148 |
public int write(Connection conn, String outputFileName, String sql, |
| 149 |
String charset) throws SQLException { |
| 150 |
Statement stat = conn.createStatement(); |
| 151 |
ResultSet rs = stat.executeQuery(sql); |
| 152 |
int rows = write(outputFileName, rs, charset); |
| 153 |
stat.close(); |
| 154 |
return rows; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Reads from the CSV file and returns a result set. The rows in the result |
| 159 |
* set are created on demand, that means the file is kept open until all |
| 160 |
* rows are read or the result set is closed. |
| 161 |
* |
| 162 |
* If the columns are read from the CSV file, then the following rules are |
| 163 |
* used: columns names that start with a letter or '_', and only |
| 164 |
* contain letters, '_', and digits, are considered case insensitive |
| 165 |
* and are converted to uppercase. Other column names are considered |
| 166 |
* case sensitive (that means they need to be quoted when accessed). |
| 167 |
* |
| 168 |
* @param inputFileName the file name |
| 169 |
* @param colNames or null if the column names should be read from the CSV |
| 170 |
* file |
| 171 |
* @param charset the charset or null to use the system default charset |
| 172 |
* @return the result set |
| 173 |
* @throws SQLException on failure |
| 174 |
*/ |
| 175 |
public ResultSet read(String inputFileName, String[] colNames, |
| 176 |
String charset) throws SQLException { |
| 177 |
init(inputFileName, charset); |
| 178 |
try { |
| 179 |
return readResultSet(colNames); |
| 180 |
} catch (IOException e) { |
| 181 |
throw convertException("IOException reading " + inputFileName, e); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Reads CSV data from a reader and returns a result set. The rows in the |
| 187 |
* result set are created on demand, that means the reader is kept open |
| 188 |
* until all rows are read or the result set is closed. |
| 189 |
* |
| 190 |
* @param reader the reader |
| 191 |
* @param colNames or null if the column names should be read from the CSV |
| 192 |
* file |
| 193 |
* @return the result set |
| 194 |
* @throws IOException on failure |
| 195 |
*/ |
| 196 |
public ResultSet read(Reader reader, String[] colNames) throws IOException { |
| 197 |
init(null, null); |
| 198 |
this.input = reader instanceof BufferedReader ? (BufferedReader) reader |
| 199 |
: new BufferedReader(reader, Constants.IO_BUFFER_SIZE); |
| 200 |
return readResultSet(colNames); |
| 201 |
} |
| 202 |
|
| 203 |
private ResultSet readResultSet(String[] colNames) throws IOException { |
| 204 |
this.columnNames = colNames; |
| 205 |
initRead(); |
| 206 |
SimpleResultSet result = new SimpleResultSet(this); |
| 207 |
makeColumnNamesUnique(); |
| 208 |
for (String columnName : columnNames) { |
| 209 |
result.addColumn(columnName, Types.VARCHAR, Integer.MAX_VALUE, 0); |
| 210 |
} |
| 211 |
return result; |
| 212 |
} |
| 213 |
|
| 214 |
private void makeColumnNamesUnique() { |
| 215 |
for (int i = 0; i < columnNames.length; i++) { |
| 216 |
StringBuilder buff = new StringBuilder(); |
| 217 |
String n = columnNames[i]; |
| 218 |
if (n == null || n.isEmpty()) { |
| 219 |
buff.append('C').append(i + 1); |
| 220 |
} else { |
| 221 |
buff.append(n); |
| 222 |
} |
| 223 |
for (int j = 0; j < i; j++) { |
| 224 |
String y = columnNames[j]; |
| 225 |
if (buff.toString().equals(y)) { |
| 226 |
buff.append('1'); |
| 227 |
j = -1; |
| 228 |
} |
| 229 |
} |
| 230 |
columnNames[i] = buff.toString(); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
private void init(String newFileName, String charset) { |
| 235 |
this.fileName = newFileName; |
| 236 |
this.characterSet = charset; |
| 237 |
} |
| 238 |
|
| 239 |
private void initWrite() throws IOException { |
| 240 |
if (output == null) { |
| 241 |
try { |
| 242 |
OutputStream out = FileUtils.newOutputStream(fileName, false); |
| 243 |
out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE); |
| 244 |
output = new BufferedWriter(characterSet != null ? |
| 245 |
new OutputStreamWriter(out, characterSet) : new OutputStreamWriter(out)); |
| 246 |
} catch (Exception e) { |
| 247 |
close(); |
| 248 |
throw DataUtils.convertToIOException(e); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
private void writeRow(String[] values) throws IOException { |
| 254 |
for (int i = 0; i < values.length; i++) { |
| 255 |
if (i > 0) { |
| 256 |
if (fieldSeparatorWrite != null) { |
| 257 |
output.write(fieldSeparatorWrite); |
| 258 |
} |
| 259 |
} |
| 260 |
String s = values[i]; |
| 261 |
if (s != null) { |
| 262 |
if (escapeCharacter != 0) { |
| 263 |
if (fieldDelimiter != 0) { |
| 264 |
output.write(fieldDelimiter); |
| 265 |
} |
| 266 |
output.write(escape(s)); |
| 267 |
if (fieldDelimiter != 0) { |
| 268 |
output.write(fieldDelimiter); |
| 269 |
} |
| 270 |
} else { |
| 271 |
output.write(s); |
| 272 |
} |
| 273 |
} else if (nullString != null && nullString.length() > 0) { |
| 274 |
output.write(nullString); |
| 275 |
} |
| 276 |
} |
| 277 |
output.write(lineSeparator); |
| 278 |
} |
| 279 |
|
| 280 |
private String escape(String data) { |
| 281 |
if (data.indexOf(fieldDelimiter) < 0) { |
| 282 |
if (escapeCharacter == fieldDelimiter || data.indexOf(escapeCharacter) < 0) { |
| 283 |
return data; |
| 284 |
} |
| 285 |
} |
| 286 |
int length = data.length(); |
| 287 |
StringBuilder buff = new StringBuilder(length); |
| 288 |
for (int i = 0; i < length; i++) { |
| 289 |
char ch = data.charAt(i); |
| 290 |
if (ch == fieldDelimiter || ch == escapeCharacter) { |
| 291 |
buff.append(escapeCharacter); |
| 292 |
} |
| 293 |
buff.append(ch); |
| 294 |
} |
| 295 |
return buff.toString(); |
| 296 |
} |
| 297 |
|
| 298 |
private void initRead() throws IOException { |
| 299 |
if (input == null) { |
| 300 |
try { |
| 301 |
input = FileUtils.newBufferedReader(fileName, |
| 302 |
characterSet != null ? Charset.forName(characterSet) : StandardCharsets.UTF_8); |
| 303 |
} catch (IOException e) { |
| 304 |
close(); |
| 305 |
throw e; |
| 306 |
} |
| 307 |
} |
| 308 |
input.mark(1); |
| 309 |
int bom = input.read(); |
| 310 |
if (bom != 0xfeff) { |
| 311 |
// Microsoft Excel compatibility |
| 312 |
// ignore pseudo-BOM |
| 313 |
input.reset(); |
| 314 |
} |
| 315 |
inputBuffer = new char[Constants.IO_BUFFER_SIZE * 2]; |
| 316 |
if (columnNames == null) { |
| 317 |
readHeader(); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
private void readHeader() throws IOException { |
| 322 |
ArrayList<String> list = new ArrayList<>(); |
| 323 |
while (true) { |
| 324 |
String v = readValue(); |
| 325 |
if (v == null) { |
| 326 |
if (endOfLine) { |
| 327 |
if (endOfFile || !list.isEmpty()) { |
| 328 |
break; |
| 329 |
} |
| 330 |
} else { |
| 331 |
v = "COLUMN" + list.size(); |
| 332 |
list.add(v); |
| 333 |
} |
| 334 |
} else { |
| 335 |
if (v.isEmpty()) { |
| 336 |
v = "COLUMN" + list.size(); |
| 337 |
} else if (!caseSensitiveColumnNames && isSimpleColumnName(v)) { |
| 338 |
v = StringUtils.toUpperEnglish(v); |
| 339 |
} |
| 340 |
list.add(v); |
| 341 |
if (endOfLine) { |
| 342 |
break; |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
columnNames = list.toArray(new String[0]); |
| 347 |
} |
| 348 |
|
| 349 |
private static boolean isSimpleColumnName(String columnName) { |
| 350 |
for (int i = 0, length = columnName.length(); i < length; i++) { |
| 351 |
char ch = columnName.charAt(i); |
| 352 |
if (i == 0) { |
| 353 |
if (ch != '_' && !Character.isLetter(ch)) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
} else { |
| 357 |
if (ch != '_' && !Character.isLetterOrDigit(ch)) { |
| 358 |
return false; |
| 359 |
} |
| 360 |
} |
| 361 |
} |
| 362 |
return columnName.length() != 0; |
| 363 |
} |
| 364 |
|
| 365 |
private void pushBack() { |
| 366 |
inputBufferPos--; |
| 367 |
} |
| 368 |
|
| 369 |
private int readChar() throws IOException { |
| 370 |
if (inputBufferPos >= inputBufferEnd) { |
| 371 |
return readBuffer(); |
| 372 |
} |
| 373 |
return inputBuffer[inputBufferPos++]; |
| 374 |
} |
| 375 |
|
| 376 |
private int readBuffer() throws IOException { |
| 377 |
if (endOfFile) { |
| 378 |
return -1; |
| 379 |
} |
| 380 |
int keep; |
| 381 |
if (inputBufferStart >= 0) { |
| 382 |
keep = inputBufferPos - inputBufferStart; |
| 383 |
if (keep > 0) { |
| 384 |
char[] src = inputBuffer; |
| 385 |
if (keep + Constants.IO_BUFFER_SIZE > src.length) { |
| 386 |
inputBuffer = new char[src.length * 2]; |
| 387 |
} |
| 388 |
System.arraycopy(src, inputBufferStart, inputBuffer, 0, keep); |
| 389 |
} |
| 390 |
inputBufferStart = 0; |
| 391 |
} else { |
| 392 |
keep = 0; |
| 393 |
} |
| 394 |
inputBufferPos = keep; |
| 395 |
int len = input.read(inputBuffer, keep, Constants.IO_BUFFER_SIZE); |
| 396 |
if (len == -1) { |
| 397 |
// ensure bufferPos > bufferEnd |
| 398 |
// even after pushBack |
| 399 |
inputBufferEnd = -1024; |
| 400 |
endOfFile = true; |
| 401 |
// ensure the right number of characters are read |
| 402 |
// in case the input buffer is still used |
| 403 |
inputBufferPos++; |
| 404 |
return -1; |
| 405 |
} |
| 406 |
inputBufferEnd = keep + len; |
| 407 |
return inputBuffer[inputBufferPos++]; |
| 408 |
} |
| 409 |
|
| 410 |
private String readValue() throws IOException { |
| 411 |
endOfLine = false; |
| 412 |
inputBufferStart = inputBufferPos; |
| 413 |
while (true) { |
| 414 |
int ch = readChar(); |
| 415 |
if (ch == fieldDelimiter) { |
| 416 |
// delimited value |
| 417 |
boolean containsEscape = false; |
| 418 |
inputBufferStart = inputBufferPos; |
| 419 |
int sep; |
| 420 |
while (true) { |
| 421 |
ch = readChar(); |
| 422 |
if (ch == fieldDelimiter) { |
| 423 |
ch = readChar(); |
| 424 |
if (ch != fieldDelimiter) { |
| 425 |
sep = 2; |
| 426 |
break; |
| 427 |
} |
| 428 |
containsEscape = true; |
| 429 |
} else if (ch == escapeCharacter) { |
| 430 |
ch = readChar(); |
| 431 |
if (ch < 0) { |
| 432 |
sep = 1; |
| 433 |
break; |
| 434 |
} |
| 435 |
containsEscape = true; |
| 436 |
} else if (ch < 0) { |
| 437 |
sep = 1; |
| 438 |
break; |
| 439 |
} |
| 440 |
} |
| 441 |
String s = new String(inputBuffer, |
| 442 |
inputBufferStart, inputBufferPos - inputBufferStart - sep); |
| 443 |
if (containsEscape) { |
| 444 |
s = unEscape(s); |
| 445 |
} |
| 446 |
inputBufferStart = -1; |
| 447 |
while (true) { |
| 448 |
if (ch == fieldSeparatorRead) { |
| 449 |
break; |
| 450 |
} else if (ch == '\n' || ch < 0 || ch == '\r') { |
| 451 |
endOfLine = true; |
| 452 |
break; |
| 453 |
} else if (ch == ' ' || ch == '\t') { |
| 454 |
// ignore |
| 455 |
} else { |
| 456 |
pushBack(); |
| 457 |
break; |
| 458 |
} |
| 459 |
ch = readChar(); |
| 460 |
} |
| 461 |
return s; |
| 462 |
} else if (ch == '\n' || ch < 0 || ch == '\r') { |
| 463 |
endOfLine = true; |
| 464 |
return null; |
| 465 |
} else if (ch == fieldSeparatorRead) { |
| 466 |
// null |
| 467 |
return null; |
| 468 |
} else if (ch <= ' ') { |
| 469 |
// ignore spaces |
| 470 |
} else if (lineComment != 0 && ch == lineComment) { |
| 471 |
// comment until end of line |
| 472 |
inputBufferStart = -1; |
| 473 |
do { |
| 474 |
ch = readChar(); |
| 475 |
} while (ch != '\n' && ch >= 0 && ch != '\r'); |
| 476 |
endOfLine = true; |
| 477 |
return null; |
| 478 |
} else { |
| 479 |
// un-delimited value |
| 480 |
while (true) { |
| 481 |
ch = readChar(); |
| 482 |
if (ch == fieldSeparatorRead) { |
| 483 |
break; |
| 484 |
} else if (ch == '\n' || ch < 0 || ch == '\r') { |
| 485 |
endOfLine = true; |
| 486 |
break; |
| 487 |
} |
| 488 |
} |
| 489 |
String s = new String(inputBuffer, |
| 490 |
inputBufferStart, inputBufferPos - inputBufferStart - 1); |
| 491 |
if (!preserveWhitespace) { |
| 492 |
s = s.trim(); |
| 493 |
} |
| 494 |
inputBufferStart = -1; |
| 495 |
// check un-delimited value for nullString |
| 496 |
return readNull(s); |
| 497 |
} |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
private String readNull(String s) { |
| 502 |
return s.equals(nullString) ? null : s; |
| 503 |
} |
| 504 |
|
| 505 |
private String unEscape(String s) { |
| 506 |
StringBuilder buff = new StringBuilder(s.length()); |
| 507 |
int start = 0; |
| 508 |
char[] chars = null; |
| 509 |
while (true) { |
| 510 |
int idx = s.indexOf(escapeCharacter, start); |
| 511 |
if (idx < 0) { |
| 512 |
idx = s.indexOf(fieldDelimiter, start); |
| 513 |
if (idx < 0) { |
| 514 |
break; |
| 515 |
} |
| 516 |
} |
| 517 |
if (chars == null) { |
| 518 |
chars = s.toCharArray(); |
| 519 |
} |
| 520 |
buff.append(chars, start, idx - start); |
| 521 |
if (idx == s.length() - 1) { |
| 522 |
start = s.length(); |
| 523 |
break; |
| 524 |
} |
| 525 |
buff.append(chars[idx + 1]); |
| 526 |
start = idx + 2; |
| 527 |
} |
| 528 |
buff.append(s, start, s.length()); |
| 529 |
return buff.toString(); |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* INTERNAL |
| 534 |
*/ |
| 535 |
@Override |
| 536 |
public Object[] readRow() throws SQLException { |
| 537 |
if (input == null) { |
| 538 |
return null; |
| 539 |
} |
| 540 |
String[] row = new String[columnNames.length]; |
| 541 |
try { |
| 542 |
int i = 0; |
| 543 |
while (true) { |
| 544 |
String v = readValue(); |
| 545 |
if (v == null) { |
| 546 |
if (endOfLine) { |
| 547 |
if (i == 0) { |
| 548 |
if (endOfFile) { |
| 549 |
return null; |
| 550 |
} |
| 551 |
// empty line |
| 552 |
continue; |
| 553 |
} |
| 554 |
break; |
| 555 |
} |
| 556 |
} |
| 557 |
if (i < row.length) { |
| 558 |
// Empty Strings should be NULL |
| 559 |
// in order to prevent conversion of zero-length String |
| 560 |
// to Number |
| 561 |
row[i++] = v!=null && v.length() > 0 |
| 562 |
? v |
| 563 |
: null; |
| 564 |
} |
| 565 |
if (endOfLine) { |
| 566 |
break; |
| 567 |
} |
| 568 |
} |
| 569 |
} catch (IOException e) { |
| 570 |
throw convertException("IOException reading from " + fileName, e); |
| 571 |
} |
| 572 |
return row; |
| 573 |
} |
| 574 |
|
| 575 |
private static SQLException convertException(String message, Exception e) { |
| 576 |
return DbException.getJdbcSQLException(ErrorCode.IO_EXCEPTION_1, e, message); |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* INTERNAL |
| 581 |
*/ |
| 582 |
@Override |
| 583 |
public void close() { |
| 584 |
IOUtils.closeSilently(input); |
| 585 |
input = null; |
| 586 |
IOUtils.closeSilently(output); |
| 587 |
output = null; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* INTERNAL |
| 592 |
*/ |
| 593 |
@Override |
| 594 |
public void reset() throws SQLException { |
| 595 |
throw new SQLException("Method is not supported", "CSV"); |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Override the field separator for writing. The default is ",". |
| 600 |
* |
| 601 |
* @param fieldSeparatorWrite the field separator |
| 602 |
*/ |
| 603 |
public void setFieldSeparatorWrite(String fieldSeparatorWrite) { |
| 604 |
this.fieldSeparatorWrite = fieldSeparatorWrite; |
| 605 |
} |
| 606 |
|
| 607 |
/** |
| 608 |
* Get the current field separator for writing. |
| 609 |
* |
| 610 |
* @return the field separator |
| 611 |
*/ |
| 612 |
public String getFieldSeparatorWrite() { |
| 613 |
return fieldSeparatorWrite; |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* Override the case sensitive column names setting. The default is false. |
| 618 |
* If enabled, the case of all column names is always preserved. |
| 619 |
* |
| 620 |
* @param caseSensitiveColumnNames whether column names are case sensitive |
| 621 |
*/ |
| 622 |
public void setCaseSensitiveColumnNames(boolean caseSensitiveColumnNames) { |
| 623 |
this.caseSensitiveColumnNames = caseSensitiveColumnNames; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Get the current case sensitive column names setting. |
| 628 |
* |
| 629 |
* @return whether column names are case sensitive |
| 630 |
*/ |
| 631 |
public boolean getCaseSensitiveColumnNames() { |
| 632 |
return caseSensitiveColumnNames; |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Override the field separator for reading. The default is ','. |
| 637 |
* |
| 638 |
* @param fieldSeparatorRead the field separator |
| 639 |
*/ |
| 640 |
public void setFieldSeparatorRead(char fieldSeparatorRead) { |
| 641 |
this.fieldSeparatorRead = fieldSeparatorRead; |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Get the current field separator for reading. |
| 646 |
* |
| 647 |
* @return the field separator |
| 648 |
*/ |
| 649 |
public char getFieldSeparatorRead() { |
| 650 |
return fieldSeparatorRead; |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Set the line comment character. The default is character code 0 (line |
| 655 |
* comments are disabled). |
| 656 |
* |
| 657 |
* @param lineCommentCharacter the line comment character |
| 658 |
*/ |
| 659 |
public void setLineCommentCharacter(char lineCommentCharacter) { |
| 660 |
this.lineComment = lineCommentCharacter; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Get the line comment character. |
| 665 |
* |
| 666 |
* @return the line comment character, or 0 if disabled |
| 667 |
*/ |
| 668 |
public char getLineCommentCharacter() { |
| 669 |
return lineComment; |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Set the field delimiter. The default is " (a double quote). |
| 674 |
* The value 0 means no field delimiter is used. |
| 675 |
* |
| 676 |
* @param fieldDelimiter the field delimiter |
| 677 |
*/ |
| 678 |
public void setFieldDelimiter(char fieldDelimiter) { |
| 679 |
this.fieldDelimiter = fieldDelimiter; |
| 680 |
} |
| 681 |
|
| 682 |
/** |
| 683 |
* Get the current field delimiter. |
| 684 |
* |
| 685 |
* @return the field delimiter |
| 686 |
*/ |
| 687 |
public char getFieldDelimiter() { |
| 688 |
return fieldDelimiter; |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* Set the escape character. The escape character is used to escape the |
| 693 |
* field delimiter. This is needed if the data contains the field delimiter. |
| 694 |
* The default escape character is " (a double quote), which is the same as |
| 695 |
* the field delimiter. If the field delimiter and the escape character are |
| 696 |
* both " (double quote), and the data contains a double quote, then an |
| 697 |
* additional double quote is added. Example: |
| 698 |
* <pre> |
| 699 |
* Data: He said "Hello". |
| 700 |
* Escape character: " |
| 701 |
* Field delimiter: " |
| 702 |
* CSV file: "He said ""Hello""." |
| 703 |
* </pre> |
| 704 |
* If the field delimiter is a double quote and the escape character is a |
| 705 |
* backslash, then escaping is done similar to Java (however, only the field |
| 706 |
* delimiter is escaped). Example: |
| 707 |
* <pre> |
| 708 |
* Data: He said "Hello". |
| 709 |
* Escape character: \ |
| 710 |
* Field delimiter: " |
| 711 |
* CSV file: "He said \"Hello\"." |
| 712 |
* </pre> |
| 713 |
* The value 0 means no escape character is used. |
| 714 |
* |
| 715 |
* @param escapeCharacter the escape character |
| 716 |
*/ |
| 717 |
public void setEscapeCharacter(char escapeCharacter) { |
| 718 |
this.escapeCharacter = escapeCharacter; |
| 719 |
} |
| 720 |
|
| 721 |
/** |
| 722 |
* Get the current escape character. |
| 723 |
* |
| 724 |
* @return the escape character |
| 725 |
*/ |
| 726 |
public char getEscapeCharacter() { |
| 727 |
return escapeCharacter; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Set the line separator used for writing. This is usually a line feed (\n |
| 732 |
* or \r\n depending on the system settings). The line separator is written |
| 733 |
* after each row (including the last row), so this option can include an |
| 734 |
* end-of-row marker if needed. |
| 735 |
* |
| 736 |
* @param lineSeparator the line separator |
| 737 |
*/ |
| 738 |
public void setLineSeparator(String lineSeparator) { |
| 739 |
this.lineSeparator = lineSeparator; |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Get the line separator used for writing. |
| 744 |
* |
| 745 |
* @return the line separator |
| 746 |
*/ |
| 747 |
public String getLineSeparator() { |
| 748 |
return lineSeparator; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Set the value that represents NULL. It is only used for non-delimited |
| 753 |
* values. |
| 754 |
* |
| 755 |
* @param nullString the null |
| 756 |
*/ |
| 757 |
public void setNullString(String nullString) { |
| 758 |
this.nullString = nullString; |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* Get the current null string. |
| 763 |
* |
| 764 |
* @return the null string. |
| 765 |
*/ |
| 766 |
public String getNullString() { |
| 767 |
return nullString; |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* Enable or disable preserving whitespace in unquoted text. |
| 772 |
* |
| 773 |
* @param value the new value for the setting |
| 774 |
*/ |
| 775 |
public void setPreserveWhitespace(boolean value) { |
| 776 |
this.preserveWhitespace = value; |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Whether whitespace in unquoted text is preserved. |
| 781 |
* |
| 782 |
* @return the current value for the setting |
| 783 |
*/ |
| 784 |
public boolean getPreserveWhitespace() { |
| 785 |
return preserveWhitespace; |
| 786 |
} |
| 787 |
|
| 788 |
/** |
| 789 |
* Enable or disable writing the column header. |
| 790 |
* |
| 791 |
* @param value the new value for the setting |
| 792 |
*/ |
| 793 |
public void setWriteColumnHeader(boolean value) { |
| 794 |
this.writeColumnHeader = value; |
| 795 |
} |
| 796 |
|
| 797 |
/** |
| 798 |
* Whether the column header is written. |
| 799 |
* |
| 800 |
* @return the current value for the setting |
| 801 |
*/ |
| 802 |
public boolean getWriteColumnHeader() { |
| 803 |
return writeColumnHeader; |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* INTERNAL. |
| 808 |
* Parse and set the CSV options. |
| 809 |
* |
| 810 |
* @param options the options |
| 811 |
* @return the character set |
| 812 |
*/ |
| 813 |
public String setOptions(String options) { |
| 814 |
String charset = null; |
| 815 |
String[] keyValuePairs = StringUtils.arraySplit(options, ' ', false); |
| 816 |
for (String pair : keyValuePairs) { |
| 817 |
if (pair.isEmpty()) { |
| 818 |
continue; |
| 819 |
} |
| 820 |
int index = pair.indexOf('='); |
| 821 |
String key = StringUtils.trimSubstring(pair, 0, index); |
| 822 |
String value = pair.substring(index + 1); |
| 823 |
char ch = value.isEmpty() ? 0 : value.charAt(0); |
| 824 |
if (isParam(key, "escape", "esc", "escapeCharacter")) { |
| 825 |
setEscapeCharacter(ch); |
| 826 |
} else if (isParam(key, "fieldDelimiter", "fieldDelim")) { |
| 827 |
setFieldDelimiter(ch); |
| 828 |
} else if (isParam(key, "fieldSeparator", "fieldSep")) { |
| 829 |
setFieldSeparatorRead(ch); |
| 830 |
setFieldSeparatorWrite(value); |
| 831 |
} else if (isParam(key, "lineComment", "lineCommentCharacter")) { |
| 832 |
setLineCommentCharacter(ch); |
| 833 |
} else if (isParam(key, "lineSeparator", "lineSep")) { |
| 834 |
setLineSeparator(value); |
| 835 |
} else if (isParam(key, "null", "nullString")) { |
| 836 |
setNullString(value); |
| 837 |
} else if (isParam(key, "charset", "characterSet")) { |
| 838 |
charset = value; |
| 839 |
} else if (isParam(key, "preserveWhitespace")) { |
| 840 |
setPreserveWhitespace(Utils.parseBoolean(value, false, false)); |
| 841 |
} else if (isParam(key, "writeColumnHeader")) { |
| 842 |
setWriteColumnHeader(Utils.parseBoolean(value, true, false)); |
| 843 |
} else if (isParam(key, "caseSensitiveColumnNames")) { |
| 844 |
setCaseSensitiveColumnNames(Utils.parseBoolean(value, false, false)); |
| 845 |
} else { |
| 846 |
throw DbException.getUnsupportedException(key); |
| 847 |
} |
| 848 |
} |
| 849 |
return charset; |
| 850 |
} |
| 851 |
|
| 852 |
private static boolean isParam(String key, String... values) { |
| 853 |
for (String v : values) { |
| 854 |
if (key.equalsIgnoreCase(v)) { |
| 855 |
return true; |
| 856 |
} |
| 857 |
} |
| 858 |
return false; |
| 859 |
} |
| 860 |
|
| 861 |
} |