From bdeb2de3d3097b8bb8b1ceec36a0d8341c1c0ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horv=C3=A1th=20D=C3=A1vid?= Date: Fri, 14 May 2021 13:50:36 +0200 Subject: [PATCH] adding regex pattern validation to string rule creator --- .../gui/rulecreator/StringRuleCreator.java | 32 ++++++++++++++++--- .../org.eclipse.sed.ifl.feature/feature.xml | 2 +- .../org.eclipse.sed.ifl.update/category.xml | 2 +- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/org.eclipse.sed.ifl.root/bundles/org.eclipse.sed.ifl/src/org/eclipse/sed/ifl/ide/gui/rulecreator/StringRuleCreator.java b/org.eclipse.sed.ifl.root/bundles/org.eclipse.sed.ifl/src/org/eclipse/sed/ifl/ide/gui/rulecreator/StringRuleCreator.java index ad871457..354f0019 100644 --- a/org.eclipse.sed.ifl.root/bundles/org.eclipse.sed.ifl/src/org/eclipse/sed/ifl/ide/gui/rulecreator/StringRuleCreator.java +++ b/org.eclipse.sed.ifl.root/bundles/org.eclipse.sed.ifl/src/org/eclipse/sed/ifl/ide/gui/rulecreator/StringRuleCreator.java @@ -2,6 +2,10 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; + +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.sed.ifl.control.score.filter.Rule; import org.eclipse.sed.ifl.control.score.filter.StringRule; @@ -111,15 +115,33 @@ public void widgetDefaultSelected(SelectionEvent e) { } - @Override - public Rule getRule() { + private boolean isInputValid(String input) { + //checking if input is empty or consists of only whitespace chars String validation = text.getText().trim(); validation.replaceAll("\\s+",""); - if(!validation.equals("")) { - return new StringRule(this.domain, text.getText(), matchingButton.getSelection(), caseButton.getSelection(), regexButton.getSelection()); - } else { + if(validation.equals("")) { MessageDialog.open(MessageDialog.ERROR, null, "Empty string input", "The provided input is an empty string." + " Please enter a string that is not empty.", SWT.NONE); + return false; + } + //checking if regex is valid + if (regexButton.getSelection()) { + try { + Pattern.compile(input); + } catch (PatternSyntaxException e) { + MessageDialog.open(MessageDialog.ERROR, null, "Invalid regular expression", "The provided regular expression is invalid." + + " Please enter a valid regular expression.", SWT.NONE); + return false; + } + } + return true; + } + + @Override + public Rule getRule() { + if(isInputValid(text.getText())) { + return new StringRule(this.domain, text.getText(), matchingButton.getSelection(), caseButton.getSelection(), regexButton.getSelection()); + } else { return null; } } diff --git a/org.eclipse.sed.ifl.root/features/org.eclipse.sed.ifl.feature/feature.xml b/org.eclipse.sed.ifl.root/features/org.eclipse.sed.ifl.feature/feature.xml index 5222a34a..f2029b41 100644 --- a/org.eclipse.sed.ifl.root/features/org.eclipse.sed.ifl.feature/feature.xml +++ b/org.eclipse.sed.ifl.root/features/org.eclipse.sed.ifl.feature/feature.xml @@ -2,7 +2,7 @@ diff --git a/org.eclipse.sed.ifl.root/releng/org.eclipse.sed.ifl.update/category.xml b/org.eclipse.sed.ifl.root/releng/org.eclipse.sed.ifl.update/category.xml index d67f45e8..0749e1f2 100644 --- a/org.eclipse.sed.ifl.root/releng/org.eclipse.sed.ifl.update/category.xml +++ b/org.eclipse.sed.ifl.root/releng/org.eclipse.sed.ifl.update/category.xml @@ -1,6 +1,6 @@ - +