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.lang.reflect.Method; |
19 | |
import java.util.ResourceBundle; |
20 | |
|
21 | |
import net.sf.oval.context.ClassContext; |
22 | |
import net.sf.oval.context.ConstructorParameterContext; |
23 | |
import net.sf.oval.context.FieldContext; |
24 | |
import net.sf.oval.context.MethodEntryContext; |
25 | |
import net.sf.oval.context.MethodExitContext; |
26 | |
import net.sf.oval.context.MethodParameterContext; |
27 | |
import net.sf.oval.context.MethodReturnValueContext; |
28 | |
import net.sf.oval.context.OValContext; |
29 | |
import net.sf.oval.localization.context.OValContextRenderer; |
30 | |
|
31 | |
import org.seasar.cubby.util.Messages; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class RequestLocaleOValContextRenderer implements OValContextRenderer { |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public String render(final OValContext ovalContext) { |
45 | |
final String fieldName; |
46 | 0 | if (ovalContext instanceof ClassContext) { |
47 | 0 | final ClassContext ctx = (ClassContext) ovalContext; |
48 | 0 | fieldName = ctx.getClazz().getSimpleName(); |
49 | 0 | } else if (ovalContext instanceof FieldContext) { |
50 | 0 | final FieldContext ctx = (FieldContext) ovalContext; |
51 | 0 | fieldName = ctx.getField().getName(); |
52 | 0 | } else if (ovalContext instanceof ConstructorParameterContext) { |
53 | 0 | final ConstructorParameterContext ctx = (ConstructorParameterContext) ovalContext; |
54 | 0 | fieldName = ctx.getParameterName(); |
55 | 0 | } else if (ovalContext instanceof MethodParameterContext) { |
56 | 0 | final MethodParameterContext ctx = (MethodParameterContext) ovalContext; |
57 | 0 | fieldName = ctx.getParameterName(); |
58 | 0 | } else if (ovalContext instanceof MethodEntryContext) { |
59 | 0 | final MethodEntryContext ctx = (MethodEntryContext) ovalContext; |
60 | 0 | final Method method = ctx.getMethod(); |
61 | 0 | if (isGetter(method)) { |
62 | 0 | fieldName = toPropertyName(method); |
63 | |
} else { |
64 | 0 | fieldName = method.getName(); |
65 | |
} |
66 | 0 | } else if (ovalContext instanceof MethodExitContext) { |
67 | 0 | final MethodExitContext ctx = (MethodExitContext) ovalContext; |
68 | 0 | final Method method = ctx.getMethod(); |
69 | 0 | if (isGetter(method)) { |
70 | 0 | fieldName = toPropertyName(method); |
71 | |
} else { |
72 | 0 | fieldName = method.getName(); |
73 | |
} |
74 | 0 | } else if (ovalContext instanceof MethodReturnValueContext) { |
75 | 0 | final MethodReturnValueContext ctx = (MethodReturnValueContext) ovalContext; |
76 | 0 | final Method method = ctx.getMethod(); |
77 | 0 | if (isGetter(method)) { |
78 | 0 | fieldName = toPropertyName(method); |
79 | |
} else { |
80 | 0 | fieldName = method.getName(); |
81 | |
} |
82 | 0 | } else { |
83 | 0 | return ovalContext.toString(); |
84 | |
} |
85 | |
|
86 | |
final String fieldNameKey; |
87 | |
|
88 | 0 | final OValValidationContext context = OValValidationContext.get(); |
89 | 0 | final String resourceKeyPrefix = context.getResourceKeyPrefix(); |
90 | 0 | if (resourceKeyPrefix == null) { |
91 | 0 | fieldNameKey = fieldName; |
92 | |
} else { |
93 | 0 | fieldNameKey = resourceKeyPrefix + fieldName; |
94 | |
} |
95 | |
|
96 | 0 | final String fieldNameMessage = Messages.getText(fieldNameKey); |
97 | 0 | return fieldNameMessage; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
protected boolean isGetter(final Method method) { |
108 | 0 | if (method.getReturnType().equals(boolean.class)) { |
109 | 0 | if (!method.getName().startsWith("is")) { |
110 | 0 | return false; |
111 | |
} |
112 | |
} else { |
113 | 0 | if (!method.getName().startsWith("get")) { |
114 | 0 | return false; |
115 | |
} |
116 | |
} |
117 | 0 | if (method.getParameterTypes().length > 0) { |
118 | 0 | return false; |
119 | |
} |
120 | 0 | if (method.getReturnType().equals(void.class)) { |
121 | 0 | return false; |
122 | |
} |
123 | 0 | return true; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
protected String toPropertyName(final Method method) { |
134 | 0 | final String methodName = method.getName(); |
135 | |
final String str1; |
136 | 0 | if (method.getReturnType().equals(boolean.class)) { |
137 | 0 | str1 = methodName.substring(2); |
138 | |
} else { |
139 | 0 | str1 = methodName.substring(3); |
140 | |
} |
141 | 0 | final String propertyName = Character.toUpperCase(str1.charAt(0)) |
142 | |
+ str1.substring(1); |
143 | 0 | return propertyName; |
144 | |
} |
145 | |
|
146 | |
} |