View Javadoc
1   package com.github.mygreen.cellformatter;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import com.github.mygreen.cellformatter.lang.ArgUtils;
7   import com.github.mygreen.cellformatter.term.AsteriskTerm;
8   import com.github.mygreen.cellformatter.term.EscapedCharTerm;
9   import com.github.mygreen.cellformatter.term.LocaelSymbolTerm;
10  import com.github.mygreen.cellformatter.term.OtherTerm;
11  import com.github.mygreen.cellformatter.term.TextTerm;
12  import com.github.mygreen.cellformatter.term.UnderscoreTerm;
13  import com.github.mygreen.cellformatter.term.WordTerm;
14  import com.github.mygreen.cellformatter.tokenizer.Token;
15  import com.github.mygreen.cellformatter.tokenizer.TokenStore;
16  
17  
18  /**
19   * 書式を解析して{@link ConditionTextFormatter}のインスタンスを作成するクラス。
20   * @author T.TSUCHIE
21   *
22   */
23  public class ConditionTextFormatterFactory extends ConditionFormatterFactory<ConditionTextFormatter> {
24      
25      /**
26       * テキストの書式かどうか判定する。
27       * @param store
28       * @return
29       */
30      public boolean isTextPattern(final TokenStore store) {
31          return store.containsInFactor("@");
32      }
33      
34      /**
35       * {@link ConditionTextFormatter}のインスタンスを作成する。
36       * @param store
37       * @return
38       * @throws IllegalArgumentException store is null.
39       */
40      public ConditionTextFormatter create(final TokenStore store) {
41          ArgUtils.notNull(store, "store");
42          
43          final ConditionTextFormatterormatter.html#ConditionTextFormatter">ConditionTextFormatter formatter = new ConditionTextFormatter(store.getConcatenatedToken());
44          
45          for(Token token : store.getTokens()) {
46              
47              if(token instanceof Token.Condition) {
48                  // 条件の場合
49                  final Token.Condition conditionToken = token.asCondition();
50                  final String condition = conditionToken.getCondition();
51                  formatter.addCondition(condition);
52                  
53                  if(isConditionOperator(conditionToken)) {
54                      setupConditionOperator(formatter, conditionToken);
55                      
56                  } else if(isConditionLocale(conditionToken)) {
57                      setupConditionLocale(formatter, conditionToken);
58                      
59                  } else if(isConditionLocaleSymbol(conditionToken)) {
60                      final LocaleSymbol localeSymbol = setupConditionLocaleSymbol(formatter, conditionToken);
61                      formatter.addTerm(new LocaelSymbolTerm<String>(localeSymbol));
62                      
63                  } else if(isConditionDbNum(conditionToken)) {
64                      setupConditionDbNum(formatter, conditionToken);
65                      
66                  } else if(isConditionColor(conditionToken)) {
67                      setupConditionColor(formatter, conditionToken);
68                      
69                  }
70                  
71              } else if(token instanceof Token.Word) {
72                  formatter.addTerm(new WordTerm<String>(token.asWord()));
73                  
74              } else if(token instanceof Token.EscapedChar) {
75                  formatter.addTerm(new EscapedCharTerm<String>(token.asEscapedChar()));
76                  
77              } else if(token instanceof Token.Underscore) {
78                  formatter.addTerm(new UnderscoreTerm<String>(token.asUnderscore()));
79                  
80              } else if(token instanceof Token.Asterisk) {
81                  formatter.addTerm(new AsteriskTerm<String>(token.asAsterisk()));
82                  
83              } else if(token instanceof Token.Factor) {
84                  // 因子を記号'@'で分割する
85                  List<Token> list = convertFactor(token.asFactor());
86                  
87                  for(Token item : list) {
88                      if(item.equals(Token.Symbol.SYMBOL_AT_MARK)) {
89                          formatter.addTerm(TextTerm.atMark(item.asSymbol()));
90                      } else {
91                          formatter.addTerm(new OtherTerm<String>(item));
92                      }
93                  }
94                  
95              }
96              
97          }
98          
99          return formatter;
100         
101     }
102     
103     /**
104      * 書式の因子を記号'@'とそれ以外に変換する。
105      * @param str
106      * @return
107      */
108     private List<Token> convertFactor(final Token.Factor factor) {
109         
110         final String item = factor.getValue();
111         final int itemLength = item.length();
112         final List<Token> list = new ArrayList<>();
113         
114         StringBuilder sb = new StringBuilder();
115         for(int i=0; i < itemLength; i++) {
116             char c = item.charAt(i);
117             if(c == '@') {
118                 // バッファから取り出し、新たにバッファを作成する
119                 list.add(Token.factor(sb.toString()));
120                 sb = new StringBuilder();
121                 
122                 list.add(Token.SYMBOL_AT_MARK);
123                 
124             } else {
125                 sb.append(c);
126             }
127         }
128         
129         if(sb.length() > 0) {
130             list.add(Token.factor(sb.toString()));
131             sb = null;
132         }
133         
134         return list;
135     }
136 }