View Javadoc

1   package gov.nasa.pds.ltdt.testLabel;
2   
3   import gov.nasa.pds.tools.dict.ElementDefinition;
4   import gov.nasa.pds.tools.dict.parser.UnknownDefinitionException;
5   import gov.nasa.pds.tools.dict.type.InvalidTypeException;
6   import gov.nasa.pds.tools.dict.type.UnsupportedTypeException;
7   
8   /***
9    * Class to handle variables found in label templates. Variables are
10   * recognized by a ${xxx} notation, where xxx represents the
11   * variable name.
12   * 
13   * @author mcayanan
14   *
15   */
16  public class Variable {
17  	private final static String VARIABLE = "(//$//{//w*//})";
18  	private final static String QUOTEDVARIABLE = "(\"//$//{//w*//}\")";
19  	
20  	/***
21  	 * Constructor
22  	 * 
23  	 */
24  	public Variable() {}
25  	
26  	/***
27  	 * Replaces variables with an appropriate test value based on the
28  	 * given element definition.
29  	 * 
30  	 * @param statement A statement containing variables.
31  	 * @param definition The element definition.
32  	 * @return A string where the variables are replaced by valid test
33  	 *         values.
34  	 * @throws InvalidTypeException
35  	 * @throws UnsupportedTypeException
36  	 * @throws UnknownDefinitionException 
37  	 */
38  	public String doSubstitution(String statement, ElementDefinition definition)
39  	                 throws InvalidTypeException, UnsupportedTypeException, 
40  	                                              UnknownDefinitionException {
41  		if(definition == null)
42  			throw new UnknownDefinitionException("Missing definition");
43  		
44  		String testValue = ElementTestValueGenerator.getValue(definition).toString();
45  		statement = statement.replaceAll(VARIABLE, testValue);
46  		return statement;
47  	}
48  	
49  	/***
50  	 * Performs substitution for variables in a pointer statement. A
51  	 * variable surrounded in quotes will be replaced by a fictituous
52  	 * file name. Un-quoted variables will be replaced by an integer value
53  	 * of '1'.
54  	 * 
55  	 * @return The pointer statement with all variables substituted with an 
56  	 * appropriate test value.
57  	 */
58  	public String doPointerSubstitution(String pointerStatement) {	
59  		pointerStatement = pointerStatement.replaceAll(QUOTEDVARIABLE,
60  				                                      "\"yourfilename.DAT\"");
61  		pointerStatement = pointerStatement.replaceAll(VARIABLE, "1");
62  		return pointerStatement;
63  	}
64  }