View Javadoc

1   /*
2    Copyright 2006 Ernest Micklei @ PhilemonWorks.com
3   
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7   
8    http://www.apache.org/licenses/LICENSE-2.0
9   
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15  
16   */
17  package com.philemonworks.selfdiagnose.check;
18  
19  import java.util.Locale;
20  
21  import org.xml.sax.Attributes;
22  
23  import com.philemonworks.selfdiagnose.DiagnoseException;
24  import com.philemonworks.selfdiagnose.DiagnoseUtil;
25  import com.philemonworks.selfdiagnose.DiagnosticTaskResult;
26  import com.philemonworks.selfdiagnose.ExecutionContext;
27  /**
28   * CheckBeanProperty is a DiagnosticTask that checks a property and matches its (String) value to a regular expression pattern.
29   * This task can only be used together with another task that provides the bean by inserting a variable into the execution context. 
30   * <p/>
31   * <pre>
32  &lt;checkbeanproperty bean="${name of var}" property="attribute name" pattern="regular expression" /&gt;
33  &lt;checkbeanproperty bean="${name of var}" method="toString"  pattern="regular expression" /&gt;
34  &lt;checkbeanproperty bean="${name of var}" pattern="regular expression" /&gt;
35   * </pre>
36   * Stores the property value into the (optional) specified variable.  
37   * @author Ernest Micklei
38   */
39  public class CheckBeanProperty extends CheckProperty {
40      private static final long serialVersionUID = -7976546053356912993L;
41      protected static final String PARAMETER_METHOD = "method";
42  	protected static final String PARAMETER_BEAN = "bean";	
43  	// optional parameter
44  	private String method;
45  	// context variable name
46  	private String bean;
47  	/**
48  	 * Return the description of this task.
49  	 */
50  	public String getDescription(){
51  		return "Check whether the value (or its String representation )of a bean property matches a pattern";
52  	}
53  	
54  	public void initializeFromAttributes(Attributes attributes) {
55  		super.initializeFromAttributes(attributes);
56  		this.setMethod(attributes.getValue(PARAMETER_METHOD));
57  		this.setBean(attributes.getValue(PARAMETER_BEAN));
58  	}
59  
60  	public void setUp(ExecutionContext ctx) throws DiagnoseException {
61  		super.setUp(ctx);
62  		DiagnoseUtil.verifyNotNull(PARAMETER_BEAN, bean, CheckBeanProperty.class);
63  	}
64  	// Property can be null because bean can be a String
65  	protected void checkPropertyAccess() throws DiagnoseException {}	
66  	
67  	public void run(ExecutionContext ctx, DiagnosticTaskResult result)
68  			throws DiagnoseException {
69  		Object beanValue = ctx.resolveValue(this.getBean());
70  		DiagnoseUtil.verifyNotNull("bean", beanValue, CheckBeanProperty.class);		
71  
72  		Object value = beanValue;
73  		if (isMessagePerformRequired())
74  			value = DiagnoseUtil.perform(beanValue, this.constructGetter(), new Object[0]);
75  		ctx.setValue(this.getVariableName(), value);
76  		checkValueAgainstPattern(result,"Bean", "property", this.constructGetter(), value);
77  	}
78  
79      private boolean isMessagePerformRequired() {
80  		return property != null || method != null;
81  	}
82  
83  	public String constructGetter(){
84  		if (method != null) 
85  			return method;
86  		if (property == null)
87  			return "this";
88   		String getter = "get"
89  			+ (String.valueOf(this.getProperty().charAt(0)).toUpperCase(Locale.getDefault()))
90  			+ this.getProperty().substring(1);
91  		return getter;
92  	}
93  	
94  	
95  	public String getMethod() {
96  		return method;
97  	}
98  
99  	public void setMethod(String method) {
100 		this.method = method;
101 	}
102 
103 	public void setBean(String aString){
104 		bean = aString;
105 	}
106 	public String getBean(){
107 		return bean;
108 	}
109 }