/** * Setter for the column mapping. * This mapping is for reading. Use of this method in conjunction with * writing is undefined. * * @param columnMapping Column names to be mapped. */ public void setColumnMapping(String... columnMapping) { if (columnMapping != null) { headerIndex.initializeHeaderIndex(columnMapping); } else { headerIndex.clear(); } columnsExplicitlySet = true; }
@Override public int findMaxFieldIndex() { return headerIndex.findMaxIndex(); }
@Override @Deprecated public Integer getColumnIndex(String name) { int[] i = headerIndex.getByName(name); return i.length == 0 ? null : i[0]; }
/** * This method generates a header that can be used for writing beans of the * type provided back to a file. * <p>The ordering of the headers is determined by the * {@link com.opencsv.bean.FieldMap} in use.</p> * <p>This method should be called first by all overriding classes to make * certain {@link #headerIndex} is properly initialized.</p> */ // The rest of the Javadoc is inherited @Override public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException { if(type == null) { throw new IllegalStateException(ResourceBundle .getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale) .getString("type.before.header")); } // Always take what's been given or previously determined first. if(headerIndex.isEmpty()) { String[] header = getFieldMap().generateHeader(bean); headerIndex.initializeHeaderIndex(header); return header; } // Otherwise, put headers in the right places. return headerIndex.getHeaderIndex(); }
@Override public void verifyLineLength(int numberOfFields) throws CsvRequiredFieldEmptyException { if(!headerIndex.isEmpty()) { if (numberOfFields != headerIndex.getHeaderIndexLength()) { throw new CsvRequiredFieldEmptyException(type, ResourceBundle .getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale) .getString("header.data.mismatch")); } } }
@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()); } } }
/** * 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()); } } } }
/** * Get the column name for a given column position. * * @param col Column position. * @return The column name or null if the position is larger than the * header array or there are no headers defined. */ public String getColumnName(int col) { // headerIndex is never null because it's final return headerIndex.getByPosition(col); }
@Override protected Object chooseMultivaluedFieldIndexFromHeaderIndex(int index) { String[] s = headerIndex.getHeaderIndex(); return index >= s.length ? null: s[index]; }
@Override public Integer getColumnIndex(String name) { if (headerIndex.isEmpty()) { throw new IllegalStateException(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("header.unread")); } return super.getColumnIndex(name); }
headerIndex.initializeHeaderIndex(header);
/** * Gets a column name. * * @param col Position of the column. * @return Column name or null if col > number of mappings. */ @Override public String getColumnName(int col) { return headerIndex.getByPosition(col); }
/** * Retrieves the column mappings. * * @return String array with the column mappings. */ public String[] getColumnMapping() { return headerIndex.getHeaderIndex(); }
@Override public String findHeader(int col) { return headerIndex.getByPosition(col); }
@Override public String getColumnName(int col) { String name = headerIndex.getByPosition(col); if(name != null) { name = columnMapping.get(name.toUpperCase()); } return name; }