KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > logging > jdk > format > 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 package org.jboss.logging.jdk.format;
17
18 import java.util.logging.LogRecord JavaDoc;
19
20 /**
21  * <p>PatternConverter is an abtract class that provides the
22  * formatting functionality that derived classes need.
23  * <p/>
24  * <p>Conversion specifiers in a conversion patterns are parsed to
25  * individual PatternConverters. Each of which is responsible for
26  * converting a logging event in a converter specific manner.
27  *
28  * @author <a HREF="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
29  * @author Ceki G&uuml;lc&uuml;
30  * @author Scott.Stark@jboss.org
31  * @version $Revision: 1958 $
32  */

33 public abstract class PatternConverter
34 {
35    public PatternConverter next;
36    int min = -1;
37    int max = 0x7FFFFFFF;
38    boolean leftAlign = false;
39
40    protected PatternConverter()
41    {
42    }
43
44    protected PatternConverter(FormattingInfo fi)
45    {
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 String JavaDoc convert(LogRecord JavaDoc event);
57
58    /**
59     * A template method for formatting in a converter specific way.
60     */

61    public void format(StringBuffer JavaDoc sbuf, LogRecord JavaDoc e)
62    {
63       String JavaDoc s = convert(e);
64
65       if (s == null)
66       {
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       {
78          if (leftAlign)
79          {
80             sbuf.append(s);
81             spacePad(sbuf, min - len);
82          }
83          else
84          {
85             spacePad(sbuf, min - len);
86             sbuf.append(s);
87          }
88       }
89       else
90          sbuf.append(s);
91    }
92
93    static String JavaDoc[] SPACES = {" ", " ", " ", " ", //1,2,4,8 spaces
94
" ", // 16 spaces
95
" "}; // 32 spaces
96

97    /**
98     * Fast space padding method.
99     */

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