KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > PatternConverter


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.spi.LoggingEvent;
20
21 /**
22
23    <p>PatternConverter is an abtract class that provides the
24    formatting functionality that derived classes need.
25
26    <p>Conversion specifiers in a conversion patterns are parsed to
27    individual PatternConverters. Each of which is responsible for
28    converting a logging event in a converter specific manner.
29
30    @author <a HREF="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
31    @author Ceki G&uuml;lc&uuml;
32
33    @since 0.8.2
34  */

35 public abstract class PatternConverter {
36   public PatternConverter next;
37   int min = -1;
38   int max = 0x7FFFFFFF;
39   boolean leftAlign = false;
40
41   protected
42   PatternConverter() { }
43   
44   protected
45   PatternConverter(FormattingInfo fi) {
46     min = fi.min;
47     max = fi.max;
48     leftAlign = fi.leftAlign;
49   }
50
51   /**
52      Derived pattern converters must override this method in order to
53      convert conversion specifiers in the correct way.
54   */

55   abstract
56   protected
57   String JavaDoc convert(LoggingEvent event);
58
59   /**
60      A template method for formatting in a converter specific way.
61    */

62   public
63   void format(StringBuffer JavaDoc sbuf, LoggingEvent e) {
64     String JavaDoc s = convert(e);
65
66     if(s == null) {
67       if(0 < min)
68     spacePad(sbuf, min);
69       return;
70     }
71
72     int len = s.length();
73
74     if(len > max)
75       sbuf.append(s.substring(len-max));
76     else if(len < min) {
77       if(leftAlign) {
78     sbuf.append(s);
79     spacePad(sbuf, min-len);
80       }
81       else {
82     spacePad(sbuf, min-len);
83     sbuf.append(s);
84       }
85     }
86     else
87       sbuf.append(s);
88   }
89
90   static String JavaDoc[] SPACES = {" ", " ", " ", " ", //1,2,4,8 spaces
91
" ", // 16 spaces
92
" " }; // 32 spaces
93

94   /**
95      Fast space padding method.
96   */

97   public
98   void spacePad(StringBuffer JavaDoc sbuf, int length) {
99     while(length >= 32) {
100       sbuf.append(SPACES[5]);
101       length -= 32;
102     }
103     
104     for(int i = 4; i >= 0; i--) {
105       if((length & (1<<i)) != 0) {
106     sbuf.append(SPACES[i]);
107       }
108     }
109   }
110 }
111
Popular Tags