View Javadoc
1   package org.csveed.bean;
2   
3   import java.lang.annotation.Annotation;
4   import java.lang.reflect.Field;
5   import java.util.Locale;
6   
7   import org.csveed.annotations.CsvCell;
8   import org.csveed.annotations.CsvConverter;
9   import org.csveed.annotations.CsvDate;
10  import org.csveed.annotations.CsvFile;
11  import org.csveed.annotations.CsvHeaderName;
12  import org.csveed.annotations.CsvHeaderValue;
13  import org.csveed.annotations.CsvIgnore;
14  import org.csveed.annotations.CsvLocalizedNumber;
15  import org.csveed.common.Column;
16  
17  public class BeanParser {
18  
19      private BeanInstructions beanInstructions;
20  
21      private ColumnColumn currentColumn = new Column();
22  
23      public BeanInstructions getBeanInstructions(Class beanClass) {
24  
25          this.beanInstructions = new BeanInstructionsImpl(beanClass);
26  
27          Annotation[] annotations = beanClass.getAnnotations();
28          for (Annotation annotation : annotations) {
29              if (annotation instanceof CsvFile) {
30                  parseCsvFile((CsvFile)annotation);
31              }
32          }
33  
34          for (BeanProperty beanProperty : beanInstructions.getProperties()) {
35              checkForAnnotations(beanProperty);
36          }
37  
38          return this.beanInstructions;
39      }
40  
41      public void checkForAnnotations(BeanProperty beanProperty) {
42  
43          Field currentField = beanProperty.getField();
44          Annotation[] annotations = currentField.getDeclaredAnnotations();
45          String propertyName = beanProperty.getPropertyName();
46          String columnName = propertyName;
47          for (Annotation annotation : annotations) {
48              if (annotation instanceof CsvCell) {
49                  columnName = parseCsvCell(propertyName, (CsvCell)annotation);
50              } else if (annotation instanceof CsvConverter) {
51                  parseCsvConverter(propertyName, (CsvConverter)annotation);
52              } else if (annotation instanceof CsvDate) {
53                  parseCsvDate(propertyName, (CsvDate)annotation);
54              } else if (annotation instanceof CsvLocalizedNumber) {
55                  parseCsvLocalizedNumber(propertyName, (CsvLocalizedNumber) annotation);
56              } else if (annotation instanceof CsvHeaderName) {
57                  this.beanInstructions.setHeaderNameToProperty(propertyName);
58              } else if (annotation instanceof CsvHeaderValue) {
59                  this.beanInstructions.setHeaderValueToProperty(propertyName);
60              } else if (annotation instanceof CsvIgnore) {
61                  this.beanInstructions.ignoreProperty(propertyName);
62                  return;
63              }
64          }
65          this.beanInstructions.mapColumnNameToProperty(columnName, propertyName);
66          this.beanInstructions.mapColumnIndexToProperty(currentColumn.getColumnIndex(), propertyName);
67          currentColumn = currentColumn.nextColumn();
68      }
69  
70      private void parseCsvLocalizedNumber(String propertyName, CsvLocalizedNumber annotation) {
71          final Locale locale;
72          if (annotation.country().isEmpty()) {
73              locale = new Locale(annotation.language());
74          } else if (annotation.variant().isEmpty()) {
75              locale = new Locale(annotation.language(), annotation.country());
76          } else {
77              locale = new Locale(annotation.language(), annotation.country(), annotation.variant());
78          }
79          this.beanInstructions.setLocalizedNumber(propertyName, locale);
80      }
81  
82      private void parseCsvFile(CsvFile csvFile) {
83  
84          this.beanInstructions
85              .setEscape(csvFile.escape())
86              .setQuote(csvFile.quote())
87              .setQuotingEnabled(csvFile.quotingEnabled())
88              .setSeparator(csvFile.separator())
89              .setComment(csvFile.comment())
90              .setEndOfLine(csvFile.endOfLine())
91              .setMapper(csvFile.mappingStrategy())
92              .setStartRow(csvFile.startRow())
93              .setUseHeader(csvFile.useHeader())
94              .skipEmptyLines(csvFile.skipEmptyLines())
95              .skipCommentLines(csvFile.skipCommentLines())
96              .setStartIndexDynamicColumns(csvFile.startIndexDynamicColumns());
97      }
98  
99      private void parseCsvDate(String propertyName, CsvDate csvDate) {
100         this.beanInstructions.setDate(propertyName, csvDate.format());
101     }
102 
103     private void parseCsvConverter(String propertyName, CsvConverter csvConverter) {
104         try {
105             this.beanInstructions.setConverter(propertyName, csvConverter.converter().newInstance());
106         } catch (Exception err) {
107             throw new RuntimeException(err);
108         }
109     }
110 
111     private String parseCsvCell(String propertyName, CsvCell csvCell) {
112         String columnName = csvCell.columnName() == null || csvCell.columnName().isEmpty() ? propertyName : csvCell.columnName();
113         this.beanInstructions.setRequired(propertyName, csvCell.required());
114         currentColumn = csvCell.columnIndex() != -1 ? new Column(csvCell.columnIndex()) : currentColumn;
115         return columnName;
116     }
117 
118 }