View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean;
12  
13  import java.util.Set;
14  
15  import org.csveed.api.Header;
16  import org.csveed.api.Row;
17  import org.csveed.common.Column;
18  import org.csveed.report.CsvException;
19  import org.csveed.report.RowError;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  /**
24   * The Class ColumnNameMapper.
25   *
26   * @param <T>
27   *            the generic type
28   */
29  public class ColumnNameMapper<T> extends AbstractMapper<T> {
30  
31      /** The Constant logger. */
32      private static final Logger logger = LoggerFactory.getLogger(ColumnNameMapper.class);
33  
34      @Override
35      protected Set<Column> keys() {
36          return beanInstructions.getProperties().columnNameKeys();
37      }
38  
39      @Override
40      public BeanProperty getBeanProperty(Column column) {
41          return beanInstructions.getProperties().fromName(column);
42      }
43  
44      @Override
45      protected void checkKey(Header header, Column key) {
46          try {
47              header.getIndex(key.getColumnName());
48          } catch (CsvException e) {
49              logger.trace("", e);
50              throw new CsvException(new RowError("The header row does not contain column \"" + key
51                      + "\". Originally mapped to property \"" + getBeanProperty(key).getPropertyName() + "\"",
52                      header.reportOnEndOfLine(), 0));
53          }
54      }
55  
56      @Override
57      protected Column getColumn(Row row) {
58          return new Column().setHeader(row.getHeader());
59      }
60  
61  }