Search in sources :

Example 6 with MatchResult

use of org.apache.oro.text.regex.MatchResult in project tdi-studio-se by Talend.

the class DataMapExpressionParser method parseTableEntryLocations.

public TableEntryLocation[] parseTableEntryLocations(String expression) {
    resultList.clear();
    if (expression != null) {
        matcher.setMultiline(true);
        if (patternMatcherInput == null) {
            patternMatcherInput = new PatternMatcherInput(expression);
        } else {
            patternMatcherInput.setInput(expression);
        }
        recompilePatternIfNecessary(locationPattern);
        while (matcher.contains(patternMatcherInput, pattern)) {
            MatchResult matchResult = matcher.getMatch();
            resultList.add(new TableEntryLocation(matchResult.group(1), matchResult.group(2)));
        }
    }
    return resultList.toArray(new TableEntryLocation[0]);
}
Also used : PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) TableEntryLocation(org.talend.designer.mapper.model.tableentry.TableEntryLocation) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 7 with MatchResult

use of org.apache.oro.text.regex.MatchResult in project tdi-studio-se by Talend.

the class DataStringConnection method getAnalyse.

/**
     * Method getAnalyse extact serveur, port, sid of stringConnection and check the dbType.
     * 
     * @param stringConnection
     * @return string[] { selectionIndex, serveur, port, sid }
     */
public String[] getAnalyse(final String stringConnection) {
    Integer selectionIndex = getSelectionIndex();
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    String[] s = { selectionIndex.toString(), "", "", "", "", "" };
    String regex = getRegex();
    if (stringConnection == "") {
        //$NON-NLS-1$
        return s;
    }
    Perl5Compiler compiler = new Perl5Compiler();
    Perl5Matcher matcher = new Perl5Matcher();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(regex);
        if (matcher.contains(stringConnection, pattern)) {
            matcher.matches(stringConnection, pattern);
            MatchResult matchResult = matcher.getMatch();
            s[0] = selectionIndex.toString();
            if (matchResult != null) {
                for (int i = 1; i < matchResult.groups(); i++) {
                    s[i] = matchResult.group(i);
                }
            }
        } else {
            // search if another regex corresponding at the string of connection
            int i = searchGoodRegex(stringConnection);
            if (i != getSelectionIndex()) {
                setSelectionIndex(i);
                s = getAnalyse(stringConnection);
            }
        }
    } catch (MalformedPatternException e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
    return s;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 8 with MatchResult

use of org.apache.oro.text.regex.MatchResult in project tdi-studio-se by Talend.

the class DataMapExpressionParserTest method main.

public static void main(String[] args) throws Exception {
    Perl5Matcher matcher = new Perl5Matcher();
    Perl5Compiler compiler = new Perl5Compiler();
    // String PATTERN_STR = "\\s*(\\w+)\\s*(\\.\\s*(\\w+)\\s*)+"; // can't get correct group count.
    String PATTERN_STR = "(\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*)" + "|(\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*)" + "|(\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*)" + "|(\\s*(\\w+)\\s*\\.\\s*(\\w+)\\s*)";
    String expression = "context. schema.  context  .table.column";
    // String expression = "context.schema.table.column";
    // String expression = "schema.table.column";
    // String expression = "table.column";
    matcher.setMultiline(true);
    PatternMatcherInput patternMatcherInput = new PatternMatcherInput(expression);
    Pattern pattern = compiler.compile(PATTERN_STR);
    while (matcher.contains(patternMatcherInput, pattern)) {
        MatchResult matchResult = matcher.getMatch();
        System.out.println("group count:" + matchResult.groups());
        for (int i = 1; i <= matchResult.groups(); i++) {
            System.out.println("group[" + i + "] content:" + matchResult.group(i));
        }
    }
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) 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 9 with MatchResult

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

the class RegexExtractor method initTemplate.

private void initTemplate() {
    if (template != null) {
        return;
    }
    // Contains Strings and Integers
    List<Object> combined = new ArrayList<>();
    String rawTemplate = getTemplate();
    PatternMatcher matcher = JMeterUtils.getMatcher();
    Pattern templatePattern = // $NON-NLS-1$
    JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
    "\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK);
    if (log.isDebugEnabled()) {
        log.debug("Pattern = '{}', template = '{}'", templatePattern.getPattern(), rawTemplate);
    }
    int beginOffset = 0;
    MatchResult currentResult;
    PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
    while (matcher.contains(pinput, templatePattern)) {
        currentResult = matcher.getMatch();
        final int beginMatch = currentResult.beginOffset(0);
        if (beginMatch > beginOffset) {
            // string is not empty
            combined.add(rawTemplate.substring(beginOffset, beginMatch));
        }
        // add match as Integer
        combined.add(Integer.valueOf(currentResult.group(1)));
        beginOffset = currentResult.endOffset(0);
    }
    if (beginOffset < rawTemplate.length()) {
        // trailing string is not empty
        combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
    }
    if (log.isDebugEnabled()) {
        log.debug("Template item count: {}", combined.size());
        int i = 0;
        for (Object o : combined) {
            log.debug("Template item-{}: {} '{}'", i++, o.getClass(), o);
        }
    }
    template = combined;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) ArrayList(java.util.ArrayList) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 10 with MatchResult

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

the class RegexExtractor method processMatches.

private List<MatchResult> processMatches(Pattern pattern, String regex, SampleResult result, int matchNumber, JMeterVariables vars) {
    log.debug("Regex = '{}'", regex);
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    List<MatchResult> matches = new ArrayList<>();
    int found = 0;
    if (isScopeVariable()) {
        String inputString = vars.get(getVariableName());
        if (inputString == null) {
            if (log.isWarnEnabled()) {
                log.warn("No variable '{}' found to process by RegexExtractor '{}', skipping processing", getVariableName(), getName());
            }
            return Collections.emptyList();
        }
        matchStrings(matchNumber, matcher, pattern, matches, found, inputString);
    } else {
        List<SampleResult> sampleList = getSampleList(result);
        for (SampleResult sr : sampleList) {
            String inputString = getInputString(sr);
            found = matchStrings(matchNumber, matcher, pattern, matches, found, inputString);
            if (matchNumber > 0 && found == matchNumber) {
                // no need to process further
                break;
            }
        }
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) SampleResult(org.apache.jmeter.samplers.SampleResult) MatchResult(org.apache.oro.text.regex.MatchResult)

Aggregations

MatchResult (org.apache.oro.text.regex.MatchResult)25 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)16 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)15 Pattern (org.apache.oro.text.regex.Pattern)14 ArrayList (java.util.ArrayList)10 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)4 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)4 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)3 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Charset (java.nio.charset.Charset)2