f18e264ae05e7fac91f86f3a945c16fe1f677c3a
[jalview.git] / srcjar2 / org / apache / log4j / helpers / PatternConverter.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.log4j.helpers;
19
20 import org.apache.log4j.spi.LoggingEvent;
21
22 /**
23
24    <p>PatternConverter is an abtract class that provides the
25    formatting functionality that derived classes need.
26
27    <p>Conversion specifiers in a conversion patterns are parsed to
28    individual PatternConverters. Each of which is responsible for
29    converting a logging event in a converter specific manner.
30
31    @author <a href="mailto:cakalijp@Maritz.com">James P. Cakalic</a>
32    @author Ceki G&uuml;lc&uuml;
33
34    @since 0.8.2
35  */
36 public abstract class PatternConverter {
37   public PatternConverter next;
38   int min = -1;
39   int max = 0x7FFFFFFF;
40   boolean leftAlign = false;
41
42   protected
43   PatternConverter() {  }
44   
45   protected
46   PatternConverter(FormattingInfo fi) {
47     min = fi.min;
48     max = fi.max;
49     leftAlign = fi.leftAlign;
50   }
51
52   /**
53      Derived pattern converters must override this method in order to
54      convert conversion specifiers in the correct way.
55   */
56   abstract
57   protected
58   String convert(LoggingEvent event);
59
60   /**
61      A template method for formatting in a converter specific way.
62    */
63   public
64   void format(StringBuffer sbuf, LoggingEvent e) {
65     String s = convert(e);
66
67     if(s == null) {
68       if(0 < min) {
69         spacePad(sbuf, min);
70     }
71       return;
72     }
73
74     int len = s.length();
75
76     if(len > max) {
77         sbuf.append(s.substring(len-max));
78     } else if(len < min) {
79       if(leftAlign) {   
80         sbuf.append(s);
81         spacePad(sbuf, min-len);
82       }
83       else {
84         spacePad(sbuf, min-len);
85         sbuf.append(s);
86       }
87     } else {
88         sbuf.append(s);
89     }
90   }     
91
92   static String[] SPACES = {" ", "  ", "    ", "        ", //1,2,4,8 spaces
93                             "                ", // 16 spaces
94                             "                                " }; // 32 spaces
95
96   /**
97      Fast space padding method.
98   */
99   public
100   void spacePad(StringBuffer sbuf, int length) {
101     while(length >= 32) {
102       sbuf.append(SPACES[5]);
103       length -= 32;
104     }
105     
106     for(int i = 4; i >= 0; i--) {       
107       if((length & (1<<i)) != 0) {
108         sbuf.append(SPACES[i]);
109       }
110     }
111   }
112 }