01: /*
02: * Copyright 2006 Dan Shellman
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: package org.iscreen;
17:
18: /**
19: * This abstract class acts as a base class for custom validators. Sub-classes
20: * can extend this class to take advantage of the default implementation this
21: * class provides.
22: *
23: * @author Shellman, Dan
24: */
25: public abstract class BaseValidator implements Validator {
26: protected FailureMessage defaultFailure;
27:
28: public BaseValidator() {
29: } //end BaseValidator()
30:
31: public Object constructBeanToValidate() {
32: return new SimpleBean();
33: } //end constructBeanToValidate()
34:
35: /**
36: * Retrieves the default failure message. If only one failure
37: * is used by the sub-class, then this property can be used to
38: * get the failure message for reporting a failure.
39: *
40: * @return Returns the default failure message.
41: */
42: public FailureMessage getDefaultFailure() {
43: return defaultFailure;
44: } //end getDefaultFailure()
45:
46: /**
47: * Sets the failure message. This is called based upon what's
48: * configured for this validator. Ensure that this is properly
49: * configured: otherwise, the call to getDefaultFailure() will
50: * return null.
51: *
52: * @param failure The default failure message to use.
53: */
54: public void setDefaultFailure(FailureMessage failure) {
55: defaultFailure = failure;
56: } //end setDefaultFailure()
57:
58: // ***
59: // Protected methods
60: // ***
61:
62: /**
63: * Gets the object to validate as a String. This method
64: * assumes the object is of type SimpleBean. If object is null,
65: * this method returns null; otherwise, it returns the value
66: * as a String.<br />
67: * <br />
68: * This method would normally be called by the validate()
69: * method like this: <br />
70: * <code>
71: * public void validate( ValidatorContext context, Object beanToValidate )
72: * {
73: * String str;
74: *
75: * str = getStringValue( beanToValidate );
76: * ...
77: * }
78: * </code>
79: *
80: * @param obj The object being validated.
81: *
82: * @return Returns the object to be validated as a String.
83: */
84: protected String getStringValue(Object obj) {
85: if (obj != null && ((SimpleBean) obj).getValue() != null) {
86: return ((SimpleBean) obj).getValue().toString();
87: }
88:
89: return null;
90: } //end getStringValue()
91: } //end BaseValidator
|