Coverage Report - org.seasar.cubby.util.CubbyUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
CubbyUtils
100%
46/46
N/A
0
 
 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  13
                 String name = left(c.getSimpleName(), "$");
 19  13
                 String actionName = toFirstLower(name.replaceAll(
 20  
                                 "(.*[.])*([^.]+)(Action$)", "$2"));
 21  13
                 if (c.getAnnotation(Url.class) != null) {
 22  5
                         actionName = ((Url) c.getAnnotation(Url.class)).value();
 23  
                 }
 24  13
                 return actionName;
 25  
         }
 26  
 
 27  
         static String getActionMethodName(Method m) {
 28  6
                 String actionName = m.getName();
 29  6
                 if (m.getAnnotation(Url.class) != null) {
 30  2
                         actionName = ((Url) m.getAnnotation(Url.class)).value();
 31  2
                 } else if ("index".equals(actionName)) {
 32  1
                         actionName = "";
 33  
                 }
 34  6
                 return actionName;
 35  
         }
 36  
 
 37  
         public static String getActionUrl(Class<?> c, Method m) {
 38  6
                 String actionMethodName = getActionMethodName(m);
 39  6
                 if (actionMethodName.startsWith("/")) {
 40  1
                         return actionMethodName;
 41  
                 } else {
 42  5
                         String actionName = CubbyUtils.getActionClassName(c);
 43  5
                         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  14
                 if (value == null) {
 55  1
                         size = 0;
 56  1
                 } else if (value.getClass().isArray()) {
 57  9
                         Object[] array = (Object[]) value;
 58  9
                         size = array.length;
 59  9
                 } else if (value instanceof Collection) {
 60  3
                         Collection<?> collection = (Collection<?>) value;
 61  3
                         size = collection.size();
 62  3
                 } else {
 63  1
                         size = 1;
 64  
                 }
 65  14
                 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  8
                 Object value = params.get(key);
 80  8
                 if (value == null) {
 81  3
                         return null;
 82  5
                 } else if (value.getClass().isArray()) {
 83  
                         Object[] values = (Object[]) value;
 84  
                         return values[0];
 85  
                 } else {
 86  5
                         return value;
 87  
                 }
 88  
         }
 89  
 
 90  
         static String toFirstLower(final String propertyName) {
 91  13
                 if (StringUtil.isEmpty(propertyName)) {
 92  
                         throw new IllegalArgumentException("properyName is empty.");
 93  
                 }
 94  13
                 final StringBuilder sb = new StringBuilder();
 95  13
                 sb.append(propertyName.substring(0, 1).toLowerCase());
 96  13
                 if (propertyName.length() > 1) {
 97  13
                         sb.append(propertyName.substring(1));
 98  
                 }
 99  13
                 return sb.toString();
 100  
         }
 101  
 
 102  
         static String left(final String str, final String sep) {
 103  13
                 final int pos = str.indexOf(sep);
 104  13
                 if (pos != -1) {
 105  
                         return str.substring(0, pos);
 106  
                 }
 107  13
                 return str;
 108  
         }
 109  
 
 110  
         public static String join(final Iterator<?> iterator, final char separator) {
 111  2
                 if (iterator == null) {
 112  
                         return null;
 113  
                 }
 114  2
                 if (!iterator.hasNext()) {
 115  
                         return "";
 116  
                 }
 117  2
                 final Object first = iterator.next();
 118  2
                 if (!iterator.hasNext()) {
 119  2
                         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  
 }