Search in sources :

Example 6 with Perl5Compiler

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

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

the class DowngradeParameterHelper method hasParent.

/*
     * check the parent form name
     */
private static boolean hasParent(final String name, final String parent) {
    if (name == null) {
        return false;
    }
    if (parent != null) {
        final String parentExp = parent + COLON;
        if (name.contains(parentExp)) {
            Perl5Matcher matcher = new Perl5Matcher();
            Perl5Compiler compiler = new Perl5Compiler();
            Pattern pattern;
            try {
                //$NON-NLS-1$ //$NON-NLS-2$
                pattern = compiler.compile("\\b(" + parentExp + ")\\b");
                if (matcher.contains(name, pattern)) {
                    return true;
                }
            } catch (MalformedPatternException e) {
            //
            }
        }
    }
    return false;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 8 with Perl5Compiler

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

the class VarsTable method checkValidColumnName.

/**
     * Check if the given name will be unique in the process. If another link already exists with that name, false will
     * be returned.
     * 
     * @param uniqueName
     * @return true if the name is unique
     */
public boolean checkValidColumnName(String connectionName) {
    for (ITableEntry entry : dataMapTableEntries) {
        if (entry.getName().equals(connectionName)) {
            return false;
        }
    }
    Perl5Matcher matcher = new Perl5Matcher();
    Perl5Compiler compiler = new Perl5Compiler();
    Pattern pattern;
    try {
        //$NON-NLS-1$
        pattern = compiler.compile("^[A-Za-z_][A-Za-z0-9_]*$");
        if (!matcher.matches(connectionName, pattern)) {
            return false;
        }
    } catch (MalformedPatternException e) {
        throw new RuntimeException(e);
    }
    return true;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) ITableEntry(org.talend.designer.abstractmap.model.tableentry.ITableEntry) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 9 with Perl5Compiler

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

the class JSONFileStep1Form method addFieldsListeners.

/**
     * Main Fields addControls.
     */
@Override
protected void addFieldsListeners() {
    // fileFieldXsd : Event modifyText
    // fileFieldXsd.addModifyListener(new ModifyListener() {
    //
    // public void modifyText(final ModifyEvent e) {
    // getConnection().setXsdFilePath(fileFieldXsd.getText());
    // treePopulator.populateTree(fileFieldXsd.getText(), treeNode);
    // checkFieldsValue();
    // }
    // });
    // fileFieldJSON.addSelectionListener(new SelectionListener() {
    //
    // public void widgetSelected(SelectionEvent event) {
    // if (fileFieldJSON.getResult() == null) {
    // return;
    // }
    // String text = fileFieldJSON.getText();
    // if (isContextMode()) {
    // ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(
    // connectionItem.getConnection(), true);
    // text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
    // }
    // // getConnection().setJSONFilePath(PathUtils.getPortablePath(JSONXsdFilePath.getText()));
    // File file = new File(text);
    // if (file.exists()) {
    // if (file.exists()) {
    // String tempxml = JSONUtil.changeJsonToXml(text);
    // JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
    // valid = treePopulator.populateTree(tempxml, treeNode);
    // }
    // // add for bug TDI-20432
    // checkFieldsValue();
    // }
    //
    // }
    //
    // public void widgetDefaultSelected(SelectionEvent e) {
    //
    // }
    // });
    readbyCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            EJsonReadbyMode eJsonReadbyMode = EJsonReadbyMode.getEJsonReadbyModeByDisplayName(readbyCombo.getText());
            if (eJsonReadbyMode == null) {
                eJsonReadbyMode = getDefaultJsonReadbyMode();
            }
            String readbyMode = eJsonReadbyMode.getValue();
            JSONFileStep1Form.this.wizard.setReadbyMode(readbyMode);
            String jsonPath = fileFieldJSON.getText();
            String text = validateJsonFilePath(jsonPath);
            if (text == null || text.isEmpty()) {
                return;
            }
            String tempxml = null;
            if (EJsonReadbyMode.JSONPATH.getValue().equals(readbyMode)) {
                tempxml = text;
            } else {
                tempxml = JSONUtil.changeJsonToXml(text);
            }
            JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
            switchPopulator(readbyMode, tempxml);
        }
    });
    // fileFieldJSON : Event modifyText
    fileFieldJSON.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(final ModifyEvent e) {
            String jsonPath = fileFieldJSON.getText();
            String _jsonPath = jsonPath;
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), connectionItem.getConnection().getContextName());
                jsonPath = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, jsonPath));
            }
            String text = validateJsonFilePath(jsonPath);
            if (text == null || text.isEmpty()) {
                return;
            }
            String tempxml = null;
            String readbyMode = JSONFileStep1Form.this.wizard.getReadbyMode();
            if (EJsonReadbyMode.JSONPATH.getValue().equals(readbyMode)) {
                tempxml = text;
            } else {
                tempxml = JSONUtil.changeJsonToXml(text);
            }
            File file = new File(text);
            if (!file.exists()) {
                file = new File(JSONUtil.tempJSONXsdPath);
            }
            JSONFileStep1Form.this.wizard.setTempJsonPath(tempxml);
            String limitString = commonNodesLimitation.getText();
            try {
                limit = Integer.valueOf(limitString);
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, limit));
            } catch (Exception excpt) {
            // nothing need to do
            }
            switchPopulator(readbyMode, tempxml);
            // }
            // add for bug TDI-20432
            checkFieldsValue();
            if (getConnection().getJSONFilePath() != null && !getConnection().getJSONFilePath().equals(text)) {
                getConnection().getLoop().clear();
                xsdPathChanged = true;
            } else {
                xsdPathChanged = false;
            }
            if (isContextMode()) {
                jsonPath = _jsonPath;
            }
            if (Path.fromOSString(jsonPath).toFile().isFile()) {
                getConnection().setJSONFilePath(PathUtils.getPortablePath(jsonPath));
            } else {
                getConnection().setJSONFilePath(jsonPath);
            }
            JSONWizard wizard = ((JSONWizard) getPage().getWizard());
            wizard.setTreeRootNode(treeNode);
            BufferedReader in = null;
            try {
                Charset guessedCharset = CharsetToolkit.guessEncoding(file, 4096);
                String str;
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), guessedCharset.displayName()));
                while ((str = in.readLine()) != null) {
                    if (str.contains("encoding")) {
                        //$NON-NLS-1$
                        //$NON-NLS-1$
                        String regex = "^<\\?JSON\\s*version=\\\"[^\\\"]*\\\"\\s*encoding=\\\"([^\\\"]*)\\\"\\?>$";
                        Perl5Compiler compiler = new Perl5Compiler();
                        Perl5Matcher matcher = new Perl5Matcher();
                        Pattern pattern = null;
                        try {
                            pattern = compiler.compile(regex);
                            if (matcher.contains(str, pattern)) {
                                MatchResult matchResult = matcher.getMatch();
                                if (matchResult != null) {
                                    encoding = matchResult.group(1);
                                }
                            }
                        } catch (MalformedPatternException malE) {
                            ExceptionHandler.process(malE);
                        }
                    }
                }
            } catch (Exception ex) {
                String fileStr = text;
                String msgError = //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                "JSON" + " \"" + fileStr.replace("\\\\", "\\") + //$NON-NLS-1$
                "\"\n";
                if (ex instanceof FileNotFoundException) {
                    msgError = msgError + "is not found";
                } else if (ex instanceof EOFException) {
                    msgError = msgError + "have an incorrect character EOF";
                } else if (ex instanceof IOException) {
                    msgError = msgError + "is locked by another soft";
                } else {
                    msgError = msgError + "doesn't exist";
                }
                if (!isReadOnly()) {
                    updateStatus(IStatus.ERROR, msgError);
                }
            // ExceptionHandler.process(ex);
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception ex2) {
                    ExceptionHandler.process(ex2);
                }
            }
            if (getConnection().getEncoding() == null || "".equals(getConnection().getEncoding())) {
                //$NON-NLS-1$
                getConnection().setEncoding(encoding);
                if (encoding != null && !("").equals(encoding)) {
                    //$NON-NLS-1$
                    encodingCombo.setText(encoding);
                } else {
                    //$NON-NLS-1$
                    encodingCombo.setText("UTF-8");
                }
            }
            // if (tempJSONXsdPath != null && getConnection().getFileContent() != null
            // && getConnection().getFileContent().length > 0 && !isModifing) {
            // valid = treePopulator.populateTree(tempJSONXsdPath, treeNode);
            // } else {
            // valid = treePopulator.populateTree(text, treeNode);
            // }
            checkFieldsValue();
            isModifing = true;
        }
    });
    // Event encodingCombo
    encodingCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(final ModifyEvent e) {
            getConnection().setEncoding(encodingCombo.getText());
            checkFieldsValue();
        }
    });
}
Also used : ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) ModifyListener(org.eclipse.swt.events.ModifyListener) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) Charset(java.nio.charset.Charset) EJsonReadbyMode(org.talend.repository.ui.wizards.metadata.connection.files.json.EJsonReadbyMode) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) MatchResult(org.apache.oro.text.regex.MatchResult) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) BusinessException(org.talend.commons.exception.BusinessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) FileInputStream(java.io.FileInputStream) ModifyEvent(org.eclipse.swt.events.ModifyEvent) BufferedReader(java.io.BufferedReader) EOFException(java.io.EOFException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) File(java.io.File)

