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

com.xceptance.common.util.StringMatcher Maven / Gradle / Ivy

Go to download

XLT (Xceptance LoadTest) is an extensive load and performance test tool developed and maintained by Xceptance.

There is a newer version: 8.1.0
Show newest version
package com.xceptance.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * The {@link StringMatcher} is used to check whether a string does (not) comply with certain rules, which are defined
 * by regular expressions. The string is checked whether it:
 * 
    *
  • does NOT match a set of "exclude" reg-ex patterns and
  • *
  • does match a set of "include" reg-ex patterns.
  • *
* Usually, either the "include" or the "exclude" setting is used, whichever is easier to define. If both settings are * used at the same time, the "exclude" patterns take precedence. An empty pattern set means that any string is included * and none is excluded, respectively. * * @author Jörg Werner (Xceptance Software Technologies GmbH) */ public class StringMatcher { /** * The list of patterns describing the strings NOT to accept. */ private final List excludePatterns; /** * The list of patterns describing the strings to accept. */ private final List includePatterns; /** * whether the patterns must match the whole input string or can match a substring only. */ private final boolean fullMatch; /** * Creates a new StringMatcher object and initializes it with the given include and exclude patterns. * * @param includePatternsSpec * a string with the include patterns, may be null * @param excludePatternsSpec * a string with the exclude patterns, may be null */ public StringMatcher(final String includePatternsSpec, final String excludePatternsSpec) { this(includePatternsSpec, excludePatternsSpec, false); } /** * Creates a new StringMatcher object and initializes it with the given include and exclude patterns. * * @param includePatternsSpec * a string with the include patterns, may be null * @param excludePatternsSpec * a string with the exclude patterns, may be null * @param fullMatch * whether the patterns must match the whole input string (true) or a substring only ( * false) */ public StringMatcher(final String includePatternsSpec, final String excludePatternsSpec, boolean fullMatch) { includePatterns = buildPatternList(includePatternsSpec); excludePatterns = buildPatternList(excludePatternsSpec); this.fullMatch = fullMatch; } /** * Creates a list of Pattern objects from the passed regex patterns string. The regex patterns are separated by * commas. * * @param patternsString * the list of regex pattern strings * @return the list of patterns objects */ private List buildPatternList(final String patternsString) { final List patterns = new ArrayList(); if (patternsString != null) { for (final String patternString : StringUtils.split(patternsString, " ,;")) { final Pattern pattern = Pattern.compile(patternString); patterns.add(pattern); } } return patterns; } /** * Checks whether the passed string is accepted. * * @param s * the string to check * @return whether the string is accepted */ public boolean isAccepted(final String s) { // do not accept the string if it matches an exclude pattern for (final Pattern pattern : excludePatterns) { final Matcher matcher = pattern.matcher(s); if (!fullMatch && matcher.find() || fullMatch && matcher.matches()) { return false; } } // do accept the string if there are no include patterns if (includePatterns.isEmpty()) { return true; } // do accept the string if it matches an include pattern for (final Pattern pattern : includePatterns) { final Matcher matcher = pattern.matcher(s); if (!fullMatch && matcher.find() || fullMatch && matcher.matches()) { return true; } } // do not accept the string return false; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy