View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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          // Finding the matching pattern to extract the required data
56          String matched = matcher.getMatchingRegexText(screenText, regex, regexGroup);
57          if (matched != null) {
58              // Setting the matched pattern as a Context variable
59              matcher.setVariable(varName, matched);
60          }else{
61              throw new JameleonException("No match found for ["+regex+"]!");
62          }
63      }
64  }