| 1 |
|
package org.trails.validation; |
| 2 |
|
|
| 3 |
|
import org.apache.tapestry.form.translator.DateTranslator; |
| 4 |
|
import org.apache.tapestry.form.translator.NumberTranslator; |
| 5 |
|
import org.apache.tapestry.form.translator.StringTranslator; |
| 6 |
|
import org.apache.tapestry.form.translator.Translator; |
| 7 |
|
import org.apache.tapestry.valid.BaseValidator; |
| 8 |
|
import org.apache.tapestry.valid.IValidator; |
| 9 |
|
import org.apache.tapestry.valid.NumberValidator; |
| 10 |
|
import org.apache.tapestry.valid.StringValidator; |
| 11 |
|
import org.trails.descriptor.IPropertyDescriptor; |
| 12 |
|
|
| 13 |
16 |
public class ValidatorTranslatorService |
| 14 |
|
{ |
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
public IValidator getValidator(IPropertyDescriptor descriptor) |
| 20 |
|
{ |
| 21 |
8 |
BaseValidator validator = null; |
| 22 |
|
|
| 23 |
8 |
if (descriptor.isNumeric()) |
| 24 |
|
{ |
| 25 |
4 |
validator = new NumberValidator(); |
| 26 |
4 |
((NumberValidator) validator).setValueTypeClass(descriptor.getPropertyType()); |
| 27 |
|
} else |
| 28 |
|
{ |
| 29 |
4 |
validator = new StringValidator(); |
| 30 |
|
} |
| 31 |
8 |
validator.setRequired(descriptor.isRequired()); |
| 32 |
8 |
return validator; |
| 33 |
|
} |
| 34 |
|
|
| 35 |
|
public Translator getTranslator(IPropertyDescriptor descriptor) |
| 36 |
|
{ |
| 37 |
12 |
if (descriptor.isNumeric()) |
| 38 |
|
{ |
| 39 |
4 |
NumberTranslator numberTranslator = new NumberTranslator(); |
| 40 |
4 |
if (descriptor.getFormat() != null) numberTranslator.setPattern(descriptor.getFormat()); |
| 41 |
4 |
return numberTranslator; |
| 42 |
8 |
} else if (descriptor.isDate()) |
| 43 |
|
{ |
| 44 |
8 |
DateTranslator dateTranslator = new DateTranslator(); |
| 45 |
8 |
if (descriptor.getFormat() != null) dateTranslator.setPattern(descriptor.getFormat()); |
| 46 |
8 |
return dateTranslator; |
| 47 |
|
} else |
| 48 |
|
{ |
| 49 |
0 |
return new StringTranslator(); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
} |
| 53 |
|
} |