KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scioworks > imap > presentation > imapWeb > TextUtil


1 package scioworks.imap.presentation.imapWeb;
2
3 import java.text.SimpleDateFormat JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.io.BufferedReader JavaDoc;
6 import java.io.StringReader JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.Vector JavaDoc;
9
10 import com.lutris.util.Config;
11 import com.lutris.util.ConfigException;
12 import com.lutris.appserver.server.Enhydra;
13
14 public class TextUtil {
15
16   private static TextUtil fSingleton;
17
18   // Constants
19
String JavaDoc INDENT = " ";
20
21   // Format date and time
22
SimpleDateFormat JavaDoc df;
23   SimpleDateFormat JavaDoc tf;
24   SimpleDateFormat JavaDoc dtf;
25
26   private TextUtil() {
27
28     Config config = Enhydra.getApplication().getConfig();
29
30     try {
31       String JavaDoc dateFormat = config.getString("ImapWeb.Util.DateFormat");
32       df = new SimpleDateFormat JavaDoc(dateFormat);
33
34     } catch (ConfigException e) {
35       e.printStackTrace();
36     }
37   }
38
39   public static synchronized TextUtil singleton() {
40     if (fSingleton == null) {
41       fSingleton = new TextUtil();
42     }
43     return fSingleton;
44   }
45
46   public String JavaDoc dateFormat(Date JavaDoc date) {
47     return df.format(date);
48   }
49
50   public String JavaDoc textToHtml(String JavaDoc str) {
51     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
52     try {
53       BufferedReader JavaDoc in = new BufferedReader JavaDoc(new StringReader JavaDoc(str));
54       String JavaDoc line;
55       while ((line = in.readLine()) != null) {
56         buf.append(line).append("<br>");
57       }
58       in.close();
59     } catch (IOException JavaDoc e) {
60       buf.append("<p><b>Error: </b>"+e.getMessage());
61     }
62     return buf.toString();
63   }
64
65   public String JavaDoc verboseFilesize(int size) {
66     if (size > 1000000) {
67       return Long.toString(Math.round(size/1000000.0))+"mb";
68     } else if (size > 1000) {
69       return Long.toString(Math.round(size/1000.0))+"k";
70     } else {
71       return size + "b";
72     }
73   }
74
75   public String JavaDoc initCap(String JavaDoc str) {
76     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
77     buf.append(Character.toUpperCase(str.charAt(0)));
78     buf.append(str.substring(1));
79     return buf.toString();
80   }
81 }
82
Popular Tags