KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > log4jMini > 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
19 package org.objectweb.util.monolog.wrapper.log4jMini;
20
21 import org.objectweb.util.monolog.file.api.Pattern;
22
23 /**
24  * This tools class permits to convert the printing pattern between log4j
25  * and monolog.
26  *
27  * @author Sebastien Chassande-Barrioz
28  */

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