Search in sources :

Example 1 with PatternCacheLRU

use of org.apache.oro.text.PatternCacheLRU in project jmeter by apache.

the class HtmlParsingUtils method isAnchorMatched.

/**
     * Check if anchor matches by checking against:
     * - protocol
     * - domain
     * - path
     * - parameter names
     *
     * @param newLink target to match
     * @param config pattern to match against
     *
     * @return true if target URL matches pattern URL
     */
public static boolean isAnchorMatched(HTTPSamplerBase newLink, HTTPSamplerBase config) {
    String query = null;
    try {
        query = URLDecoder.decode(newLink.getQueryString(), StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        // UTF-8 unsupported? You must be joking!
        log.error("UTF-8 encoding not supported!");
        throw new Error("Should not happen: " + e.toString(), e);
    }
    final Arguments arguments = config.getArguments();
    final Perl5Matcher matcher = JMeterUtils.getMatcher();
    final PatternCacheLRU patternCache = JMeterUtils.getPatternCache();
    if (!isEqualOrMatches(newLink.getProtocol(), config.getProtocol(), matcher, patternCache)) {
        return false;
    }
    final String domain = config.getDomain();
    if (domain != null && domain.length() > 0) {
        if (!isEqualOrMatches(newLink.getDomain(), domain, matcher, patternCache)) {
            return false;
        }
    }
    final String path = config.getPath();
    if (!newLink.getPath().equals(path) && !matcher.matches(newLink.getPath(), // $NON-NLS-1$
    patternCache.getPattern(// $NON-NLS-1$
    "[/]*" + path, Perl5Compiler.READ_ONLY_MASK))) {
        return false;
    }
    for (JMeterProperty argument : arguments) {
        Argument item = (Argument) argument.getObjectValue();
        final String name = item.getName();
        if (!query.contains(name + "=")) {
            // $NON-NLS-1$
            if (!(matcher.contains(query, patternCache.getPattern(name, Perl5Compiler.READ_ONLY_MASK)))) {
                return false;
            }
        }
    }
    return true;
}
Also used : JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Argument(org.apache.jmeter.config.Argument) Arguments(org.apache.jmeter.config.Arguments) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternCacheLRU(org.apache.oro.text.PatternCacheLRU)

Example 2 with PatternCacheLRU

use of org.apache.oro.text.PatternCacheLRU in project jmeter by apache.

the class HtmlParsingUtils method isArgumentMatched.

/**
     * Arguments match if the input name matches the corresponding pattern name
     * and the input value matches the pattern value, where the matching is done
     * first using String equals, and then Regular Expression matching if the equals test fails.
     *
     * @param arg - input Argument
     * @param patternArg - pattern to match against
     * @return true if both name and value match
     */
public static boolean isArgumentMatched(Argument arg, Argument patternArg) {
    final Perl5Matcher matcher = JMeterUtils.getMatcher();
    final PatternCacheLRU patternCache = JMeterUtils.getPatternCache();
    return isEqualOrMatches(arg.getName(), patternArg.getName(), matcher, patternCache) && isEqualOrMatches(arg.getValue(), patternArg.getValue(), matcher, patternCache);
}
Also used : Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternCacheLRU(org.apache.oro.text.PatternCacheLRU)

Example 3 with PatternCacheLRU

use of org.apache.oro.text.PatternCacheLRU in project jmeter by apache.

the class RenderAsRegexp method process.

private String process(String textToParse) {
    Perl5Matcher matcher = new Perl5Matcher();
    PatternMatcherInput input = new PatternMatcherInput(textToParse);
    PatternCacheLRU pcLRU = new PatternCacheLRU();
    Pattern pattern;
    try {
        pattern = pcLRU.getPattern(regexpField.getText(), Perl5Compiler.READ_ONLY_MASK);
    } catch (MalformedCachePatternException e) {
        return e.toString();
    }
    List<MatchResult> matches = new LinkedList<>();
    while (matcher.contains(input, pattern)) {
        matches.add(matcher.getMatch());
    }
    // Construct a multi-line string with all matches
    StringBuilder sb = new StringBuilder();
    final int size = matches.size();
    sb.append("Match count: ").append(size).append("\n");
    for (int j = 0; j < size; j++) {
        MatchResult mr = matches.get(j);
        final int groups = mr.groups();
        for (int i = 0; i < groups; i++) {
            sb.append("Match[").append(j + 1).append("][").append(i).append("]=").append(mr.group(i)).append("\n");
        }
    }
    return sb.toString();
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) LinkedList(java.util.LinkedList) PatternCacheLRU(org.apache.oro.text.PatternCacheLRU)

Aggregations

PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)3 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 LinkedList (java.util.LinkedList)1 Argument (org.apache.jmeter.config.Argument)1 Arguments (org.apache.jmeter.config.Arguments)1 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)1 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)1 MatchResult (org.apache.oro.text.regex.MatchResult)1 Pattern (org.apache.oro.text.regex.Pattern)1 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)1