Search in sources :

Example 26 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project otter by alibaba.

the class ConfigHelperTest method testWildCard.

@Test
public void testWildCard() {
    PatternMatcher matcher = new Perl5Matcher();
    Pattern pattern = null;
    PatternCompiler pc = new Perl5Compiler();
    try {
        pattern = pc.compile("havana_us_.*", Perl5Compiler.DEFAULT_MASK);
    } catch (MalformedPatternException e) {
        throw new ConfigException(e);
    }
    boolean ismatch = matcher.matches("havana_us_0001", pattern);
    System.out.println(ismatch);
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) ConfigException(com.alibaba.otter.shared.common.model.config.ConfigException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.common.BaseOtterTest)

Example 27 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project otter by alibaba.

the class ConfigHelper method parseMode.

/**
 * 解析DataMedia中的namespace和name,支持offer[1-128]分库的定义
 */
public static ModeValue parseMode(String value) {
    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.matches(value, patterns.get(MODE_PATTERN))) {
        MatchResult matchResult = matcher.getMatch();
        String prefix = matchResult.group(1);
        String startStr = matchResult.group(3);
        String ednStr = matchResult.group(4);
        int start = Integer.valueOf(startStr);
        int end = Integer.valueOf(ednStr);
        String postfix = matchResult.group(5);
        List<String> values = new ArrayList<String>();
        for (int i = start; i <= end; i++) {
            StringBuilder builder = new StringBuilder(value.length());
            String str = String.valueOf(i);
            // 处理0001类型
            if (startStr.length() == ednStr.length() && startStr.startsWith("0")) {
                str = StringUtils.leftPad(String.valueOf(i), startStr.length(), '0');
            }
            builder.append(prefix).append(str).append(postfix);
            values.add(builder.toString());
        }
        return new ModeValue(Mode.MULTI, values);
    } else if (isWildCard(value)) {
        // 通配符支持
        return new ModeValue(Mode.WILDCARD, Arrays.asList(value));
    } else {
        return new ModeValue(Mode.SINGLE, Arrays.asList(value));
    }
}
Also used : ModeValue(com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 28 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project otter by alibaba.

the class ActionProtectedImpl method check.

public boolean check(String action, String method) {
    if (!StringUtil.isBlank(action)) {
        PatternMatcher matcher = new Perl5Matcher();
        Iterator<ActionPatternHolder> iter = actionPatternList.iterator();
        while (iter.hasNext()) {
            ActionPatternHolder holder = (ActionPatternHolder) iter.next();
            if (StringUtils.isNotEmpty(action) && matcher.matches(action, holder.getActionPattern()) && StringUtils.isNotEmpty(method) && matcher.matches(method, holder.getMethodPattern())) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Candidate is: '" + action + "|" + method + "'; pattern is " + holder.getActionName() + "|" + holder.getMethodName() + "; matched=true");
                }
                return true;
            }
        }
    }
    return false;
}
Also used : Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 29 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project otter by alibaba.

the class URLProtectedImpl method check.

public boolean check(String requestUrl) {
    if (StringUtils.isBlank(requestUrl)) {
        return false;
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Converted URL to lowercase, from: '" + requestUrl + "'; to: '" + requestUrl + "'");
    }
    PatternMatcher matcher = new Perl5Matcher();
    Iterator<URLPatternHolder> iter = urlProtectedList.iterator();
    while (iter.hasNext()) {
        URLPatternHolder holder = (URLPatternHolder) iter.next();
        if (matcher.matches(requestUrl, holder.getCompiledPattern())) {
            if (logger.isDebugEnabled()) {
                logger.debug("Candidate is: '" + requestUrl + "'; pattern is " + holder.getCompiledPattern().getPattern() + "; matched=true");
            }
            return true;
        }
    }
    return false;
}
Also used : Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 30 with Perl5Matcher

use of org.apache.oro.text.regex.Perl5Matcher in project jmeter by apache.

the class CSVSaveService method getSampleSaveConfiguration.

/**
 * Parse a CSV header line
 *
 * @param headerLine
 *            from CSV file
 * @param filename
 *            name of file (for log message only)
 * @return config corresponding to the header items found or null if not a
 *         header line
 */
public static SampleSaveConfiguration getSampleSaveConfiguration(String headerLine, String filename) {
    // Try
    String[] parts = splitHeader(headerLine, _saveConfig.getDelimiter());
    // default
    // delimiter
    String delim = null;
    if (parts == null) {
        Perl5Matcher matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = new PatternMatcherInput(headerLine);
        Pattern pattern = JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
        "\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*", // last entries may be quoted strings
        Perl5Compiler.READ_ONLY_MASK);
        if (matcher.matches(input, pattern)) {
            delim = matcher.getMatch().group(2);
            // now validate the
            parts = splitHeader(headerLine, delim);
        // result
        }
    }
    if (parts == null) {
        // failed to recognise the header
        return null;
    }
    // We know the column names all exist, so create the config
    SampleSaveConfiguration saveConfig = new SampleSaveConfiguration(false);
    int varCount = 0;
    for (String label : parts) {
        if (isVariableName(label)) {
            varCount++;
        } else {
            Functor set = headerLabelMethods.get(label);
            set.invoke(saveConfig, new Boolean[] { Boolean.TRUE });
        }
    }
    if (delim != null) {
        if (log.isWarnEnabled()) {
            log.warn("Default delimiter '{}' did not work; using alternate '{}' for reading {}", _saveConfig.getDelimiter(), delim, filename);
        }
        saveConfig.setDelimiter(delim);
    }
    saveConfig.setVarCount(varCount);
    return saveConfig;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) SampleSaveConfiguration(org.apache.jmeter.samplers.SampleSaveConfiguration) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) Functor(org.apache.jorphan.reflect.Functor)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)72 Pattern (org.apache.oro.text.regex.Pattern)50 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)23 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)22 MatchResult (org.apache.oro.text.regex.MatchResult)21 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)17 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)14 ArrayList (java.util.ArrayList)12 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)8 IOException (java.io.IOException)6 MalformedURLException (java.net.MalformedURLException)6 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)5 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 File (java.io.File)2