Example 10 with Perl5Compiler

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

the class ColumnListController method filterColumns.

/**
     * 
     * <p>
     * You can fill the filter as the format below:
     * </p>
     * <l> <li>CUSTOM_COLUMNS:col1</li> <li>CUSTOM_COLUMNS:col1, col2, ...</li> <li>CUSTOM_COLUMNS:*</li> <li>
     * CUSTOM_COLUMNS:REGEXP:any regular expressions</li> <li>NONE_CUSTOM_COLUMNS:col1</li> <li>
     * NONE_CUSTOM_COLUMNS:col1, col2, ...</li> <li>NONE_CUSTOM_COLUMNS:*</li> <li>NONE_CUSTOM_COLUMNS:REGEXP:any
     * regular expressions</li> <li>REGEXP:any regular expressions</li> <li>col1</li> <li>col1,col2, ...</li> <li>*</li>
     * </l>
     * 
     * <br>
     * You can refer to {@link ColumnListControllerTest } to know how does this method work. </br> <br>
     * DOC ycbai Comment method "filterColumns".
     * 
     * @param param
     * @param curColumnNameList
     * @param curColumnValueList
     * @param customColMap
     * @return
     */
private static List<String[]> filterColumns(IElementParameter param, String[] curColumnNameList, String[] curColumnValueList, Map<String, Boolean> customColMap) {
    List<String[]> updatedColumns = new ArrayList<String[]>();
    String[] columnNameList = curColumnNameList;
    String[] columnValueList = curColumnValueList;
    String filterColumns = param.getFilter();
    if (filterColumns != null) {
        // Hide all filter columns.
        try {
            String[] tmpColumnNameList = new String[curColumnNameList.length];
            System.arraycopy(curColumnNameList, 0, tmpColumnNameList, 0, tmpColumnNameList.length);
            String filter = filterColumns;
            boolean onlyFilterCustom = false;
            if (filter.startsWith(FILTER_PREFIX_CUSTOM)) {
                filter = filter.substring(FILTER_PREFIX_CUSTOM.length());
                onlyFilterCustom = true;
            }
            boolean onlyFilterNoneCustom = false;
            if (filter.startsWith(FILTER_PREFIX_NONE_CUSTOM)) {
                filter = filter.substring(FILTER_PREFIX_NONE_CUSTOM.length());
                onlyFilterNoneCustom = true;
            }
            boolean unlimited = !onlyFilterCustom && !onlyFilterNoneCustom;
            boolean hasReg = false;
            if (filter.startsWith(FILTER_PREFIX_REGEXP)) {
                filter = filter.substring(FILTER_PREFIX_REGEXP.length());
                hasReg = true;
            }
            boolean filterAll = false;
            if (filter.equals(FILTER_ALL)) {
                filterAll = true;
            }
            if (hasReg) {
                Perl5Matcher matcher = new Perl5Matcher();
                Perl5Compiler compiler = new Perl5Compiler();
                Pattern pattern = compiler.compile(filter);
                for (String colName : tmpColumnNameList) {
                    if (!matcher.matches(colName, pattern) && (onlyFilterCustom && customColMap.get(colName) || onlyFilterNoneCustom && !customColMap.get(colName) || unlimited)) {
                        columnNameList = (String[]) ArrayUtils.removeElement(columnNameList, colName);
                        columnValueList = (String[]) ArrayUtils.removeElement(columnValueList, colName);
                    }
                }
            } else {
                if (filterAll) {
                    for (String colName : tmpColumnNameList) {
                        if (onlyFilterCustom && customColMap.get(colName) || onlyFilterNoneCustom && !customColMap.get(colName) || unlimited) {
                            columnNameList = (String[]) ArrayUtils.removeElement(columnNameList, colName);
                            columnValueList = (String[]) ArrayUtils.removeElement(columnValueList, colName);
                        }
                    }
                } else {
                    String[] filterColumnsArray = filter.split(FILTER_SEPARATOR);
                    for (String colName : filterColumnsArray) {
                        if (onlyFilterCustom && customColMap.get(colName) || onlyFilterNoneCustom && !customColMap.get(colName) || unlimited) {
                            columnNameList = (String[]) ArrayUtils.removeElement(columnNameList, colName);
                            columnValueList = (String[]) ArrayUtils.removeElement(columnValueList, colName);
                        }
                    }
                }
            }
        } catch (Exception e) {
            //$NON-NLS-1$
            log.warn(Messages.getString("ColumnListController.invalidRegx", param.getName()), e);
        }
    }
    updatedColumns.add(columnNameList);
    updatedColumns.add(columnValueList);
    return updatedColumns;
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) ArrayList(java.util.ArrayList) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) JSONException(org.talend.utils.json.JSONException)

Aggregations

Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)30 Pattern (org.apache.oro.text.regex.Pattern)26 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)23 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)23 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)10 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)7 MatchResult (org.apache.oro.text.regex.MatchResult)6 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)5 MalformedURLException (java.net.MalformedURLException)4 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)3 ITableEntry (org.talend.designer.abstractmap.model.tableentry.ITableEntry)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStreamReader (java.io.InputStreamReader)2 Charset (java.nio.charset.Charset)2