View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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  import java.util.Date;
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import ch.qos.logback.core.Context;
21  import ch.qos.logback.core.pattern.Converter;
22  import ch.qos.logback.core.pattern.ConverterUtil;
23  import ch.qos.logback.core.pattern.LiteralConverter;
24  import ch.qos.logback.core.pattern.parser.Node;
25  import ch.qos.logback.core.pattern.parser.Parser;
26  import ch.qos.logback.core.spi.ScanException;
27  import ch.qos.logback.core.pattern.util.AlmostAsIsEscapeUtil;
28  import ch.qos.logback.core.spi.ContextAwareBase;
29  
30  /**
31   * After parsing file name patterns, given a number or a date, instances of this
32   * class can be used to compute a file name according to the file name pattern
33   * and the current date or integer.
34   * 
35   * @author Ceki Gülcü
36   * 
37   */
38  public class FileNamePattern extends ContextAwareBase {
39  
40      static final Map<String, String> CONVERTER_MAP = new HashMap<String, String>();
41      static {
42          CONVERTER_MAP.put(IntegerTokenConverter.CONVERTER_KEY, IntegerTokenConverter.class.getName());
43          CONVERTER_MAP.put(DateTokenConverter.CONVERTER_KEY, DateTokenConverter.class.getName());
44      }
45  
46      String pattern;
47      Converter<Object> headTokenConverter;
48  
49      public FileNamePattern(String patternArg, Context contextArg) {
50          // the pattern is slashified
51          setPattern(FileFilterUtil.slashify(patternArg));
52          setContext(contextArg);
53          parse();
54          ConverterUtil.startConverters(this.headTokenConverter);
55      }
56  
57      
58      void parse() {
59          try {
60              // http://jira.qos.ch/browse/LOGBACK-197
61              // we escape ')' for parsing purposes. Note that the original pattern is preserved
62              // because it is shown to the user in status messages. We don't want the escaped version
63              // to leak out.
64              String patternForParsing = escapeRightParantesis(pattern);
65              Parser<Object> p = new Parser<Object>(patternForParsing, new AlmostAsIsEscapeUtil());
66              p.setContext(context);
67              Node t = p.parse();
68              this.headTokenConverter = p.compile(t, CONVERTER_MAP);
69  
70          } catch (ScanException sce) {
71              addError("Failed to parse pattern \"" + pattern + "\".", sce);
72          }
73      }
74  
75      String escapeRightParantesis(String in) {
76          return pattern.replace(")", "\\)");
77      }
78  
79      public String toString() {
80          return pattern;
81      }
82  
83      @Override
84      public int hashCode() {
85          final int prime = 31;
86          int result = 1;
87          result = prime * result + ((pattern == null) ? 0 : pattern.hashCode());
88          return result;
89      }
90  
91  
92      @Override
93      public boolean equals(Object obj) {
94          if (this == obj)
95              return true;
96          if (obj == null)
97              return false;
98          if (getClass() != obj.getClass())
99              return false;
100         FileNamePattern other = (FileNamePattern) obj;
101         if (pattern == null) {
102             if (other.pattern != null)
103                 return false;
104         } else if (!pattern.equals(other.pattern))
105             return false;
106         return true;
107     }
108 
109 
110     public DateTokenConverter<Object> getPrimaryDateTokenConverter() {
111         Converter<Object> p = headTokenConverter;
112 
113         while (p != null) {
114             if (p instanceof DateTokenConverter) {
115                 DateTokenConverter<Object> dtc = (DateTokenConverter<Object>) p;
116                 // only primary converters should be returned as
117                 if (dtc.isPrimary())
118                     return dtc;
119             }
120 
121             p = p.getNext();
122         }
123 
124         return null;
125     }
126 
127     public IntegerTokenConverter getIntegerTokenConverter() {
128         Converter<Object> p = headTokenConverter;
129 
130         while (p != null) {
131             if (p instanceof IntegerTokenConverter) {
132                 return (IntegerTokenConverter) p;
133             }
134 
135             p = p.getNext();
136         }
137         return null;
138     }
139 
140     public boolean hasIntegerTokenCOnverter() {
141         IntegerTokenConverter itc = getIntegerTokenConverter();
142         return itc != null;
143     }
144     
145     public String convertMultipleArguments(Object... objectList) {
146         StringBuilder buf = new StringBuilder();
147         Converter<Object> c = headTokenConverter;
148         while (c != null) {
149             if (c instanceof MonoTypedConverter) {
150                 MonoTypedConverter monoTyped = (MonoTypedConverter) c;
151                 for (Object o : objectList) {
152                     if (monoTyped.isApplicable(o)) {
153                         buf.append(c.convert(o));
154                     }
155                 }
156             } else {
157                 buf.append(c.convert(objectList));
158             }
159             c = c.getNext();
160         }
161         return buf.toString();
162     }
163 
164     public String convert(Object o) {
165         StringBuilder buf = new StringBuilder();
166         Converter<Object> p = headTokenConverter;
167         while (p != null) {
168             buf.append(p.convert(o));
169             p = p.getNext();
170         }
171         return buf.toString();
172     }
173 
174     public String convertInt(int i) {
175         return convert(i);
176     }
177 
178     public void setPattern(String pattern) {
179         if (pattern != null) {
180             // Trailing spaces in the pattern are assumed to be undesired.
181             this.pattern = pattern.trim();
182         }
183     }
184 
185     public String getPattern() {
186         return pattern;
187     }
188 
189     
190     /**
191      * Given date, convert this instance to a regular expression.
192      *
193      * Used to compute sub-regex when the pattern has both %d and %i, and the
194      * date is known.
195      * 
196      * @param date - known date
197      */
198     public String toRegexForFixedDate(Date date) {
199         StringBuilder buf = new StringBuilder();
200         Converter<Object> p = headTokenConverter;
201         while (p != null) {
202             if (p instanceof LiteralConverter) {
203                 buf.append(p.convert(null));
204             } else if (p instanceof IntegerTokenConverter) {
205                 buf.append("(\\d+)");
206             } else if (p instanceof DateTokenConverter) {
207                 buf.append(p.convert(date));
208             }
209             p = p.getNext();
210         }
211         return buf.toString();
212     }
213 
214     /**
215      * Given date, convert this instance to a regular expression
216      */
217     public String toRegex() {
218         StringBuilder buf = new StringBuilder();
219         Converter<Object> p = headTokenConverter;
220         while (p != null) {
221             if (p instanceof LiteralConverter) {
222                 buf.append(p.convert(null));
223             } else if (p instanceof IntegerTokenConverter) {
224                 buf.append("\\d+");
225             } else if (p instanceof DateTokenConverter) {
226                 DateTokenConverter<Object> dtc = (DateTokenConverter<Object>) p;
227                 buf.append(dtc.toRegex());
228             }
229             p = p.getNext();
230         }
231         return buf.toString();
232     }
233 }