1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.plugins.oval.validation; |
17 | |
|
18 | |
import java.util.List; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import net.sf.oval.ConstraintViolation; |
22 | |
import net.sf.oval.Validator; |
23 | |
import net.sf.oval.context.ClassContext; |
24 | |
import net.sf.oval.context.ConstructorParameterContext; |
25 | |
import net.sf.oval.context.FieldContext; |
26 | |
import net.sf.oval.context.MethodEntryContext; |
27 | |
import net.sf.oval.context.MethodExitContext; |
28 | |
import net.sf.oval.context.MethodParameterContext; |
29 | |
import net.sf.oval.context.MethodReturnValueContext; |
30 | |
import net.sf.oval.context.OValContext; |
31 | |
|
32 | |
import org.seasar.cubby.action.ActionErrors; |
33 | |
import org.seasar.cubby.action.FieldInfo; |
34 | |
import org.seasar.cubby.spi.ContainerProvider; |
35 | |
import org.seasar.cubby.spi.ProviderFactory; |
36 | |
import org.seasar.cubby.spi.container.Container; |
37 | |
import org.seasar.cubby.spi.container.LookupException; |
38 | |
import org.seasar.cubby.validator.ValidationException; |
39 | |
import org.seasar.cubby.validator.ValidationRule; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public class OValValidationRule implements ValidationRule { |
47 | |
|
48 | |
|
49 | |
private final String resourceKeyPrefix; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public OValValidationRule() { |
55 | 0 | this(null); |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 0 | public OValValidationRule(final String resourceKeyPrefix) { |
65 | 0 | this.resourceKeyPrefix = resourceKeyPrefix; |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void apply(final Map<String, Object[]> params, final Object form, |
72 | |
final ActionErrors errors) throws ValidationException { |
73 | 0 | final OValValidationContext context = OValValidationContext.get(); |
74 | 0 | context.setResourceKeyPrefix(resourceKeyPrefix); |
75 | |
try { |
76 | 0 | final Validator validator = buildValidator(); |
77 | 0 | final List<ConstraintViolation> violations = validator |
78 | |
.validate(form); |
79 | 0 | processViolations(violations, errors); |
80 | |
} finally { |
81 | 0 | OValValidationContext.remove(); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
protected Validator buildValidator() { |
91 | 0 | final Container container = ProviderFactory |
92 | |
.get(ContainerProvider.class).getContainer(); |
93 | |
try { |
94 | 0 | return container.lookup(Validator.class); |
95 | 0 | } catch (final LookupException e) { |
96 | 0 | return new Validator(); |
97 | |
} |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
protected void processViolations( |
112 | |
final List<ConstraintViolation> violations, |
113 | |
final ActionErrors errors) { |
114 | 0 | for (final ConstraintViolation violation : violations) { |
115 | 0 | final String message = violation.getMessage(); |
116 | 0 | final FieldInfo fieldInfo = createFieldInfo(violation.getContext()); |
117 | 0 | if (fieldInfo != null) { |
118 | 0 | errors.add(message, fieldInfo); |
119 | |
} else { |
120 | 0 | errors.add(message); |
121 | |
} |
122 | 0 | } |
123 | 0 | } |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
protected FieldInfo createFieldInfo(final OValContext ovalContext) { |
133 | |
final FieldInfo fieldInfo; |
134 | 0 | if (ovalContext instanceof ClassContext) { |
135 | 0 | fieldInfo = null; |
136 | 0 | } else if (ovalContext instanceof FieldContext) { |
137 | 0 | final FieldContext ctx = (FieldContext) ovalContext; |
138 | 0 | fieldInfo = new FieldInfo(ctx.getField().getName()); |
139 | 0 | } else if (ovalContext instanceof ConstructorParameterContext) { |
140 | 0 | final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext; |
141 | 0 | fieldInfo = new FieldInfo(ctx.getParameterName()); |
142 | 0 | } else if (ovalContext instanceof MethodParameterContext) { |
143 | 0 | final MethodParameterContext ctx = (MethodParameterContext) ovalContext; |
144 | 0 | fieldInfo = new FieldInfo(ctx.getParameterName()); |
145 | 0 | } else if (ovalContext instanceof MethodEntryContext) { |
146 | 0 | final MethodEntryContext ctx = (MethodEntryContext) ovalContext; |
147 | 0 | fieldInfo = new FieldInfo(ctx.getMethod().getName()); |
148 | 0 | } else if (ovalContext instanceof MethodExitContext) { |
149 | 0 | final MethodExitContext ctx = (MethodExitContext) ovalContext; |
150 | 0 | fieldInfo = new FieldInfo(ctx.getMethod().getName()); |
151 | 0 | } else if (ovalContext instanceof MethodReturnValueContext) { |
152 | 0 | final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext; |
153 | 0 | fieldInfo = new FieldInfo(ctx.getMethod().getName()); |
154 | 0 | } else { |
155 | 0 | fieldInfo = null; |
156 | |
} |
157 | |
|
158 | 0 | return fieldInfo; |
159 | |
} |
160 | |
|
161 | |
} |