001: /**
002: * Copyright (C) 2001-2003 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.log4jMini;
018:
019: import org.objectweb.util.monolog.file.api.Pattern;
020:
021: /**
022: * This tools class permits to convert the printing pattern between log4j
023: * and monolog.
024: *
025: * @author Sebastien Chassande-Barrioz
026: */
027: public class PatternConverter implements Pattern {
028:
029: public static String monolog2log4j(String p) {
030: String res = new String(p);
031: int percentPos = res.indexOf('%', 0);
032: boolean end = (percentPos == -1);
033: int nextPercentPos = 0;
034: while (!end) {
035: nextPercentPos = res.indexOf('%', percentPos + 1);
036: if (nextPercentPos == -1) {
037: nextPercentPos = res.length();
038: end = true;
039: }
040: for (int i = percentPos + 1; i < nextPercentPos; i++) {
041: char el = res.charAt(i);
042: if (el == Pattern.LEVEL) {
043: res = substitute(res, 'p', i);
044: break;
045: } else if (el == Pattern.OBJECT) {
046: res = substitute(res, 'C', i);
047: break;
048: } else if (el == Pattern.THREAD) {
049: res = substitute(res, 't', i);
050: break;
051: } else if (el == Pattern.TOPIC) {
052: res = substitute(res, 'c', i);
053: break;
054: } else if (Character.isLetter(el)) {
055: break;
056: }
057: }
058: percentPos = nextPercentPos;
059: }
060: return res;
061: }
062:
063: public static String log4j2monolog(String p) {
064: String res = new String(p);
065: int percentPos = res.indexOf('%', 0);
066: boolean end = (percentPos == -1);
067: int nextPercentPos = 0;
068: while (!end) {
069: nextPercentPos = res.indexOf('%', percentPos + 1);
070: if (nextPercentPos == -1) {
071: nextPercentPos = res.length();
072: end = true;
073: }
074: for (int i = percentPos + 1; i < nextPercentPos; i++) {
075: char el = res.charAt(i);
076: if (el == 'p') {
077: res = substitute(res, Pattern.LEVEL, i);
078: break;
079: } else if (el == 'C') {
080: res = substitute(res, Pattern.OBJECT, i);
081: break;
082: } else if (el == 't') {
083: res = substitute(res, Pattern.THREAD, i);
084: break;
085: } else if (el == 'c') {
086: res = substitute(res, Pattern.TOPIC, i);
087: break;
088: }
089: }
090: percentPos = nextPercentPos;
091: }
092: return res;
093: }
094:
095: private static String substitute(String s, char c, int pos) {
096: if (s == null)
097: return null;
098: if (s.length() <= pos)
099: return s;
100: return s.substring(0, pos) + c
101: + s.substring(pos + 1, s.length());
102: }
103: }
|