Coverage Report - net.sf.jmatchparser.util.csv.fieldreader.FieldSourceFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldSourceFactory
95%
21/22
83%
5/6
1,667
FieldSourceFactory$FieldSourceFactoryFields
100%
10/10
N/A
1,667
 
 1  
 /*
 2  
  * Copyright (c) 2008 - 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.fieldreader;
 34  
 
 35  
 import java.io.IOException;
 36  
 import java.util.Arrays;
 37  
 import java.util.Collection;
 38  
 import java.util.HashMap;
 39  
 import java.util.Map;
 40  
 
 41  
 import net.sf.jmatchparser.util.csv.AbstractCSVReader;
 42  
 import net.sf.jmatchparser.util.csv.fieldreader.FieldDefinition.MandatoryState;
 43  
 import net.sf.jmatchparser.util.csv.fieldreader.FieldReaderException.FieldReaderExceptionInfo;
 44  
 import net.sf.jmatchparser.util.csv.fieldreader.FieldSource.EmptyAction;
 45  
 
 46  
 /**
 47  
  * Class to create field sources from another CSV file.
 48  
  */
 49  
 public class FieldSourceFactory {
 50  
 
 51  
         /**
 52  
          * Fields used in the CSV file for defining field sources
 53  
          */
 54  2
         public enum FieldSourceFactoryFields implements FieldEnum<Object> {
 55  
 
 56  
                 /**
 57  
                  * Name of the field to be defined.
 58  
                  */
 59  1
                 FIELD(new SimpleFieldDefinition(Integer.MAX_VALUE, MandatoryState.MANDATORY)),
 60  
 
 61  
                 /**
 62  
                  * Columns to be used, in the same format as used by
 63  
                  * {@link FieldSource#parse(String, EmptyAction, String, String)}.
 64  
                  */
 65  1
                 COLUMNS(new SimpleFieldDefinition(Integer.MAX_VALUE, MandatoryState.MANDATORY)),
 66  
 
 67  
                 /**
 68  
                  * {@link EmptyAction} to be used.
 69  
                  */
 70  1
                 EMPTYACTION(new FieldDefinition<EmptyAction>("[A-Z_]+", 15, MandatoryState.MANDATORY, ParseFormat.forEnum(EmptyAction.class))),
 71  
 
 72  
                 /**
 73  
                  * Regex mask.
 74  
                  */
 75  1
                 MASK(new SimpleFieldDefinition()),
 76  
 
 77  
                 /**
 78  
                  * Regex mapping, in the same format as used by
 79  
                  * {@link FieldSource#withMappings(FieldSource, String)}.
 80  
                  */
 81  1
                 MAPPINGS(new SimpleFieldDefinition());
 82  
 
 83  
                 private final FieldDefinition<? extends Object> definition;
 84  
 
 85  5
                 private FieldSourceFactoryFields(FieldDefinition<? extends Object> definition) {
 86  5
                         this.definition = definition;
 87  5
                 }
 88  
 
 89  
                 @Override
 90  
                 public FieldDefinition<? extends Object> getDefinition() {
 91  5
                         return definition;
 92  
                 }
 93  
         }
 94  
 
 95  
         private FieldReader<FieldSourceFactoryFields, Object> reader;
 96  
 
 97  
         /**
 98  
          * Create a new field source factory that reads its field sources from the
 99  
          * given CSV reader.
 100  
          */
 101  1
         public FieldSourceFactory(AbstractCSVReader reader) {
 102  1
                 this.reader = new EnumFieldReader<FieldSourceFactory.FieldSourceFactoryFields, Object>(FieldSourceFactoryFields.values(), reader);
 103  1
                 this.reader.setEmptyFieldValue("");
 104  1
         }
 105  
 
 106  
         /**
 107  
          * Get the {@link FieldReader} used to parse the CSV file, for customizing
 108  
          * its field sources or other parameters.
 109  
          */
 110  
         public FieldReader<FieldSourceFactoryFields, Object> getReader() {
 111  2
                 return reader;
 112  
         }
 113  
 
 114  
         /**
 115  
          * Create field sources from the CSV file.
 116  
          * 
 117  
          * @param <K>
 118  
          *            Type of the fields used
 119  
          * @param possibleFields
 120  
          *            Array of all possible fields, used to convert to string via
 121  
          *            {@link #toString()} and compare
 122  
          */
 123  
         public <K> Map<K, FieldSource> createFieldSources(K[] possibleFields) throws FieldReaderException, IOException {
 124  1
                 return createFieldSources(Arrays.asList(possibleFields));
 125  
         }
 126  
 
 127  
         /**
 128  
          * Create field sources from the CSV file.
 129  
          * 
 130  
          * @param <K>
 131  
          *            Type of the fields used
 132  
          * @param possibleFields
 133  
          *            All possible fields, used to convert to string via
 134  
          *            {@link #toString()} and compare
 135  
          */
 136  
         public <K> Map<K, FieldSource> createFieldSources(Collection<K> possibleFields) throws FieldReaderException, IOException {
 137  1
                 Map<String, K> fieldsByName = new HashMap<String, K>();
 138  1
                 for (K field : possibleFields) {
 139  5
                         fieldsByName.put(field.toString(), field);
 140  
                 }
 141  1
                 Map<K, FieldSource> result = new HashMap<K, FieldSource>();
 142  
                 Map<FieldSourceFactoryFields, Object> record;
 143  6
                 while ((record = reader.read()) != null) {
 144  5
                         String field = (String) record.get(FieldSourceFactoryFields.FIELD);
 145  5
                         String columns = (String) record.get(FieldSourceFactoryFields.COLUMNS);
 146  5
                         EmptyAction emptyAction = (EmptyAction) record.get(FieldSourceFactoryFields.EMPTYACTION);
 147  5
                         String mask = (String) record.get(FieldSourceFactoryFields.MASK);
 148  5
                         String mappings = (String) record.get(FieldSourceFactoryFields.MAPPINGS);
 149  5
                         if (!fieldsByName.containsKey(field)) {
 150  0
                                 throw new FieldReaderException(FieldReaderErrorCodes.INVALID_FORMAT, false, new FieldReaderExceptionInfo<FieldSourceFactoryFields, Object>(FieldSourceFactoryFields.FIELD, field, record));
 151  
                         }
 152  5
                         result.put(fieldsByName.get(field), FieldSource.parse(columns, emptyAction, mask, mappings));
 153  5
                 }
 154  1
                 reader.close();
 155  1
                 return result;
 156  
         }
 157  
 }