private String extractHeaderName(final BeanField<T> beanField) { if (beanField == null || beanField.getField() == null || beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0) { return StringUtils.EMPTY; } final CsvBindByName bindByNameAnnotation = beanField.getField() .getDeclaredAnnotationsByType(CsvBindByName.class)[0]; return bindByNameAnnotation.column(); } }
for(ComplexFieldMapEntry<String, String, T> r : complexMapList) { @SuppressWarnings("unchecked") final MultiValuedMap<String,T> m = (MultiValuedMap<String,T>) r.getBeanField().getFieldValue(bean); if(m != null && !m.isEmpty()) { for(Map.Entry<String,T> entry : m.entries()) { if(r.getBeanField().isRequired()) { missingRequiredHeaders.add(r.getBeanField().getField());
@Override public void verifyLineLength(int numberOfFields) throws CsvRequiredFieldEmptyException { if (!headerIndex.isEmpty()) { BeanField f; StringBuilder sb = null; for (int i = numberOfFields; i <= headerIndex.findMaxIndex(); i++) { f = findField(i); if (f != null && f.isRequired()) { if (sb == null) { sb = new StringBuilder(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("multiple.required.field.empty")); } sb.append(' '); sb.append(f.getField().getName()); } } if (sb != null) { throw new CsvRequiredFieldEmptyException(type, sb.toString()); } } }
.converter(); BeanField<T> bean = instantiateCustomConverter(converter); bean.setField(field); required = annotation.required(); bean.setRequired(required); fieldMap.put(columnName, bean);
/** * Attempts to instantiate the class of the custom converter specified. * * @param converter The class for a custom converter * @return The custom converter * @throws CsvBadConverterException If the class cannot be instantiated */ protected BeanField<T> instantiateCustomConverter(Class<? extends AbstractBeanField> converter) throws CsvBadConverterException { try { BeanField<T> c = converter.newInstance(); c.setErrorLocale(errorLocale); return c; } catch (IllegalAccessException | InstantiationException oldEx) { CsvBadConverterException newEx = new CsvBadConverterException(converter, String.format(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("custom.converter.invalid"), converter.getCanonicalName())); newEx.initCause(oldEx); throw newEx; } }
if(entry.getValue().isRequired()) { requiredStringList.add(entry.getKey()); if(r.getBeanField().isRequired()) { requiredRegexList.add(r);
/** * Populates the field corresponding to the column position indicated of the * bean passed in according to the rules of the mapping strategy. * This method performs conversion on the input string and assigns the * result to the proper field in the provided bean. * * @param bean Object containing the field to be set. * @param value String containing the value to set the field to. * @param column The column position from the CSV file under which this * value was found. * @throws CsvDataTypeMismatchException When the result of data conversion returns * an object that cannot be assigned to the selected field * @throws CsvRequiredFieldEmptyException When a field is mandatory, but there is no * input datum in the CSV file * @throws CsvConstraintViolationException When the internal structure of * data would be violated by the data in the CSV file * @since 4.2 */ protected void setFieldValue(T bean, String value, int column) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException, CsvConstraintViolationException { BeanField<T> beanField = findField(column); if (beanField != null) { beanField.setFieldValue(bean, value, findHeader(column)); } }
firstIndex = chooseMultivaluedFieldIndexFromHeaderIndex(i); String[] fields = firstBeanField != null ? firstBeanField.write(bean, firstIndex) : ArrayUtils.EMPTY_STRING_ARRAY;
final SortedMap<Integer, String> headerMap = new TreeMap<>(writeOrder); for(Map.Entry<Integer, BeanField<T>> entry : simpleMap.entrySet()) { headerMap.put(entry.getKey(), entry.getValue().getField().getName()); final MultiValuedMap<Integer,T> m = (MultiValuedMap<Integer, T>) r.getBeanField().getFieldValue(bean); boolean oneEntryMatched = false; if(m != null && !m.isEmpty()) { Integer key = entry.getKey(); if(r.contains(key)) { headerMap.put(entry.getKey(), r.getBeanField().getField().getName()); oneEntryMatched = true; if(r.getBeanField().isRequired()) { missingRequiredHeaders.add(r.getBeanField().getField());
Class<? extends AbstractBeanField> converter = annotation.converter(); BeanField<T> bean = instantiateCustomConverter(converter); bean.setField(field); required = annotation.required(); bean.setRequired(required); fieldMap.put(annotation.position(), bean);
@Override public void setErrorLocale(Locale errorLocale) { this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault()); // It's very possible that setType() was called first, which creates all // of the BeanFields, so we need to go back through the list and correct // them all. if(getFieldMap() != null) { getFieldMap().setErrorLocale(this.errorLocale); for(BeanField<T> f : getFieldMap().values()) { f.setErrorLocale(this.errorLocale); } } }
private String extractHeaderName(final BeanField<T> beanField) { if (beanField == null || beanField.getField() == null || beanField.getField().getDeclaredAnnotationsByType(CsvBindByName.class).length == 0) { return StringUtils.EMPTY; } final CsvBindByName bindByNameAnnotation = beanField.getField() .getDeclaredAnnotationsByType(CsvBindByName.class)[0]; return bindByNameAnnotation.column(); } }
@Override @Deprecated public PropertyDescriptor findDescriptor(int col) { BeanField beanField = findField(col); if(beanField != null) { return findDescriptor(beanField.getField().getName()); } String columnName = getColumnName(col); if(StringUtils.isNotBlank(columnName)) { return findDescriptor(columnName); } return null; }
/** * There is no header per se for this mapping strategy, but this method * checks the first line to determine how many fields are present and * adjusts its field map accordingly. */ // The rest of the Javadoc is inherited @Override public void captureHeader(CSVReader reader) throws IOException { // Validation if (type == null) { throw new IllegalStateException(ResourceBundle .getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale) .getString("type.unset")); } String[] firstLine = reader.peek(); fieldMap.setMaxIndex(firstLine.length - 1); if (!columnsExplicitlySet) { headerIndex.clear(); for (FieldMapByPositionEntry entry : fieldMap) { Field f = entry.getField().getField(); if (f.getAnnotation(CsvCustomBindByPosition.class) != null || f.getAnnotation(CsvBindAndSplitByPosition.class) != null || f.getAnnotation(CsvBindAndJoinByPosition.class) != null || f.getAnnotation(CsvBindByPosition.class) != null) { headerIndex.put(entry.getPosition(), f.getName().toUpperCase().trim()); } } } }
requiredHeaderNames[i] = fme.getName(); requiredFields.add(fme.getField().getField());