1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.util;
17
18 import static org.seasar.cubby.CubbyConstants.INTERNAL_FORWARD_DIRECTORY;
19
20 import java.lang.reflect.Method;
21 import java.util.Collection;
22
23 import javax.servlet.http.HttpServletRequest;
24
25 import org.seasar.cubby.action.Accept;
26 import org.seasar.cubby.action.Action;
27 import org.seasar.cubby.action.ActionResult;
28 import org.seasar.cubby.action.Path;
29 import org.seasar.cubby.action.RequestMethod;
30 import org.seasar.framework.util.StringUtil;
31
32
33
34
35
36
37
38 public class CubbyUtils {
39
40
41 private static final String INDEX_METHOD_NAME = "index";
42
43
44 private static Accept DEFAULT_ACCEPT_ANNOTATION;
45 static {
46 @Accept
47 class AcceptDummy {
48 }
49 DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class
50 .getAnnotation(Accept.class);
51 }
52
53
54
55
56
57
58
59
60 public static String getActionDirectory(final Class<?> actionClass) {
61 final String actionName;
62 final Path path = actionClass.getAnnotation(Path.class);
63 if (path != null && !StringUtil.isEmpty(path.value())) {
64 actionName = path.value();
65 } else {
66 final String name = left(actionClass.getSimpleName(), "$");
67 actionName = toFirstLower(name.replaceAll(
68 "(.*[.])*([^.]+)(Action$)", "$2"));
69 }
70 return actionName;
71 }
72
73
74
75
76
77
78
79
80
81
82 private static String left(final String text, final String sep) {
83 final int pos = text.indexOf(sep);
84 if (pos != -1) {
85 return text.substring(0, pos);
86 }
87 return text;
88 }
89
90
91
92
93
94
95
96
97 private static String toFirstLower(final String text) {
98 if (StringUtil.isEmpty(text)) {
99 throw new IllegalArgumentException("text is empty.");
100 }
101 final StringBuilder sb = new StringBuilder();
102 sb.append(text.substring(0, 1).toLowerCase());
103 if (text.length() > 1) {
104 sb.append(text.substring(1));
105 }
106 return sb.toString();
107 }
108
109
110
111
112
113
114
115
116
117
118 public static String getActionPath(final Class<?> actionClass,
119 final Method method) {
120 final String path;
121 final String actionMethodName = getActionMethodName(method);
122 if (actionMethodName.startsWith("/")) {
123 return path = actionMethodName;
124 } else {
125 final String actionDirectory = CubbyUtils
126 .getActionDirectory(actionClass);
127 if ("/".equals(actionDirectory)) {
128 path = "/" + actionMethodName;
129 } else {
130 path = "/" + actionDirectory + "/" + actionMethodName;
131 }
132 }
133 return path;
134 }
135
136
137
138
139
140
141
142
143 private static String getActionMethodName(final Method method) {
144 final String actionName;
145 final Path path = method.getAnnotation(Path.class);
146 if (path != null && !StringUtil.isEmpty(path.value())) {
147 actionName = path.value();
148 } else {
149 final String methodName = method.getName();
150 if (INDEX_METHOD_NAME.equals(methodName)) {
151 actionName = "";
152 } else {
153 actionName = methodName;
154 }
155 }
156 return actionName;
157 }
158
159
160
161
162
163
164
165
166
167
168 public static RequestMethod[] getAcceptableRequestMethods(
169 final Class<?> actionClass, final Method method) {
170 Accept accept = method.getAnnotation(Accept.class);
171 if (accept == null) {
172 accept = actionClass.getAnnotation(Accept.class);
173 if (accept == null) {
174 accept = DEFAULT_ACCEPT_ANNOTATION;
175 }
176 }
177 return accept.value();
178 }
179
180
181
182
183
184
185
186
187 public static int getObjectSize(final Object value) {
188 final int size;
189 if (value == null) {
190 size = 0;
191 } else if (value.getClass().isArray()) {
192 final Object[] array = (Object[]) value;
193 size = array.length;
194 } else if (value instanceof Collection) {
195 final Collection<?> collection = (Collection<?>) value;
196 size = collection.size();
197 } else {
198 size = 1;
199 }
200 return size;
201 }
202
203
204
205
206
207
208
209
210 public static String getPath(final HttpServletRequest request) {
211 final String uri = request.getRequestURI();
212 final String contextPath = request.getContextPath();
213 final String path;
214 if ("/".equals(contextPath)) {
215 path = uri;
216 } else {
217 path = uri.substring(contextPath.length());
218 }
219 return path;
220 }
221
222
223
224
225
226
227
228
229
230
231 public static String getInternalForwardPath(
232 final Class<? extends Action> actionClass, final String methodName) {
233 return "/" + INTERNAL_FORWARD_DIRECTORY + "/"
234 + actionClass.getCanonicalName() + "/" + methodName;
235 }
236
237
238
239
240
241
242
243
244
245 public static boolean isActionClass(final Class<?> clazz) {
246 return Action.class.isAssignableFrom(clazz);
247 }
248
249
250
251
252
253
254
255
256
257 public static boolean isActionMethod(final Method method) {
258 return method.getReturnType().isAssignableFrom(ActionResult.class)
259 && method.getParameterTypes().length == 0;
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273 public static String replaceFirst(final String text, final String replace,
274 final String with) {
275 if (text == null || replace == null || with == null) {
276 return text;
277 }
278 final int index = text.indexOf(replace);
279 if (index == -1) {
280 return text;
281 }
282 final StringBuilder builder = new StringBuilder(100);
283 builder.append(text.substring(0, index));
284 builder.append(with);
285 builder.append(text.substring(index + replace.length()));
286 return builder.toString();
287 }
288
289
290
291
292
293
294
295
296
297
298 public static String[] split2(final String text, final char delim) {
299 if (text == null) {
300 return null;
301 }
302 int index = text.indexOf(delim);
303 if (index == -1) {
304 return new String[] { text };
305 }
306 String[] tokens = new String[2];
307 tokens[0] = text.substring(0, index);
308 tokens[1] = text.substring(index + 1);
309 return tokens;
310 }
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 public static String escapeHtml(final Object str) {
348 if (str == null) {
349 return "";
350 }
351 String text;
352 if (str instanceof String) {
353 text = (String) str;
354 } else {
355 text = str.toString();
356 }
357 text = StringUtil.replace(text, "&", "&");
358 text = StringUtil.replace(text, "<", "<");
359 text = StringUtil.replace(text, ">", ">");
360 text = StringUtil.replace(text, "\"", """);
361 text = StringUtil.replace(text, "'", "'");
362 return text;
363 }
364
365 }