View Javadoc

1   /**
2    * LOGBack: the reliable, fast and flexible logging library for Java.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  
11  package ch.qos.logback.core.rolling.helper;
12  
13  import java.util.Date;
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import ch.qos.logback.core.Context;
18  import ch.qos.logback.core.pattern.Converter;
19  import ch.qos.logback.core.pattern.ConverterUtil;
20  import ch.qos.logback.core.pattern.parser.Node;
21  import ch.qos.logback.core.pattern.parser.Parser;
22  import ch.qos.logback.core.pattern.parser.ScanException;
23  import ch.qos.logback.core.pattern.util.AlmostAsIsEscapeUtil;
24  import ch.qos.logback.core.spi.ContextAwareBase;
25  
26  
27  /**
28   * 
29   * After parsing file name patterns, given a number or a date, instances of this class 
30   * can be used to compute a file name according to the file name pattern and the given 
31   * integer or date.
32   * 
33   * @author Ceki Gülcü
34   * 
35   */
36  public class FileNamePattern extends ContextAwareBase {
37  
38    static final Map<String, String> CONVERTER_MAP = new HashMap<String, String>();
39    static {
40      CONVERTER_MAP.put("i", IntegerTokenConverter.class.getName());
41      CONVERTER_MAP.put("d", DateTokenConverter.class.getName());
42    }
43  
44    String pattern;
45    Converter<Object> headTokenConverter;
46  
47    public FileNamePattern(String patternArg, Context contextArg) {
48      setPattern(patternArg);
49      setContext(contextArg);
50      parse();
51      ConverterUtil.startConverters(this.headTokenConverter);
52    }
53  
54    void parse() {
55      try {
56        Parser<Object> p = new Parser<Object>(pattern, new AlmostAsIsEscapeUtil());
57        p.setContext(context);
58        Node t = p.parse();
59        this.headTokenConverter = p.compile(t, CONVERTER_MAP);
60  
61      } catch (ScanException sce) {
62        addError("Failed to parse pattern \"" + pattern + "\".", sce);
63      }
64    }
65  
66    public String toString() {
67      return pattern;
68    }
69  
70    // void parsex() {
71    // int lastIndex = 0;
72    //
73    // TokenConverter tc = null;
74    //
75    // MAIN_LOOP: while (true) {
76    // int i = pattern.indexOf('%', lastIndex);
77    //
78    // if (i == -1) {
79    // String remainingStr = pattern.substring(lastIndex);
80    //
81    // // System.out.println("adding the identity token, I");
82    // addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
83    //
84    // break;
85    // } else {
86    // // test for degenerate case where the '%' character is at the end.
87    // if (i == (patternLength - 1)) {
88    // String remainingStr = pattern.substring(lastIndex);
89    // addTokenConverter(tc, new IdentityTokenConverter(remainingStr));
90    //
91    // break;
92    // }
93    //
94    // // System.out.println("adding the identity token, II");
95    // tc = addTokenConverter(tc, new IdentityTokenConverter(pattern
96    // .substring(lastIndex, i)));
97    //
98    // // At this stage, we can suppose that i < patternLen -1
99    // char nextChar = pattern.charAt(i + 1);
100   //
101   // switch (nextChar) {
102   // case 'i':
103   // tc = addTokenConverter(tc, new IntegerTokenConverter());
104   // lastIndex = i + 2;
105   //
106   // break; // break from switch statement
107   //
108   // case 'd':
109   //
110   // int optionEnd = getOptionEnd(i + 2);
111   //
112   // String option;
113   //
114   // if (optionEnd != -1) {
115   // option = pattern.substring(i + 3, optionEnd);
116   // lastIndex = optionEnd + 1;
117   // } else {
118   // addInfo("Assuming daily rotation schedule");
119   // option = "yyyy-MM-dd";
120   // lastIndex = i + 2;
121   // }
122   // tc = addTokenConverter(tc, new DateTokenConverter(option));
123   // break; // break from switch statement
124   //
125   // case '%':
126   // tc = addTokenConverter(tc, new IdentityTokenConverter("%"));
127   // lastIndex = i + 2;
128   //
129   // break;
130   //
131   // default:
132   // throw new IllegalArgumentException("The pattern[" + pattern
133   // + "] does not contain a valid specifer at position " + (i + 1));
134   // }
135   // }
136   // }
137   // }
138   //
139   // /**
140   // * Find the position of the last character of option enclosed within the
141   // '{}'
142   // * characters inside the pattern
143   // */
144   // protected int getOptionEnd(int i) {
145   // // logger.debug("Char at "+i+" "+pattern.charAt(i));
146   // if ((i < patternLength) && (pattern.charAt(i) == '{')) {
147   // int end = pattern.indexOf('}', i);
148   //
149   // if (end > i) {
150   // return end;
151   // } else {
152   // return -1;
153   // }
154   // }
155   //
156   // return -1;
157   // }
158   //
159   // TokenConverter addTokenConverter(TokenConverter tc,
160   // TokenConverter newTokenConverter) {
161   // if (tc == null) {
162   // tc = headTokenConverter = newTokenConverter;
163   // } else {
164   // tc.next = newTokenConverter;
165   // tc = newTokenConverter;
166   // }
167   //
168   // return tc;
169   // }
170 
171   public DateTokenConverter getDateTokenConverter() {
172     Converter p = headTokenConverter;
173 
174     while (p != null) {
175       if (p instanceof DateTokenConverter) {
176         return (DateTokenConverter) p;
177       }
178 
179       p = p.getNext();
180     }
181 
182     return null;
183   }
184 
185   public IntegerTokenConverter getIntegerTokenConverter() {
186     Converter p = headTokenConverter;
187 
188     while (p != null) {
189       if (p instanceof IntegerTokenConverter) {
190         return (IntegerTokenConverter) p;
191       }
192 
193       p = p.getNext();
194     }
195     return null;
196   }
197 
198   public String convert(Object o) {
199     Converter<Object> p = headTokenConverter;
200     StringBuffer buf = new StringBuffer();
201     while (p != null) {
202       buf.append(p.convert(o));
203       p = p.getNext();
204     }
205     return buf.toString();
206   }
207 
208   public String convertInt(int i) {
209     Integer integerArg = new Integer(i);
210     return convert(integerArg);
211   }
212 
213   public String convertDate(Date date) {
214     return convert(date);
215   }
216 
217   public void setPattern(String pattern) {
218     if (pattern != null) {
219       // Trailing spaces in the pattern are assumed to be undesired.
220       this.pattern = pattern.trim();
221     }
222   }
223 
224   public String getPattern() {
225     return pattern;
226   }
227 }