KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > filter > plugins > BodyFilter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 JavaDoc;
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 JavaDoc 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 JavaDoc uid)
43             throws Exception JavaDoc {
44         // get message body
45

46         InputStream JavaDoc messageSourceStream = ((IMailbox)folder).getMessageSourceStream(uid);
47         StringBuffer JavaDoc body = StreamUtils.readCharacterStream(messageSourceStream);
48         String JavaDoc bodyText = pattern;
49
50         boolean result = false;
51
52         switch (condition) {
53         case FilterCriteria.CONTAINS:
54
55             if (body.indexOf(bodyText) != -1) {
56                 result = true;
57             }
58
59             break;
60
61         case FilterCriteria.CONTAINS_NOT:
62
63             if (body.indexOf(bodyText) == -1) {
64                 result = true;
65             }
66
67             break;
68         }
69
70         return result;
71     }
72
73     /**
74      * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
75      */

76     public void setUp(IFilterCriteria f) {
77
78         // string to search
79
pattern = f.getPatternString();
80
81         condition = f.getCriteria();
82     }
83 }
Popular Tags