View Javadoc
1   package com.github.mygreen.supercsv.cellprocessor.constraint;
2   
3   import java.util.ArrayList;
4   import java.util.Arrays;
5   import java.util.List;
6   import java.util.Optional;
7   
8   import org.supercsv.cellprocessor.ift.CellProcessor;
9   
10  import com.github.mygreen.supercsv.annotation.constraint.CsvWordForbid;
11  import com.github.mygreen.supercsv.builder.Configuration;
12  import com.github.mygreen.supercsv.builder.FieldAccessor;
13  import com.github.mygreen.supercsv.cellprocessor.ConstraintProcessorFactory;
14  import com.github.mygreen.supercsv.cellprocessor.format.TextFormatter;
15  import com.github.mygreen.supercsv.exception.SuperCsvInvalidAnnotationException;
16  import com.github.mygreen.supercsv.localization.MessageBuilder;
17  
18  /**
19   * アノテーション{@link CsvWordForbid}をハンドリングして、CellProcessorの{@link WordForbid}を作成する。
20   * 
21   * @since 2.0
22   * @author T.TSUCHIE
23   *
24   */
25  public class WordForbidFactory implements ConstraintProcessorFactory<CsvWordForbid> {
26      
27      @Override
28      public Optional<CellProcessor> create(final CsvWordForbid anno, final Optional<CellProcessor> next,
29              final FieldAccessor field, final TextFormatter<?> formatter, final Configuration config) {
30          
31          final List<String> words = new ArrayList<>();
32          if(anno.value().length > 0) {
33              words.addAll(Arrays.asList(anno.value()));
34          }
35              
36          if(anno.provider().length > 0) {
37              final ForbiddenWordProvider/com/github/mygreen/supercsv/cellprocessor/constraint/ForbiddenWordProvider.html#ForbiddenWordProvider">ForbiddenWordProvider provider = (ForbiddenWordProvider) config.getBeanFactory().create(anno.provider()[0]);
38              words.addAll(provider.getForbiddenWords(field));
39              
40          }
41          
42          if(anno.value().length == 0 && anno.provider().length == 0) {
43              throw new SuperCsvInvalidAnnotationException(anno, MessageBuilder.create("anno.attr.required")
44                      .var("property", field.getNameWithClass())
45                      .varWithAnno("anno", anno.annotationType())
46                      .var("attrName", "value or provider")
47                      .format());
48          }
49          
50          final WordForbidtraint/WordForbid.html#WordForbid">WordForbid processor = next.map(n -> new WordForbid(words, n))
51                  .orElseGet(() -> new WordForbid(words));
52          processor.setValidationMessage(anno.message());
53          
54          return Optional.of(processor);
55      }
56      
57  }