Coverage Report - net.sf.jmatchparser.util.csv.PropertiesCSVWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesCSVWriter
67%
19/28
66%
4/6
1,571
 
 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.text.Format;
 38  
 import java.util.Properties;
 39  
 
 40  
 /**
 41  
  * Class to create a {@link Properties} file as if it was a CSV (Comma Separated
 42  
  * Value) file.
 43  
  */
 44  
 public class PropertiesCSVWriter extends AbstractCSVWriter {
 45  
 
 46  
         private final OutputStream out;
 47  
         private final Properties properties;
 48  
         private final Format[] columnFormats;
 49  1
         private int nextIndex = 0;
 50  
 
 51  
         /**
 52  
          * Create a new {@link PropertiesCSVWriter} that writes to the given
 53  
          * {@link Properties} file. Each columnFormat is evaluated for each row
 54  
          * number to return the respective property name for this column.
 55  
          * 
 56  
          * @param properties
 57  
          *            Property file to write to
 58  
          * @param firstIndex
 59  
          *            index of the first row
 60  
          * @param columnFormats
 61  
          *            column formats
 62  
          */
 63  
         public PropertiesCSVWriter(Properties properties, int firstIndex, String... columnFormats) {
 64  0
                 this(properties, firstIndex, PropertiesCSVReader.buildFormats(columnFormats));
 65  0
         }
 66  
 
 67  
         /**
 68  
          * Create a new {@link PropertiesCSVWriter} that writes to the given
 69  
          * {@link Properties} file. Each columnFormat is evaluated for each row
 70  
          * number to return the respective property name for this column.
 71  
          * 
 72  
          * @param properties
 73  
          *            Property file to write to
 74  
          * @param firstIndex
 75  
          *            index of the first row
 76  
          * @param columnFormats
 77  
          *            column formats
 78  
          */
 79  0
         public PropertiesCSVWriter(Properties properties, int firstIndex, Format... columnFormats) {
 80  0
                 this.out = null;
 81  0
                 this.properties = properties;
 82  0
                 this.nextIndex = firstIndex;
 83  0
                 this.columnFormats = columnFormats;
 84  0
         }
 85  
 
 86  
         /**
 87  
          * Create a new {@link PropertiesCSVWriter} that writes to the given output
 88  
          * stream. Each columnFormat is evaluated for each row number to return the
 89  
          * respective property name for this column.
 90  
          * 
 91  
          * @param out
 92  
          *            Output stream to write to
 93  
          * @param firstIndex
 94  
          *            index of the first row
 95  
          * @param columnFormats
 96  
          *            column formats
 97  
          */
 98  
         public PropertiesCSVWriter(OutputStream out, int firstIndex, String... columnFormats) {
 99  1
                 this(out, firstIndex, PropertiesCSVReader.buildFormats(columnFormats));
 100  1
         }
 101  
 
 102  
         /**
 103  
          * Create a new {@link PropertiesCSVWriter} that writes to the given output
 104  
          * stream. Each columnFormat is evaluated for each row number to return the
 105  
          * respective property name for this column.
 106  
          * 
 107  
          * @param out
 108  
          *            Output stream to write to
 109  
          * @param firstIndex
 110  
          *            index of the first row
 111  
          * @param columnFormats
 112  
          *            column formats
 113  
          */
 114  1
         public PropertiesCSVWriter(OutputStream out, int firstIndex, Format... columnFormats) {
 115  1
                 this.out = out;
 116  1
                 this.properties = new Properties();
 117  1
                 this.nextIndex = firstIndex;
 118  1
                 this.columnFormats = columnFormats;
 119  1
         }
 120  
 
 121  
         @Override
 122  
         public void flush() throws IOException {
 123  1
         }
 124  
 
 125  
         @Override
 126  
         public void close() throws IOException {
 127  1
                 if (out != null) {
 128  1
                         properties.store(out, "Written by PropertiesCSVWriter");
 129  1
                         out.close();
 130  
                 }
 131  1
         }
 132  
 
 133  
         @Override
 134  
         public void write(String... record) throws IOException {
 135  2
                 if (record.length > columnFormats.length)
 136  0
                         throw new IOException("Too many columns");
 137  6
                 for (int i = 0; i < record.length; i++) {
 138  4
                         properties.setProperty(columnFormats[i].format(new Object[] { nextIndex }), record[i]);
 139  
                 }
 140  2
                 nextIndex++;
 141  2
         }
 142  
 }