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 + ". Only \\\\, \\_" + commaSeperatedEscapeChars
46                                  + ", \\t, \\n, \\r combinations are allowed as escape characters.");
47              }
48      }
49  
50      String formatEscapeCharsForListing(String escapeChars) {
51          StringBuilder commaSeperatedEscapeChars = new StringBuilder();
52          for (int i = 0; i < escapeChars.length(); i++) {
53              commaSeperatedEscapeChars.append(", \\").append(escapeChars.charAt(i));
54          }
55          return commaSeperatedEscapeChars.toString();
56      }
57  
58      public static String basicEscape(String s) {
59          char c;
60          int len = s.length();
61          StringBuilder sbuf = new StringBuilder(len);
62  
63          int i = 0;
64          while (i < len) {
65              c = s.charAt(i++);
66              if (c == '\\') {
67                  c = s.charAt(i++);
68                  if (c == 'n') {
69                      c = '\n';
70                  } else if (c == 'r') {
71                      c = '\r';
72                  } else if (c == 't') {
73                      c = '\t';
74                  } else if (c == 'f') {
75                      c = '\f';
76                  } else if (c == '\b') {
77                      c = '\b';
78                  } else if (c == '\"') {
79                      c = '\"';
80                  } else if (c == '\'') {
81                      c = '\'';
82                  } else if (c == '\\') {
83                      c = '\\';
84                  }
85              }
86              sbuf.append(c);
87          }
88          return sbuf.toString();
89      }
90  }