KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > DateSentMailboxFilter


1 /*
2  * DateSentMailboxFilter.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: DateSentMailboxFilter.java,v 1.1.1.1 2002/09/24 16:10:04 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.util.Calendar JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import org.armedbear.j.Debug;
27 import org.armedbear.j.FastStringReader;
28 import org.armedbear.j.Log;
29
30 public final class DateSentMailboxFilter extends MailboxFilter
31 {
32     private RFC822Date begin;
33     private RFC822Date end;
34
35     private DateSentMailboxFilter(RFC822Date date)
36     {
37         begin = end = date;
38     }
39
40     private DateSentMailboxFilter(RFC822Date begin, RFC822Date end)
41     {
42         this.begin = begin;
43         this.end = end;
44     }
45
46     public static MailboxFilter getMailboxFilter(FastStringReader reader)
47     {
48         final String JavaDoc pattern = reader.readToken();
49         if (pattern.length() > 0) {
50             char c = pattern.charAt(0);
51             if (c == '<' || c == '>')
52                 return RelativeDateMailboxFilter.getMailboxFilter(pattern);
53             try {
54                 int index = pattern.indexOf('-');
55                 if (index < 0) {
56                     RFC822Date date = parseDate(pattern);
57                     return new DateSentMailboxFilter(date);
58                 } else {
59                     RFC822Date begin = parseDate(pattern.substring(0, index));
60                     RFC822Date end = parseDate(pattern.substring(index + 1));
61                     return new DateSentMailboxFilter(begin, end);
62                 }
63             }
64             catch (InvalidDateException e) {}
65         }
66         return null;
67     }
68
69     public boolean accept(MailboxEntry entry)
70     {
71         RFC822Date date = entry.getDate();
72         if (begin != null)
73             if (date.before(begin))
74                 return false;
75         if (end != null)
76             if (date.after(end))
77                 return false;
78         return true;
79     }
80
81     // Input must be in DD/MM/YY format.
82
private static RFC822Date parseDate(String JavaDoc input) throws InvalidDateException
83     {
84         input = input.trim();
85         if (input.length() == 0)
86             return null; // Not an error (e.g. "~d -2/2/02").
87
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(input, "/");
88         int count = st.countTokens();
89         if (count < 1 || count > 3)
90             throw new InvalidDateException();
91         Calendar JavaDoc cal = Calendar.getInstance();
92         int day = cal.get(Calendar.DAY_OF_MONTH);
93         int month = cal.get(Calendar.MONTH) + 1;
94         int year = cal.get(Calendar.YEAR);
95         try {
96             day = Integer.parseInt(st.nextToken());
97             if (count > 1) {
98                 month = Integer.parseInt(st.nextToken());
99                 if (count > 2)
100                     year = Integer.parseInt(st.nextToken());
101             }
102         }
103         catch (NumberFormatException JavaDoc e) {
104             Log.error(e);
105             throw new InvalidDateException();
106         }
107         if (year < 0)
108             throw new InvalidDateException();
109         if (year < 100) {
110             year += 1900;
111             if (year < 1971) // There was no email before 1971.
112
year += 100;
113         }
114         if (month < 1 || month > 12)
115             throw new InvalidDateException();
116         if (day < 1)
117             throw new InvalidDateException();
118         switch (month) {
119             case 1:
120             case 3:
121             case 5:
122             case 7:
123             case 8:
124             case 10:
125             case 12:
126                 if (day > 31)
127                     throw new InvalidDateException();
128                 break;
129             case 2:
130                 if (day > 29)
131                     throw new InvalidDateException();
132                 if (day == 29 && (year % 4) != 0)
133                     throw new InvalidDateException();
134                 break;
135             case 4:
136             case 6:
137             case 9:
138             case 11:
139                 if (day > 30)
140                     throw new InvalidDateException();
141                 break;
142             default:
143                 Debug.assertTrue(false);
144         }
145         cal.set(year, month - 1, day, 0, 0);
146         return new RFC822Date(cal.getTime());
147     }
148
149     private static class InvalidDateException extends Exception JavaDoc
150     {
151     }
152 }
153
Popular Tags