Search in sources :

Example 1 with PatternMatcher

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

the class NodeQueryCheckUtil method apacheRegexMatch.

/**
     * See bug 5836. java.util.regex works too slow here. Use apache oro regex library instead.
     * <p>
     * DOC xye Comment method "apacheRegexMatch".
     * 
     * @param patternString
     * @param flag
     * @param input
     * @return
     */
private static boolean apacheRegexMatch(final String patternString, final int flag, final String input) {
    PatternCompiler pc = new Perl5Compiler();
    org.apache.oro.text.regex.Pattern pattern = null;
    try {
        pattern = pc.compile(patternString, flag);
        PatternMatcher columnMatcher = new Perl5Matcher();
        return columnMatcher.matches(input, pattern);
    } catch (MalformedPatternException e) {
        return false;
    }
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) 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 2 with PatternMatcher

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

the class MapperComponent method hasOrRenameData.

/**
     * 
     * DOC amaumont Comment method "hasOrRenameData".
     * 
     * @param oldName
     * @param newName can be null if <code>renameAction</code> is false
     * @param renameAction true to rename in all expressions, false to get boolean if present in one of the expressions
     * @return
     */
@Override
protected boolean hasOrRenameData(String oldName, String newName, boolean renameAction) {
    if (oldName == null || newName == null && renameAction) {
        throw new NullPointerException();
    }
    if (externalData != null) {
        List<ExternalMapperTable> tables = new ArrayList<ExternalMapperTable>(externalData.getInputTables());
        tables.addAll(externalData.getOutputTables());
        if (externalData.getVarsTables() != null) {
            tables.addAll(externalData.getVarsTables());
        }
        for (ExternalMapperTable table : tables) {
            if (table.getExpressionFilter() != null) {
                Pattern pattern = getRenamePattern(oldName);
                if (pattern != null) {
                    PatternMatcher matcher = new Perl5Matcher();
                    ((Perl5Matcher) matcher).setMultiline(true);
                    if (renameAction) {
                        Perl5Substitution substitution = getRenameSubstitution(newName);
                        String expression = renameDataIntoExpression(pattern, matcher, substitution, table.getExpressionFilter());
                        table.setExpressionFilter(expression);
                    } else {
                        if (hasDataIntoExpression(pattern, matcher, table.getExpressionFilter())) {
                            return true;
                        }
                    }
                }
            }
            List<ExternalMapperTableEntry> metadataTableEntries = table.getMetadataTableEntries();
            if (metadataTableEntries != null) {
                // loop on all entries of current table
                for (ExternalMapperTableEntry entry : metadataTableEntries) {
                    if (hasOrRenameEntry(entry, oldName, newName, renameAction)) {
                        // existed
                        return true;
                    }
                }
            // for (ExternalMapperTableEntry entry : metadataTableEntries) {
            }
            if (table.getConstraintTableEntries() != null) {
                for (ExternalMapperTableEntry entry : table.getConstraintTableEntries()) {
                    if (hasOrRenameEntry(entry, oldName, newName, renameAction)) {
                        // existed
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution) ExternalMapperTableEntry(org.talend.designer.mapper.external.data.ExternalMapperTableEntry) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) ExternalMapperTable(org.talend.designer.mapper.external.data.ExternalMapperTable) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 3 with PatternMatcher

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

the class MapDataDelegateHelper method matchExpression.

private boolean matchExpression(String regex, String expression) {
    PatternCompiler compiler = new Perl5Compiler();
    try {
        //$NON-NLS-1$ //$NON-NLS-2$
        Pattern pattern = compiler.compile("\\b(" + UpdateContextVariablesHelper.replaceSpecialChar(regex) + ")(\\b|\\_)");
        PatternMatcher matcher = new Perl5Matcher();
        ((Perl5Matcher) matcher).setMultiline(true);
        if (matcher.contains(expression, pattern)) {
            return true;
        }
    } catch (MalformedPatternException e) {
    //
    }
    return false;
}
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 4 with PatternMatcher

use of org.apache.oro.text.regex.PatternMatcher 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 Collection filterCollection(Collection c) {
    ArrayList<Object> result = new ArrayList<Object>();
    PatternMatcher pm = new Perl5Matcher();
    for (Iterator i = c.iterator(); i.hasNext(); ) {
        String pageName = null;
        Object objectje = i.next();
        if (objectje instanceof WikiPage) {
            pageName = ((WikiPage) objectje).getName();
        } else {
            pageName = (String) objectje;
        }
        // 
        // 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 (int j = 0; j < m_include.length; j++) {
                if (pm.matches(pageName, m_include[j])) {
                    includeThis = true;
                    break;
                }
            }
        }
        if (m_exclude != null) {
            for (int j = 0; j < m_exclude.length; j++) {
                if (pm.matches(pageName, m_exclude[j])) {
                    includeThis = false;
                    // The inner loop, continue on the next item
                    break;
                }
            }
        }
        if (includeThis) {
            if (objectje instanceof WikiPage) {
                result.add(objectje);
            } else {
                result.add(pageName);
            }
            // 
            // if we want to show the last modified date of the most recently change page, we keep a "high watermark" here:
            WikiPage page = null;
            if (m_lastModified) {
                page = m_engine.getPage(pageName);
                if (page != null) {
                    Date lastModPage = page.getLastModified();
                    if (log.isDebugEnabled()) {
                        log.debug("lastModified Date of page " + pageName + " : " + m_dateLastModified);
                    }
                    if (lastModPage.after(m_dateLastModified)) {
                        m_dateLastModified = lastModPage;
                    }
                }
            }
        }
    }
    return result;
}
Also used : WikiPage(org.apache.wiki.WikiPage) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) Date(java.util.Date)

Example 5 with PatternMatcher

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

the class RegexpCondition method checkCondition.

@Override
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
    Object fieldVal = fieldFma.get(methodContext.getEnvMap());
    if (fieldVal == null) {
        fieldVal = "";
    } else if (!(fieldVal instanceof String)) {
        try {
            fieldVal = MiniLangUtil.convertType(fieldVal, String.class, methodContext.getLocale(), methodContext.getTimeZone(), null);
        } catch (Exception e) {
            throw new MiniLangRuntimeException(e, this);
        }
    }
    String regExp = exprFse.expandString(methodContext.getEnvMap());
    Pattern pattern = null;
    try {
        pattern = PatternFactory.createOrGetPerl5CompiledPattern(regExp, true);
    } catch (MalformedPatternException e) {
        Debug.logError(e, "Regular Expression [" + regExp + "] is mal-formed: " + e.toString(), module);
        throw new MiniLangRuntimeException(e, this);
    }
    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.matches((String) fieldVal, pattern)) {
        // Debug.logInfo("The string [" + fieldVal + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
        return true;
    } else {
        // Debug.logInfo("The string [" + fieldVal + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
        return false;
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Aggregations

PatternMatcher (org.apache.oro.text.regex.PatternMatcher)21 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)15 Pattern (org.apache.oro.text.regex.Pattern)9 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)7 ArrayList (java.util.ArrayList)6 MatchResult (org.apache.oro.text.regex.MatchResult)5 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)5 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)5 Map (java.util.Map)3 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)3 IOException (java.io.IOException)2 NoSuchElementException (java.util.NoSuchElementException)2 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)2 PluginException (org.apache.wiki.api.exceptions.PluginException)2 BaseOtterTest (com.alibaba.otter.shared.common.BaseOtterTest)1 ConfigException (com.alibaba.otter.shared.common.model.config.ConfigException)1 ModeValue (com.alibaba.otter.shared.common.model.config.data.DataMedia.ModeValue)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 ResourceBundle (java.util.ResourceBundle)1