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