KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > search > BodyTerm


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)BodyTerm.java 1.10 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.search;
29
30 import javax.mail.*;
31
32 /**
33  * This class implements searches on a Message Body.
34  * All parts of the message that are of MIME type "text/*" are searched.
35  *
36  * @author Bill Shannon
37  * @author John Mani
38  */

39 public final class BodyTerm extends StringTerm JavaDoc {
40
41     private static final long serialVersionUID = -4888862527916911385L;
42
43     /**
44      * Constructor
45      * @param pattern The String to search for
46      */

47     public BodyTerm(String JavaDoc pattern) {
48     // Note: comparison is case-insensitive
49
super(pattern);
50     }
51
52     /**
53      * The match method.
54      *
55      * @param msg The pattern search is applied on this Message's body
56      * @return true if the pattern is found; otherwise false
57      */

58     public boolean match(Message msg) {
59     return matchPart(msg);
60     }
61
62     /**
63      * Search all the parts of the message for any text part
64      * that matches the pattern.
65      */

66     private boolean matchPart(Part p) {
67     try {
68         /*
69          * Using isMimeType to determine the content type avoids
70          * fetching the actual content data until we need it.
71          */

72         if (p.isMimeType("text/*")) {
73         String JavaDoc s = (String JavaDoc)p.getContent();
74         if (s == null)
75             return false;
76         /*
77          * We invoke our superclass' (i.e., StringTerm) match method.
78          * Note however that StringTerm.match() is not optimized
79          * for substring searches in large string buffers. We really
80          * need to have a StringTerm subclass, say BigStringTerm,
81          * with its own match() method that uses a better algorithm ..
82          * and then subclass BodyTerm from BigStringTerm.
83          */

84         return super.match(s);
85         } else if (p.isMimeType("multipart/*")) {
86         Multipart mp = (Multipart)p.getContent();
87         int count = mp.getCount();
88         for (int i = 0; i < count; i++)
89             if (matchPart(mp.getBodyPart(i)))
90             return true;
91         } else if (p.isMimeType("message/rfc822")) {
92         return matchPart((Part)p.getContent());
93         }
94     } catch (Exception JavaDoc ex) {
95     }
96     return false;
97     }
98
99     /**
100      * Equality comparison.
101      */

102     public boolean equals(Object JavaDoc obj) {
103     if (!(obj instanceof BodyTerm JavaDoc))
104         return false;
105     return super.equals(obj);
106     }
107 }
108
Popular Tags