1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.rolling.helper;
15
16
17
18
19
20
21
22
23 class SequenceToRegex4SDF {
24 final char c;
25 int occurrences;
26
27 public SequenceToRegex4SDF(char c) {
28 this.c = c;
29 this.occurrences = 1;
30 }
31
32 void inc() {
33 occurrences++;
34 }
35
36 String toRegex() {
37 switch (c) {
38 case 'G':
39 case 'z':
40 return ".*";
41 case 'M':
42 if (occurrences >= 3) {
43 return ".*";
44 } else {
45 return number(occurrences);
46 }
47 case 'y':
48 case 'w':
49 case 'W':
50 case 'D':
51 case 'd':
52 case 'F':
53 case 'H':
54 case 'k':
55 case 'K':
56 case 'h':
57 case 'm':
58 case 's':
59 case 'S':
60 return number(occurrences);
61 case 'E':
62 return ".{2,12}";
63 case 'a':
64 return ".{2}";
65 case 'Z':
66 return "(\\+|-)\\d{4}";
67 case '.':
68 return "\\.";
69 case '\\':
70 throw new IllegalStateException("Forward slashes are not allowed");
71 case '\'':
72 if (occurrences == 1) {
73 return "";
74 }
75 throw new IllegalStateException("Too many single quotes");
76 default:
77 if (occurrences == 1) {
78 return "" + c;
79 } else {
80 return c + "{" + occurrences + "}";
81 }
82 }
83 }
84
85 @Override
86 public String toString() {
87 return c + "(" + occurrences + ")";
88 }
89
90 private String number(int occurences) {
91 return "\\d{" + occurrences + "}";
92 }
93 }