Coverage Report - net.sf.jmatchparser.util.csv.AbstractCSVReader
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractCSVReader
28%
6/21
16%
2/12
2,6
 
 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.util.Arrays;
 37  
 import java.util.HashMap;
 38  
 import java.util.Map;
 39  
 
 40  
 /**
 41  
  * Abstract base class to read data from several CSV-like file formats.
 42  
  */
 43  26
 public abstract class AbstractCSVReader {
 44  
 
 45  
         /**
 46  
          * Read a line from the CSV file, and pass its columns into a {@link Map}
 47  
          * using the given keys/headers.
 48  
          * 
 49  
          * @param <T>
 50  
          *            Type of the keys
 51  
          * @param headers
 52  
          *            Keys/Headers
 53  
          * @param includeEmptyColumns
 54  
          *            Whether to include empty column values in the map
 55  
          */
 56  
         public final <T> Map<T, String> read(T[] headers, boolean includeEmptyColumns) throws IOException {
 57  0
                 Map<T, String> result = new HashMap<T, String>();
 58  0
                 String[] tempResult = read(headers.length);
 59  0
                 for (int i = 0; i < tempResult.length; i++) {
 60  0
                         if (tempResult[i].length() > 0 || includeEmptyColumns)
 61  0
                                 result.put(headers[i], tempResult[i]);
 62  
                 }
 63  0
                 return result;
 64  
         }
 65  
 
 66  
         /**
 67  
          * Read a line from the CSV file and pad it to the given number of columns
 68  
          * with empty strings.
 69  
          * 
 70  
          * @param columnCount
 71  
          *            Desired number of columns
 72  
          * @return The padded line
 73  
          * @throws IOException
 74  
          *             if there are too many columns in the original line.
 75  
          */
 76  
         public final String[] read(int columnCount) throws IOException {
 77  0
                 String[] tempResult = read();
 78  0
                 if (tempResult.length == columnCount)
 79  0
                         return tempResult;
 80  0
                 if (tempResult.length > columnCount)
 81  0
                         throw new IOException("Too many columns: " + Arrays.asList(tempResult));
 82  0
                 String[] result = new String[columnCount];
 83  0
                 System.arraycopy(tempResult, 0, result, 0, tempResult.length);
 84  0
                 Arrays.fill(result, tempResult.length + 1, result.length, "");
 85  0
                 return result;
 86  
         }
 87  
 
 88  
         /**
 89  
          * Write the complete content of this reader to the given
 90  
          * {@link AbstractCSVWriter} and close both reader and writer.
 91  
          * 
 92  
          * @param writer
 93  
          *            The writer to write to
 94  
          */
 95  
         public void writeTo(AbstractCSVWriter writer) throws IOException {
 96  
                 String[] record;
 97  3
                 while ((record = read()) != null)
 98  2
                         writer.write(record);
 99  1
                 close();
 100  1
                 writer.close();
 101  1
         }
 102  
 
 103  
         /**
 104  
          * Read a line from the CSV file.
 105  
          */
 106  
         public abstract String[] read() throws IOException;
 107  
 
 108  
         /**
 109  
          * Close this reader.
 110  
          */
 111  
         public abstract void close() throws IOException;
 112  
 }