Configuration.java

  1. package com.gh.mygreen.xlsmapper;

  2. import java.util.Optional;

  3. import com.gh.mygreen.xlsmapper.annotation.XlsSheet;
  4. import com.gh.mygreen.xlsmapper.cellconverter.CellConverterRegistry;
  5. import com.gh.mygreen.xlsmapper.expression.ExpressionLanguageJEXLImpl;
  6. import com.gh.mygreen.xlsmapper.fieldprocessor.FieldProcessorRegistry;
  7. import com.gh.mygreen.xlsmapper.localization.MessageInterpolator;
  8. import com.gh.mygreen.xlsmapper.validation.SheetBindingErrors;
  9. import com.gh.mygreen.xlsmapper.xml.bind.AnnotationMappingInfo;


  10. /**
  11.  * マッピングする際の設定などを保持するクラス。
  12.  *
  13.  * @version 2.1
  14.  * @author T.TSUCHIE
  15.  *
  16.  */
  17. public class Configuration {

  18.     /** シートが見つからなくても無視するかどうか */
  19.     private boolean ignoreSheetNotFound = false;

  20.     /** ラベルを正規化するかどうか */
  21.     private boolean normalizeLabelText = false;

  22.     /** ラベルを正規表現でマッピングするかどうか */
  23.     private boolean regexLabelText = false;

  24.     /** 型変換エラーが発生しても処理を続けるかどうか */
  25.     private boolean continueTypeBindFailure = false;

  26.     /** 書き込み時にセルの結合を行うかどうか */
  27.     private boolean mergeCellOnSave = false;

  28.     /** 書き込み時に名前の定義範囲を修正するかどうか */
  29.     private boolean correctNameRangeOnSave = false;

  30.     /** 書き込み時にセルの入力規則を修正するかどうか */
  31.     private boolean correctCellDataValidationOnSave = false;

  32.     /** 書き込み時に式の再計算をするかどうか */
  33.     private boolean formulaRecalcurationOnSave = true;

  34.     /** 読み込み時にセルの値のキャッシュを行うかどうか */
  35.     private boolean cacheCellValueOnLoad = true;

  36.     /** POIのセルの値のフォーマッター */
  37.     private CellFormatter cellFormatter = new DefaultCellFormatter();

  38.     /** アノテーションを処理するプロセッサの管理クラス */
  39.     private FieldProcessorRegistry fieldProcessorRegistry = new FieldProcessorRegistry();

  40.     /** セルの値の型変換の管理クラス */
  41.     private CellConverterRegistry converterRegistry = new CellConverterRegistry();

  42.     /** 読み込み時のBeanのインスタンスの作成クラス */
  43.     private BeanFactory<Class<?>, Object> beanFactory = new DefaultBeanFactory();

  44.     /** {@link SheetBindingErrors}のインスタンス作成クラス */
  45.     private SheetBindingErrorsFactory bindingErrorsFactory = new SheetBindingErrorsFactory();

  46.     /** 処理対象のシートを取得するクラス */
  47.     private SheetFinder sheetFinder = new SheetFinder();

  48.     /** 数式をフォーマットするクラス */
  49.     private MessageInterpolator formulaFormatter = new MessageInterpolator(new ExpressionLanguageJEXLImpl());
  50.    
  51.     /** セルコメントを操作するクラス */
  52.     private CellCommentOperator commentOperator = new CellCommentOperator();

  53.     /** Beanに対するアノテーションのマッピング情報 */
  54.     private AnnotationMappingInfo annotationMapping = null;
  55.    
  56.     /**
  57.      * 指定したクラスタイプのインスタンスを作成する
  58.      * @param clazz
  59.      * @return
  60.      */
  61.     @SuppressWarnings("unchecked")
  62.     public <P> P createBean(final Class<P> clazz) {
  63.         return (P) beanFactory.create(clazz);
  64.     }

  65.     /**
  66.      * シートが見つからなくても無視するかどうか。
  67.      * @return 初期値は、'false'です。
  68.      */
  69.     public boolean isIgnoreSheetNotFound() {
  70.         return ignoreSheetNotFound;
  71.     }

  72.     /**
  73.      * シートが見つからなくても無視するかどうか設定します。
  74.      * @param ignoreSheetNotFound 初期値は、'false'です。
  75.      * @return 自身のインスタンス
  76.      */
  77.     public Configuration setIgnoreSheetNotFound(boolean ignoreSheetNotFound) {
  78.         this.ignoreSheetNotFound = ignoreSheetNotFound;
  79.         return this;
  80.     }

  81.     /**
  82.      * ラベルの文字列を空白などを正規化してマッピングするかどうか。
  83.      * <p>正規化は、改行コード({@literal \n},{@literal \r})の除去、タブ({@literal \t})、空白(全角、半角)の除去を行います。</p>
  84.      * @since 1.1
  85.      * @return 初期値は、'false'です。
  86.      */
  87.     public boolean isNormalizeLabelText() {
  88.         return normalizeLabelText;
  89.     }

  90.     /**
  91.      * ラベルの文字列を空白などを正規化してマッピングするかどうか設定します。
  92.      * <p>正規化は、改行コード({@literal \n},{@literal \r})の除去、タブ({@literal \t})、空白(全角、半角)の除去を行います。</p>
  93.      * @since 1.1
  94.      * @param normalizeLabelText 初期値は、'false'です。
  95.      * @return 自身のインスタンス
  96.      */
  97.     public Configuration setNormalizeLabelText(boolean normalizeLabelText) {
  98.         this.normalizeLabelText = normalizeLabelText;
  99.         return this;
  100.     }

  101.     /**
  102.      * ラベルを正規表現でマッピングするかどうか。
  103.      * <p>ラベルに{@literal /正規表現/}と記述しておくと正規表現でマッピングできる。</p>
  104.      * @since 1.1
  105.      * @return 初期値は、'false'です。
  106.      */
  107.     public boolean isRegexLabelText() {
  108.         return regexLabelText;
  109.     }

  110.     /**
  111.      * ラベルを正規表現でマッピングするかどうか設置します。
  112.      * <p>ラベルに{@literal /正規表現/}と記述しておくと正規表現でマッピングできる。</p>
  113.      * @since 1.1
  114.      * @param regexLabelText 正規表現でマッピングするかどうか。
  115.      * @return 自身のインスタンス
  116.      */
  117.     public Configuration setRegexLabelText(boolean regexLabelText) {
  118.         this.regexLabelText = regexLabelText;
  119.         return this;
  120.     }

  121.     /**
  122.      * 型変換エラーが発生しても処理を続けるかどうか。
  123.      * @return 初期値は、'false'です。
  124.      */
  125.     public boolean isContinueTypeBindFailure() {
  126.         return continueTypeBindFailure;
  127.     }

  128.     /**
  129.      * 型変換エラーが発生しても処理を続けるかどうか。
  130.      * @param continueTypeBindFailure 初期値は、'false'です。
  131.      * @return
  132.      */
  133.     public Configuration setContinueTypeBindFailure(boolean continueTypeBindFailure) {
  134.         this.continueTypeBindFailure = continueTypeBindFailure;
  135.         return this;
  136.     }

  137.     /**
  138.      * 書き込み時にセルの結合を行うかどうか
  139.      * @return 初期値は、'false'です。
  140.      */
  141.     public boolean isMergeCellOnSave() {
  142.         return mergeCellOnSave;
  143.     }

  144.     /**
  145.      * 書き込み時にセルの結合を行うかどうか設定します。
  146.      * @param mergeCellOnSave 初期値は、'false'です。
  147.      * @return 自身のインスタンス
  148.      */
  149.     public Configuration setMergeCellOnSave(boolean mergeCellOnSave) {
  150.         this.mergeCellOnSave = mergeCellOnSave;
  151.         return this;
  152.     }

  153.     /**
  154.      * 書き込み時に名前の定義範囲を修正するかどうか
  155.      * @since 0.3
  156.      * @return 初期値は、'false'です。
  157.      */
  158.     public boolean isCorrectNameRangeOnSave() {
  159.         return correctNameRangeOnSave;
  160.     }

  161.     /**
  162.      * 書き込み時に名前の定義範囲を修正するかどうか設定します。
  163.      * @since 0.3
  164.      * @param correctNameRangeOnSave 初期値は、'false'です。
  165.      * @return 自身のインスタンス
  166.      */
  167.     public Configuration setCorrectNameRangeOnSave(boolean correctNameRangeOnSave) {
  168.         this.correctNameRangeOnSave = correctNameRangeOnSave;
  169.         return this;
  170.     }

  171.     /**
  172.      * 書き込み時にセルの入力規則を修正するかどうか。
  173.      * @since 0.3
  174.      * @return 初期値は、'false'です。
  175.      */
  176.     public boolean isCorrectCellDataValidationOnSave() {
  177.         return correctCellDataValidationOnSave;
  178.     }

  179.     /**
  180.      * 書き込み時にセルの入力規則を修正するかどうか。
  181.      * <p>trueの場合(修正する場合)、POI-3.11以上が必要になります。
  182.      * <p>POI-3.10以前の場合、データの修正は行われません。
  183.      * @param correctCellDataValidationOnSave 初期値は、'false'です。
  184.      */
  185.     public Configuration setCorrectCellDataValidationOnSave(boolean correctCellDataValidationOnSave) {
  186.         this.correctCellDataValidationOnSave = correctCellDataValidationOnSave;
  187.         return this;
  188.     }

  189.      /**
  190.      * 書き込み時に式の再計算をするか設定します。
  191.      * <p>数式を含むシートを出力したファイルを開いた場合、一般的には数式が開いたときに再計算されます。
  192.      * <p>ただし、大量で複雑な数式が記述されていると、パフォーマンスが落ちるため無効にすることもできます。
  193.      * @since 1.5
  194.      * @return 初期値は、'true'です。
  195.      */
  196.     public boolean isFormulaRecalcurationOnSave() {
  197.         return formulaRecalcurationOnSave;
  198.     }

  199.     /**
  200.      * 書き込み時に式の再計算をするか設定します。
  201.      * <p>数式を含むシートを出力したファイルを開いた場合、一般的には数式が開いたときに再計算されます。
  202.      * <p>ただし、大量で複雑な数式が記述されていると、パフォーマンスが落ちるため無効にすることもできます。
  203.      * @since 1.5
  204.      * @param formulaRecalcurationOnSave
  205.      * @return 自身のインスタンス
  206.      */
  207.     public Configuration setFormulaRecalcurationOnSave(boolean formulaRecalcurationOnSave) {
  208.         this.formulaRecalcurationOnSave = formulaRecalcurationOnSave;
  209.         return this;
  210.     }

  211.     /**
  212.      * 読み込み時にセルの値のキャッシュを行うかどうか設定します。
  213.      * @since 2.0
  214.      * @return true 初期値は、'true'です。
  215.      */
  216.     public boolean isCacheCellValueOnLoad() {
  217.         return cacheCellValueOnLoad;
  218.     }

  219.     /**
  220.      * 読み込み時にセルの値のキャッシュを行うかどうか設定します。
  221.      * @since 2.0
  222.      * @param cacheCellValueOnLoad trueのときキャッシュを行います。
  223.      */
  224.     public Configuration setCacheCellValueOnLoad(boolean cacheCellValueOnLoad) {
  225.         this.cacheCellValueOnLoad = cacheCellValueOnLoad;
  226.         return this;
  227.     }

  228.     /**
  229.      * POIのセルのフォーマッターを取得します。
  230.      * @return セルのフォーマッタ。
  231.      */
  232.     public CellFormatter getCellFormatter() {
  233.         return cellFormatter;
  234.     }

  235.     /**
  236.      * POIのセルのフォーマッターを指定します。
  237.      * @param cellFormatter セルのフォーマッタ
  238.      * @return 自身のインスタンス
  239.      */
  240.     public Configuration setCellFormatter(CellFormatter cellFormatter) {
  241.         this.cellFormatter = cellFormatter;
  242.         return this;
  243.     }

  244.     /**
  245.      * セルの値の型変換の管理クラスを取得します。
  246.      * @return
  247.      */
  248.     public CellConverterRegistry getConverterRegistry() {
  249.         return converterRegistry;
  250.     }

  251.     /**
  252.      * セルの値の型変換の管理クラスを設定します。
  253.      * @param converterRegistry
  254.      * @return 自身のインスタンス
  255.      */
  256.     public Configuration setConverterRegistry(CellConverterRegistry converterRegistry) {
  257.         this.converterRegistry = converterRegistry;
  258.         return this;
  259.     }

  260.     /**
  261.      * アノテーションを処理するプロセッサの管理クラスを取得します。
  262.      * @return
  263.      */
  264.     public FieldProcessorRegistry getFieldProcessorRegistry() {
  265.         return fieldProcessorRegistry;
  266.     }

  267.     /**
  268.      * アノテーションを処理するプロセッサの管理クラスを設定します。
  269.      * @return 自身のインスタンス
  270.      */
  271.     public Configuration setFieldProcessorRegistry(FieldProcessorRegistry fieldProcessorRegistry) {
  272.         this.fieldProcessorRegistry = fieldProcessorRegistry;
  273.         return this;
  274.     }

  275.     /**
  276.      * Beanを生成するためのFactoryクラスを設定します。
  277.      * @return
  278.      */
  279.     public Configuration setBeanFactory(BeanFactory<Class<?>, Object> beanFactory) {
  280.         this.beanFactory = beanFactory;
  281.         return this;
  282.     }

  283.     /**
  284.      * Beanを生成するためのFactoryクラスを取得します。
  285.      * @since 1.0
  286.      * @return
  287.      */
  288.     public BeanFactory<Class<?>, Object> getBeanFactory() {
  289.         return beanFactory;
  290.     }

  291.     /**
  292.      * {@link SheetBindingErrors}を生成するためのFactoryクラスを取得します。
  293.      * @since 2.0
  294.      * @return
  295.      */
  296.     public SheetBindingErrorsFactory getBindingErrorsFactory() {
  297.         return bindingErrorsFactory;
  298.     }

  299.     /**
  300.      * {@link SheetBindingErrors}を生成するためのFactoryクラスを設定します。
  301.      * @since 2.0
  302.      * @param bindingErrorsFactory
  303.      */
  304.     public void setBindingErrorsFactory(SheetBindingErrorsFactory bindingErrorsFactory) {
  305.         this.bindingErrorsFactory = bindingErrorsFactory;
  306.     }

  307.     /**
  308.      * 処理対象のシートを取得するためのクラスを取得します。
  309.      * <p>アノテーション{@link XlsSheet} を処理します。
  310.      * @since 1.1
  311.      * @return 現在設定されいるシートを処理するクラスのインタンス。
  312.      */
  313.     public SheetFinder getSheetFinder() {
  314.         return sheetFinder;
  315.     }

  316.     /**
  317.      * 処理対象のシートを取得するためのクラスを設定します。
  318.      * <p>アノテーション{@link XlsSheet} を処理します。
  319.      * @since 1.1
  320.      * @param sheetFinder シートを処理するクラスのインタンス。
  321.      * @return 自身のインスタンス
  322.      */
  323.     public Configuration setSheetFinder(SheetFinder sheetFinder) {
  324.         this.sheetFinder = sheetFinder;
  325.         return this;
  326.     }

  327.     /**
  328.      * 数式をフォーマットするためのクラスを取得します。
  329.      * <p>出力するセルの数式を設定する際に、独自の変数やEL式を指定したときにフォーマットする際に利用します。
  330.      *
  331.      * @since 1.5
  332.      * @return 数式をフォーマットするクラス。
  333.      */
  334.     public MessageInterpolator getFormulaFormatter() {
  335.         return formulaFormatter;
  336.     }

  337.     /**
  338.      * 数式をフォーマットするためのクラスを取得します。
  339.      * <p>出力するセルの数式を設定する際に、独自の変数やEL式を指定したときにフォーマットする際に利用します。
  340.      *
  341.      * @since 1.5
  342.      * @param formulaFormatter 数式をフォーマットするクラス。
  343.      * @return 自身のインスタンス
  344.      */
  345.     public Configuration setFormulaFormatter(MessageInterpolator formulaFormatter) {
  346.         this.formulaFormatter = formulaFormatter;
  347.         return this;
  348.     }

  349.     /**
  350.      * アノテーションのマッピング情報を取得する。
  351.      * @since 2.0
  352.      * @return 設定されていない場合は、空を返す。
  353.      */
  354.     public Optional<AnnotationMappingInfo> getAnnotationMapping() {
  355.         return Optional.ofNullable(annotationMapping);
  356.     }

  357.     /**
  358.      * アノテーションのマッピング情報を設定します。
  359.      * @since 2.0
  360.      * @param annotationMapping アノテーションの設定情報
  361.      */
  362.     public void setAnnotationMapping(AnnotationMappingInfo annotationMapping) {
  363.         this.annotationMapping = annotationMapping;
  364.     }

  365.     /**
  366.      * セルコメントを操作するクラスを取得します。
  367.      * @since 2.1
  368.      * @return セルコメントを操作するクラス
  369.      */
  370.     public CellCommentOperator getCommentOperator() {
  371.         return commentOperator;
  372.     }

  373.     /**
  374.      * セルコメントを操作するクラスを設定します。
  375.      * @since 2.1
  376.      * @param commentOperator セルコメントを操作するクラス
  377.      * @return 自身のインスタンス
  378.      */
  379.     public Configuration setCommentOperator(CellCommentOperator commentOperator) {
  380.         this.commentOperator = commentOperator;
  381.         return this;
  382.     }

  383. }