Coverage Report - net.sf.jmatchparser.util.csv.CSVWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
CSVWriter
94%
16/17
100%
4/4
1,333
 
 1  
 /*
 2  
  * Copyright (c) 2006 - 2011 Michael Schierl
 3  
  * 
 4  
  * All rights reserved.
 5  
  * 
 6  
  * Redistribution and use in source and binary forms, with or without
 7  
  * modification, are permitted provided that the following conditions
 8  
  * are met:
 9  
  * 
 10  
  * - Redistributions of source code must retain the above copyright notice,
 11  
  *   this list of conditions and the following disclaimer.
 12  
  *   
 13  
  * - Redistributions in binary form must reproduce the above copyright
 14  
  *   notice, this list of conditions and the following disclaimer in the
 15  
  *   documentation and/or other materials provided with the distribution.
 16  
  *   
 17  
  * - Neither name of the copyright holders nor the names of its
 18  
  *   contributors may be used to endorse or promote products derived from
 19  
  *   this software without specific prior written permission.
 20  
  *   
 21  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND THE CONTRIBUTORS
 22  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 23  
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 24  
  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 25  
  * HOLDERS OR THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 26  
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 27  
  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 28  
  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 29  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 30  
  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 31  
  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32  
  */
 33  
 package net.sf.jmatchparser.util.csv;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.io.OutputStream;
 37  
 import java.io.OutputStreamWriter;
 38  
 import java.io.UnsupportedEncodingException;
 39  
 import java.io.Writer;
 40  
 
 41  
 /**
 42  
  * Class to create CSV files.
 43  
  */
 44  
 public class CSVWriter extends AbstractCSVWriter {
 45  
         private final Writer w;
 46  
         private final char separator;
 47  
 
 48  
         /**
 49  
          * Create a new CSV writer that writes to the given {@link Writer}.
 50  
          * 
 51  
          * @param w
 52  
          *            the writer to write to.
 53  
          */
 54  5
         public CSVWriter(Writer w, char separator) {
 55  5
                 this.w = w;
 56  5
                 this.separator = separator;
 57  5
         }
 58  
 
 59  
         /**
 60  
          * Create a new CSV writer that writes tot he given stream in the given
 61  
          * charset.
 62  
          * 
 63  
          * @param out
 64  
          *            Output stream
 65  
          * @param charsetName
 66  
          *            charset
 67  
          */
 68  
         public CSVWriter(OutputStream out, String charsetName, char separator) throws UnsupportedEncodingException {
 69  1
                 this(new OutputStreamWriter(out, charsetName), separator);
 70  1
         }
 71  
 
 72  
         @Override
 73  
         public void flush() throws IOException {
 74  2
                 w.flush();
 75  2
         }
 76  
 
 77  
         @Override
 78  
         public void close() throws IOException {
 79  5
                 w.close();
 80  5
         }
 81  
 
 82  
         /**
 83  
          * Get the separator character used by this CSV writer.
 84  
          */
 85  
         public char getSeparator() {
 86  0
                 return separator;
 87  
         }
 88  
 
 89  
         /**
 90  
          * Write a record (line) into this CSV file, properly separating/quoting the
 91  
          * fields as CSV requires.
 92  
          */
 93  
         @Override
 94  
         public void write(String... record) throws IOException {
 95  33
                 for (int i = 0; i < record.length; i++) {
 96  22
                         if (i != 0)
 97  11
                                 w.write(separator);
 98  22
                         w.write("\"" + record[i].replaceAll("\"", "\"\"") + "\"");
 99  
                 }
 100  11
                 w.write("\r\n");
 101  11
         }
 102  
 }