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