1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.formengine.service.validator;
35
36 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.CheckRule;
37 import fr.paris.lutece.portal.service.util.AppException;
38 import fr.paris.lutece.portal.service.util.AppPropertiesService;
39
40
41
42
43
44 public class ValidatorFactory
45 {
46 private static final String PROPERTY_VALIDATOR_PREFIX = "formengine.validator.";
47 private static final String PROPERTY_VALIDATOR_SPECIFIC_TYPE_NAME = "formengine.validator.specific.type.name";
48
49
50
51
52 public ValidatorFactory( )
53 {
54 }
55
56
57
58
59
60
61 public static Validator getValidator( CheckRule rule )
62 {
63 Validator validator = null;
64 String strValidatorClass;
65
66 String strValidatorSpecificRuleType = AppPropertiesService.getProperty( PROPERTY_VALIDATOR_SPECIFIC_TYPE_NAME );
67
68 String strType = rule.getType( );
69 String strParameter = rule.getParameter( );
70
71 try
72 {
73
74 if ( ( strType != null ) && ( strType.trim( ).equals( strValidatorSpecificRuleType ) ) )
75 {
76 strValidatorClass = strParameter;
77 validator = (Validator) Class.forName( strValidatorClass ).newInstance( );
78 }
79
80
81 else
82 {
83 strValidatorClass = AppPropertiesService.getProperty( PROPERTY_VALIDATOR_PREFIX + strType );
84 validator = (Validator) Class.forName( strValidatorClass ).newInstance( );
85 validator.setRuleParameter( rule.getParameter( ) );
86 }
87
88
89 validator.setMin( rule.getMin( ) );
90 validator.setMax( rule.getMax( ) );
91
92
93 if ( ( rule.getErrorMessage( ) != null ) && ( !rule.getErrorMessage( ).trim( ).equals( "" ) ) )
94 {
95 validator.setErrorMessage( rule.getErrorMessage( ) );
96 }
97 else if ( ( rule.getErrorKey( ) != null ) && ( !rule.getErrorKey( ).trim( ).equals( "" ) ) )
98 {
99 String strErrorMessage = AppPropertiesService.getProperty( rule.getErrorKey( ) );
100 validator.setErrorMessage( strErrorMessage );
101 }
102 }
103 catch ( InstantiationException e )
104 {
105 throw new AppException( "Formengine : an error occurred while instancing a validator", e );
106 }
107 catch ( IllegalAccessException e )
108 {
109 throw new AppException( "Formengine : an error occurred while instancing a validator", e );
110 }
111 catch ( ClassNotFoundException e )
112 {
113 throw new AppException( "Formengine : an error occurred while instancing a validator", e );
114 }
115
116 return validator;
117 }
118 }