All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jsieve.comparators.ComparatorUtils Maven / Gradle / Ivy

Go to download

Apache jSieve is a server side mail filtering system implementing RFC3028. Apache jSieve is developed by the JAMES project.

There is a newer version: 0.8
Show newest version
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

package org.apache.jsieve.comparators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.jsieve.SieveContext;
import org.apache.jsieve.exception.LookupException;
import org.apache.jsieve.exception.SieveException;
import org.apache.jsieve.exception.SievePatternException;

/**
 * Class ComparatorUtils implements utility methods used by Comparators.
 */
public class ComparatorUtils implements MatchTypeTags {

    /**
     * Constructor for ComparatorUtils.
     */
    private ComparatorUtils() {
        super();
    }

    /**
     * Method match answers a boolean indicating if the parameter
     * matchTarget compares to parameter
     * matchArgument is a match of matchType using
     * the comparator comparatorName.
     * 
     * @param comparatorName
     * @param matchType
     * @param matchTarget
     * @param matchArgument
     * @param context
     *            TODO
     * @return boolean
     */
    public static boolean match(String comparatorName, String matchType,
            String matchTarget, String matchArgument, SieveContext context)
            throws SieveException {
        boolean isMatched = false;
        if (matchType.equals(IS_TAG))
            isMatched = is(comparatorName, matchTarget, matchArgument, context);
        else if (matchType.equals(CONTAINS_TAG))
            isMatched = contains(comparatorName, matchTarget, matchArgument,
                    context);
        else if (matchType.equals(MATCHES_TAG))
            isMatched = matches(comparatorName, matchTarget, matchArgument,
                    context);
        return isMatched;
    }

    /**
     * 

* Method matches answers a boolean indicating if the * parameter string matches the glob pattern described by * parameter glob. * * @param string * @param glob * @return boolean * @throws SievePatternException */ static public boolean matches(String string, String glob) throws SievePatternException { // TODO Is there a way to re-use the compiled pattern? try { String regex = sieveToJavaRegex(glob); final Matcher matcher = Pattern.compile(regex).matcher(string); return matcher.matches(); } catch (PatternSyntaxException e) { throw new SievePatternException(e.getMessage()); } } /** *

* Method contains answers a boolean indicating if the * parameter container contains the parameter * contents. *

* * @param container * @param contents * @return boolean */ static public boolean contains(String container, String contents) { return container.indexOf(contents) > -1; } /** *

* Method equals answers a boolean indicating if the * parameter string1 is equal to the parameter * string2. *

* * @param string1 * @param string2 * @return boolean */ static public boolean equals(String string1, String string2) { return string1.equals(string2); } /** * Returns true if the char is a special char for regex */ private static boolean isRegexSpecialChar(char ch) { return (ch == '*' || ch == '?' || ch == '+' || ch == '[' || ch == ']' || ch == '(' || ch == ')' || ch == '|' || ch == '^' || ch == '$' || ch == '.' || ch == '{' || ch == '}' || ch == '\\'); } /** * Returns true if the char is a special char for sieve matching */ private static boolean isSieveMatcherSpecialChar(char ch) { return (ch == '*' || ch == '?' || ch == '\\'); } /** * Converts a Sieve pattern in a java regex pattern */ public static String sieveToJavaRegex(String pattern) { int ch; StringBuffer buffer = new StringBuffer(2 * pattern.length()); boolean lastCharWasStar = false; for (ch = 0; ch < pattern.length(); ch++) { final char nextChar = pattern.charAt(ch); switch (nextChar) { case '*': // // Java Matcher has issues with repeated stars // if (!lastCharWasStar) { buffer.append(".*"); } break; case '?': buffer.append('.'); break; case '\\': buffer.append('\\'); if (ch == pattern.length() - 1) buffer.append('\\'); else if (isSieveMatcherSpecialChar(pattern.charAt(ch + 1))) buffer.append(pattern.charAt(++ch)); else buffer.append('\\'); break; default: if (isRegexSpecialChar(nextChar)) buffer.append('\\'); buffer.append(nextChar); break; } // Workaround for issue with Java Matcher lastCharWasStar = '*' == nextChar; } return buffer.toString(); } /** * Method contains answers a boolean indicating if the parameter * container contains the parameter contents using an * instance of comparatorName. * @param comparatorName * @param container * @param contents * @param context TODO * @return boolean */ public static boolean contains(String comparatorName, String container, String contents, SieveContext context) throws LookupException { Contains comparatorObj = context.getComparator(comparatorName); return comparatorObj.contains(container, contents); } /** * Method is answers a boolean indicating if the parameter * container is equal to the parameter contents using * an instance of comparatorName. * @param comparatorName * @param string1 * @param string2 * @param context TODO * @return boolean */ public static boolean is(String comparatorName, String string1, String string2, SieveContext context) throws LookupException { Equals comparatorObj = context.getComparator(comparatorName); return comparatorObj.equals(string1, string2); } /** * Method matches answers a boolean indicating if the * parameter * string/code> is matched by the patterm glob using an * instance of comparatorName. * @param comparatorName * @param string * @param glob * @param context TODO * @return boolean */ public static boolean matches(String comparatorName, String string, String glob, SieveContext context) throws SieveException { Matches comparatorObj = context.getComparator(comparatorName); return comparatorObj.matches(string, glob); } }