KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > exception > ExceptionWrapperImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.exception;
5
6 public class ExceptionWrapperImpl implements ExceptionWrapper {
7
8   private final int MAX_STAR_COUNT = 79;
9
10   public String JavaDoc wrap(String JavaDoc message) {
11     message = (message == null) ? String.valueOf(message) : message;
12     int starCount = longestLineCharCount(message);
13     if(starCount > MAX_STAR_COUNT) {
14       starCount = MAX_STAR_COUNT;
15     }
16     return "\n" + getStars(starCount) + "\n" + message
17            + "\n" + getStars(starCount) + "\n";
18   }
19
20   private String JavaDoc getStars(int starCount) {
21     StringBuffer JavaDoc stars = new StringBuffer JavaDoc();
22     while(starCount-- > 0) {
23       stars.append('*');
24     }
25     return stars.toString();
26   }
27
28   private int longestLineCharCount(String JavaDoc message) {
29     int count = 0;
30     int sidx = 0, eidx = 0;
31     while ((eidx = message.indexOf('\n', sidx)) >= 0) {
32       if (count < (eidx - sidx)) {
33         count = (eidx - sidx);
34       }
35       sidx = eidx + 1;
36     }
37     if (sidx < message.length() && count < (message.length() - sidx)) {
38       count = (message.length() - sidx);
39     }
40     return count;
41   }
42
43 }
44
Popular Tags