1 4 package com.tc.text; 5 6 public class Banner { 7 8 public static void errorBanner(String message) { 9 System.err.println(makeBanner(message, "ERROR")); 10 System.err.flush(); 11 } 12 13 public static void warnBanner(String message) { 14 System.err.println(makeBanner(message, "WARNING")); 15 System.err.flush(); 16 } 17 18 private static final int MAX_LINE = 72; 19 private static final int BOX_WIDTH = MAX_LINE + 4; 20 21 private static String makeBanner(String message, String type) { 22 if (message == null) { 23 message = "<no message>"; 24 } 25 26 final int topStars = BOX_WIDTH - (type.length() + 2); final int begin = topStars / 2; 28 final int end = (topStars % 2 == 0) ? begin : begin + 1; 29 30 StringBuffer buf = new StringBuffer (); 31 buf.append("\n"); 32 33 for (int i = 0; i < begin; i++) { 34 buf.append('*'); 35 } 36 37 buf.append(' ').append(type).append(' '); 38 39 for (int i = 0; i < end; i++) { 40 buf.append('*'); 41 } 42 43 String [] words = message.split(" "); 44 45 int word = 0; 46 while (word < words.length) { 47 int length = words[word].length(); 48 buf.append("\n* ").append(words[word]); 49 word++; 50 51 while (length <= MAX_LINE && word < words.length) { 52 int next = words[word].length() + 1; if (length + next <= MAX_LINE) { 54 buf.append(' ').append(words[word++]); 55 length += next; 56 } else { 57 break; 58 } 59 } 60 } 61 62 buf.append("\n"); 63 for (int i = 0; i < BOX_WIDTH; i++) { 64 buf.append('*'); 65 } 66 buf.append("\n"); 67 68 return buf.toString(); 69 } 70 } 71 | Popular Tags |