Search in sources :

Example 1 with Perl5Substitution

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

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

the class AbstractMapComponent method getRenameSubstitution.

protected final Perl5Substitution getRenameSubstitution(String newName) {
    if (substitutionsCache.containsKey(newName)) {
        return substitutionsCache.get(newName);
    }
    //$NON-NLS-1$
    Perl5Substitution ps = new Perl5Substitution(newName + "$2", Perl5Substitution.INTERPOLATE_ALL);
    substitutionsCache.put(newName, ps);
    return ps;
}
Also used : Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution)

Example 3 with Perl5Substitution

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

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

the class XmlMapExpressionManager method replaceExpression.

public String replaceExpression(String expression, TableEntryLocation oldLocation, TableEntryLocation newLocation) {
    String returnedExpression = expression;
    boolean needPrefixAndSuffix = oldLocation.prefix != null && !"".equals(oldLocation.prefix) && oldLocation.sufix != null && !"".equals(oldLocation.sufix);
    String tempPattern = StringHelper.replacePrms(SUBS_PATTERN_FOR_REPLACE_LOCATION, new Object[] { needPrefixAndSuffix ? (XmlMapUtil.DOUBLE_ESCAPE + oldLocation.prefix) : "", oldLocation.tableName, oldLocation.columnValue, needPrefixAndSuffix ? (XmlMapUtil.DOUBLE_ESCAPE + oldLocation.sufix) : "" });
    recompilePatternIfNecessary(tempPattern);
    if (returnedExpression != null) {
        matcher.setMultiline(true);
        Perl5Substitution substitution = new Perl5Substitution(oldLocation.prefix + "$1" + newLocation.tableName + "$2" + XmlMapUtil.EXPRESSION_SEPARATOR_SPLIT + "$3" + newLocation.columnValue + "$4" + oldLocation.sufix, Perl5Substitution.INTERPOLATE_ALL);
        returnedExpression = Util.substitute(matcher, pattern, substitution, returnedExpression, Util.SUBSTITUTE_ALL);
    }
    return returnedExpression;
}
Also used : Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution)

Example 5 with Perl5Substitution

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

the class DataMapExpressionParser method addRefArrayPointer.

public String addRefArrayPointer(String expression, TableEntryLocation[] locations) {
    String returnedExpression = expression;
    PerlLanguage perlLanguage = (PerlLanguage) language;
    for (TableEntryLocation location : locations) {
        recompilePatternIfNecessary(StringHelper.replacePrms(perlLanguage.getSubstPatternToAddRefArrayPointer(), new Object[] { location.tableName }));
        if (returnedExpression != null) {
            matcher.setMultiline(true);
            Perl5Substitution substitution = new Perl5Substitution(//$NON-NLS-1$
            language.getPrefixTableRegexp() + "$1->" + perlLanguage.getPrefixFieldRegexp() + "$2" + perlLanguage.getSuffixFieldRegexp(), //$NON-NLS-1$
            Perl5Substitution.INTERPOLATE_ALL);
            returnedExpression = substitute(matcher, pattern, substitution, returnedExpression, Util.SUBSTITUTE_ALL);
        }
    }
    return returnedExpression;
}
Also used : Perl5Substitution(org.apache.oro.text.regex.Perl5Substitution) TableEntryLocation(org.talend.designer.mapper.model.tableentry.TableEntryLocation) PerlLanguage(org.talend.designer.mapper.language.perl.PerlLanguage)

Aggregations

Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)13 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)5 Pattern (org.apache.oro.text.regex.Pattern)4 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)3 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)3 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)2 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)2 TableEntryLocation (org.talend.designer.mapper.model.tableentry.TableEntryLocation)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)1 CoreException (org.eclipse.core.runtime.CoreException)1 SystemException (org.talend.commons.exception.SystemException)1 ElementParameterType (org.talend.designer.core.model.utils.emf.talendfile.ElementParameterType)1 TableEntryLocation (org.talend.designer.dbmap.model.tableentry.TableEntryLocation)1 ExternalMapperTable (org.talend.designer.mapper.external.data.ExternalMapperTable)1 ExternalMapperTableEntry (org.talend.designer.mapper.external.data.ExternalMapperTableEntry)1 PerlLanguage (org.talend.designer.mapper.language.perl.PerlLanguage)1