1 package org.columba.mail.gui.message.viewer; 2 3 import java.io.InputStream ; 4 import java.nio.charset.Charset ; 5 import java.nio.charset.IllegalCharsetNameException ; 6 import java.nio.charset.UnsupportedCharsetException ; 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 20 public class MessageParser { 21 22 private static DocumentParser parser = new DocumentParser(); 23 24 28 public static String transformTextToHTML(String bodyText, String css, boolean enableSmilies) throws Exception { 29 String body = null; 30 31 body = HtmlParser.substituteSpecialCharacters(bodyText); 34 35 body = HtmlParser.substituteURL(body); 37 38 body = HtmlParser.substituteEmailAddress(body); 40 41 body = DocumentParser.markQuotings(body); 43 44 if (enableSmilies == true) { 46 body = DocumentParser.addSmilies(body); 47 } 48 49 body = transformToHTML(new StringBuffer (body), css); 51 52 return body; 53 } 54 55 60 private static String transformToHTML(StringBuffer buf, String css) { 61 buf.insert(0, "<HTML><HEAD>" + css 63 + "</HEAD><BODY class=\"bodytext\"><P>"); 64 65 buf.append("</P></BODY></HTML>"); 67 68 return buf.toString(); 69 } 70 71 76 public static InputStream decodeBodyStream(MimePart bodyPart, 77 InputStream bodyStream) throws Exception { 78 79 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 109 public static Charset extractCharset(Charset charset, MimePart bodyPart) { 110 111 if (charset == null) { 114 String charsetName = null; 115 116 if (bodyPart != null) { 117 charsetName = bodyPart.getHeader().getContentParameter( 118 "charset"); 119 } 120 121 if (charsetName == null) { 122 charsetName = System.getProperty("file.encoding"); 125 charset = Charset.forName(charsetName); 126 } else { 127 try { 128 charset = Charset.forName(charsetName); 129 } catch (UnsupportedCharsetException e) { 130 charsetName = System.getProperty("file.encoding"); 131 charset = Charset.forName(charsetName); 132 } catch (IllegalCharsetNameException e) { 133 charsetName = System.getProperty("file.encoding"); 134 charset = Charset.forName(charsetName); 135 } 136 } 137 138 140 } 141 return charset; 142 } 143 144 145 } 146 | Popular Tags |