1 16 package org.jboss.logging.jdk.format; 17 18 import java.util.logging.LogRecord ; 19 20 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 55 abstract 56 protected String convert(LogRecord event); 57 58 61 public void format(StringBuffer sbuf, LogRecord e) 62 { 63 String 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 [] SPACES = {" ", " ", " ", " ", " ", " "}; 97 100 public void spacePad(StringBuffer 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 |