001: /*
002: * DateSentMailboxFilter.java
003: *
004: * Copyright (C) 2002 Peter Graves
005: * $Id: DateSentMailboxFilter.java,v 1.1.1.1 2002/09/24 16:10:04 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.util.Calendar;
025: import java.util.StringTokenizer;
026: import org.armedbear.j.Debug;
027: import org.armedbear.j.FastStringReader;
028: import org.armedbear.j.Log;
029:
030: public final class DateSentMailboxFilter extends MailboxFilter {
031: private RFC822Date begin;
032: private RFC822Date end;
033:
034: private DateSentMailboxFilter(RFC822Date date) {
035: begin = end = date;
036: }
037:
038: private DateSentMailboxFilter(RFC822Date begin, RFC822Date end) {
039: this .begin = begin;
040: this .end = end;
041: }
042:
043: public static MailboxFilter getMailboxFilter(FastStringReader reader) {
044: final String pattern = reader.readToken();
045: if (pattern.length() > 0) {
046: char c = pattern.charAt(0);
047: if (c == '<' || c == '>')
048: return RelativeDateMailboxFilter
049: .getMailboxFilter(pattern);
050: try {
051: int index = pattern.indexOf('-');
052: if (index < 0) {
053: RFC822Date date = parseDate(pattern);
054: return new DateSentMailboxFilter(date);
055: } else {
056: RFC822Date begin = parseDate(pattern.substring(0,
057: index));
058: RFC822Date end = parseDate(pattern
059: .substring(index + 1));
060: return new DateSentMailboxFilter(begin, end);
061: }
062: } catch (InvalidDateException e) {
063: }
064: }
065: return null;
066: }
067:
068: public boolean accept(MailboxEntry entry) {
069: RFC822Date date = entry.getDate();
070: if (begin != null)
071: if (date.before(begin))
072: return false;
073: if (end != null)
074: if (date.after(end))
075: return false;
076: return true;
077: }
078:
079: // Input must be in DD/MM/YY format.
080: private static RFC822Date parseDate(String input)
081: throws InvalidDateException {
082: input = input.trim();
083: if (input.length() == 0)
084: return null; // Not an error (e.g. "~d -2/2/02").
085: StringTokenizer st = new StringTokenizer(input, "/");
086: int count = st.countTokens();
087: if (count < 1 || count > 3)
088: throw new InvalidDateException();
089: Calendar cal = Calendar.getInstance();
090: int day = cal.get(Calendar.DAY_OF_MONTH);
091: int month = cal.get(Calendar.MONTH) + 1;
092: int year = cal.get(Calendar.YEAR);
093: try {
094: day = Integer.parseInt(st.nextToken());
095: if (count > 1) {
096: month = Integer.parseInt(st.nextToken());
097: if (count > 2)
098: year = Integer.parseInt(st.nextToken());
099: }
100: } catch (NumberFormatException e) {
101: Log.error(e);
102: throw new InvalidDateException();
103: }
104: if (year < 0)
105: throw new InvalidDateException();
106: if (year < 100) {
107: year += 1900;
108: if (year < 1971) // There was no email before 1971.
109: year += 100;
110: }
111: if (month < 1 || month > 12)
112: throw new InvalidDateException();
113: if (day < 1)
114: throw new InvalidDateException();
115: switch (month) {
116: case 1:
117: case 3:
118: case 5:
119: case 7:
120: case 8:
121: case 10:
122: case 12:
123: if (day > 31)
124: throw new InvalidDateException();
125: break;
126: case 2:
127: if (day > 29)
128: throw new InvalidDateException();
129: if (day == 29 && (year % 4) != 0)
130: throw new InvalidDateException();
131: break;
132: case 4:
133: case 6:
134: case 9:
135: case 11:
136: if (day > 30)
137: throw new InvalidDateException();
138: break;
139: default:
140: Debug.assertTrue(false);
141: }
142: cal.set(year, month - 1, day, 0, 0);
143: return new RFC822Date(cal.getTime());
144: }
145:
146: private static class InvalidDateException extends Exception {
147: }
148: }
|