001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.filter.plugins;
019:
020: import org.columba.core.filter.AbstractFilter;
021: import org.columba.core.filter.FilterCriteria;
022: import org.columba.core.filter.IFilterCriteria;
023: import org.columba.core.folder.api.IFolder;
024: import org.columba.mail.filter.MailFilterCriteria;
025: import org.columba.mail.folder.IMailbox;
026: import org.columba.ristretto.coder.EncodedWord;
027: import org.columba.ristretto.message.Header;
028:
029: /**
030: * Search for a string in a certain headerfield.
031: * <p>
032: * "headerfield" is for example Subject <br>
033: * "criteria" can be "contains" or "contains not" <br>
034: * "pattern" specifies the search string <br>
035: *
036: * @author fdietz
037: */
038: public class HeaderfieldFilter extends AbstractFilter {
039:
040: // get headerfield to search in (for example: Subject)
041: private String headerfield;
042:
043: // string to search
044: private String pattern;
045:
046: private int condition;
047:
048: /**
049: * Constructor for HeaderfieldFilter.
050: *
051: * @param filter
052: */
053: public HeaderfieldFilter() {
054: super ();
055: }
056:
057: /**
058: *
059: * Check if the requested headerfield contains the search string and return
060: * true if match was found, otherwise return false
061: *
062: * @see org.columba.core.filter.AbstractFilter#process(org.columba.mail.folder.Folder,
063: * java.lang.Object, org.columba.mail.filter.Filter)
064: */
065: public boolean process(IFolder folder, Object uid) throws Exception {
066: // get message header
067: Header header = ((IMailbox) folder).getHeaderFields(uid,
068: new String[] { headerfield });
069:
070: if (header == null) {
071: return false;
072: }
073:
074: String headerItem = (String) header.get(headerfield);
075: // cancel if headerfield doesn't exist
076: if (headerItem == null)
077: return false;
078:
079: // decode headerfield
080: headerItem = EncodedWord.decode(headerItem).toString();
081:
082: // see if theirs a match
083: boolean result = match(headerItem, condition, pattern);
084:
085: return result;
086: }
087:
088: /**
089: *
090: * check if a match exists in the requested headerfield
091: *
092: * @param headerItem
093: * String to specify headerfield (example:Subject)
094: * @param condition
095: * contains, contains not
096: * @param pattern
097: * search string
098: *
099: * @return boolean return true if match was found, otherwise return false
100: */
101: protected boolean match(String headerItem, int condition,
102: String pattern) {
103: boolean result = false;
104:
105: // skip if message doesn't contain the requested headerfield
106: if (headerItem == null) {
107: return false;
108: }
109:
110: switch (condition) {
111: case FilterCriteria.CONTAINS:
112:
113: if (headerItem.toLowerCase().indexOf(pattern.toLowerCase()) != -1) {
114: result = true;
115: }
116:
117: break;
118:
119: case FilterCriteria.CONTAINS_NOT:
120:
121: if (headerItem.toLowerCase().indexOf(pattern.toLowerCase()) == -1) {
122: result = true;
123: }
124:
125: break;
126:
127: case FilterCriteria.IS:
128:
129: if (headerItem.equalsIgnoreCase(pattern)) {
130: result = true;
131: }
132:
133: break;
134:
135: case FilterCriteria.IS_NOT:
136:
137: if (!headerItem.equalsIgnoreCase(pattern)) {
138: result = true;
139: }
140:
141: break;
142:
143: case FilterCriteria.BEGINS_WITH:
144:
145: if (headerItem.toLowerCase().startsWith(
146: pattern.toLowerCase())) {
147: result = true;
148: }
149:
150: break;
151:
152: case FilterCriteria.ENDS_WITH:
153:
154: if (headerItem.toLowerCase()
155: .endsWith(pattern.toLowerCase())) {
156: result = true;
157: }
158:
159: break;
160: }
161:
162: return result;
163: }
164:
165: /**
166: * @see org.columba.core.filter.AbstractFilter#setUp(org.columba.mail.filter.FilterCriteria)
167: */
168: public void setUp(IFilterCriteria f) {
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: }
|