1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.util;
17
18 import java.lang.reflect.Method;
19 import java.util.Collection;
20 import java.util.Iterator;
21
22 import javax.servlet.http.HttpServletRequest;
23
24 import org.seasar.cubby.action.Accept;
25 import org.seasar.cubby.action.Action;
26 import org.seasar.cubby.action.ActionResult;
27 import org.seasar.cubby.action.Path;
28 import org.seasar.cubby.action.RequestMethod;
29 import org.seasar.framework.util.StringUtil;
30
31 public class CubbyUtils {
32
33 private static Accept DEFAULT_ACCEPT_ANNOTATION;
34 static {
35 @Accept
36 class AcceptDummy {
37 }
38 DEFAULT_ACCEPT_ANNOTATION = AcceptDummy.class.getAnnotation(Accept.class);
39 }
40
41 public static String getActionClassName(final Class<?> c) {
42 final String actionName;
43 final Path path = c.getAnnotation(Path.class);
44 if (path != null && !StringUtil.isEmpty(path.value())) {
45 actionName = path.value();
46 } else {
47 final String name = left(c.getSimpleName(), "$");
48 actionName = toFirstLower(name.replaceAll(
49 "(.*[.])*([^.]+)(Action$)", "$2"));
50 }
51 return actionName;
52 }
53
54 static String getActionMethodName(final Method m) {
55 final String actionName;
56 final Path path = m.getAnnotation(Path.class);
57 if (path != null && !StringUtil.isEmpty(path.value())) {
58 actionName = path.value();
59 } else {
60 final String methodName = m.getName();
61 if ("index".equals(methodName)) {
62 actionName = "";
63 } else {
64 actionName = methodName;
65 }
66 }
67 return actionName;
68 }
69
70 public static String getActionUrl(final Class<?> c, final Method m) {
71 final String actionMethodName = getActionMethodName(m);
72 if (actionMethodName.startsWith("/")) {
73 return actionMethodName;
74 } else {
75 final String actionName = CubbyUtils.getActionClassName(c);
76 if ("/".equals(actionName)) {
77 return "/" + actionMethodName;
78 } else {
79 return "/" + actionName + "/" + actionMethodName;
80 }
81 }
82 }
83
84 public static RequestMethod[] getAcceptableRequestMethods(
85 final Class<?> c, final Method m) {
86 Accept accept = m.getAnnotation(Accept.class);
87 if (accept == null) {
88 accept = c.getAnnotation(Accept.class);
89 if (accept == null) {
90 accept = DEFAULT_ACCEPT_ANNOTATION;
91 }
92 }
93 return accept.value();
94 }
95
96 public static boolean isActionMethod(final Method m) {
97 return m.getReturnType().isAssignableFrom(ActionResult.class)
98 && m.getParameterTypes().length == 0;
99 }
100
101 public static int getObjectSize(final Object value) {
102 final int size;
103 if (value == null) {
104 size = 0;
105 } else if (value.getClass().isArray()) {
106 final Object[] array = (Object[]) value;
107 size = array.length;
108 } else if (value instanceof Collection) {
109 final Collection<?> collection = (Collection<?>) value;
110 size = collection.size();
111 } else {
112 size = 1;
113 }
114 return size;
115 }
116
117 public static String getPath(final HttpServletRequest request) {
118 final String uri = request.getRequestURI();
119 final String contextPath = request.getContextPath();
120 final String path;
121 if ("/".equals(contextPath)) {
122 path = uri;
123 } else {
124 path = uri.substring(contextPath.length());
125 }
126 return path;
127 }
128
129 public static boolean isActionClass(final Class<?> c) {
130 return Action.class.isAssignableFrom(c);
131 }
132
133 static String toFirstLower(final String propertyName) {
134 if (StringUtil.isEmpty(propertyName)) {
135 throw new IllegalArgumentException("properyName is empty.");
136 }
137 final StringBuilder sb = new StringBuilder();
138 sb.append(propertyName.substring(0, 1).toLowerCase());
139 if (propertyName.length() > 1) {
140 sb.append(propertyName.substring(1));
141 }
142 return sb.toString();
143 }
144
145 static String left(final String str, final String sep) {
146 final int pos = str.indexOf(sep);
147 if (pos != -1) {
148 return str.substring(0, pos);
149 }
150 return str;
151 }
152
153 public static String join(final Iterator<?> iterator, final char separator) {
154 if (iterator == null) {
155 return null;
156 }
157 if (!iterator.hasNext()) {
158 return "";
159 }
160 final Object first = iterator.next();
161 if (!iterator.hasNext()) {
162 return first != null ? first.toString() : "";
163 }
164 final StringBuffer buf = new StringBuffer(256);
165 if (first != null) {
166 buf.append(first);
167 }
168 while (iterator.hasNext()) {
169 buf.append(separator);
170 final Object obj = iterator.next();
171 if (obj != null) {
172 buf.append(obj);
173 }
174 }
175 return buf.toString();
176 }
177 }