Search in sources :

Example 11 with PatternMatcherInput

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

the class RegexFunction method generateTemplate.

private Object[] generateTemplate(String rawTemplate) {
    List<String> pieces = new ArrayList<>();
    // String or Integer
    List<Object> combined = new LinkedList<>();
    PatternMatcher matcher = JMeterUtils.getMatcher();
    Util.split(pieces, matcher, templatePattern, rawTemplate);
    PatternMatcherInput input = new PatternMatcherInput(rawTemplate);
    boolean startsWith = isFirstElementGroup(rawTemplate);
    if (startsWith) {
        // Remove initial empty entry
        pieces.remove(0);
    }
    Iterator<String> iter = pieces.iterator();
    while (iter.hasNext()) {
        boolean matchExists = matcher.contains(input, templatePattern);
        if (startsWith) {
            if (matchExists) {
                combined.add(Integer.valueOf(matcher.getMatch().group(1)));
            }
            combined.add(iter.next());
        } else {
            combined.add(iter.next());
            if (matchExists) {
                combined.add(Integer.valueOf(matcher.getMatch().group(1)));
            }
        }
    }
    if (matcher.contains(input, templatePattern)) {
        combined.add(Integer.valueOf(matcher.getMatch().group(1)));
    }
    return combined.toArray();
}
Also used : PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) ArrayList(java.util.ArrayList) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) LinkedList(java.util.LinkedList)

Example 12 with PatternMatcherInput

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

the class RegexFunction method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    //$NON-NLS-1$
    String valueIndex = "";
    //$NON-NLS-1$
    String defaultValue = "";
    //$NON-NLS-1$ 
    String between = "";
    //$NON-NLS-1$
    String name = "";
    //$NON-NLS-1$
    String inputVariable = "";
    Pattern searchPattern;
    Object[] tmplt;
    try {
        searchPattern = JMeterUtils.getPatternCache().getPattern(((CompoundVariable) values[0]).execute(), Perl5Compiler.READ_ONLY_MASK);
        tmplt = generateTemplate(((CompoundVariable) values[1]).execute());
        if (values.length > 2) {
            valueIndex = ((CompoundVariable) values[2]).execute();
        }
        if (valueIndex.length() == 0) {
            //$NON-NLS-1$
            valueIndex = "1";
        }
        if (values.length > 3) {
            between = ((CompoundVariable) values[3]).execute();
        }
        if (values.length > 4) {
            String dv = ((CompoundVariable) values[4]).execute();
            if (dv.length() != 0) {
                defaultValue = dv;
            }
        }
        if (values.length > 5) {
            name = ((CompoundVariable) values[5]).execute();
        }
        if (values.length > 6) {
            inputVariable = ((CompoundVariable) values[6]).execute();
        }
    } catch (MalformedCachePatternException e) {
        log.error("Malformed cache pattern:" + values[0], e);
        throw new InvalidVariableException("Malformed cache pattern:" + values[0], e);
    }
    // Relatively expensive operation, so do it once
    JMeterVariables vars = getVariables();
    if (vars == null) {
        // Can happen if called during test closedown
        return defaultValue;
    }
    if (name.length() > 0) {
        vars.put(name, defaultValue);
    }
    String textToMatch = null;
    if (inputVariable.length() > 0) {
        textToMatch = vars.get(inputVariable);
    } else if (previousResult != null) {
        textToMatch = previousResult.getResponseDataAsString();
    }
    if (textToMatch == null || textToMatch.length() == 0) {
        return defaultValue;
    }
    List<MatchResult> collectAllMatches = new ArrayList<>();
    try {
        PatternMatcher matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = new PatternMatcherInput(textToMatch);
        while (matcher.contains(input, searchPattern)) {
            MatchResult match = matcher.getMatch();
            if (match != null) {
                collectAllMatches.add(match);
            }
        }
    } finally {
        if (name.length() > 0) {
            //$NON-NLS-1$
            vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size()));
        }
    }
    if (collectAllMatches.isEmpty()) {
        return defaultValue;
    }
    if (valueIndex.equals(ALL)) {
        StringBuilder value = new StringBuilder();
        Iterator<MatchResult> it = collectAllMatches.iterator();
        boolean first = true;
        while (it.hasNext()) {
            if (!first) {
                value.append(between);
            } else {
                first = false;
            }
            value.append(generateResult(it.next(), name, tmplt, vars));
        }
        return value.toString();
    } else if (valueIndex.equals(RAND)) {
        MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size()));
        return generateResult(result, name, tmplt, vars);
    } else {
        try {
            int index = Integer.parseInt(valueIndex) - 1;
            if (index >= collectAllMatches.size()) {
                return defaultValue;
            }
            MatchResult result = collectAllMatches.get(index);
            return generateResult(result, name, tmplt, vars);
        } catch (NumberFormatException e) {
            float ratio = Float.parseFloat(valueIndex);
            MatchResult result = collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
            return generateResult(result, name, tmplt, vars);
        }
    }
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Pattern(org.apache.oro.text.regex.Pattern) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) ArrayList(java.util.ArrayList) MatchResult(org.apache.oro.text.regex.MatchResult) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 13 with PatternMatcherInput

use of org.apache.oro.text.regex.PatternMatcherInput 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 = (Functor) 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)

Example 14 with PatternMatcherInput

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

the class HtmlParsingUtils method extractStyleURLs.

public static void extractStyleURLs(final URL baseUrl, final URLCollection urls, String styleTagStr) {
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    Pattern pattern = JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
    "URL\\(\\s*('|\")(.*)('|\")\\s*\\)", Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
    PatternMatcherInput input = null;
    input = new PatternMatcherInput(styleTagStr);
    while (matcher.contains(input, pattern)) {
        MatchResult match = matcher.getMatch();
        // The value is in the second group
        String styleUrl = match.group(2);
        urls.addURL(styleUrl, baseUrl);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 15 with PatternMatcherInput

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

the class HttpMirrorThread method getPositionOfBody.

private static int getPositionOfBody(String stringToCheck) {
    Perl5Matcher localMatcher = JMeterUtils.getMatcher();
    // The headers and body are divided by a blank line (the \r is to allow for the CR before LF)
    // $NON-NLS-1$
    String regularExpression = "^\\r$";
    Pattern pattern = JMeterUtils.getPattern(regularExpression, Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.MULTILINE_MASK);
    PatternMatcherInput input = new PatternMatcherInput(stringToCheck);
    if (localMatcher.contains(input, pattern)) {
        MatchResult match = localMatcher.getMatch();
        return match.beginOffset(0);
    }
    // No divider was found
    return -1;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Aggregations

PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)28 MatchResult (org.apache.oro.text.regex.MatchResult)20 Pattern (org.apache.oro.text.regex.Pattern)15 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)14 ArrayList (java.util.ArrayList)9 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)5 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)5 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Array (lucee.runtime.type.Array)2 ArrayImpl (lucee.runtime.type.ArrayImpl)2 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Struct (lucee.runtime.type.Struct)1