Coverage Report - net.sf.jmatchparser.util.csv.FixedWidthCSVWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
FixedWidthCSVWriter
63%
23/36
75%
6/8
1,417
 
 1  
 /*
 2  
  * Copyright (c) 2010 - 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 fixed width CSV (Comma Separated Value) files, where every
 43  
  * column has a fixed width and may optionally additionally delimited by
 44  
  * separator characters.
 45  
  */
 46  
 public class FixedWidthCSVWriter extends AbstractCSVWriter {
 47  
 
 48  
         private final Writer w;
 49  
         private final String prefix;
 50  
         private final String separator;
 51  
         private final String suffix;
 52  
         private final FixedWidthColumn[] columns;
 53  
         private boolean truncateValues;
 54  
 
 55  
         /**
 56  
          * Create a new CSV writer that writes to the given {@link Writer} using
 57  
          * left-aligned space-padded columns only and no separators.
 58  
          * 
 59  
          * @param w
 60  
          *            the writer to write to.
 61  
          * @param widths
 62  
          *            The width of each column
 63  
          */
 64  
         public FixedWidthCSVWriter(Writer w, int... widths) {
 65  0
                 this(w, FixedWidthCSVReader.buildColumns(widths));
 66  0
         }
 67  
 
 68  
         /**
 69  
          * Create a new CSV writer that writes to the given {@link Writer} using
 70  
          * columns parsed from the given column specs and no separators.
 71  
          * 
 72  
          * @param w
 73  
          *            the writer to write to.
 74  
          * @param columnSpecs
 75  
          *            The format specification of each column
 76  
          */
 77  
         public FixedWidthCSVWriter(Writer w, String... columnSpecs) {
 78  0
                 this(w, FixedWidthCSVReader.buildColumns(columnSpecs));
 79  0
         }
 80  
 
 81  
         /**
 82  
          * Create a new CSV writer that writes to the given {@link Writer} using the
 83  
          * given columns and no separators.
 84  
          * 
 85  
          * @param w
 86  
          *            the writer to write to.
 87  
          * @param columns
 88  
          *            The format of each column
 89  
          */
 90  
         public FixedWidthCSVWriter(Writer w, FixedWidthColumn... columns) {
 91  0
                 this(w, "", "", "", columns);
 92  0
         }
 93  
 
 94  
         /**
 95  
          * Create a new CSV writer that writes to the given {@link Writer} using the
 96  
          * given columns and separators.
 97  
          * 
 98  
          * @param w
 99  
          *            the writer to write to.
 100  
          * @param prefix
 101  
          *            The prefix to add to each line
 102  
          * @param separator
 103  
          *            The separator to add between each field
 104  
          * @param suffix
 105  
          *            The suffix to add to each line
 106  
          * @param columns
 107  
          *            The format of each column
 108  
          */
 109  1
         public FixedWidthCSVWriter(Writer w, String prefix, String separator, String suffix, FixedWidthColumn... columns) {
 110  1
                 this.w = w;
 111  1
                 this.prefix = prefix;
 112  1
                 this.separator = separator;
 113  1
                 this.suffix = suffix;
 114  1
                 this.columns = columns;
 115  1
         }
 116  
 
 117  
         /**
 118  
          * Create a new CSV writer that writes to the given stream in the given
 119  
          * charset using left-aligned space-padded columns only and no separators.
 120  
          * 
 121  
          * @param out
 122  
          *            Output stream
 123  
          * @param charsetName
 124  
          *            charset
 125  
          * @param widths
 126  
          *            The width of each column
 127  
          */
 128  
         public FixedWidthCSVWriter(OutputStream out, String charsetName, int... widths) throws UnsupportedEncodingException {
 129  0
                 this(new OutputStreamWriter(out, charsetName), widths);
 130  0
         }
 131  
 
 132  
         /**
 133  
          * Create a new CSV writer that writes to the given stream in the given
 134  
          * charset using columns parsed from the given column specs and no
 135  
          * separators.
 136  
          * 
 137  
          * @param out
 138  
          *            Output stream
 139  
          * @param charsetName
 140  
          *            charset
 141  
          * @param columnSpecs
 142  
          *            The format specification of each column
 143  
          */
 144  
         public FixedWidthCSVWriter(OutputStream out, String charsetName, String... columnSpecs) throws UnsupportedEncodingException {
 145  0
                 this(new OutputStreamWriter(out, charsetName), columnSpecs);
 146  0
         }
 147  
 
 148  
         /**
 149  
          * Create a new CSV writer that writes to the given stream in the given
 150  
          * charset using the given columns and no separators.
 151  
          * 
 152  
          * @param out
 153  
          *            Output stream
 154  
          * @param charsetName
 155  
          *            charset
 156  
          * @param columns
 157  
          *            The format of each column
 158  
          */
 159  
         public FixedWidthCSVWriter(OutputStream out, String charsetName, FixedWidthColumn... columns) throws UnsupportedEncodingException {
 160  0
                 this(new OutputStreamWriter(out, charsetName), columns);
 161  0
         }
 162  
 
 163  
         /**
 164  
          * Create a new CSV writer that writes to the given stream in the given
 165  
          * charset using the given columns and separators.
 166  
          * 
 167  
          * @param out
 168  
          *            Output stream
 169  
          * @param charsetName
 170  
          *            charset
 171  
          * @param prefix
 172  
          *            The prefix to add to each line
 173  
          * @param separator
 174  
          *            The separator to add between each field
 175  
          * @param suffix
 176  
          *            The suffix to add to each line
 177  
          * @param columns
 178  
          *            The format of each column
 179  
          */
 180  
         public FixedWidthCSVWriter(OutputStream out, String charsetName, String prefix, String separator, String suffix, FixedWidthColumn... columns) throws UnsupportedEncodingException {
 181  1
                 this(new OutputStreamWriter(out, charsetName), prefix, separator, suffix, columns);
 182  1
         }
 183  
 
 184  
         @Override
 185  
         public void flush() throws IOException {
 186  1
                 w.flush();
 187  1
         }
 188  
 
 189  
         @Override
 190  
         public void close() throws IOException {
 191  1
                 w.close();
 192  1
         }
 193  
 
 194  
         /**
 195  
          * Set whether overlong values should be truncated automatically. If
 196  
          * disabled, an exception will be thrown.
 197  
          */
 198  
         public void setTruncateValues(boolean truncateValues) {
 199  1
                 this.truncateValues = truncateValues;
 200  1
         }
 201  
 
 202  
         @Override
 203  
         public void write(String... record) throws IOException {
 204  2
                 if (record.length > columns.length)
 205  0
                         throw new IOException("Too many columns");
 206  6
                 for (int i = 0; i < columns.length; i++) {
 207  4
                         if (i == 0)
 208  2
                                 w.write(prefix);
 209  
                         else
 210  2
                                 w.write(separator);
 211  4
                         w.write(columns[i].formatValue(i < record.length ? record[i] : "", truncateValues, false));
 212  
                 }
 213  2
                 w.write(suffix + "\r\n");
 214  2
         }
 215  
 }