1 21 22 27 28 package com.sun.mail.imap.protocol; 29 30 import java.util.*; 31 import java.io.IOException ; 32 33 import javax.mail.*; 34 import javax.mail.search.*; 35 import com.sun.mail.iap.*; 36 37 43 class SearchSequence { 44 45 48 static Argument generateSequence(SearchTerm term, String charset) 49 throws SearchException, IOException { 50 53 if (term instanceof AndTerm) return and((AndTerm)term, charset); 55 else if (term instanceof OrTerm) return or((OrTerm)term, charset); 57 else if (term instanceof NotTerm) return not((NotTerm)term, charset); 59 else if (term instanceof HeaderTerm) return header((HeaderTerm)term, charset); 61 else if (term instanceof FlagTerm) return flag((FlagTerm)term); 63 else if (term instanceof FromTerm) { FromTerm fterm = (FromTerm)term; 65 return from(fterm.getAddress().toString(), charset); 66 } 67 else if (term instanceof FromStringTerm) { FromStringTerm fterm = (FromStringTerm)term; 69 return from(fterm.getPattern(), charset); 70 } 71 else if (term instanceof RecipientTerm) { RecipientTerm rterm = (RecipientTerm)term; 73 return recipient(rterm.getRecipientType(), 74 rterm.getAddress().toString(), 75 charset); 76 } 77 else if (term instanceof RecipientStringTerm) { RecipientStringTerm rterm = (RecipientStringTerm)term; 79 return recipient(rterm.getRecipientType(), 80 rterm.getPattern(), 81 charset); 82 } 83 else if (term instanceof SubjectTerm) return subject((SubjectTerm)term, charset); 85 else if (term instanceof BodyTerm) return body((BodyTerm)term, charset); 87 else if (term instanceof SizeTerm) return size((SizeTerm)term); 89 else if (term instanceof SentDateTerm) return sentdate((SentDateTerm)term); 91 else if (term instanceof ReceivedDateTerm) return receiveddate((ReceivedDateTerm)term); 93 else if (term instanceof MessageIDTerm) return messageid((MessageIDTerm)term, charset); 95 else 96 throw new SearchException("Search too complex"); 97 } 98 99 103 static boolean isAscii(SearchTerm term) { 104 if (term instanceof AndTerm || term instanceof OrTerm) { 105 SearchTerm[] terms; 106 if (term instanceof AndTerm) 107 terms = ((AndTerm)term).getTerms(); 108 else 109 terms = ((OrTerm)term).getTerms(); 110 111 for (int i = 0; i < terms.length; i++) 112 if (!isAscii(terms[i])) return false; 114 } else if (term instanceof NotTerm) 115 return isAscii(((NotTerm)term).getTerm()); 116 else if (term instanceof StringTerm) 117 return isAscii(((StringTerm)term).getPattern()); 118 else if (term instanceof AddressTerm) 119 return isAscii(((AddressTerm)term).getAddress().toString()); 120 121 return true; 123 } 124 125 private static boolean isAscii(String s) { 126 int l = s.length(); 127 128 for (int i=0; i < l; i++) { 129 if ((int)s.charAt(i) > 0177) return false; 131 } 132 return true; 133 } 134 135 private static Argument and(AndTerm term, String charset) 136 throws SearchException, IOException { 137 SearchTerm[] terms = term.getTerms(); 139 Argument result = generateSequence(terms[0], charset); 141 for (int i = 1; i < terms.length; i++) 143 result.append(generateSequence(terms[i], charset)); 144 return result; 145 } 146 147 private static Argument or(OrTerm term, String charset) 148 throws SearchException, IOException { 149 SearchTerm[] terms = term.getTerms(); 150 151 155 if (terms.length > 2) { 156 SearchTerm t = terms[0]; 157 158 for (int i = 1; i < terms.length; i++) 160 t = new OrTerm(t, terms[i]); 161 162 term = (OrTerm)t; terms = term.getTerms(); 165 } 166 167 Argument result = new Argument(); 169 170 result.writeAtom("OR"); 172 173 178 if (terms[0] instanceof AndTerm || terms[0] instanceof FlagTerm) 179 result.writeArgument(generateSequence(terms[0], charset)); 180 else 181 result.append(generateSequence(terms[0], charset)); 182 183 if (terms[1] instanceof AndTerm || terms[1] instanceof FlagTerm) 185 result.writeArgument(generateSequence(terms[1], charset)); 186 else 187 result.append(generateSequence(terms[1], charset)); 188 189 return result; 190 } 191 192 private static Argument not(NotTerm term, String charset) 193 throws SearchException, IOException { 194 Argument result = new Argument(); 195 196 result.writeAtom("NOT"); 198 199 204 SearchTerm nterm = term.getTerm(); 205 if (nterm instanceof AndTerm || nterm instanceof FlagTerm) 206 result.writeArgument(generateSequence(nterm, charset)); 207 else 208 result.append(generateSequence(nterm, charset)); 209 210 return result; 211 } 212 213 private static Argument header(HeaderTerm term, String charset) 214 throws SearchException, IOException { 215 Argument result = new Argument(); 216 result.writeAtom("HEADER"); 217 result.writeString(term.getHeaderName()); 218 result.writeString(term.getPattern(), charset); 219 return result; 220 } 221 222 private static Argument messageid(MessageIDTerm term, String charset) 223 throws SearchException, IOException { 224 Argument result = new Argument(); 225 result.writeAtom("HEADER"); 226 result.writeString("Message-ID"); 227 result.writeString(term.getPattern(), charset); 229 return result; 230 } 231 232 private static Argument flag(FlagTerm term) throws SearchException { 233 boolean set = term.getTestSet(); 234 235 Argument result = new Argument(); 236 237 Flags flags = term.getFlags(); 238 Flags.Flag[] sf = flags.getSystemFlags(); 239 String [] uf = flags.getUserFlags(); 240 if (sf.length == 0 && uf.length == 0) 241 throw new SearchException("Invalid FlagTerm"); 242 243 for (int i = 0; i < sf.length; i++) { 244 if (sf[i] == Flags.Flag.DELETED) 245 result.writeAtom(set ? "DELETED": "UNDELETED"); 246 else if (sf[i] == Flags.Flag.ANSWERED) 247 result.writeAtom(set ? "ANSWERED": "UNANSWERED"); 248 else if (sf[i] == Flags.Flag.DRAFT) 249 result.writeAtom(set ? "DRAFT": "UNDRAFT"); 250 else if (sf[i] == Flags.Flag.FLAGGED) 251 result.writeAtom(set ? "FLAGGED": "UNFLAGGED"); 252 else if (sf[i] == Flags.Flag.RECENT) 253 result.writeAtom(set ? "RECENT": "OLD"); 254 else if (sf[i] == Flags.Flag.SEEN) 255 result.writeAtom(set ? "SEEN": "UNSEEN"); 256 } 257 258 for (int i = 0; i < uf.length; i++) { 259 result.writeAtom(set ? "KEYWORD" : "UNKEYWORD"); 260 result.writeAtom(uf[i]); 261 } 262 263 return result; 264 } 265 266 private static Argument from(String address, String charset) 267 throws SearchException, IOException { 268 Argument result = new Argument(); 269 result.writeAtom("FROM"); 270 result.writeString(address, charset); 271 return result; 272 } 273 274 private static Argument recipient(Message.RecipientType type, 275 String address, String charset) 276 throws SearchException, IOException { 277 Argument result = new Argument(); 278 279 if (type == Message.RecipientType.TO) 280 result.writeAtom("TO"); 281 else if (type == Message.RecipientType.CC) 282 result.writeAtom("CC"); 283 else if (type == Message.RecipientType.BCC) 284 result.writeAtom("BCC"); 285 else 286 throw new SearchException("Illegal Recipient type"); 287 288 result.writeString(address, charset); 289 return result; 290 } 291 292 private static Argument subject(SubjectTerm term, String charset) 293 throws SearchException, IOException { 294 Argument result = new Argument(); 295 296 result.writeAtom("SUBJECT"); 297 result.writeString(term.getPattern(), charset); 298 return result; 299 } 300 301 private static Argument body(BodyTerm term, String charset) 302 throws SearchException, IOException { 303 Argument result = new Argument(); 304 305 result.writeAtom("BODY"); 306 result.writeString(term.getPattern(), charset); 307 return result; 308 } 309 310 private static Argument size(SizeTerm term) 311 throws SearchException { 312 Argument result = new Argument(); 313 314 switch (term.getComparison()) { 315 case ComparisonTerm.GT: 316 result.writeAtom("LARGER"); 317 break; 318 case ComparisonTerm.LT: 319 result.writeAtom("SMALLER"); 320 break; 321 default: 322 throw new SearchException("Cannot handle Comparison"); 324 } 325 326 result.writeNumber(term.getNumber()); 327 return result; 328 } 329 330 332 341 private static String monthTable[] = { 342 "Jan", "Feb", "Mar", "Apr", "May", "Jun", 343 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" 344 }; 345 346 private static Calendar cal = new GregorianCalendar(); 348 349 private static String toIMAPDate(Date date) { 350 StringBuffer s = new StringBuffer (); 351 cal.setTime(date); 352 353 s.append(cal.get(Calendar.DATE)).append("-"); 354 s.append(monthTable[cal.get(Calendar.MONTH)]).append('-'); 355 s.append(cal.get(Calendar.YEAR)); 356 357 return s.toString(); 358 } 359 360 private static Argument sentdate(DateTerm term) 361 throws SearchException { 362 Argument result = new Argument(); 363 String date = toIMAPDate(term.getDate()); 364 365 switch (term.getComparison()) { 366 case ComparisonTerm.GT: 367 result.writeAtom("SENTSINCE " + date); 368 break; 369 case ComparisonTerm.EQ: 370 result.writeAtom("SENTON " + date); 371 break; 372 case ComparisonTerm.LT: 373 result.writeAtom("SENTBEFORE " + date); 374 break; 375 case ComparisonTerm.GE: 376 result.writeAtom("OR SENTSINCE " + date + " SENTON " + date); 377 break; 378 case ComparisonTerm.LE: 379 result.writeAtom("OR SENTBEFORE " + date + " SENTON " + date); 380 break; 381 case ComparisonTerm.NE: 382 result.writeAtom("NOT SENTON " + date); 383 break; 384 default: 385 throw new SearchException("Cannot handle Date Comparison"); 386 } 387 388 return result; 389 } 390 391 private static Argument receiveddate(DateTerm term) 392 throws SearchException { 393 Argument result = new Argument(); 394 String date = toIMAPDate(term.getDate()); 395 396 switch (term.getComparison()) { 397 case ComparisonTerm.GT: 398 result.writeAtom("SINCE " + date); 399 break; 400 case ComparisonTerm.EQ: 401 result.writeAtom("ON " + date); 402 break; 403 case ComparisonTerm.LT: 404 result.writeAtom("BEFORE " + date); 405 break; 406 case ComparisonTerm.GE: 407 result.writeAtom("OR SINCE " + date + " ON " + date); 408 break; 409 case ComparisonTerm.LE: 410 result.writeAtom("OR BEFORE " + date + " ON " + date); 411 break; 412 case ComparisonTerm.NE: 413 result.writeAtom("NOT ON " + date); 414 break; 415 default: 416 throw new SearchException("Cannot handle Date Comparison"); 417 } 418 419 return result; 420 } 421 } 422 | Popular Tags |