KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > mbox > MboxParser


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

17 package org.columba.mail.folder.mbox;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import org.columba.ristretto.io.Source;
27 import org.columba.ristretto.parser.CharSequenceSearcher;
28
29 public class MboxParser {
30     
31     private static final Pattern JavaDoc YEAR_DIGITS = Pattern.compile("\\d{4}$");
32     
33     public static MboxMessage[] parseMbox(Source mailboxSource) throws IOException JavaDoc {
34         Matcher JavaDoc mboxHeaderMatcher = YEAR_DIGITS.matcher("");
35         List JavaDoc messages = new LinkedList JavaDoc();
36
37         CharSequenceSearcher searcher = new CharSequenceSearcher("From ");
38         List JavaDoc boundaries = searcher.match(mailboxSource);
39
40         Iterator JavaDoc it = boundaries.iterator();
41         int start = ((Integer JavaDoc) it.next()).intValue();
42         int lastEnd = findNext(mailboxSource, start + 5, '\n') + 1;
43
44         int newUid = 0;
45
46
47         while (it.hasNext()) {
48             start = ((Integer JavaDoc) it.next()).intValue();
49             
50             int possibleEnd = findNext(mailboxSource, start + 5, '\n') + 1;
51
52             mboxHeaderMatcher.reset(mailboxSource.subSequence(start, possibleEnd-1));
53             while( !mboxHeaderMatcher.find() && it.hasNext()) {
54                 start = ((Integer JavaDoc) it.next()).intValue();
55                 possibleEnd = findNext(mailboxSource, start + 5, '\n') + 1;
56                 mboxHeaderMatcher.reset(mailboxSource.subSequence(start, possibleEnd));
57             }
58             
59             messages.add(new MboxMessage(new Integer JavaDoc(newUid++), lastEnd,
60                     start - lastEnd));
61
62             lastEnd = possibleEnd;
63         }
64
65         messages.add(new MboxMessage(new Integer JavaDoc(newUid++), lastEnd,
66                 mailboxSource.length() - lastEnd));
67         
68         return (MboxMessage[]) messages.toArray(new MboxMessage[0]);
69     }
70
71     private static int findNext(Source source, int pos, char ch) {
72         pos++;
73         while (!source.isEOF()) {
74             if (source.charAt(pos) == ch) {
75                 return pos;
76             } else {
77                 pos++;
78             }
79         }
80
81         return -1;
82     }
83
84 }
85
Popular Tags