KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4j > PatternConverter


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.util.monolog.wrapper.log4j;
19
20 import org.objectweb.util.monolog.file.api.Pattern;
21
22 /**
23  * This tools class permits to convert the printing pattern between log4j
24  * and monolog.
25  *
26  * @author Sebastien Chassande-Barrioz
27  */

28 public class PatternConverter implements Pattern {
29
30     public static String JavaDoc monolog2log4j(String JavaDoc p) {
31         String JavaDoc res = new String JavaDoc(p);
32         int percentPos = res.indexOf('%', 0);
33         boolean end = (percentPos == -1);
34         int nextPercentPos = 0;
35         while (!end) {
36             nextPercentPos = res.indexOf('%', percentPos + 1);
37             if (nextPercentPos == -1) {
38                 nextPercentPos = res.length();
39                 end = true;
40             }
41             for (int i = percentPos + 1; i < nextPercentPos; i++) {
42                 char el = res.charAt(i);
43                 if (el == Pattern.LEVEL) {
44                     res = substitute(res, 'p', i);
45                     break;
46                 }
47                 else if (el == Pattern.OBJECT) {
48                     res = substitute(res, 'C', i);
49                     break;
50                 }
51                 else if (el == Pattern.THREAD) {
52                     res = substitute(res, 't', i);
53                     break;
54                 }
55                 else if (el == Pattern.TOPIC) {
56                     res = substitute(res, 'c', i);
57                     break;
58                 }
59                 else if(Character.isLetter(el)) {
60                     break;
61                 }
62             }
63             percentPos = nextPercentPos;
64         }
65         return res;
66     }
67
68     public static String JavaDoc log4j2monolog(String JavaDoc p) {
69         String JavaDoc res = new String JavaDoc(p);
70         int percentPos = res.indexOf('%', 0);
71         boolean end = (percentPos == -1);
72         int nextPercentPos = 0;
73         while (!end) {
74             nextPercentPos = res.indexOf('%', percentPos + 1);
75             if (nextPercentPos == -1) {
76                 nextPercentPos = res.length();
77                 end = true;
78             }
79             for (int i = percentPos + 1; i < nextPercentPos; i++) {
80                 char el = res.charAt(i);
81                 if (el == 'p') {
82                     res = substitute(res, Pattern.LEVEL, i);
83                     break;
84                 }
85                 else if (el == 'C') {
86                     res = substitute(res, Pattern.OBJECT, i);
87                     break;
88                 }
89                 else if (el == 't') {
90                     res = substitute(res, Pattern.THREAD, i);
91                     break;
92                 }
93                 else if (el == 'c') {
94                     res = substitute(res, Pattern.TOPIC, i);
95                     break;
96                 }
97             }
98             percentPos = nextPercentPos;
99         }
100         return res;
101     }
102
103     private static String JavaDoc substitute(String JavaDoc s, char c, int pos) {
104         if (s == null)
105             return null;
106         if (s.length() <= pos)
107             return s;
108         return s.substring(0, pos) + c + s.substring(pos + 1, s.length());
109     }
110 }
111
Popular Tags