FileDocCategorySizeDatePackage
BodyTerm.javaAPI DocGlassfish v2 API4111Mon May 14 15:28:50 BST 2007javax.mail.search

BodyTerm

public final class BodyTerm extends StringTerm
This class implements searches on a Message Body. All parts of the message that are of MIME type "text/*" are searched.
author
Bill Shannon
author
John Mani

Fields Summary
private static final long
serialVersionUID
Constructors Summary
public BodyTerm(String pattern)
Constructor

param
pattern The String to search for


       	         
       
	// Note: comparison is case-insensitive
	super(pattern);
    
Methods Summary
public booleanequals(java.lang.Object obj)
Equality comparison.

	if (!(obj instanceof BodyTerm))
	    return false;
	return super.equals(obj);
    
public booleanmatch(Message msg)
The match method.

param
msg The pattern search is applied on this Message's body
return
true if the pattern is found; otherwise false

	return matchPart(msg);
    
private booleanmatchPart(javax.mail.Part p)
Search all the parts of the message for any text part that matches the pattern.

	try {
	    /*
	     * Using isMimeType to determine the content type avoids
	     * fetching the actual content data until we need it.
	     */
	    if (p.isMimeType("text/*")) {
		String s = (String)p.getContent();
		if (s == null)
		    return false;
		/*
		 * We invoke our superclass' (i.e., StringTerm) match method.
		 * Note however that StringTerm.match() is not optimized 
		 * for substring searches in large string buffers. We really
		 * need to have a StringTerm subclass, say BigStringTerm, 
		 * with its own match() method that uses a better algorithm ..
		 * and then subclass BodyTerm from BigStringTerm.
		 */ 
		return super.match(s);
	    } else if (p.isMimeType("multipart/*")) {
		Multipart mp = (Multipart)p.getContent();
		int count = mp.getCount();
		for (int i = 0; i < count; i++)
		    if (matchPart(mp.getBodyPart(i)))
			return true;
	    } else if (p.isMimeType("message/rfc822")) {
		return matchPart((Part)p.getContent());
	    }
	} catch (Exception ex) {
	}
	return false;