- /*
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
-
- package javax.mail.search;
-
- import javax.mail.*;
-
- /**
- * This class implements searches on Message Body.
- * Body searches are done only for:
- * (1) single-part messages whose primary-type is Text
- * OR
- * (2) multipart/mixed messages whose first body-part's
- * primary-type is Text. In this case, the search is
- * done on the first body-part.
- *
- * @author Bill Shannon
- * @author John Mani
- */
- public final class BodyTerm extends StringTerm {
-
- /**
- * Constructor
- * @param pattern The String to search for
- */
- public BodyTerm(String pattern) {
- // Note: comparison is case-insensitive
- super(pattern);
- }
-
- /**
- * The match method.
- *
- * @param msg The pattern search is applied on this Message's body
- * @return true if the pattern is found; otherwise false
- */
- public boolean match(Message msg) {
- Object data = null;
- String type = null;
- Part part = (Part)msg;
-
- try {
- type = part.getContentType();
- } catch (Exception e) {
- return false;
- }
-
- if (type.regionMatches(true, 0, "text/", 0, 5)) {
- // Single-part text
- try {
- data = part.getContent();
- } catch (Exception e) {}
- } else if (type.regionMatches(true, 0, "multipart/mixed",0,15)) {
- // multipart/mixed, get first BodyPart
- try {
- part = ((Multipart)part.getContent()).getBodyPart(0);
- type = part.getContentType();
- if (type.regionMatches(true, 0, "text/", 0, 5))
- data = part.getContent();
- } catch (Exception e) {}
- }
-
- if (data == null || !(data instanceof String))
- return false;
- /*
- * We invoke our superclass' (ie 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((String)data);
- }
-
- /**
- * Equality comparison.
- */
- public boolean equals(Object obj) {
- if (!(obj instanceof BodyTerm))
- return false;
- return super.equals(obj);
- }
- }