1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.jameleon.bean;
20
21 import net.sf.jameleon.exception.JameleonException;
22 import net.sf.jameleon.util.TextMatcher;
23
24 /***
25 * This class is used to be a generic regex text matcher helper to be used in
26 * plug-ins wanting the regex (and maybe later the xpath) functionality to match
27 * the given expression and set the matching text in the provided context variable.
28 */
29 public class MatchingTextHelper{
30
31 protected TextMatcher matcher;
32
33 /***
34 * Creates a MatchingTextHelper with a TextMatcher that is used for the following.
35 * <ol>
36 * <li>get the current screen as text.</li>
37 * <li>Get the matching regular expression.</li>
38 * <li>Set the variable in the context.</li>
39 * </ol>
40 * You should find the last two are methods that are already implemented in the FunctionTag.
41 * @param matcher - The interface used to communicate with the plug-in.
42 */
43 public MatchingTextHelper(TextMatcher matcher){
44 this.matcher = matcher;
45 }
46
47 /***
48 * Sets the matching text in the provided context variable.
49 * @param varName - The context variable name
50 * @param regex - The regular with the grouping representing the text to match
51 * @param regexGroup - The number of the group to match.
52 */
53 public void setMatchingTextInContext(String varName, String regex, int regexGroup){
54 String screenText = matcher.getCurrentScreenAsText();
55
56 String matched = matcher.getMatchingRegexText(screenText, regex, regexGroup);
57 if (matched != null) {
58
59 matcher.setVariable(varName, matched);
60 }else{
61 throw new JameleonException("No match found for ["+regex+"]!");
62 }
63 }
64 }