1 package org.seasar.cubby.util; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Arrays; 5 import java.util.Collection; 6 import java.util.Date; 7 import java.util.Map; 8 9 import org.seasar.framework.util.StringUtil; 10 11 public class CubbyFunctions { 12 13 public static Boolean contains(Object c, Object value) { 14 if (c instanceof Collection) { 15 return _contains((Collection<?>) c, value); 16 } else if (c != null && c.getClass().isArray()) { 17 return _contains(Arrays.asList((Object[]) c), value); 18 } else { 19 return false; 20 } 21 } 22 23 public static Boolean _contains(Collection<?> c, Object value) { 24 return c.contains(value); 25 } 26 27 public static Boolean containsKey(Map<?, ?> m, Object value) { 28 return m.containsKey(value); 29 } 30 31 public static Boolean containsValue(Map<?, ?> m, Object value) { 32 return m.containsValue(value); 33 } 34 35 public static String odd(Integer index, String classnames) { 36 String[] c = classnames.split(","); 37 return c[index % c.length]; 38 } 39 40 public static String out(Object value) { 41 return value == null ? "" : escapeHtml(value.toString()); 42 } 43 44 public static String escapeHtml(Object value) { 45 if (value == null) { 46 return ""; 47 } 48 String text; 49 if (value instanceof String) { 50 text = (String) value; 51 } else { 52 text = value.toString(); 53 } 54 text = StringUtil.replace(text, "&", "&"); 55 text = StringUtil.replace(text, "<", "<"); 56 text = StringUtil.replace(text, ">", ">"); 57 text = StringUtil.replace(text, "\"", """); 58 text = StringUtil.replace(text, "'", "'"); 59 return text; 60 } 61 62 public static String dateFormat(Object date, String pattern) { 63 if (date instanceof Date) { 64 SimpleDateFormat format = new SimpleDateFormat(pattern); 65 return format.format(date); 66 } else { 67 return ""; 68 } 69 } 70 71 }