use of org.apache.oro.text.regex.Perl5Compiler in project otter by alibaba.
the class ActionProtectedEditor method convertStringToPattern.
/**
* 把字符串转成Pattern和UrlType
*
* @param perl5RegExp
* @return
*/
private ActionPatternHolder convertStringToPattern(String line) {
ActionPatternHolder holder = new ActionPatternHolder();
String[] strs = org.apache.commons.lang.StringUtils.split(line, "|");
if (strs.length != 2) {
throw new IllegalArgumentException("illegal expression: " + line);
}
Pattern compiledPattern;
Perl5Compiler compiler = new Perl5Compiler();
try {
holder.setActionName(strs[0]);
compiledPattern = compiler.compile(strs[0], Perl5Compiler.READ_ONLY_MASK);
holder.setActionPattern(compiledPattern);
} catch (MalformedPatternException mpe) {
throw new IllegalArgumentException("Malformed regular expression: " + strs[0]);
}
try {
holder.setMethodName(strs[1]);
compiledPattern = compiler.compile(strs[1], Perl5Compiler.READ_ONLY_MASK);
holder.setMethodPattern(compiledPattern);
} catch (MalformedPatternException mpe) {
throw new IllegalArgumentException("Malformed regular expression: " + strs[1]);
}
return holder;
}
use of org.apache.oro.text.regex.Perl5Compiler in project otter by alibaba.
the class URLProtectedEditor method convertStringToPattern.
/**
* 把字符串转成Pattern和UrlType
*
* @param perl5RegExp
* @return
*/
private URLPatternHolder convertStringToPattern(String line) {
URLPatternHolder holder = new URLPatternHolder();
Pattern compiledPattern;
Perl5Compiler compiler = new Perl5Compiler();
String perl5RegExp = line;
try {
compiledPattern = compiler.compile(perl5RegExp, Perl5Compiler.READ_ONLY_MASK);
holder.setCompiledPattern(compiledPattern);
} catch (MalformedPatternException mpe) {
throw new IllegalArgumentException("Malformed regular expression: " + perl5RegExp);
}
if (logger.isDebugEnabled()) {
logger.debug("Added regular expression: " + compiledPattern.getPattern().toString());
}
return holder;
}
use of org.apache.oro.text.regex.Perl5Compiler in project intellij-community by JetBrains.
the class ListChooseByNameModel method getTaskPattern.
@Nullable
private Pattern getTaskPattern(String pattern) {
if (!Comparing.strEqual(pattern, myPattern)) {
myCompiledPattern = null;
myPattern = pattern;
}
if (myCompiledPattern == null) {
final String regex = "^.*" + NameUtil.buildRegexp(pattern, 0, true, true);
final Perl5Compiler compiler = new Perl5Compiler();
try {
myCompiledPattern = compiler.compile(regex);
} catch (MalformedPatternException ignored) {
}
}
return myCompiledPattern;
}
use of org.apache.oro.text.regex.Perl5Compiler in project tdi-studio-se by Talend.
the class AbstractMapComponent method getRenamePattern.
protected final Pattern getRenamePattern(String oldName) {
if (patternsCache.containsKey(oldName)) {
return patternsCache.get(oldName);
}
PatternCompiler compiler = new Perl5Compiler();
Pattern pattern = null;
try {
//$NON-NLS-1$ //$NON-NLS-2$
pattern = compiler.compile("\\b(" + UpdateContextVariablesHelper.replaceSpecialChar(oldName) + ")(\\b|\\_)");
patternsCache.put(oldName, pattern);
return pattern;
} catch (MalformedPatternException e) {
ExceptionHandler.process(e);
return null;
}
}
use of org.apache.oro.text.regex.Perl5Compiler 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();
}
});
}
Aggregations