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