Search in sources :

Example 6 with PatternCompiler

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

the class NodeQueryCheckUtil method compareNodeTableColumnsWithFunc.

/**
     * 
     * DOC wzhang Comment method "compareNodeTableColumnsWithFunc".
     * 
     * @param node
     * @param columns
     * @return
     */
private static boolean compareNodeTableColumnsWithFunc(Node node, String columns) {
    String originalColumns = columns;
    if (node.getMetadataList().size() == 0) {
        return true;
    }
    IMetadataTable metaTable = node.getMetadataList().get(0);
    if (metaTable == null || metaTable.getListColumns() == null) {
        return true;
    }
    int originColumnSize = metaTable.getListColumns().size();
    // modified by wzhang. replace the field to one String if it contains function
    //$NON-NLS-1$  
    columns = columns.replaceAll(FUNC_SPLIT, "column");
    //$NON-NLS-1$
    String[] columnArray = columns.split(",");
    // columns not match
    if (columnArray.length != originColumnSize) {
        // if can not match , we should match the columns with function
        try {
            PatternCompiler pc = new Perl5Compiler();
            org.apache.oro.text.regex.Pattern pattern = null;
            pattern = pc.compile(SQL_FUNC_REGX, REGX_FLAG);
            PatternMatcher columnMatcher = new Perl5Matcher();
            if (columnMatcher.matches(originalColumns, pattern)) {
                String columnWithFunc = columnMatcher.getMatch().group(4).trim();
                if (columnWithFunc != null) {
                    //$NON-NLS-1$
                    String[] columnWithFuncArray = columnWithFunc.split(",");
                    if (columnWithFuncArray.length > 1) {
                        //$NON-NLS-1$
                        originalColumns = originalColumns.replace(columnWithFunc, "columnWithFunction");
                        return compareNodeTableColumnsWithFunc(node, originalColumns);
                    }
                }
            }
        } catch (MalformedPatternException e) {
            return false;
        }
        return false;
    }
    return true;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IMetadataTable(org.talend.core.model.metadata.IMetadataTable) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Example 7 with PatternCompiler

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

the class ReplaceRunJobLabelVariableMigrationTask method replaceValue.

private boolean replaceValue(NodeType node, boolean replace) {
    if (node == null) {
        return false;
    }
    //$NON-NLS-1$
    ElementParameterType param = ComponentUtilities.getNodeProperty(node, "LABEL");
    if (param != null && param.getField().equals(EParameterFieldType.TEXT.getName())) {
        String value = param.getValue();
        if (value != null) {
            PatternCompiler compiler = new Perl5Compiler();
            Perl5Matcher matcher = new Perl5Matcher();
            try {
                Pattern pattern = compiler.compile(//$NON-NLS-1$
                "\\b(" + "__PROCESS_TYPE_PROCESS__" + //$NON-NLS-1$ //$NON-NLS-2$
                ")(\\b)");
                if (matcher.contains(value, pattern)) {
                    if (replace) {
                        // replace
                        Perl5Substitution substitution = new //$NON-NLS-1$ //$NON-NLS-2$
                        Perl5Substitution(//$NON-NLS-1$ //$NON-NLS-2$
                        "__PROCESS__" + "$2", Perl5Substitution.INTERPOLATE_ALL);
                        String newValue = Util.substitute(matcher, pattern, substitution, value, Util.SUBSTITUTE_ALL);
                        param.setValue(newValue);
                    }
                    return true;
                }
            } catch (MalformedPatternException e) {
            //
            }
        }
    }
    return false;
}
Also used : ElementParameterType(org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 8 with PatternCompiler

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

the class VirtualRowGeneratorNode method renameValues.

// add for bug 9471
private String renameValues(final String value, final String oldName, final String newName) {
    if (value == null || oldName == null || newName == null) {
        // keep original value
        return value;
    }
    PatternCompiler compiler = new Perl5Compiler();
    Perl5Matcher matcher = new Perl5Matcher();
    matcher.setMultiline(true);
    Perl5Substitution substitution = new //$NON-NLS-1$
    Perl5Substitution(//$NON-NLS-1$
    newName + "$2", Perl5Substitution.INTERPOLATE_ALL);
    Pattern pattern;
    try {
        pattern = compiler.compile(//$NON-NLS-1$
        "\\b(" + UpdateContextVariablesHelper.replaceSpecialChar(oldName) + //$NON-NLS-1$
        ")(\\b|\\_)");
    } catch (MalformedPatternException e) {
        // keep original value
        return value;
    }
    if (matcher.contains(value, pattern)) {
        // replace
        String returnValue = Util.substitute(matcher, pattern, substitution, value, Util.SUBSTITUTE_ALL);
        return returnValue;
    }
    // keep original value
    return value;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Aggregations

MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)8 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)8 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)8 Pattern (org.apache.oro.text.regex.Pattern)6 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)6 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)5 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)2 BaseOtterTest (com.alibaba.otter.shared.common.BaseOtterTest)1 ConfigException (com.alibaba.otter.shared.common.model.config.ConfigException)1 StringProperty (org.apache.jmeter.testelement.property.StringProperty)1 StringSubstitution (org.apache.oro.text.regex.StringSubstitution)1 IMetadataTable (org.talend.core.model.metadata.IMetadataTable)1 ElementParameterType (org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType)1 Test (org.testng.annotations.Test)1