CsvWriter.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /**
  2. *
  3. * Copyright (c) behosoft Co.,Ltd.
  4. * All Rights Reserved.
  5. *
  6. * This software is the confidential and proprietary information of behosoft.
  7. * (Social Security Department). You shall not disclose such
  8. * Confidential Information and shall use it only in accordance with
  9. * the terms of the license agreement you entered into with behosoft.
  10. *
  11. * Distributable under GNU LGPL license by gnu.org
  12. */
  13. package com.behosoft.util;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.OutputStream;
  17. import java.io.OutputStreamWriter;
  18. import java.io.BufferedWriter;
  19. import java.io.Writer;
  20. import java.nio.charset.Charset;
  21. /**
  22. * A stream based writer for writing delimited text data to a file or a stream.
  23. */
  24. public class CsvWriter {
  25. private Writer outputStream = null;
  26. private String fileName = null;
  27. private boolean firstColumn = true;
  28. private boolean useCustomRecordDelimiter = false;
  29. private Charset charset = null;
  30. // this holds all the values for switches that the user is allowed to set
  31. private UserSettings userSettings = new UserSettings();
  32. private boolean initialized = false;
  33. private boolean closed = false;
  34. private String systemRecordDelimiter = System.getProperty("line.separator");
  35. /**
  36. * Double up the text qualifier to represent an occurrence of the text
  37. * qualifier.
  38. */
  39. public static final int ESCAPE_MODE_DOUBLED = 1;
  40. /**
  41. * Use a backslash character before the text qualifier to represent an
  42. * occurrence of the text qualifier.
  43. */
  44. public static final int ESCAPE_MODE_BACKSLASH = 2;
  45. /**
  46. * Creates a {@link com.csvreader.CsvWriter CsvWriter} object using a file
  47. * as the data destination.
  48. *
  49. * @param fileName
  50. * The path to the file to output the data.
  51. * @param delimiter
  52. * The character to use as the column delimiter.
  53. * @param charset
  54. * The {@link java.nio.charset.Charset Charset} to use while
  55. * writing the data.
  56. */
  57. public CsvWriter(String fileName, char delimiter, Charset charset) {
  58. if (fileName == null) {
  59. throw new IllegalArgumentException("Parameter fileName can not be null.");
  60. }
  61. if (charset == null) {
  62. throw new IllegalArgumentException("Parameter charset can not be null.");
  63. }
  64. this.fileName = fileName;
  65. userSettings.Delimiter = delimiter;
  66. this.charset = charset;
  67. }
  68. /**
  69. * Creates a {@link com.csvreader.CsvWriter CsvWriter} object using a file
  70. * as the data destination. Uses a comma as the column delimiter and
  71. * ISO-8859-1 as the {@link java.nio.charset.Charset Charset}.
  72. *
  73. * @param fileName
  74. * The path to the file to output the data.
  75. */
  76. public CsvWriter(String fileName) {
  77. this(fileName, Letters.COMMA, Charset.forName("ISO-8859-1"));
  78. }
  79. /**
  80. * Creates a {@link com.csvreader.CsvWriter CsvWriter} object using a Writer
  81. * to write data to.
  82. *
  83. * @param outputStream
  84. * The stream to write the column delimited data to.
  85. * @param delimiter
  86. * The character to use as the column delimiter.
  87. */
  88. public CsvWriter(Writer outputStream, char delimiter) {
  89. if (outputStream == null) {
  90. throw new IllegalArgumentException("Parameter outputStream can not be null.");
  91. }
  92. this.outputStream = outputStream;
  93. userSettings.Delimiter = delimiter;
  94. initialized = true;
  95. }
  96. /**
  97. * Creates a {@link com.csvreader.CsvWriter CsvWriter} object using an
  98. * OutputStream to write data to.
  99. *
  100. * @param outputStream
  101. * The stream to write the column delimited data to.
  102. * @param delimiter
  103. * The character to use as the column delimiter.
  104. * @param charset
  105. * The {@link java.nio.charset.Charset Charset} to use while
  106. * writing the data.
  107. */
  108. public CsvWriter(OutputStream outputStream, char delimiter, Charset charset) {
  109. this(new OutputStreamWriter(outputStream, charset), delimiter);
  110. }
  111. /**
  112. * Gets the character being used as the column delimiter.
  113. *
  114. * @return The character being used as the column delimiter.
  115. */
  116. public char getDelimiter() {
  117. return userSettings.Delimiter;
  118. }
  119. /**
  120. * Sets the character to use as the column delimiter.
  121. *
  122. * @param delimiter
  123. * The character to use as the column delimiter.
  124. */
  125. public void setDelimiter(char delimiter) {
  126. userSettings.Delimiter = delimiter;
  127. }
  128. public char getRecordDelimiter() {
  129. return userSettings.RecordDelimiter;
  130. }
  131. /**
  132. * Sets the character to use as the record delimiter.
  133. *
  134. * @param recordDelimiter
  135. * The character to use as the record delimiter. Default is
  136. * combination of standard end of line characters for Windows,
  137. * Unix, or Mac.
  138. */
  139. public void setRecordDelimiter(char recordDelimiter) {
  140. useCustomRecordDelimiter = true;
  141. userSettings.RecordDelimiter = recordDelimiter;
  142. }
  143. /**
  144. * Gets the character to use as a text qualifier in the data.
  145. *
  146. * @return The character to use as a text qualifier in the data.
  147. */
  148. public char getTextQualifier() {
  149. return userSettings.TextQualifier;
  150. }
  151. /**
  152. * Sets the character to use as a text qualifier in the data.
  153. *
  154. * @param textQualifier
  155. * The character to use as a text qualifier in the data.
  156. */
  157. public void setTextQualifier(char textQualifier) {
  158. userSettings.TextQualifier = textQualifier;
  159. }
  160. /**
  161. * Whether text qualifiers will be used while writing data or not.
  162. *
  163. * @return Whether text qualifiers will be used while writing data or not.
  164. */
  165. public boolean getUseTextQualifier() {
  166. return userSettings.UseTextQualifier;
  167. }
  168. /**
  169. * Sets whether text qualifiers will be used while writing data or not.
  170. *
  171. * @param useTextQualifier
  172. * Whether to use a text qualifier while writing data or not.
  173. */
  174. public void setUseTextQualifier(boolean useTextQualifier) {
  175. userSettings.UseTextQualifier = useTextQualifier;
  176. }
  177. public int getEscapeMode() {
  178. return userSettings.EscapeMode;
  179. }
  180. public void setEscapeMode(int escapeMode) {
  181. userSettings.EscapeMode = escapeMode;
  182. }
  183. public void setComment(char comment) {
  184. userSettings.Comment = comment;
  185. }
  186. public char getComment() {
  187. return userSettings.Comment;
  188. }
  189. /**
  190. * Whether fields will be surrounded by the text qualifier even if the
  191. * qualifier is not necessarily needed to escape this field.
  192. *
  193. * @return Whether fields will be forced to be qualified or not.
  194. */
  195. public boolean getForceQualifier() {
  196. return userSettings.ForceQualifier;
  197. }
  198. /**
  199. * Use this to force all fields to be surrounded by the text qualifier even
  200. * if the qualifier is not necessarily needed to escape this field. Default
  201. * is false.
  202. *
  203. * @param forceQualifier
  204. * Whether to force the fields to be qualified or not.
  205. */
  206. public void setForceQualifier(boolean forceQualifier) {
  207. userSettings.ForceQualifier = forceQualifier;
  208. }
  209. /**
  210. * Writes another column of data to this record.
  211. *
  212. * @param content
  213. * The data for the new column.
  214. * @param preserveSpaces
  215. * Whether to preserve leading and trailing whitespace in this
  216. * column of data.
  217. * @exception IOException
  218. * Thrown if an error occurs while writing data to the
  219. * destination stream.
  220. */
  221. public void write(String content, boolean preserveSpaces)
  222. throws IOException {
  223. checkClosed();
  224. checkInit();
  225. if (content == null) {
  226. content = "";
  227. }
  228. if (!firstColumn) {
  229. outputStream.write(userSettings.Delimiter);
  230. }
  231. boolean textQualify = userSettings.ForceQualifier;
  232. if (!preserveSpaces && content.length() > 0) {
  233. content = content.trim();
  234. }
  235. if (!textQualify
  236. && userSettings.UseTextQualifier
  237. && (content.indexOf(userSettings.TextQualifier) > -1
  238. || content.indexOf(userSettings.Delimiter) > -1
  239. || (!useCustomRecordDelimiter && (content
  240. .indexOf(Letters.LF) > -1 || content
  241. .indexOf(Letters.CR) > -1))
  242. || (useCustomRecordDelimiter && content
  243. .indexOf(userSettings.RecordDelimiter) > -1)
  244. || (firstColumn && content.length() > 0 && content
  245. .charAt(0) == userSettings.Comment) ||
  246. // check for empty first column, which if on its own line must
  247. // be qualified or the line will be skipped
  248. (firstColumn && content.length() == 0))) {
  249. textQualify = true;
  250. }
  251. if (userSettings.UseTextQualifier && !textQualify
  252. && content.length() > 0 && preserveSpaces) {
  253. char firstLetter = content.charAt(0);
  254. if (firstLetter == Letters.SPACE || firstLetter == Letters.TAB) {
  255. textQualify = true;
  256. }
  257. if (!textQualify && content.length() > 1) {
  258. char lastLetter = content.charAt(content.length() - 1);
  259. if (lastLetter == Letters.SPACE || lastLetter == Letters.TAB) {
  260. textQualify = true;
  261. }
  262. }
  263. }
  264. if (textQualify) {
  265. outputStream.write(userSettings.TextQualifier);
  266. if (userSettings.EscapeMode == ESCAPE_MODE_BACKSLASH) {
  267. content = replace(content, "" + Letters.BACKSLASH, ""
  268. + Letters.BACKSLASH + Letters.BACKSLASH);
  269. content = replace(content, "" + userSettings.TextQualifier, ""
  270. + Letters.BACKSLASH + userSettings.TextQualifier);
  271. } else {
  272. content = replace(content, "" + userSettings.TextQualifier, ""
  273. + userSettings.TextQualifier
  274. + userSettings.TextQualifier);
  275. }
  276. } else if (userSettings.EscapeMode == ESCAPE_MODE_BACKSLASH) {
  277. content = replace(content, "" + Letters.BACKSLASH, ""
  278. + Letters.BACKSLASH + Letters.BACKSLASH);
  279. content = replace(content, "" + userSettings.Delimiter, ""
  280. + Letters.BACKSLASH + userSettings.Delimiter);
  281. if (useCustomRecordDelimiter) {
  282. content = replace(content, "" + userSettings.RecordDelimiter,
  283. "" + Letters.BACKSLASH + userSettings.RecordDelimiter);
  284. } else {
  285. content = replace(content, "" + Letters.CR, ""
  286. + Letters.BACKSLASH + Letters.CR);
  287. content = replace(content, "" + Letters.LF, ""
  288. + Letters.BACKSLASH + Letters.LF);
  289. }
  290. if (firstColumn && content.length() > 0
  291. && content.charAt(0) == userSettings.Comment) {
  292. if (content.length() > 1) {
  293. content = "" + Letters.BACKSLASH + userSettings.Comment
  294. + content.substring(1);
  295. } else {
  296. content = "" + Letters.BACKSLASH + userSettings.Comment;
  297. }
  298. }
  299. }
  300. outputStream.write(content);
  301. if (textQualify) {
  302. outputStream.write(userSettings.TextQualifier);
  303. }
  304. firstColumn = false;
  305. }
  306. /**
  307. * Writes another column of data to this record. Does not preserve
  308. * leading and trailing whitespace in this column of data.
  309. *
  310. * @param content
  311. * The data for the new column.
  312. * @exception IOException
  313. * Thrown if an error occurs while writing data to the
  314. * destination stream.
  315. */
  316. public void write(String content) throws IOException {
  317. write(content, false);
  318. }
  319. public void writeComment(String commentText) throws IOException {
  320. checkClosed();
  321. checkInit();
  322. outputStream.write(userSettings.Comment);
  323. outputStream.write(commentText);
  324. if (useCustomRecordDelimiter) {
  325. outputStream.write(userSettings.RecordDelimiter);
  326. } else {
  327. outputStream.write(systemRecordDelimiter);
  328. }
  329. firstColumn = true;
  330. }
  331. /**
  332. * Writes a new record using the passed in array of values.
  333. *
  334. * @param values
  335. * Values to be written.
  336. *
  337. * @param preserveSpaces
  338. * Whether to preserver leading and trailing spaces in columns
  339. * while writing out to the record or not.
  340. *
  341. * @throws IOException
  342. * Thrown if an error occurs while writing data to the
  343. * destination stream.
  344. */
  345. public void writeRecord(String[] values, boolean preserveSpaces)
  346. throws IOException {
  347. if (values != null && values.length > 0) {
  348. for (int i = 0; i < values.length; i++) {
  349. write(values[i], preserveSpaces);
  350. }
  351. endRecord();
  352. }
  353. }
  354. /**
  355. * Writes a new record using the passed in array of values.
  356. *
  357. * @param values
  358. * Values to be written.
  359. *
  360. * @throws IOException
  361. * Thrown if an error occurs while writing data to the
  362. * destination stream.
  363. */
  364. public void writeRecord(String[] values) throws IOException {
  365. writeRecord(values, false);
  366. }
  367. /**
  368. * Ends the current record by sending the record delimiter.
  369. *
  370. * @exception IOException
  371. * Thrown if an error occurs while writing data to the
  372. * destination stream.
  373. */
  374. public void endRecord() throws IOException {
  375. checkClosed();
  376. checkInit();
  377. if (useCustomRecordDelimiter) {
  378. outputStream.write(userSettings.RecordDelimiter);
  379. } else {
  380. outputStream.write(systemRecordDelimiter);
  381. }
  382. firstColumn = true;
  383. }
  384. /**
  385. *
  386. */
  387. private void checkInit() throws IOException {
  388. if (!initialized) {
  389. if (fileName != null) {
  390. outputStream = new BufferedWriter(new OutputStreamWriter(
  391. new FileOutputStream(fileName), charset));
  392. }
  393. initialized = true;
  394. }
  395. }
  396. /**
  397. * Clears all buffers for the current writer and causes any buffered data to
  398. * be written to the underlying device.
  399. * @exception IOException
  400. * Thrown if an error occurs while writing data to the
  401. * destination stream.
  402. */
  403. public void flush() throws IOException {
  404. outputStream.flush();
  405. }
  406. /**
  407. * Closes and releases all related resources.
  408. */
  409. public void close() {
  410. if (!closed) {
  411. close(true);
  412. closed = true;
  413. }
  414. }
  415. /**
  416. *
  417. */
  418. private void close(boolean closing) {
  419. if (!closed) {
  420. if (closing) {
  421. charset = null;
  422. }
  423. try {
  424. if (initialized) {
  425. outputStream.close();
  426. }
  427. } catch (Exception e) {
  428. // just eat the exception
  429. }
  430. outputStream = null;
  431. closed = true;
  432. }
  433. }
  434. /**
  435. *
  436. */
  437. private void checkClosed() throws IOException {
  438. if (closed) {
  439. throw new IOException(
  440. "This instance of the CsvWriter class has already been closed.");
  441. }
  442. }
  443. /**
  444. *
  445. */
  446. protected void finalize() {
  447. close(false);
  448. }
  449. private class Letters {
  450. public static final char LF = '\n';
  451. public static final char CR = '\r';
  452. public static final char QUOTE = '"';
  453. public static final char COMMA = ',';
  454. public static final char SPACE = ' ';
  455. public static final char TAB = '\t';
  456. public static final char POUND = '#';
  457. public static final char BACKSLASH = '\\';
  458. public static final char NULL = '\0';
  459. }
  460. private class UserSettings {
  461. // having these as publicly accessible members will prevent
  462. // the overhead of the method call that exists on properties
  463. public char TextQualifier;
  464. public boolean UseTextQualifier;
  465. public char Delimiter;
  466. public char RecordDelimiter;
  467. public char Comment;
  468. public int EscapeMode;
  469. public boolean ForceQualifier;
  470. public UserSettings() {
  471. TextQualifier = Letters.QUOTE;
  472. UseTextQualifier = true;
  473. Delimiter = Letters.COMMA;
  474. RecordDelimiter = Letters.NULL;
  475. Comment = Letters.POUND;
  476. EscapeMode = ESCAPE_MODE_DOUBLED;
  477. ForceQualifier = false;
  478. }
  479. }
  480. public static String replace(String original, String pattern, String replace) {
  481. final int len = pattern.length();
  482. int found = original.indexOf(pattern);
  483. if (found > -1) {
  484. StringBuffer sb = new StringBuffer();
  485. int start = 0;
  486. while (found != -1) {
  487. sb.append(original.substring(start, found));
  488. sb.append(replace);
  489. start = found + len;
  490. found = original.indexOf(pattern, start);
  491. }
  492. sb.append(original.substring(start));
  493. return sb.toString();
  494. } else {
  495. return original;
  496. }
  497. }
  498. }