1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.cubby.internal.util; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
|
20 | |
import org.seasar.cubby.action.Accept; |
21 | |
import org.seasar.cubby.action.OnSubmit; |
22 | |
import org.seasar.cubby.action.Path; |
23 | |
import org.seasar.cubby.action.RequestMethod; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | 0 | public class MetaUtils { |
32 | |
|
33 | |
|
34 | |
private static final String INDEX_METHOD_NAME = "index"; |
35 | |
|
36 | |
|
37 | |
private static final Accept DEFAULT_ACCEPT_ANNOTATION; |
38 | |
static { |
39 | |
@Accept |
40 | 0 | class AcceptDummy { |
41 | |
} |
42 | 3 | DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class |
43 | |
.getAnnotation(Accept.class); |
44 | 3 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public static String getActionDirectory(final Class<?> actionClass) { |
54 | |
final String actionName; |
55 | 912 | final Path path = actionClass.getAnnotation(Path.class); |
56 | 912 | if (path != null && !StringUtils.isEmpty(path.value())) { |
57 | 387 | actionName = path.value(); |
58 | |
} else { |
59 | 525 | final String name = left(actionClass.getSimpleName(), "$"); |
60 | 525 | actionName = toFirstLower(name.replaceAll( |
61 | |
"(.*[.])*([^.]+)(Action$)", "$2")); |
62 | |
} |
63 | 912 | return actionName; |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
private static String left(final String text, final String sep) { |
76 | 525 | final int pos = text.indexOf(sep); |
77 | 525 | if (pos != -1) { |
78 | 0 | return text.substring(0, pos); |
79 | |
} |
80 | 525 | return text; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
private static String toFirstLower(final String text) { |
91 | 525 | if (StringUtils.isEmpty(text)) { |
92 | 0 | throw new IllegalArgumentException("text is empty."); |
93 | |
} |
94 | 525 | final StringBuilder sb = new StringBuilder(); |
95 | 525 | sb.append(text.substring(0, 1).toLowerCase()); |
96 | 525 | if (text.length() > 1) { |
97 | 525 | sb.append(text.substring(1)); |
98 | |
} |
99 | 525 | return sb.toString(); |
100 | |
} |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public static String getActionPath(final Class<?> actionClass, |
112 | |
final Method method) { |
113 | |
final String path; |
114 | 897 | final String actionMethodName = getActionMethodName(method); |
115 | 897 | if (actionMethodName.startsWith("/")) { |
116 | 15 | path = actionMethodName; |
117 | |
} else { |
118 | 882 | final String actionDirectory = getActionDirectory(actionClass); |
119 | 882 | if ("/".equals(actionDirectory)) { |
120 | 147 | path = "/" + actionMethodName; |
121 | |
} else { |
122 | 735 | path = "/" + actionDirectory + "/" + actionMethodName; |
123 | |
} |
124 | |
} |
125 | 897 | return path; |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
private static String getActionMethodName(final Method method) { |
136 | |
final String actionName; |
137 | 897 | final Path path = method.getAnnotation(Path.class); |
138 | 897 | if (path != null && !StringUtils.isEmpty(path.value())) { |
139 | 513 | actionName = path.value(); |
140 | |
} else { |
141 | 384 | final String methodName = method.getName(); |
142 | 384 | if (INDEX_METHOD_NAME.equals(methodName)) { |
143 | 147 | actionName = ""; |
144 | |
} else { |
145 | 237 | actionName = methodName; |
146 | |
} |
147 | |
} |
148 | 897 | return actionName; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public static RequestMethod[] getAcceptableRequestMethods( |
161 | |
final Class<?> actionClass, final Method method) { |
162 | |
final Accept accept; |
163 | 864 | if (method.isAnnotationPresent(Accept.class)) { |
164 | 135 | accept = method.getAnnotation(Accept.class); |
165 | 729 | } else if (actionClass.isAnnotationPresent(Accept.class)) { |
166 | 0 | accept = actionClass.getAnnotation(Accept.class); |
167 | |
} else { |
168 | 729 | accept = DEFAULT_ACCEPT_ANNOTATION; |
169 | |
} |
170 | 864 | return accept.value(); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public static int getPriority(final Method method) { |
181 | 1647 | final Path path = method.getAnnotation(Path.class); |
182 | 1647 | return path != null ? path.priority() : Integer.MAX_VALUE; |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public static String getOnSubmit(final Method method) { |
196 | 1638 | final OnSubmit onSubmit = method.getAnnotation(OnSubmit.class); |
197 | |
final String parameterName; |
198 | 1638 | if (onSubmit == null) { |
199 | 1638 | parameterName = null; |
200 | |
} else { |
201 | 0 | parameterName = onSubmit.value(); |
202 | |
} |
203 | 1638 | return parameterName; |
204 | |
} |
205 | |
|
206 | |
} |