KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MessageDecomposer


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package MessageDecomposer;
37
38 import java.io.File JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.nio.charset.Charset JavaDoc;
43 import java.text.DateFormat JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46
47 import org.columba.ristretto.coder.Base64DecoderInputStream;
48 import org.columba.ristretto.coder.CharsetDecoderInputStream;
49 import org.columba.ristretto.coder.QuotedPrintableDecoderInputStream;
50 import org.columba.ristretto.io.FileSource;
51 import org.columba.ristretto.io.StreamUtils;
52 import org.columba.ristretto.message.BasicHeader;
53 import org.columba.ristretto.message.Header;
54 import org.columba.ristretto.message.LocalMimePart;
55 import org.columba.ristretto.message.Message;
56 import org.columba.ristretto.message.MimeHeader;
57 import org.columba.ristretto.message.MimeTree;
58 import org.columba.ristretto.message.MimeType;
59 import org.columba.ristretto.parser.MessageParser;
60 import org.columba.ristretto.parser.ParserException;
61
62 public class MessageDecomposer {
63
64     private static final String JavaDoc helpMessage =
65         "Usage : MessageDecomposer mail-file <outdir> (optional)\n\n"
66             + "Example: POPStats mail.txt result_dir\n\n";
67
68     /**
69      * @param args
70      */

71     public static void main(String JavaDoc[] args) {
72         if( args.length < 1) {
73             System.out.println(helpMessage);
74             return;
75         }
76         
77         File JavaDoc messageFile = new File JavaDoc(args[0]);
78         File JavaDoc outDir;
79         
80         if( args.length > 1) {
81             outDir = new File JavaDoc(args[1]);
82         } else {
83             outDir = new File JavaDoc(".");
84         }
85         
86         
87         if( !messageFile.exists() ) {
88             System.out.println("File not found " + messageFile.toString());
89             return;
90         }
91         
92         if( !outDir.exists() || !outDir.isDirectory()) {
93             if( !outDir.mkdir() ) {
94                 System.out.println("Could not create directory" + outDir.toString());
95                 return;
96             }
97         }
98         
99         try {
100             Message message = MessageParser.parse(new FileSource(messageFile));
101             
102             writeStream(message.getHeader().getInputStream(), new File JavaDoc(outDir, "header.txt"));
103             printHeader(message.getHeader());
104             
105             MimeTree mimeTree = message.getMimePartTree();
106             List JavaDoc mimeParts = mimeTree.getAllLeafs();
107             
108             Iterator JavaDoc it = mimeParts.iterator();
109
110             int counter = 1;
111             while(it.hasNext()) {
112                 LocalMimePart part = (LocalMimePart)it.next();
113                 MimeHeader h = part.getHeader();
114                 String JavaDoc filename = h.getFileName();
115                 if( filename == null) {
116                     MimeType type = h.getMimeType();
117                     if( type.getSubtype().equals("html")) {
118                         filename = "part"+ counter + ".html";
119                     } else if (type.getSubtype().equals("plain")) {
120                         filename = "part"+ counter + ".txt";
121                     } else {
122                         filename = "part"+counter;
123                     }
124                 }
125                 
126                 InputStream JavaDoc stream = part.getInputStream();
127                 if( h.getContentTransferEncoding() == MimeHeader.BASE64) {
128                     stream = new Base64DecoderInputStream(stream);
129                 } else if( h.getContentTransferEncoding() == MimeHeader.QUOTED_PRINTABLE) {
130                     stream = new QuotedPrintableDecoderInputStream(stream);
131                 }
132                 
133                 if( h.getContentParameter("charset") != null) {
134                     stream = new CharsetDecoderInputStream(stream, Charset.forName(h.getContentParameter("charset")));
135                 }
136                 
137                 writeStream(stream, new File JavaDoc(outDir, filename));
138                 
139                 counter++;
140             }
141             
142         } catch (ParserException e) {
143             System.out.println("Parser Exception: " + e.getLocalizedMessage());
144         } catch (IOException JavaDoc e) {
145             System.out.println("IO Exception: " + e.getLocalizedMessage());
146         }
147         
148         System.exit(0);
149     }
150
151     private static void writeStream(InputStream JavaDoc in, File JavaDoc file) throws IOException JavaDoc {
152         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(file);
153         StreamUtils.streamCopy(in, out);
154         in.close();
155         out.close();
156     }
157     
158     private static void printHeader(Header rawHeader) {
159         BasicHeader header = new BasicHeader(rawHeader);
160         
161         System.out.println(header.getFrom().toString());
162         System.out.println(DateFormat.getInstance().format(header.getDate()));
163         System.out.println(header.getSubject());
164     }
165     
166
167 }
168
Popular Tags