01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.mail.filter.plugins;
17:
18: import java.io.InputStream;
19:
20: import org.columba.core.filter.AbstractFilter;
21: import org.columba.core.filter.FilterCriteria;
22: import org.columba.core.filter.IFilterCriteria;
23: import org.columba.core.folder.api.IFolder;
24: import org.columba.core.io.StreamUtils;
25: import org.columba.mail.folder.IMailbox;
26:
27: /**
28: * Search for a certain string in the message body.
29: *
30: * @author fdietz
31: */
32: public class BodyFilter extends AbstractFilter {
33: private String pattern;
34:
35: private int condition;
36:
37: /**
38: * @see org.columba.core.filter.AbstractFilter#process(java.lang.Object,
39: * org.columba.mail.folder.Folder, java.lang.Object,
40: * org.columba.api.command.IWorkerStatusController)
41: */
42: public boolean process(IFolder folder, Object uid) throws Exception {
43: // get message body
44:
45: InputStream messageSourceStream = ((IMailbox) folder)
46: .getMessageSourceStream(uid);
47: StringBuffer body = StreamUtils
48: .readCharacterStream(messageSourceStream);
49: String bodyText = pattern;
50:
51: boolean result = false;
52:
53: switch (condition) {
54: case FilterCriteria.CONTAINS:
55:
56: if (body.indexOf(bodyText) != -1) {
57: result = true;
58: }
59:
60: break;
61:
62: case FilterCriteria.CONTAINS_NOT:
63:
64: if (body.indexOf(bodyText) == -1) {
65: result = true;
66: }
67:
68: break;
69: }
70:
71: return result;
72: }
73:
74: /**
75: * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
76: */
77: public void setUp(IFilterCriteria f) {
78:
79: // string to search
80: pattern = f.getPatternString();
81:
82: condition = f.getCriteria();
83: }
84: }
|