001: package org.columba.mail.gui.message.viewer;
002:
003: import java.io.InputStream;
004: import java.nio.charset.Charset;
005: import java.nio.charset.IllegalCharsetNameException;
006: import java.nio.charset.UnsupportedCharsetException;
007:
008: import org.columba.mail.gui.message.util.DocumentParser;
009: import org.columba.mail.parser.text.HtmlParser;
010: import org.columba.ristretto.coder.Base64DecoderInputStream;
011: import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
012: import org.columba.ristretto.message.MimeHeader;
013: import org.columba.ristretto.message.MimePart;
014:
015: /**
016: * Provides text processing helper methods for the text viewer component.
017: *
018: * @author fdietz
019: */
020: public class MessageParser {
021:
022: private static DocumentParser parser = new DocumentParser();
023:
024: /**
025: * @param bodyText
026: * @throws Exception
027: */
028: public static String transformTextToHTML(String bodyText,
029: String css, boolean enableSmilies) throws Exception {
030: String body = null;
031:
032: // substitute special characters like:
033: // <,>,&,\t,\n
034: body = HtmlParser.substituteSpecialCharacters(bodyText);
035:
036: // parse for urls and substite with HTML-code
037: body = HtmlParser.substituteURL(body);
038:
039: // parse for email addresses and substite with HTML-code
040: body = HtmlParser.substituteEmailAddress(body);
041:
042: // parse for quotings and color the darkgray
043: body = DocumentParser.markQuotings(body);
044:
045: // add smilies
046: if (enableSmilies == true) {
047: body = DocumentParser.addSmilies(body);
048: }
049:
050: // encapsulate bodytext in html-code
051: body = transformToHTML(new StringBuffer(body), css);
052:
053: return body;
054: }
055:
056: /*
057: *
058: * encapsulate bodytext in HTML code
059: *
060: */
061: private static String transformToHTML(StringBuffer buf, String css) {
062: // prepend
063: buf.insert(0, "<HTML><HEAD>" + css
064: + "</HEAD><BODY class=\"bodytext\"><P>");
065:
066: // append
067: buf.append("</P></BODY></HTML>");
068:
069: return buf.toString();
070: }
071:
072: /**
073: * @param bodyPart
074: * @param bodyStream
075: * @return
076: */
077: public static InputStream decodeBodyStream(MimePart bodyPart,
078: InputStream bodyStream) throws Exception {
079:
080: // default encoding is plain
081: int encoding = MimeHeader.PLAIN;
082:
083: if (bodyPart != null) {
084: encoding = bodyPart.getHeader()
085: .getContentTransferEncoding();
086: }
087:
088: switch (encoding) {
089: case MimeHeader.QUOTED_PRINTABLE: {
090: bodyStream = new QuotedPrintableDecoderInputStream(
091: bodyStream);
092:
093: break;
094: }
095:
096: case MimeHeader.BASE64: {
097: bodyStream = new Base64DecoderInputStream(bodyStream);
098:
099: break;
100: }
101: }
102:
103: return bodyStream;
104: }
105:
106: /**
107: * @param mediator
108: * @param bodyPart
109: * @return
110: */
111: public static Charset extractCharset(Charset charset,
112: MimePart bodyPart) {
113:
114: // no charset specified -> automatic mode
115: // -> try to determine charset based on content parameter
116: if (charset == null) {
117: String charsetName = null;
118:
119: if (bodyPart != null) {
120: charsetName = bodyPart.getHeader().getContentParameter(
121: "charset");
122: }
123:
124: if (charsetName == null) {
125: // There is no charset info -> the default system charset is
126: // used
127: charsetName = System.getProperty("file.encoding");
128: charset = Charset.forName(charsetName);
129: } else {
130: try {
131: charset = Charset.forName(charsetName);
132: } catch (UnsupportedCharsetException e) {
133: charsetName = System.getProperty("file.encoding");
134: charset = Charset.forName(charsetName);
135: } catch (IllegalCharsetNameException e) {
136: charsetName = System.getProperty("file.encoding");
137: charset = Charset.forName(charsetName);
138: }
139: }
140:
141: // ((CharsetOwnerInterface) mediator).setCharset(charset);
142:
143: }
144: return charset;
145: }
146:
147: }
|