001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.pattern.util;
015
016/**
017 * This implementation is intended for use in PatternLayout.
018 * 
019 * @author Ceki Gülcü
020 */
021public class RegularEscapeUtil implements IEscapeUtil {
022
023    public void escape(String escapeChars, StringBuffer buf, char next, int pointer) {
024        if (escapeChars.indexOf(next) >= 0) {
025            buf.append(next);
026        } else
027            switch (next) {
028            case '_':
029                // the \_ sequence is swallowed
030                break;
031            case '\\':
032                buf.append(next);
033                break;
034            case 't':
035                buf.append('\t');
036                break;
037            case 'r':
038                buf.append('\r');
039                break;
040            case 'n':
041                buf.append('\n');
042                break;
043            default:
044                String commaSeperatedEscapeChars = formatEscapeCharsForListing(escapeChars);
045                throw new IllegalArgumentException("Illegal char '" + next + " at column " + pointer
046                        + ". Only \\\\, \\_" + commaSeperatedEscapeChars
047                        + ", \\t, \\n, \\r combinations are allowed as escape characters.");
048            }
049    }
050
051    String formatEscapeCharsForListing(String escapeChars) {
052        StringBuilder commaSeperatedEscapeChars = new StringBuilder();
053        for (int i = 0; i < escapeChars.length(); i++) {
054            commaSeperatedEscapeChars.append(", \\").append(escapeChars.charAt(i));
055        }
056        return commaSeperatedEscapeChars.toString();
057    }
058
059    // s might be path such as c:\\toto\\file.log
060    // as of version 1.3.0-beta1 this method is no longer used
061    public static String basicEscape(String s) {
062        char c;
063        int len = s.length();
064        StringBuilder sbuf = new StringBuilder(len);
065
066        int i = 0;
067        while (i < len) {
068            c = s.charAt(i++);
069            if (c == '\\' && i < len  ) {
070                c = s.charAt(i++);
071                if (c == 'n') {
072                    c = '\n';
073                } else if (c == 'r') {
074                    c = '\r';
075                } else if (c == 't') {
076                    c = '\t';
077                } else if (c == 'f') {
078                    c = '\f';
079                } else if (c == '\b') {
080                    c = '\b';
081                } else if (c == '\"') {
082                    c = '\"';
083                } else if (c == '\'') {
084                    c = '\'';
085                } else if (c == '\\') {
086                    c = '\\';
087                }
088                /////
089            }
090            sbuf.append(c);
091        } // while
092        return sbuf.toString();
093    }
094}