Coverage Report - net.sf.jmatchparser.util.csv.PropertiesCSVReader
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesCSVReader
100%
24/24
100%
8/8
2
 
 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.text.Format;
 37  
 import java.text.MessageFormat;
 38  
 import java.util.Properties;
 39  
 
 40  
 /**
 41  
  * Class to read data from a {@link Properties} file as if it was a CSV (Comma
 42  
  * Separated Value) file.
 43  
  */
 44  
 public class PropertiesCSVReader extends AbstractCSVReader {
 45  
 
 46  
         private final Properties properties;
 47  
         private final Format[] columnFormats;
 48  2
         private int nextIndex = 0;
 49  
 
 50  
         /**
 51  
          * Create a new {@link PropertiesCSVReader} that reads from the given
 52  
          * {@link Properties} file. Each columnFormat is evaluated for each row
 53  
          * number to return the respective property name for this column.
 54  
          * 
 55  
          * @param properties
 56  
          *            Property file to write to
 57  
          * @param firstIndex
 58  
          *            index of the first row
 59  
          * @param columnFormats
 60  
          *            column formats
 61  
          */
 62  
         public PropertiesCSVReader(Properties properties, int firstIndex, String... columnFormats) {
 63  2
                 this(properties, firstIndex, buildFormats(columnFormats));
 64  2
         }
 65  
 
 66  
         /**
 67  
          * Create a new {@link PropertiesCSVReader} that reads from the given
 68  
          * {@link Properties} file. Each columnFormat is evaluated for each row
 69  
          * number to return the respective property name for this column.
 70  
          * 
 71  
          * @param properties
 72  
          *            Property file to write to
 73  
          * @param firstIndex
 74  
          *            index of the first row
 75  
          * @param columnFormats
 76  
          *            column formats
 77  
          */
 78  2
         public PropertiesCSVReader(Properties properties, int firstIndex, Format... columnFormats) {
 79  2
                 this.properties = properties;
 80  2
                 nextIndex = firstIndex;
 81  2
                 this.columnFormats = columnFormats;
 82  2
         }
 83  
 
 84  
         static Format[] buildFormats(String... formats) {
 85  3
                 Format[] result = new Format[formats.length];
 86  9
                 for (int i = 0; i < result.length; i++) {
 87  6
                         result[i] = new MessageFormat(formats[i]);
 88  
                 }
 89  3
                 return result;
 90  
         }
 91  
 
 92  
         /**
 93  
          * Close this reader. This is a no-op.
 94  
          */
 95  
         @Override
 96  
         public void close() throws IOException {
 97  1
         }
 98  
 
 99  
         /**
 100  
          * Read a row from this XML file.
 101  
          */
 102  
         @Override
 103  
         public String[] read() throws IOException {
 104  6
                 String[] record = new String[columnFormats.length];
 105  6
                 boolean found = false;
 106  18
                 for (int i = 0; i < record.length; i++) {
 107  12
                         record[i] = properties.getProperty(columnFormats[i].format(new Object[] { nextIndex }));
 108  12
                         if (record[i] == null)
 109  4
                                 record[i] = "";
 110  
                         else
 111  8
                                 found = true;
 112  
                 }
 113  6
                 if (!found)
 114  2
                         return null;
 115  4
                 nextIndex++;
 116  4
                 return record;
 117  
         }
 118  
 }