1 21 22 package org.armedbear.j.mail; 23 24 import java.util.Calendar ; 25 import java.util.StringTokenizer ; 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 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 private static RFC822Date parseDate(String input) throws InvalidDateException 83 { 84 input = input.trim(); 85 if (input.length() == 0) 86 return null; StringTokenizer st = new StringTokenizer (input, "/"); 88 int count = st.countTokens(); 89 if (count < 1 || count > 3) 90 throw new InvalidDateException(); 91 Calendar 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 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) 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 150 { 151 } 152 } 153 | Popular Tags |