StringCellConverterFactory.java

  1. package com.gh.mygreen.xlsmapper.cellconverter.impl;

  2. import java.util.Optional;

  3. import org.apache.poi.ss.usermodel.Cell;

  4. import com.gh.mygreen.xlsmapper.Configuration;
  5. import com.gh.mygreen.xlsmapper.annotation.XlsTrim;
  6. import com.gh.mygreen.xlsmapper.cellconverter.BaseCellConverter;
  7. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  8. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  9. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactorySupport;
  10. import com.gh.mygreen.xlsmapper.cellconverter.TypeBindException;
  11. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  12. import com.gh.mygreen.xlsmapper.textformatter.TextFormatter;
  13. import com.gh.mygreen.xlsmapper.textformatter.TextParseException;

  14. /**
  15.  * {@link String}を処理する{@link CellConverter}を作成するためのファクトリクラス。
  16.  *
  17.  * @since 2.0
  18.  * @author T.TSUCHIE
  19.  *
  20.  */
  21. public class StringCellConverterFactory extends CellConverterFactorySupport<String>
  22.         implements CellConverterFactory<String> {
  23.    
  24.     @Override
  25.     public StringCellConverter create(final FieldAccessor field, final Configuration config) {
  26.        
  27.         final StringCellConverter cellConverter = new StringCellConverter(field, config);
  28.         setupCellConverter(cellConverter, field, config);
  29.        
  30.         return cellConverter;
  31.        
  32.     }
  33.    
  34.     @Override
  35.     protected void setupCustom(final BaseCellConverter<String> cellConverter, final FieldAccessor field, final Configuration config) {
  36.         // 何もしない
  37.     }
  38.    
  39.     @Override
  40.     protected TextFormatter<String> createTextFormatter(final FieldAccessor field, final Configuration config) {
  41.        
  42.         return new TextFormatter<String>() {
  43.            
  44.             @Override
  45.             public String parse(String text) throws TextParseException {
  46.                
  47.                 if(text.isEmpty() && !field.hasAnnotation(XlsTrim.class)) {
  48.                     // トリムを行わない場合は、空文字をnullに補完する。
  49.                     return null;
  50.                 }
  51.                
  52.                 return text;
  53.             }
  54.            
  55.             @Override
  56.             public String format(String value) {
  57.                 return value;
  58.             }
  59.         };
  60.     }
  61.    
  62.     public class StringCellConverter extends BaseCellConverter<String> {
  63.        
  64.         private StringCellConverter(final FieldAccessor field, final Configuration config) {
  65.             super(field, config);
  66.         }
  67.        
  68.         @Override
  69.         protected String parseCell(final Cell evaluatedCell, final String formattedValue) throws TypeBindException {
  70.            
  71.             return textFormatter.parse(formattedValue);
  72.         }
  73.        
  74.         @Override
  75.         protected void setupCell(final Cell cell, final Optional<String> cellValue) throws TypeBindException {
  76.            
  77.             if(cellValue.isPresent() && !cellValue.get().isEmpty()) {
  78.                 cell.setCellValue(cellValue.get());
  79.                
  80.             } else {
  81.                 cell.setBlank();
  82.             }
  83.         }
  84.        
  85.     }
  86.    
  87. }