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