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.pattern.util;
15  
16  /**
17   * This implementation is intended for use in PatternLayout.
18   * 
19   * @author Ceki Gülcü
20   */
21  public class RegularEscapeUtil implements IEscapeUtil {
22  
23      public void escape(String escapeChars, StringBuffer buf, char next, int pointer) {
24          if (escapeChars.indexOf(next) >= 0) {
25              buf.append(next);
26          } else
27              switch (next) {
28              case '_':
29                  // the \_ sequence is swallowed
30                  break;
31              case '\\':
32                  buf.append(next);
33                  break;
34              case 't':
35                  buf.append('\t');
36                  break;
37              case 'r':
38                  buf.append('\r');
39                  break;
40              case 'n':
41                  buf.append('\n');
42                  break;
43              default:
44                  String commaSeperatedEscapeChars = formatEscapeCharsForListing(escapeChars);
45                  throw new IllegalArgumentException("Illegal char '" + next + " at column " + pointer
46                          + ". Only \\\\, \\_" + commaSeperatedEscapeChars
47                          + ", \\t, \\n, \\r combinations are allowed as escape characters.");
48              }
49      }
50  
51      String formatEscapeCharsForListing(String escapeChars) {
52          StringBuilder commaSeperatedEscapeChars = new StringBuilder();
53          for (int i = 0; i < escapeChars.length(); i++) {
54              commaSeperatedEscapeChars.append(", \\").append(escapeChars.charAt(i));
55          }
56          return commaSeperatedEscapeChars.toString();
57      }
58  
59      // s might be path such as c:\\toto\\file.log
60      // as of version 1.3.0-beta1 this method is no longer used
61      public static String basicEscape(String s) {
62          char c;
63          int len = s.length();
64          StringBuilder sbuf = new StringBuilder(len);
65  
66          int i = 0;
67          while (i < len) {
68              c = s.charAt(i++);
69              if (c == '\\' && i < len  ) {
70                  c = s.charAt(i++);
71                  if (c == 'n') {
72                      c = '\n';
73                  } else if (c == 'r') {
74                      c = '\r';
75                  } else if (c == 't') {
76                      c = '\t';
77                  } else if (c == 'f') {
78                      c = '\f';
79                  } else if (c == '\b') {
80                      c = '\b';
81                  } else if (c == '\"') {
82                      c = '\"';
83                  } else if (c == '\'') {
84                      c = '\'';
85                  } else if (c == '\\') {
86                      c = '\\';
87                  }
88                  /////
89              }
90              sbuf.append(c);
91          } // while
92          return sbuf.toString();
93      }
94  }