Search in sources :

Example 66 with Perl5Matcher

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

the class JSONFileOutputStep1Form method addFieldsListeners.

@Override
protected void addFieldsListeners() {
    jsonFilePath.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent event) {
            if (jsonFilePath.getResult() == null) {
                return;
            }
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            // getConnection().setJSONFilePath(PathUtils.getPortablePath(JSONXsdFilePath.getText()));
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(text);
            }
            File file = new File(text);
            if (file.exists()) {
                List<ATreeNode> treeNodes = new ArrayList<ATreeNode>();
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                checkFieldsValue();
                if (!valid) {
                    return;
                }
                if (treeNodes.size() > 0) {
                    treeNode = treeNodes.get(0);
                }
                updateConnection(text);
            }
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    jsonFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent event) {
            String text = jsonFilePath.getText();
            if (isContextMode()) {
                ContextType contextType = ConnectionContextHelper.getContextTypeForContextMode(connectionItem.getConnection(), true);
                text = TalendQuoteUtils.removeQuotes(ConnectionContextHelper.getOriginalValue(contextType, text));
            }
            if (getConnection().getJSONFilePath() != null && !getConnection().getJSONFilePath().equals(text)) {
                getConnection().getLoop().clear();
                getConnection().getRoot().clear();
                getConnection().getGroup().clear();
                xsdPathChanged = true;
            } else {
                xsdPathChanged = false;
            }
            if (Path.fromOSString(jsonFilePath.getText()).toFile().isFile()) {
                getConnection().setJSONFilePath(PathUtils.getPortablePath(jsonFilePath.getText()));
            } else {
                getConnection().setJSONFilePath(jsonFilePath.getText());
            }
            // updateConnection(text);
            StringBuilder fileContent = new StringBuilder();
            BufferedReader in = null;
            File file = null;
            if (tempJSONPath != null && getConnection().getFileContent() != null && getConnection().getFileContent().length > 0 && !isModifing) {
                file = new File(tempJSONPath);
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e2) {
                        ExceptionHandler.process(e2);
                    }
                    FileOutputStream outStream;
                    try {
                        outStream = new FileOutputStream(file);
                        outStream.write(getConnection().getFileContent());
                        outStream.close();
                    } catch (FileNotFoundException e1) {
                        ExceptionHandler.process(e1);
                    } catch (IOException e) {
                        ExceptionHandler.process(e);
                    }
                }
            } else {
                file = new File(text);
            }
            String str;
            try {
                Charset guessCharset = CharsetToolkit.guessEncoding(file, 4096);
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), guessCharset.displayName()));
                while ((str = in.readLine()) != null) {
                    fileContent.append(str + "\n");
                    // for encoding
                    if (str.contains("encoding")) {
                        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);
                        }
                    }
                }
                fileContentText.setText(new String(fileContent));
            } catch (Exception e) {
                String msgError = "File" + " \"" + jsonFilePath.getText().replace("\\\\", "\\") + "\"\n";
                if (e instanceof FileNotFoundException) {
                    msgError = msgError + "is not found";
                } else if (e instanceof EOFException) {
                    msgError = msgError + "have an incorrect character EOF";
                } else if (e instanceof IOException) {
                    msgError = msgError + "is locked by another soft";
                } else {
                    msgError = msgError + "doesn't exist";
                }
                fileContentText.setText(msgError);
                if (!isReadOnly()) {
                    updateStatus(IStatus.ERROR, msgError);
                }
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception exception) {
                    ExceptionHandler.process(exception);
                }
            }
            if (getConnection().getEncoding() == null || "".equals(getConnection().getEncoding())) {
                getConnection().setEncoding(encoding);
                if (encoding != null && !"".equals(encoding)) {
                    encodingCombo.setText(encoding);
                } else {
                    encodingCombo.setText("UTF-8");
                }
            }
            // }
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONUtil.changeJsonToXml(text), treeNode);
                updateConnection(text);
            }
            checkFieldsValue();
            isModifing = true;
        }
    });
    encodingCombo.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setEncoding(encodingCombo.getText());
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            String str = commonNodesLimitation.getText();
            if ((!str.matches("\\d+")) || (Integer.valueOf(str) < 0)) {
                commonNodesLimitation.setText(String.valueOf(treePopulator.getLimit()));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
            } else {
                treePopulator.setLimit(Integer.valueOf(str));
                labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, str));
            }
            if (JSONFileOutputStep1Form.this.tempPath == null) {
                JSONFileOutputStep1Form.this.tempPath = JSONUtil.changeJsonToXml(jsonFilePath.getText());
            }
            File file = new File(JSONFileOutputStep1Form.this.tempPath);
            if (file.exists()) {
                valid = treePopulator.populateTree(JSONFileOutputStep1Form.this.tempPath, treeNode);
            }
            checkFieldsValue();
        }
    });
    commonNodesLimitation.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
        }

        @Override
        public void focusLost(FocusEvent e) {
            commonNodesLimitation.setText(String.valueOf(TreePopulator.getLimit()));
            labelLimitation.setToolTipText(MessageFormat.format(Messages.JSONLimitToolTip, commonNodesLimitation.getText()));
        }
    });
    outputFilePath.addModifyListener(new ModifyListener() {

        @Override
        public void modifyText(ModifyEvent e) {
            getConnection().setOutputFilePath(PathUtils.getPortablePath(outputFilePath.getText()));
            checkFieldsValue();
        }
    });
}
Also used : ModifyListener(org.eclipse.swt.events.ModifyListener) FileNotFoundException(java.io.FileNotFoundException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MatchResult(org.apache.oro.text.regex.MatchResult) FocusEvent(org.eclipse.swt.events.FocusEvent) ModifyEvent(org.eclipse.swt.events.ModifyEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EOFException(java.io.EOFException) List(java.util.List) ArrayList(java.util.ArrayList) EList(org.eclipse.emf.common.util.EList) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) ContextType(org.talend.designer.core.model.utils.emf.talendfile.ContextType) Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Charset(java.nio.charset.Charset) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PersistenceException(org.talend.commons.exception.PersistenceException) ATreeNode(org.talend.datatools.xml.utils.ATreeNode) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) RecordFile(orgomg.cwm.resource.record.RecordFile) File(java.io.File) FocusListener(org.eclipse.swt.events.FocusListener) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 67 with Perl5Matcher

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

