| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CSVPrinter |
|
| 2.3333333333333335;2.333 |
| 1 | /* | |
| 2 | * Copyright (c) 2004 International Decision Systems, Inc. All Rights Reserved. | |
| 3 | * | |
| 4 | * By using this Software, You acknowledge that the Software is a valuable asset | |
| 5 | * and trade secret of either International Decision Systems, Inc. ("IDSI") or a | |
| 6 | * third party supplier of IDSI and constitutes confidential and proprietary | |
| 7 | * information. | |
| 8 | * | |
| 9 | * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES | |
| 10 | * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER | |
| 11 | * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR | |
| 12 | * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF | |
| 13 | * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS | |
| 14 | * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING | |
| 15 | * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED | |
| 16 | * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE, | |
| 17 | * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED. | |
| 18 | */ | |
| 19 | package net.sourceforge.addam.impexp.csv; | |
| 20 | ||
| 21 | import java.io.IOException; | |
| 22 | import java.io.Writer; | |
| 23 | ||
| 24 | /** | |
| 25 | * Writes CSV files according to the rules outlined on | |
| 26 | * <a href="http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm#FileFormat">Creativyst</a> | |
| 27 | * | |
| 28 | * @author TIM3 | |
| 29 | * @since Jul 30, 2004 | |
| 30 | */ | |
| 31 | public class CSVPrinter { | |
| 32 | private final Writer out; | |
| 33 | private final boolean alwaysQuote; | |
| 34 | ||
| 35 | public CSVPrinter(Writer out) { | |
| 36 | 0 | this(out, false); |
| 37 | 0 | } |
| 38 | ||
| 39 | 0 | public CSVPrinter(Writer out, boolean alwaysQuote) { |
| 40 | 0 | this.out = out; |
| 41 | 0 | this.alwaysQuote = alwaysQuote; |
| 42 | 0 | } |
| 43 | ||
| 44 | /** | |
| 45 | * writes each element in the strings array to a line in the CSV file | |
| 46 | * | |
| 47 | * @param strings | |
| 48 | */ | |
| 49 | public void writeRecord(String[] strings) throws IOException { | |
| 50 | 0 | StringBuffer buf = new StringBuffer(); |
| 51 | 0 | for (int i = 0; i < strings.length; i++) { |
| 52 | 0 | String string = strings[i]; |
| 53 | 0 | boolean first = (i > 0); |
| 54 | 0 | boolean quote = alwaysQuote || |
| 55 | 0 | string.indexOf(',') != -1 || |
| 56 | 0 | string.indexOf('\n') != -1 || |
| 57 | 0 | string.startsWith(" ") || |
| 58 | 0 | string.endsWith(" "); |
| 59 | 0 | if (first) buf.append(','); |
| 60 | 0 | if (quote) buf.append("\""); |
| 61 | 0 | buf.append(string.replaceAll("\\\"", "\"\"")); |
| 62 | 0 | if (quote) buf.append("\""); |
| 63 | } | |
| 64 | 0 | buf.append("\n"); |
| 65 | 0 | out.write(buf.toString()); |
| 66 | 0 | } |
| 67 | } |