package au.com.bytecode.opencsv.bean;
import au.com.bytecode.opencsv.CSVReader;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
protected String[] header;
protected Map<String, PropertyDescriptor> descriptorMap = null;
protected Class<T> type;
public void (CSVReader reader) throws IOException {
header = reader.readNext();
}
public PropertyDescriptor
findDescriptor(
int col)
throws IntrospectionException {
String columnName = getColumnName(col);
return (null != columnName && columnName.trim().length() > 0) ? findDescriptor(columnName) : null;
}
return (null != header && col < header.length) ? header[col] : null;
}
protected PropertyDescriptor
findDescriptor(String name)
throws IntrospectionException {
if (null == descriptorMap) descriptorMap = loadDescriptorMap(getType());
return descriptorMap.get(name.toUpperCase().trim());
}
protected boolean matches(String name, PropertyDescriptor desc) {
return desc.getName().equals(name.trim());
}
protected Map<String, PropertyDescriptor>
loadDescriptorMap(Class<T> cls)
throws IntrospectionException {
Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>();
PropertyDescriptor[] descriptors;
descriptors = loadDescriptors(getType());
for (PropertyDescriptor descriptor : descriptors) {
map.put(descriptor.getName().toUpperCase().trim(), descriptor);
}
return map;
}
private PropertyDescriptor[]
loadDescriptors(Class<T> cls)
throws IntrospectionException {
BeanInfo beanInfo = Introspector.getBeanInfo(cls);
return beanInfo.getPropertyDescriptors();
}
public T
createBean()
throws InstantiationException, IllegalAccessException {
return type.newInstance();
}
return type;
}
public void setType(Class<T> type) {
this.type = type;
}
}