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

org.mockserver.matchers.StringMatcher Maven / Gradle / Ivy

There is a newer version: 5.15.0
Show newest version
package org.mockserver.matchers;

import com.google.common.base.Strings;
import org.mockserver.model.ModelObject;

import java.util.regex.PatternSyntaxException;

/**
 * @author jamesdbloom
 */
public class StringMatcher extends ModelObject implements Matcher {

    private final String path;

    public StringMatcher(String path) {
        this.path = path;
    }

    public boolean matches(String path) {
        boolean result = false;

        if (Strings.isNullOrEmpty(this.path)) {
            result = true;
        } else if (path != null) {
            try {
                if (path.matches(this.path)) {
                    result = true;
                }
            } catch (PatternSyntaxException pse) {
                logger.error("Error while matching regex [" +  this.path + "] for string [" + path + "] " + pse.getMessage());
            }
            try {
                if (this.path.matches(path)) {
                    result = true;
                }
            } catch (PatternSyntaxException pse) {
                logger.error("Error while matching regex [" + path + "] for string [" + this.path + "] " + pse.getMessage());
            }
        }

        if (!result) {
            logger.trace("Failed to match [{}] with [{}]", path, this.path);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy