Search in sources :

Example 81 with Perl5Matcher

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

the class PluginContent method parsePluginLine.

/**
 * Parses a plugin invocation and returns a DOM element.
 *
 * @param context     The WikiContext
 * @param commandline The line to parse
 * @param pos         The position in the stream parsing.
 * @return A DOM element
 * @throws PluginException If plugin invocation is faulty
 * @since 2.10.0
 */
public static PluginContent parsePluginLine(final Context context, final String commandline, final int pos) throws PluginException {
    final PatternMatcher matcher = new Perl5Matcher();
    try {
        final PluginManager pm = context.getEngine().getManager(PluginManager.class);
        if (matcher.contains(commandline, pm.getPluginPattern())) {
            final MatchResult res = matcher.getMatch();
            final String plugin = res.group(2);
            final String args = commandline.substring(res.endOffset(0), commandline.length() - (commandline.charAt(commandline.length() - 1) == '}' ? 1 : 0));
            final Map<String, String> arglist = pm.parseArgs(args);
            // set wikitext bounds of plugin as '_bounds' parameter, e.g., [345,396]
            if (pos != -1) {
                final int end = pos + commandline.length() + 2;
                final String bounds = pos + "|" + end;
                arglist.put(PluginManager.PARAM_BOUNDS, bounds);
            }
            return new PluginContent(plugin, arglist);
        }
    } catch (final ClassCastException e) {
        log.error("Invalid type offered in parsing plugin arguments.", e);
        throw new InternalWikiException("Oops, someone offered !String!", e);
    } catch (final NoSuchElementException e) {
        final String msg = "Missing parameter in plugin definition: " + commandline;
        log.warn(msg, e);
        throw new PluginException(msg);
    } catch (final IOException e) {
        final String msg = "Zyrf.  Problems with parsing arguments: " + commandline;
        log.warn(msg, e);
        throw new PluginException(msg);
    }
    return null;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) MatchResult(org.apache.oro.text.regex.MatchResult) InternalWikiException(org.apache.wiki.InternalWikiException) PluginManager(org.apache.wiki.plugin.PluginManager) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) NoSuchElementException(java.util.NoSuchElementException)

Example 82 with Perl5Matcher

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

the class AbstractReferralPlugin method filterCollection.

/**
 *  Filters a collection according to the include and exclude parameters.
 *
 *  @param c The collection to filter.
 *  @return A filtered collection.
 */
protected List<String> filterCollection(final Collection<String> c) {
    final ArrayList<String> result = new ArrayList<>();
    final PatternMatcher pm = new Perl5Matcher();
    for (final String pageName : c) {
        // 
        // If include parameter exists, then by default we include only those
        // pages in it (excluding the ones in the exclude pattern list).
        // 
        // include='*' means the same as no include.
        // 
        boolean includeThis = m_include == null;
        if (m_include != null) {
            for (final Pattern pattern : m_include) {
                if (pm.matches(pageName, pattern)) {
                    includeThis = true;
                    break;
                }
            }
        }
        if (m_exclude != null) {
            for (final Pattern pattern : m_exclude) {
                if (pm.matches(pageName, pattern)) {
                    includeThis = false;
                    // The inner loop, continue on the next item
                    break;
                }
            }
        }
        if (includeThis) {
            result.add(pageName);
            // if we want to show the last modified date of the most recently change page, we keep a "high watermark" here:
            final Page page;
            if (m_lastModified) {
                page = m_engine.getManager(PageManager.class).getPage(pageName);
                if (page != null) {
                    final Date lastModPage = page.getLastModified();
                    log.debug("lastModified Date of page {} : {}", pageName, m_dateLastModified);
                    if (lastModPage.after(m_dateLastModified)) {
                        m_dateLastModified = lastModPage;
                    }
                }
            }
        }
    }
    return result;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) Page(org.apache.wiki.api.core.Page) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) Date(java.util.Date)

Example 83 with Perl5Matcher

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

the class RegexUtil method parser.

public static String[] parser(String src, String regex) {
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return null;
    }
    PatternMatcher matcher = new Perl5Matcher();
    MatchResult result = null;
    String[] tokens = null;
    boolean match = matcher.matches(src, pattern);
    if (match) {
        result = matcher.getMatch();
        int size = result.groups() - 1;
        tokens = new String[size];
        for (int i = 0; i < size; i++) {
            tokens[i] = result.group(i + 1);
        }
    }
    return tokens;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 84 with Perl5Matcher

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

the class RegexUtil method isMatch.

/**
 * 检查src与正则表达式regex匹配结果
 * @param src String
 * @param regex String
 * @return boolean
 */
public static boolean isMatch(String src, String regex) {
    if (src == null)
        return false;
    boolean flag = false;
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return false;
    }
    PatternMatcher matcher = new Perl5Matcher();
    // log.debug("src:" + src);;
    // log.debug("pattern:" + pattern);
    // log.debug("regex:" + regex);
    flag = matcher.matches(src, pattern);
    return flag;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 85 with Perl5Matcher

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

the class RegexUtil method isContain.

public static boolean isContain(String src, String regex) {
    if (src == null)
        return false;
    String patternStr = regex;
    /**
     * 编译正则表达式patternStr,并用该表达式与传入的src字符串进行模式匹配,
     * 如果匹配正确,则从匹配对象中提取出以上定义好的6部分,存放到数组中并返回
     * 该数组
     */
    PatternCompiler compiler = new Perl5Compiler();
    Pattern pattern = null;
    try {
        pattern = compiler.compile(patternStr, default_mask);
    } catch (MalformedPatternException e) {
        e.printStackTrace();
        return false;
    }
    PatternMatcher matcher = new Perl5Matcher();
    return matcher.contains(src, pattern);
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)102 Pattern (org.apache.oro.text.regex.Pattern)78 MatchResult (org.apache.oro.text.regex.MatchResult)39 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)38 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)37 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)32 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)22 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)20 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)8 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)7 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)7 MalformedURLException (java.net.MalformedURLException)5 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchElementException (java.util.NoSuchElementException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2