AbstractFieldProcessor.java

  1. package com.gh.mygreen.xlsmapper.fieldprocessor;

  2. import java.lang.annotation.Annotation;

  3. import com.gh.mygreen.xlsmapper.Configuration;
  4. import com.gh.mygreen.xlsmapper.annotation.XlsConverter;
  5. import com.gh.mygreen.xlsmapper.cellconverter.CellConverter;
  6. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterFactory;
  7. import com.gh.mygreen.xlsmapper.cellconverter.ConversionException;
  8. import com.gh.mygreen.xlsmapper.fieldaccessor.FieldAccessor;
  9. import com.gh.mygreen.xlsmapper.localization.MessageBuilder;


  10. /**
  11.  * 各種アノテーションを処理するためのクラスの抽象クラス。
  12.  * <p>通常はこのクラスを継承して作成します。</p>
  13.  *
  14.  * @param <A> サポートするアノテーション
  15.  * @version 2.0
  16.  * @author T.TSUCHIE
  17.  *
  18.  */
  19. public abstract class AbstractFieldProcessor<A extends Annotation> implements FieldProcessor<A> {

  20.     /**
  21.      * 該当するタイプのConverterが見つからないときの例外のインスタンスを作成する。
  22.      * @param targetType クラスタイプ
  23.      * @return {@link ConversionException} のインスタンス
  24.      */
  25.     protected ConversionException newNotFoundCellConverterExpcetion(final Class<?> targetType) {
  26.         return new ConversionException(
  27.                 MessageBuilder.create("cellConverter.notFound")
  28.                     .varWithClass("classType", targetType)
  29.                     .format(),
  30.                 targetType);
  31.     }

  32.     /**
  33.      * 指定したタイプに対する{@link CellConverter}を取得します。
  34.      * <p>アノテーション「{@link XlsConverter}」が付与されている場合、そちらの設定値を優先します。</p>
  35.      *
  36.      * @param accessor フィールド情報
  37.      * @param config システム情報設定。
  38.      * @return {@link CellConverter}のインスタンス
  39.      * @throws ConversionException {@link CellConverter}が見つからない場合。
  40.      */
  41.     protected CellConverter<?> getCellConverter(final FieldAccessor accessor, final Configuration config) throws ConversionException {

  42.         final CellConverter<?> converter;

  43.         if(accessor.hasAnnotation(XlsConverter.class)) {
  44.             XlsConverter converterAnno = accessor.getAnnotationNullable(XlsConverter.class);
  45.             converter = config.createBean(converterAnno.value()).create(accessor, config);

  46.         } else {
  47.             CellConverterFactory<?> converterFactory = config.getConverterRegistry().getConverterFactory(accessor.getType());
  48.             if(converterFactory == null) {
  49.                 throw newNotFoundCellConverterExpcetion(accessor.getType());
  50.             }
  51.             converter = converterFactory.create(accessor, config);
  52.         }

  53.         return converter;
  54.     }

  55.     /**
  56.      * コンポーネントタイプを指定して、指定したタイプに対する{@link CellConverter}を取得します。
  57.      * <p>アノテーション「{@link XlsConverter}」が付与されている場合、そちらの設定値を優先します。</p>
  58.      *
  59.      * @param componentType コンポーネントのクラスタイプ
  60.      * @param accessor フィールド情報
  61.      * @param config システム情報設定。
  62.      * @return {@link CellConverter}のインスタンス
  63.      * @throws ConversionException {@link CellConverter}が見つからない場合。
  64.      */
  65.     protected CellConverter<?> getCellConverter(final Class<?> componentType, final FieldAccessor accessor, final Configuration config)
  66.             throws ConversionException {

  67.         final CellConverter<?> converter;

  68.         if(accessor.hasAnnotation(XlsConverter.class)) {
  69.             XlsConverter converterAnno = accessor.getAnnotationNullable(XlsConverter.class);
  70.             converter = config.createBean(converterAnno.value()).create(accessor, config);

  71.         } else {
  72.             CellConverterFactory<?> converterFactory = config.getConverterRegistry().getConverterFactory(componentType);
  73.             if(converterFactory == null) {
  74.                 throw newNotFoundCellConverterExpcetion(componentType);
  75.             }
  76.             converter = converterFactory.create(accessor, config);
  77.         }

  78.         return converter;
  79.     }

  80. }