KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MBOXMailImportFilter


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22
23 import org.columba.api.command.IWorkerStatusController;
24 import org.columba.mail.folder.IMailbox;
25 import org.columba.mail.folder.mailboximport.AbstractMailboxImporter;
26
27 public class MBOXMailImportFilter extends AbstractMailboxImporter {
28
29     public MBOXMailImportFilter() {
30         super();
31     }
32
33     public MBOXMailImportFilter(IMailbox destinationFolder, File JavaDoc[] sourceFiles) {
34         super(destinationFolder, sourceFiles);
35     }
36
37     public int getType() {
38         return TYPE_FILE;
39     }
40
41     public void importMailboxFile(File JavaDoc file, IWorkerStatusController worker,
42             IMailbox destFolder) throws Exception JavaDoc {
43
44         boolean success = false;
45
46         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc();
47
48         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
49         String JavaDoc str;
50
51         // parse line by line
52
while ((str = in.readLine()) != null) {
53             // if user cancelled task exit immediately
54
if (worker.cancelled())
55                 return;
56
57             // if line doesn't start with "From" or line length is 0
58
// -> save everything in StringBuffer
59
if (!str.startsWith("From ") || (str.length() == 0)) {
60                 strbuf.append(str + "\n");
61             } else {
62
63                 // -> import message in Columba
64

65                 if (strbuf.length() != 0) {
66                     // found new message
67

68                     saveMessage(strbuf.toString(), worker,
69                             getDestinationFolder());
70
71                     success = true;
72
73                 }
74                 strbuf = new StringBuffer JavaDoc();
75
76             }
77
78         }
79
80         // save last message, because while loop aborted before being able to
81
// save message
82
if (success && (strbuf.length() > 0)) {
83             saveMessage(strbuf.toString(), worker, getDestinationFolder());
84         }
85
86         in.close();
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.columba.mail.folder.mailboximport.DefaultMailboxImporter#getDescription()
93      */

94     public String JavaDoc getDescription() {
95         return "Example MBOX import filter";
96     }
97 }
98
Popular Tags