KickJava   Java API By Example, From Geeks To Geeks.

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


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.filter.plugins;
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.mail.filter.MailFilterCriteria;
25 import org.columba.mail.folder.IMailbox;
26 import org.columba.ristretto.coder.EncodedWord;
27 import org.columba.ristretto.message.Header;
28
29 /**
30  * Search for a string in a certain headerfield.
31  * <p>
32  * "headerfield" is for example Subject <br>
33  * "criteria" can be "contains" or "contains not" <br>
34  * "pattern" specifies the search string <br>
35  *
36  * @author fdietz
37  */

38 public class HeaderfieldFilter extends AbstractFilter {
39     
40
41     // get headerfield to search in (for example: Subject)
42
private String JavaDoc headerfield;
43
44     // string to search
45
private String JavaDoc pattern;
46
47     private int condition;
48
49     /**
50      * Constructor for HeaderfieldFilter.
51      *
52      * @param filter
53      */

54     public HeaderfieldFilter() {
55         super();
56     }
57
58     /**
59      *
60      * Check if the requested headerfield contains the search string and return
61      * true if match was found, otherwise return false
62      *
63      * @see org.columba.core.filter.AbstractFilter#process(org.columba.mail.folder.Folder,
64      * java.lang.Object, org.columba.mail.filter.Filter)
65      */

66     public boolean process(IFolder folder, Object JavaDoc uid)
67             throws Exception JavaDoc {
68         // get message header
69
Header header = ((IMailbox)folder).getHeaderFields(uid,
70                 new String JavaDoc[] { headerfield });
71
72         if (header == null) {
73             return false;
74         }
75
76         String JavaDoc headerItem = (String JavaDoc) header.get(headerfield);
77         // cancel if headerfield doesn't exist
78
if (headerItem == null)
79             return false;
80
81         // decode headerfield
82
headerItem = EncodedWord.decode(headerItem).toString();
83
84         // see if theirs a match
85
boolean result = match(headerItem, condition, pattern);
86
87         return result;
88     }
89
90     /**
91      *
92      * check if a match exists in the requested headerfield
93      *
94      * @param headerItem
95      * String to specify headerfield (example:Subject)
96      * @param condition
97      * contains, contains not
98      * @param pattern
99      * search string
100      *
101      * @return boolean return true if match was found, otherwise return false
102      */

103     protected boolean match(String JavaDoc headerItem, int condition, String JavaDoc pattern) {
104         boolean result = false;
105
106         // skip if message doesn't contain the requested headerfield
107
if (headerItem == null) {
108             return false;
109         }
110
111         switch (condition) {
112         case FilterCriteria.CONTAINS:
113
114             if (headerItem.toLowerCase().indexOf(pattern.toLowerCase()) != -1) {
115                 result = true;
116             }
117
118             break;
119
120         case FilterCriteria.CONTAINS_NOT:
121
122             if (headerItem.toLowerCase().indexOf(pattern.toLowerCase()) == -1) {
123                 result = true;
124             }
125
126             break;
127
128         case FilterCriteria.IS:
129
130             if (headerItem.equalsIgnoreCase(pattern)) {
131                 result = true;
132             }
133
134             break;
135
136         case FilterCriteria.IS_NOT:
137
138             if (!headerItem.equalsIgnoreCase(pattern)) {
139                 result = true;
140             }
141
142             break;
143
144         case FilterCriteria.BEGINS_WITH:
145
146             if (headerItem.toLowerCase().startsWith(pattern.toLowerCase())) {
147                 result = true;
148             }
149
150             break;
151
152         case FilterCriteria.ENDS_WITH:
153
154             if (headerItem.toLowerCase().endsWith(pattern.toLowerCase())) {
155                 result = true;
156             }
157
158             break;
159         }
160
161         return result;
162     }
163
164     /**
165      * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
166      */

167     public void setUp(IFilterCriteria f) {
168         
169
170         // get headerfield to search in (for example: Subject)
171
headerfield = new MailFilterCriteria(f).getHeaderfieldString();
172
173         // string to search
174
pattern = f.getPatternString();
175
176         // get condition and convert it to constant as defined in
177
// FilterCriteria
178
condition = f.getCriteria();
179     }
180 }
Popular Tags