1 /*** 2 * Copyright (C) 2008 rweber <quietgenie@users.sourceforge.net> 3 * 4 * This file is part of CsvObjectMapper. 5 * 6 * CsvObjectMapper is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU Lesser General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * CsvObjectMapper is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public License 17 * along with CsvObjectMapper. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 /*** 21 * 22 */ 23 package com.projectnine.csvmapper; 24 25 import java.util.List; 26 import java.util.Map; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 /*** 32 * This class is a simple construct for holding a {@link List} of 33 * {@link CsvFieldMapping}s and generating new instances of the Class that the 34 * CSV record is declared to represent. 35 * 36 * @author robweber 37 * 38 */ 39 public class CsvMappingDefinition { 40 private static final Log log = LogFactory 41 .getLog(CsvMappingDefinition.class); 42 43 /*** 44 * The name of the class into which the CSV record will be transformed. 45 */ 46 private String beanClassName; 47 48 /*** 49 * The number of fields that we expect to find in the CSV record. Leave this 50 * value alone if you don't care how many fields there are. 51 */ 52 private int expectedNumberOfFields = -1; 53 54 /*** 55 * A list of the {@link CsvFieldMapping}s. 56 */ 57 private List<CsvFieldMapping> fieldMappings; 58 59 /*** 60 * How should the generated object be represented in JEXL expressions 61 * (Object to CSV only). Defaults to "generatedObject". 62 */ 63 private String beanVariableName = "generatedObject"; 64 65 /*** 66 * Should any expressions be evaluated before the Object is converted to 67 * CSV? 68 */ 69 private Map<String, String> extendedContext; 70 71 /*** 72 * Get a new instance of the Class represented by {@link #beanClassName}. 73 * This method assumes that the default constructor is defined for the class 74 * represented by {@link #beanClassName}. 75 * 76 * @return 77 */ 78 public Object getNewBeanInstance() { 79 Object newBeanInstance = null; 80 try { 81 newBeanInstance = Class.forName(beanClassName).newInstance(); 82 } catch (ClassNotFoundException e) { 83 log.warn("The specified bean class name, " + beanClassName 84 + ", is not on the classpath. Returning null.", e); 85 } catch (InstantiationException e) { 86 log.warn("An error occurred while instantiating " + beanClassName 87 + ". Returning null.", e); 88 } catch (IllegalAccessException e) { 89 log.warn("No access to the constructor of " + beanClassName + ".", 90 e); 91 } 92 93 return newBeanInstance; 94 } 95 96 /*** 97 * Get the list of {@link CsvFieldMapping}s. 98 * 99 * @return 100 */ 101 public List<CsvFieldMapping> getFieldMappings() { 102 return fieldMappings; 103 } 104 105 /*** 106 * @param fieldMappings 107 * the fieldMappings to set 108 */ 109 public void setFieldMappings(List<CsvFieldMapping> fieldMappings) { 110 this.fieldMappings = fieldMappings; 111 } 112 113 /*** 114 * @param beanClassName 115 * the beanClassName to set 116 */ 117 public void setBeanClassName(String beanClassName) { 118 this.beanClassName = beanClassName; 119 } 120 121 /*** 122 * @param expectedNumberOfFields 123 * the expectedNumberOfFields to set 124 */ 125 public void setExpectedNumberOfFields(int expectedNumberOfFields) { 126 this.expectedNumberOfFields = expectedNumberOfFields; 127 } 128 129 /*** 130 * @return the expectedNumberOfFields 131 */ 132 public int getExpectedNumberOfFields() { 133 return expectedNumberOfFields; 134 } 135 136 /*** 137 * @return the beanVariableName 138 */ 139 public String getBeanVariableName() { 140 return beanVariableName; 141 } 142 143 /*** 144 * @param beanVariableName 145 * the beanVariableName to set 146 */ 147 public void setBeanVariableName(String beanVariableName) { 148 this.beanVariableName = beanVariableName; 149 } 150 151 /*** 152 * @return the perLinePreProcessingInstructions 153 */ 154 public Map<String, String> getExtendedContext() { 155 return extendedContext; 156 } 157 158 /*** 159 * @param perLinePreProcessingInstructions 160 * the perLinePreProcessingInstructions to set 161 */ 162 public void setExtendedContext(Map<String, String> extendedContext) { 163 this.extendedContext = extendedContext; 164 } 165 }