KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > matchers > GenericRegexMatcher


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.transport.matchers;
19
20 import org.apache.james.util.RFC2822Headers;
21 import org.apache.mailet.GenericMatcher;
22 import org.apache.mailet.Mail;
23 import org.apache.oro.text.regex.MalformedPatternException;
24 import org.apache.oro.text.regex.Pattern;
25 import org.apache.oro.text.regex.Perl5Compiler;
26 import org.apache.oro.text.regex.Perl5Matcher;
27
28 import javax.mail.MessagingException JavaDoc;
29 import javax.mail.internet.MimeMessage JavaDoc;
30 import java.util.Collection JavaDoc;
31
32 /**
33  * This is a generic matcher that uses regular expressions. If any of
34  * the regular expressions match, the matcher is considered to have
35  * matched. This is an abstract class that must be subclassed to feed
36  * patterns. Patterns are provided by calling the compile method. A
37  * subclass will generally call compile() once during init(), but it
38  * could subclass match(), and call it as necessary during message
39  * processing (e.g., if a file of expressions changed).
40  *
41  * @
42  */

43
44 abstract public class GenericRegexMatcher extends GenericMatcher {
45     protected Object JavaDoc[][] patterns;
46
47     public void compile(Object JavaDoc[][] patterns) throws MalformedPatternException {
48         // compile a bunch of regular expressions
49
this.patterns = patterns;
50         for (int i = 0; i < patterns.length; i++) {
51             String JavaDoc pattern = (String JavaDoc)patterns[i][1];
52             patterns[i][1] = new Perl5Compiler().compile(pattern, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
53         }
54     }
55
56     abstract public void init() throws MessagingException JavaDoc;
57
58     public Collection JavaDoc match(Mail mail) throws MessagingException JavaDoc {
59         MimeMessage JavaDoc message = mail.getMessage();
60         Perl5Matcher matcher = new Perl5Matcher();
61
62         //Loop through all the patterns
63
if (patterns != null) for (int i = 0; i < patterns.length; i++) {
64             //Get the header name
65
String JavaDoc headerName = (String JavaDoc)patterns[i][0];
66             //Get the patterns for that header
67
Pattern pattern = (Pattern)patterns[i][1];
68             //Get the array of header values that match that
69
String JavaDoc headers[] = message.getHeader(headerName);
70             //Loop through the header values
71
if (headers != null) for (int j = 0; j < headers.length; j++) {
72                 if (matcher.matches(headers[j], pattern)) {
73                     // log("Match: " + headerName + "[" + j + "]: " + headers[j]);
74
return mail.getRecipients();
75                 }
76                 //log(" " + headerName + "[" + j + "]: " + headers[j]);
77
}
78         }
79         return null;
80     }
81 }
82
Popular Tags