Search in sources :

Example 6 with PatternMatcherInput

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

the class MapExpressionParser method parseInTableEntryLocations2.

/**
     * 
     * DOC parse to talbename and column map
     * 
     * @param expression
     * @return
     */
public List<Map<String, String>> parseInTableEntryLocations2(String expression) {
    List<Map<String, String>> result = new ArrayList<Map<String, String>>();
    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();
            Map<String, String> map = new HashMap<String, String>();
            String group1 = matchResult.group(1);
            if (group1 != null && !"".equals(group1) && matchResult.group(matchResult.groups() - 1) != null) {
                map.put(matchResult.group(matchResult.groups() - 1).trim(), group1.substring(0, group1.length() - 1).trim());
            } else {
                String string = matchResult.toString();
                int lastIndexOf = string.lastIndexOf(".");
                if (lastIndexOf != -1) {
                    map.put(string.substring(lastIndexOf + 1, string.length()).trim(), string.substring(0, lastIndexOf).trim());
                }
            }
            result.add(map);
        }
    }
    return result;
}
Also used : PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 7 with PatternMatcherInput

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

the class JavaRoutineSynchronizer method renamePigudfClass.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.designer.codegen.AbstractRoutineSynchronizer#renamePigudfClass(org.talend.core.model.properties.
     * RoutineItem)
     */
@Override
public void renamePigudfClass(PigudfItem pigudfItem, String oldLabel) {
    if (pigudfItem == null) {
        return;
    }
    String routineContent = new String(pigudfItem.getContent().getInnerContent());
    String label = pigudfItem.getProperty().getLabel();
    //
    Perl5Matcher matcher = new Perl5Matcher();
    Perl5Compiler compiler = new Perl5Compiler();
    PatternMatcherInput patternMatcherInput = new PatternMatcherInput(routineContent);
    //$NON-NLS-1$//$NON-NLS-2$
    String regx = "public(\\s)+class(\\s)+" + oldLabel + "(\\s)(.+)\\{";
    String extendsText = "";
    try {
        org.apache.oro.text.regex.Pattern pattern = compiler.compile(regx);
        boolean contains = matcher.contains(patternMatcherInput, pattern);
        if (contains) {
            org.apache.oro.text.regex.MatchResult matchResult = matcher.getMatch();
            extendsText = matchResult.group(matchResult.groups() - 1);
        }
        //$NON-NLS-1$
        String regexp = "public(\\s)+class(\\s)+\\w+(\\s)\\{";
        if (extendsText != null) {
            extendsText = extendsText.trim();
            //$NON-NLS-1$//$NON-NLS-2$
            regexp = "public(\\s)+class(\\s)+\\w+(\\s)+" + extendsText + "(\\s)*\\{";
        }
        // rename class name
        //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
        routineContent = routineContent.replaceFirst(regexp, "public class " + label + " " + extendsText + " {");
        // rename constructor
        String constructorRegx = "(\\s+)" + oldLabel + "(\\((.*)\\))";
        String toReplace = "$1" + label + "$1$2";
        pattern = compiler.compile(constructorRegx);
        Perl5Substitution substitution = new Perl5Substitution(toReplace, Perl5Substitution.INTERPOLATE_ALL);
        routineContent = Util.substitute(matcher, pattern, substitution, routineContent, Util.SUBSTITUTE_ALL);
    } catch (MalformedPatternException e) {
        ExceptionHandler.process(new Exception("Rename pigudf failed"));
    }
    pigudfItem.getContent().setInnerContent(routineContent.getBytes());
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) CoreException(org.eclipse.core.runtime.CoreException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) IOException(java.io.IOException) SystemException(org.talend.commons.exception.SystemException) Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 8 with PatternMatcherInput

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

the class RegexpHTMLParser method getEmbeddedResourceURLs.

/**
     * {@inheritDoc}
     */
@Override
public Iterator<URL> getEmbeddedResourceURLs(String userAgent, byte[] html, URL baseUrl, URLCollection urls, String encoding) throws HTMLParseException {
    Pattern pattern = null;
    Perl5Matcher matcher = null;
    try {
        matcher = JMeterUtils.getMatcher();
        PatternMatcherInput input = localInput.get();
        // TODO: find a way to avoid the cost of creating a String here --
        // probably a new PatternMatcherInput working on a byte[] would do
        // better.
        input.setInput(new String(html, encoding));
        pattern = JMeterUtils.getPatternCache().getPattern(REGEXP, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.SINGLELINE_MASK | Perl5Compiler.READ_ONLY_MASK);
        while (matcher.contains(input, pattern)) {
            MatchResult match = matcher.getMatch();
            String s;
            if (log.isDebugEnabled()) {
                log.debug("match groups " + match.groups() + " " + match.toString());
            }
            // Check for a BASE HREF:
            for (int g = 1; g <= NUM_BASE_GROUPS && g <= match.groups(); g++) {
                s = match.group(g);
                if (s != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("new baseUrl: " + s + " - " + baseUrl.toString());
                    }
                    try {
                        baseUrl = ConversionUtils.makeRelativeURL(baseUrl, s);
                    } catch (MalformedURLException e) {
                        // Maybe it isn't: Ignore the exception.
                        if (log.isDebugEnabled()) {
                            log.debug("Can't build base URL from RL " + s + " in page " + baseUrl, e);
                        }
                    }
                }
            }
            for (int g = NUM_BASE_GROUPS + 1; g <= match.groups(); g++) {
                s = match.group(g);
                if (s != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("group " + g + " - " + match.group(g));
                    }
                    urls.addURL(s, baseUrl);
                }
            }
        }
        return urls.iterator();
    } catch (UnsupportedEncodingException | MalformedCachePatternException e) {
        throw new HTMLParseException(e.getMessage(), e);
    } finally {
        JMeterUtils.clearMatcherMemory(matcher, pattern);
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MalformedURLException(java.net.MalformedURLException) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) MalformedCachePatternException(org.apache.oro.text.MalformedCachePatternException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 9 with PatternMatcherInput

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

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

the class RegexExtractor method matchStrings.

private int matchStrings(int matchNumber, Perl5Matcher matcher, Pattern pattern, List<MatchResult> matches, int found, String inputString) {
    PatternMatcherInput input = new PatternMatcherInput(inputString);
    while (matchNumber <= 0 || found != matchNumber) {
        if (matcher.contains(input, pattern)) {
            log.debug("RegexExtractor: Match found!");
            matches.add(matcher.getMatch());
            found++;
        } else {
            break;
        }
    }
    return found;
}
Also used : PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput)

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