Example 68 with Perl5Matcher

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

the class InputDataMapTableView method checkValidName.

/**
     * Check if the given name will be unique in the process. If already exists with that name, false will be returned.
     * 
     * @param uniqueName
     * @return true if the name is unique
     */
public boolean checkValidName(String name) {
    for (ITableEntry entry : getInputTable().getGlobalMapEntries()) {
        if (TalendQuoteUtils.removeQuotesIfExist(entry.getName()).equals(name)) {
            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(name, 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 69 with Perl5Matcher

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

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

Aggregations

Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)102 Pattern (org.apache.oro.text.regex.Pattern)78 MatchResult (org.apache.oro.text.regex.MatchResult)39 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)38 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)37 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)32 PatternMatcherInput (org.apache.oro.text.regex.PatternMatcherInput)22 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)20 ArrayList (java.util.ArrayList)17 IOException (java.io.IOException)8 MalformedCachePatternException (org.apache.oro.text.MalformedCachePatternException)7 Perl5Substitution (org.apache.oro.text.regex.Perl5Substitution)7 MalformedURLException (java.net.MalformedURLException)5 PatternCacheLRU (org.apache.oro.text.PatternCacheLRU)5 PluginException (org.apache.wiki.api.exceptions.PluginException)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 NoSuchElementException (java.util.NoSuchElementException)3 SampleResult (org.apache.jmeter.samplers.SampleResult)3 BufferedReader (java.io.BufferedReader)2 EOFException (java.io.EOFException)2