1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.util;
15
16 import java.text.SimpleDateFormat;
17 import java.util.ArrayList;
18 import java.util.List;
19
20
21
22
23
24
25
26 public class DatePatternToRegexUtil {
27
28 final String datePattern;
29 final int datePatternLength;
30 final CharSequenceToRegexMapper regexMapper = new CharSequenceToRegexMapper();
31
32 public DatePatternToRegexUtil(String datePattern) {
33 this.datePattern = datePattern;
34 datePatternLength = datePattern.length();
35 }
36
37 public String toRegex() {
38 List<CharSequenceState> charSequenceList = tokenize();
39 StringBuilder sb = new StringBuilder();
40 for (CharSequenceState seq : charSequenceList) {
41 sb.append(regexMapper.toRegex(seq));
42 }
43 return sb.toString();
44 }
45
46 private List<CharSequenceState> tokenize() {
47 List<CharSequenceState> sequenceList = new ArrayList<CharSequenceState>();
48
49 CharSequenceState lastCharSequenceState = null;
50
51 for (int i = 0; i < datePatternLength; i++) {
52 char t = datePattern.charAt(i);
53 if (lastCharSequenceState == null || lastCharSequenceState.c != t) {
54 lastCharSequenceState = new CharSequenceState(t);
55 sequenceList.add(lastCharSequenceState);
56 } else {
57 lastCharSequenceState.incrementOccurrences();
58 }
59 }
60 return sequenceList;
61 }
62 }