Search in sources :

Example 56 with Pattern

use of org.apache.oro.text.regex.Pattern in project xwiki-platform by xwiki.

the class Util method getAllMatches.

public List<String> getAllMatches(String content, String spattern, int group) throws MalformedPatternException {
    List<String> list = new ArrayList<String>();
    PatternMatcherInput input = new PatternMatcherInput(content);
    Pattern pattern = patterns.addPattern(spattern);
    while (this.matcher.contains(input, pattern)) {
        MatchResult result = this.matcher.getMatch();
        String smatch = result.group(group);
        list.add(smatch);
    }
    return list;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) PatternMatcherInput(org.apache.oro.text.regex.PatternMatcherInput) ArrayList(java.util.ArrayList) MatchResult(org.apache.oro.text.regex.MatchResult)

Example 57 with Pattern

use of org.apache.oro.text.regex.Pattern in project photon-model by vmware.

the class Recurser method matches.

private boolean matches(String pattern, String path) throws FinderException {
    Pattern compiled;
    try {
        compiled = this.globCompiler.compile(pattern);
    } catch (MalformedPatternException e) {
        throw new FinderException("Bad glob pattern: " + pattern, e);
    }
    path = basename(path);
    return this.matcher.matches(path, compiled);
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 58 with Pattern

use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.

the class UtilHttp method checkURLforSpiders.

/**
 * checks, if the current request comes from a searchbot
 *
 * @param request
 * @return whether the request is from a web searchbot
 */
public static boolean checkURLforSpiders(HttpServletRequest request) {
    boolean result = false;
    String spiderRequest = (String) request.getAttribute("_REQUEST_FROM_SPIDER_");
    if (UtilValidate.isNotEmpty(spiderRequest)) {
        if ("Y".equals(spiderRequest)) {
            return true;
        }
        return false;
    }
    String initialUserAgent = request.getHeader("User-Agent") != null ? request.getHeader("User-Agent") : "";
    List<String> spiderList = StringUtil.split(UtilProperties.getPropertyValue("url", "link.remove_lsessionid.user_agent_list"), ",");
    if (UtilValidate.isNotEmpty(spiderList)) {
        for (String spiderNameElement : spiderList) {
            Pattern pattern = null;
            try {
                pattern = PatternFactory.createOrGetPerl5CompiledPattern(spiderNameElement, false);
            } catch (MalformedPatternException e) {
                Debug.logError(e, module);
            }
            PatternMatcher matcher = new Perl5Matcher();
            if (matcher.contains(initialUserAgent, pattern)) {
                request.setAttribute("_REQUEST_FROM_SPIDER_", "Y");
                result = true;
                break;
            }
        }
    }
    if (!result) {
        request.setAttribute("_REQUEST_FROM_SPIDER_", "N");
    }
    return result;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 59 with Pattern

use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.

the class PatternFactory method createOrGetPerl5CompiledPattern.

/**
 * Compiles and caches a Perl5 regexp pattern for the given string pattern.
 * This would be of no benefits (and may bloat memory usage) if stringPattern is never the same.
 * @param stringPattern a Perl5 pattern string
 * @param caseSensitive case sensitive true/false
 * @return a <code>Pattern</code> instance for the given string pattern
 * @throws MalformedPatternException
 */
public static Pattern createOrGetPerl5CompiledPattern(String stringPattern, boolean caseSensitive) throws MalformedPatternException {
    Pattern pattern = compiledPerl5Patterns.get(stringPattern);
    if (pattern == null) {
        Perl5Compiler compiler = new Perl5Compiler();
        if (caseSensitive) {
            // READ_ONLY_MASK guarantees immutability
            pattern = compiler.compile(stringPattern, Perl5Compiler.READ_ONLY_MASK);
        } else {
            pattern = compiler.compile(stringPattern, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK);
        }
        pattern = compiledPerl5Patterns.putIfAbsentAndGet(stringPattern, pattern);
        if (Debug.verboseOn()) {
            Debug.logVerbose("Compiled and cached the pattern: '" + stringPattern, module);
        }
    }
    return pattern;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler)

Example 60 with Pattern

use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.

the class SeoTransform method seoUrl.

/**
 * Transform a url according to seo pattern regular expressions.
 *
 * @param url , String to do the seo transform
 * @param isAnon , boolean to indicate whether it's an anonymous visit.
 *
 * @return String, the transformed url.
 */
public static String seoUrl(String url, boolean isAnon) {
    Perl5Matcher matcher = new Perl5Matcher();
    if (SeoConfigUtil.checkUseUrlRegexp() && matcher.matches(url, SeoConfigUtil.getGeneralRegexpPattern())) {
        Iterator<String> keys = SeoConfigUtil.getSeoPatterns().keySet().iterator();
        boolean foundMatch = false;
        while (keys.hasNext()) {
            String key = keys.next();
            Pattern pattern = SeoConfigUtil.getSeoPatterns().get(key);
            if (pattern.getPattern().contains(";jsessionid=")) {
                if (isAnon) {
                    if (SeoConfigUtil.isJSessionIdAnonEnabled()) {
                        continue;
                    }
                } else {
                    if (SeoConfigUtil.isJSessionIdUserEnabled()) {
                        continue;
                    }
                    boolean foundException = false;
                    for (int i = 0; i < SeoConfigUtil.getUserExceptionPatterns().size(); i++) {
                        if (matcher.matches(url, SeoConfigUtil.getUserExceptionPatterns().get(i))) {
                            foundException = true;
                            break;
                        }
                    }
                    if (foundException) {
                        continue;
                    }
                }
            }
            String replacement = SeoConfigUtil.getSeoReplacements().get(key);
            if (matcher.matches(url, pattern)) {
                for (int i = 1; i < matcher.getMatch().groups(); i++) {
                    replacement = replacement.replaceAll("\\$" + i, matcher.getMatch().group(i));
                }
                // break if found any matcher
                url = replacement;
                foundMatch = true;
                break;
            }
        }
        if (!foundMatch) {
            if (Debug.verboseOn()) {
                Debug.logVerbose("Can NOT find a seo transform pattern for this url: " + url, module);
            }
        }
    }
    return url;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher)

Aggregations

Pattern (org.apache.oro.text.regex.Pattern)70 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)48 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)27 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)24 MatchResult (org.apache.oro.text.regex.MatchResult)17 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)14 ArrayList (java.util.ArrayList)12 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)9 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)7 IOException (java.io.IOException)6 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)6 MalformedURLException (java.net.MalformedURLException)4 BufferedReader (java.io.BufferedReader)3 FileInputStream (java.io.FileInputStream)3 InputStreamReader (java.io.InputStreamReader)3 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)3 EOFException (java.io.EOFException)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2