KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > message > viewer > MessageParser


1 package org.columba.mail.gui.message.viewer;
2
3 import java.io.InputStream JavaDoc;
4 import java.nio.charset.Charset JavaDoc;
5 import java.nio.charset.IllegalCharsetNameException JavaDoc;
6 import java.nio.charset.UnsupportedCharsetException JavaDoc;
7
8 import org.columba.mail.gui.message.util.DocumentParser;
9 import org.columba.mail.parser.text.HtmlParser;
10 import org.columba.ristretto.coder.Base64DecoderInputStream;
11 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
12 import org.columba.ristretto.message.MimeHeader;
13 import org.columba.ristretto.message.MimePart;
14
15 /**
16  * Provides text processing helper methods for the text viewer component.
17  *
18  * @author fdietz
19  */

20 public class MessageParser {
21
22     private static DocumentParser parser = new DocumentParser();
23     
24     /**
25      * @param bodyText
26      * @throws Exception
27      */

28     public static String JavaDoc transformTextToHTML(String JavaDoc bodyText, String JavaDoc css, boolean enableSmilies) throws Exception JavaDoc {
29         String JavaDoc body = null;
30
31         // substitute special characters like:
32
// <,>,&,\t,\n
33
body = HtmlParser.substituteSpecialCharacters(bodyText);
34
35         // parse for urls and substite with HTML-code
36
body = HtmlParser.substituteURL(body);
37
38         // parse for email addresses and substite with HTML-code
39
body = HtmlParser.substituteEmailAddress(body);
40
41         // parse for quotings and color the darkgray
42
body = DocumentParser.markQuotings(body);
43
44         // add smilies
45
if (enableSmilies == true) {
46             body = DocumentParser.addSmilies(body);
47         }
48
49         // encapsulate bodytext in html-code
50
body = transformToHTML(new StringBuffer JavaDoc(body), css);
51
52         return body;
53     }
54
55     /*
56      *
57      * encapsulate bodytext in HTML code
58      *
59      */

60     private static String JavaDoc transformToHTML(StringBuffer JavaDoc buf, String JavaDoc css) {
61         // prepend
62
buf.insert(0, "<HTML><HEAD>" + css
63                 + "</HEAD><BODY class=\"bodytext\"><P>");
64
65         // append
66
buf.append("</P></BODY></HTML>");
67
68         return buf.toString();
69     }
70     
71     /**
72      * @param bodyPart
73      * @param bodyStream
74      * @return
75      */

76     public static InputStream JavaDoc decodeBodyStream(MimePart bodyPart,
77             InputStream JavaDoc bodyStream) throws Exception JavaDoc {
78
79         // default encoding is plain
80
int encoding = MimeHeader.PLAIN;
81
82         if (bodyPart != null) {
83             encoding = bodyPart.getHeader().getContentTransferEncoding();
84         }
85
86         switch (encoding) {
87         case MimeHeader.QUOTED_PRINTABLE: {
88             bodyStream = new QuotedPrintableDecoderInputStream(bodyStream);
89
90             break;
91         }
92
93         case MimeHeader.BASE64: {
94             bodyStream = new Base64DecoderInputStream(bodyStream);
95
96             break;
97         }
98         }
99
100
101         return bodyStream;
102     }
103
104     /**
105      * @param mediator
106      * @param bodyPart
107      * @return
108      */

109     public static Charset JavaDoc extractCharset(Charset JavaDoc charset, MimePart bodyPart) {
110
111         // no charset specified -> automatic mode
112
// -> try to determine charset based on content parameter
113
if (charset == null) {
114             String JavaDoc charsetName = null;
115
116             if (bodyPart != null) {
117                 charsetName = bodyPart.getHeader().getContentParameter(
118                         "charset");
119             }
120
121             if (charsetName == null) {
122                 // There is no charset info -> the default system charset is
123
// used
124
charsetName = System.getProperty("file.encoding");
125                 charset = Charset.forName(charsetName);
126             } else {
127                 try {
128                     charset = Charset.forName(charsetName);
129                 } catch (UnsupportedCharsetException JavaDoc e) {
130                     charsetName = System.getProperty("file.encoding");
131                     charset = Charset.forName(charsetName);
132                 } catch (IllegalCharsetNameException JavaDoc e) {
133                     charsetName = System.getProperty("file.encoding");
134                     charset = Charset.forName(charsetName);
135                 }
136             }
137
138             // ((CharsetOwnerInterface) mediator).setCharset(charset);
139

140         }
141         return charset;
142     }
143
144     
145 }
146
Popular Tags