Search in sources :

Example 11 with MalformedPatternException

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

the class VarsTable method validateColumnName.

/**
     * 
     * DOC amaumont Comment method "validateColumnName".
     * 
     * @param columnName
     * @return true if columnName has a valid value
     */
public String validateColumnName(String columnName, int beanPosition) {
    if (columnName == null) {
        //$NON-NLS-1$
        return Messages.getString("VarsTable.columnNameIsNull");
    }
    Pattern validPatternColumnNameRegexp = null;
    if (validPatternColumnNameRegexp == null) {
        try {
            validPatternColumnNameRegexp = COMPILER.compile(VALID_PATTERN_COLUMN_NAME);
        } catch (MalformedPatternException e) {
            throw new RuntimeException(e);
        }
    }
    Perl5Matcher matcher = new Perl5Matcher();
    boolean match = matcher.matches(columnName, validPatternColumnNameRegexp);
    // System.out.println(columnName + " -> "+ match);
    if (!match) {
        //$NON-NLS-1$ //$NON-NLS-2$
        return Messages.getString("VarsTable.columnNameTip") + columnName + Messages.getString("VarsTable.invalidTip");
    }
    int lstSize = dataMapTableEntries.size();
    for (int i = 0; i < lstSize; i++) {
        if (columnName.equals(dataMapTableEntries.get(i).getName()) && i != beanPosition) {
            //$NON-NLS-1$ //$NON-NLS-2$
            return Messages.getString("VarsTable.columnNameTip") + columnName + Messages.getString("VarsTable.existTip");
        }
    }
    return null;
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException)

Example 12 with MalformedPatternException

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

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

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

use of org.apache.oro.text.regex.MalformedPatternException in project jmeter by apache.

the class ReplaceFunctionsWithStrings method transformValue.

@Override
public JMeterProperty transformValue(JMeterProperty prop) throws InvalidVariableException {
    PatternMatcher pm = JMeterUtils.getMatcher();
    PatternCompiler compiler = new Perl5Compiler();
    String input = prop.getStringValue();
    if (input == null) {
        return prop;
    }
    for (Entry<String, String> entry : getVariables().entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (regexMatch) {
            try {
                Pattern pattern = compiler.compile(constructPattern(value));
                input = Util.substitute(pm, pattern, new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX), input, Util.SUBSTITUTE_ALL);
            } catch (MalformedPatternException e) {
                log.warn("Malformed pattern: {}", value);
            }
        } else {
            input = StringUtilities.substitute(input, value, FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX);
        }
    }
    return new StringProperty(prop.getName(), input);
}
Also used : Perl5Compiler(org.apache.oro.text.regex.Perl5Compiler) Pattern(org.apache.oro.text.regex.Pattern) PatternCompiler(org.apache.oro.text.regex.PatternCompiler) StringProperty(org.apache.jmeter.testelement.property.StringProperty) StringSubstitution(org.apache.oro.text.regex.StringSubstitution) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher)

Aggregations

MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)24 Perl5Compiler (org.apache.oro.text.regex.Perl5Compiler)22 Pattern (org.apache.oro.text.regex.Pattern)20 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)19 PatternCompiler (org.apache.oro.text.regex.PatternCompiler)8 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)5 IOException (java.io.IOException)3 MatchResult (org.apache.oro.text.regex.MatchResult)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 MalformedURLException (java.net.MalformedURLException)2 Charset (java.nio.charset.Charset)2 ModifyEvent (org.eclipse.swt.events.ModifyEvent)2 ModifyListener (org.eclipse.swt.events.ModifyListener)2