01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tc.text;
06:
07: public class Banner {
08:
09: public static void errorBanner(String message) {
10: System.err.println(makeBanner(message, "ERROR"));
11: System.err.flush();
12: }
13:
14: public static void warnBanner(String message) {
15: System.err.println(makeBanner(message, "WARNING"));
16: System.err.flush();
17: }
18:
19: public static void infoBanner(String message) {
20: System.out.println(makeBanner(message, "INFO"));
21: System.out.flush();
22: }
23:
24: private static final int MAX_LINE = 72;
25: private static final int BOX_WIDTH = MAX_LINE + 4;
26:
27: public static String makeBanner(String message, String type) {
28: if (message == null) {
29: message = "<no message>";
30: }
31:
32: final int topStars = BOX_WIDTH - (type.length() + 2); // +2 for spaces on either side of "type"
33: final int begin = topStars / 2;
34: final int end = (topStars % 2 == 0) ? begin : begin + 1;
35:
36: StringBuffer buf = new StringBuffer();
37: buf.append("\n");
38:
39: for (int i = 0; i < begin; i++) {
40: buf.append('*');
41: }
42:
43: buf.append(' ').append(type).append(' ');
44:
45: for (int i = 0; i < end; i++) {
46: buf.append('*');
47: }
48:
49: String[] lines = message.split("\n");
50:
51: for (int i = 0; i < lines.length; i++) {
52: String[] words = lines[i].split(" ");
53:
54: int word = 0;
55: if (words.length == 0)
56: buf.append("\n* ");
57: while (word < words.length) {
58: int length = words[word].length();
59: buf.append("\n* ").append(words[word]);
60: word++;
61:
62: while (length <= MAX_LINE && word < words.length) {
63: int next = words[word].length() + 1; // +1 for space
64: if (length + next <= MAX_LINE) {
65: buf.append(' ').append(words[word++]);
66: length += next;
67: } else {
68: break;
69: }
70: }
71: }
72: }
73: buf.append("\n");
74: for (int i = 0; i < BOX_WIDTH; i++) {
75: buf.append('*');
76: }
77: buf.append("\n");
78:
79: return buf.toString();
80: }
81: }
|