FileDocCategorySizeDatePackage
GenericRegexMatcher.javaAPI DocApache James 2.3.13782Fri Jan 12 12:56:32 GMT 2007org.apache.james.transport.matchers

GenericRegexMatcher

public abstract class GenericRegexMatcher extends org.apache.mailet.GenericMatcher
This is a generic matcher that uses regular expressions. If any of the regular expressions match, the matcher is considered to have matched. This is an abstract class that must be subclassed to feed patterns. Patterns are provided by calling the compile method. A subclass will generally call compile() once during init(), but it could subclass match(), and call it as necessary during message processing (e.g., if a file of expressions changed).

Fields Summary
protected Object[]
patterns
Constructors Summary
Methods Summary
public voidcompile(java.lang.Object[][] patterns)

        // compile a bunch of regular expressions
        this.patterns = patterns;
        for (int i = 0; i < patterns.length; i++) {
            String pattern = (String)patterns[i][1];
            patterns[i][1] = new Perl5Compiler().compile(pattern, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
        }
    
public abstract voidinit()

public java.util.Collectionmatch(org.apache.mailet.Mail mail)

        MimeMessage message = mail.getMessage();
        Perl5Matcher matcher = new Perl5Matcher();

        //Loop through all the patterns
        if (patterns != null) for (int i = 0; i < patterns.length; i++) {
            //Get the header name
            String headerName = (String)patterns[i][0];
            //Get the patterns for that header
            Pattern pattern = (Pattern)patterns[i][1];
            //Get the array of header values that match that
            String headers[] = message.getHeader(headerName);
            //Loop through the header values
            if (headers != null) for (int j = 0; j < headers.length; j++) {
                if (matcher.matches(headers[j], pattern)) {
                    // log("Match: " + headerName + "[" + j + "]: " + headers[j]);
                    return mail.getRecipients();
                }
                //log("       " + headerName + "[" + j + "]: " + headers[j]);
            }
        }
        return null;