Reputation: 13
Hi I am adding a column level filter to a tableviewer, which will have special characters in the column being filtered. That column will contain xpath string as data, which obviously will contain special characters like the below,
xpath=//a[@name='panels:swappableContent:toggleButton']/span[@class='standardized-icon expandIcon']
I have created a filter object, code is given below,
public class ObjectLocatorFilter extends ViewerFilter {
private String searchString;
public void setSearchText(String string) {
// ensure that the value can be used for matching
this.searchString = ".*" + string + ".*";
}
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if(searchString == null || searchString.length() == 0){
return true;
}
Element elem = (Element) element;
if(elem.getObjectLocator().matches(searchString)){
return true;
}
return false;
}
}
and added the filter to the table viewer, using the below code,
objectLocatorFilter = new ObjectLocatorFilter();
tableViewerPageObjects.addFilter(objectLocatorFilter);
tableViewerPageObjects.refresh();
but when tried to search with text like [@class='', it results with below exception,
java.util.regex.PatternSyntaxException: Unclosed character class near index 13
.*[@class=''.*
^
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.clazz(Pattern.java:2548)
Pls guide me on how to add resolve this issue. thanks in advance.
Upvotes: 0
Views: 324
Reputation: 111142
If you don't want Pattern
to handle special characters in your search string you must quote
it:
this.searchString = ".*" + Pattern.quote(string) + ".*";
But since you are just looking for the search string in the element you don't really need a regular expression at all. Just use the contains
method of String
:
if (stringToSearch.contains(stringToFind)) {
....
}
Upvotes: 0