-4

I'd like to use SWTBotTree.expandNode (final String nodeText, final boolean recursive) function with a String that is not always exactly the same. The beginning of the string is the same but the end can be varying. I thought it can be solved with reg. exp. something like: "constant part of the string"+".*" but it was not recognized as a reg. exp. What should I do??

psuzzi
  • 2,187
  • 1
  • 17
  • 21
vdani
  • 27
  • 2
  • 6
    Please show your code. And the proper exception. What do you mean "not recognized as a regexp"? – ppeterka Jan 24 '13 at 14:34
  • how do you apply the regex to the Tree? – THarms Jan 24 '13 at 14:37
  • My code looks like: 'SWTBotTree projs = bot.tree(); 'String ProjectName="ProjectName"; String temp=ProjectName+".*"; SWTBotTreeItem proj = projs.expandNode(temp,false);' I got the following exception: org.eclipse.swtbot.swt.finder.widgets.TimeoutException: Timeout after: 5000 ms.: Could not find node with text ProjectName.* – vdani Jan 24 '13 at 15:33
  • So the question is that how can I tell the SWTBot to expand a Node with a name starting with "ProjectName" and ends with some various characters. – vdani Jan 24 '13 at 15:37

2 Answers2

3

(Somewhat speculative, but hopefully helpful.)

If "constant part of the string" contains characters which have special meaning in a regular expression, that could easily be a problem. The way to solve this is to use Pattern.quote. For example:

// ^ to anchor the match to the start of the line/string. May not be required.
Pattern pattern = Pattern.compile("^" + Pattern.quote(expectedPrefix) + ".*");

Of course I'd only use this if I had to use regular expressions. Otherwise I'd just use:

if (actualText.startsWith(expectedText))
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    +1 Jon Skeet fact in action: Jon Skeet answers a question without actually knowing the problem - and solves it. (in two different ways...) – ppeterka Jan 24 '13 at 14:39
2

I am not sure exactly how you are trying to do this, but if you are doing:

swtBotTree.expandNode("constant part of string" + ".*", true);

Then you are most likely doing it wrong...

The javadoc for expandNode gives no mention that it supports regular expressions...

Looking into the source (just some version I found when searching), expandNode uses another method called getItem(...). That method does something like if (item.getText().equals(nodeText)). In other words, that method uses string equality comparison, not regex matching.

In Java, there is support for regular expressions (take a look at Pattern javadoc and Matcher javadoc or on String.matches(...) javadoc), but only if that functionality is used in the implementation. You cannot pass a regex to a method which expects a normal String and expect it to do regex matching.

Short answer:

Java has classes which provide regex functionality, but the SWTBotTree.expandNode(String, boolean) method does not support regex.

EDIT:

As far as I understand, there is no direct support in SWTBotTree for doing regex/wildcard lookups.

If you desperately want to do this, you could try something like:

 for(SWTBotTreeItem item : swtBotTree.getAllItems()){
     if(item.getText().matches("some regex goes here")){
         item.expand();
     }
 }
Alderath
  • 3,761
  • 1
  • 25
  • 43
  • Thanks for the answer! I thought that the problem is something like that, but I hoped there is a solution for this. – vdani Jan 24 '13 at 15:40
  • @vdani Added a suggestion about how it is probably possible to achieve this. – Alderath Jan 24 '13 at 16:29