01: package org.jreform.internal;
02:
03: import javax.servlet.http.HttpServletRequest;
04:
05: import org.jreform.Criterion;
06: import org.jreform.Input;
07: import org.jreform.InputDataType;
08:
09: /**
10: * @author armandino (at) gmail.com
11: */
12: class InputImpl<T> extends AbstractInputControl<T> implements Input<T> {
13: private T value;
14: private String valueAttribute;
15:
16: InputImpl(InputDataType<T> type, String name,
17: Criterion<T>... criteria) {
18: super (type, name, criteria);
19: }
20:
21: public final T getValue() {
22: return value;
23: }
24:
25: public final void setValue(T value) {
26: this .value = value;
27: }
28:
29: /**
30: * Returns the <tt>value</tt> attribute or an empty string
31: * if it's <code>null</code>.
32: */
33: public final String getValueAttribute() {
34: return valueAttribute == null ? "" : valueAttribute;
35: }
36:
37: /**
38: * Sets value attribute trimming leading/trailing whitespace.
39: */
40: public void setValueAttribute(String input) {
41: valueAttribute = input == null ? null : input.trim();
42: }
43:
44: public final boolean isBlank() {
45: return valueAttribute == null || valueAttribute.equals("");
46: }
47:
48: /**
49: * Returns the string representation of the value as returned
50: * by <tt>getValue().toString()</tt> or an empty string if the
51: * value is <code>null</code>.
52: */
53: public final String getStringValue() {
54: return value == null ? "" : value.toString();
55: }
56:
57: boolean validate(HttpServletRequest req) {
58: String value = req.getParameter(getInputName());
59: setValueAttribute(value);
60:
61: ValueAttributeValidator<T> validator = new ValueAttributeValidator<T>(
62: this );
63: ValidationResult<T> result = validator
64: .validate(getValueAttribute());
65:
66: setValue(result.getParsedValue());
67: setValid(result.isValid());
68: setOnError(result.getErrorMessage());
69:
70: return isValid();
71: }
72:
73: public final String toString() {
74: return getValueAttribute() == null ? "" : getValueAttribute();
75: }
76:
77: }
|