View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  /**
17   * This class supports mapping tokens (set of same character sequences) to
18   * regular expressions as appropriate for SimpleDateFormatter.
19   * 
20   * @author ceki
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  }