View Javadoc

1   package org.seasar.cubby.util;
2   
3   import static java.lang.Boolean.TRUE;
4   import static org.seasar.cubby.CubbyConstants.ATTR_OUTPUT_VALUES;
5   import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
6   import static org.seasar.cubby.CubbyConstants.ATTR_VALIDATION_FAIL;
7   
8   import java.util.Collection;
9   import java.util.Map;
10  import java.util.Map.Entry;
11  
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.jsp.JspContext;
14  import javax.servlet.jsp.PageContext;
15  
16  import org.seasar.framework.beans.BeanDesc;
17  import org.seasar.framework.beans.PropertyDesc;
18  import org.seasar.framework.beans.factory.BeanDescFactory;
19  import org.seasar.framework.util.StringUtil;
20  
21  public class CubbyHelperFunctions {
22  
23  	public static String joinPropertyValue(Object beans, String propertyName,
24  			String delim) {
25  		return _joinPropertyValue(toArray(beans), propertyName, delim);
26  	}
27  
28  	public static Object[] toArray(Object value) {
29  		if (value.getClass().isArray()) {
30  			return (Object[]) value;
31  		} else if (value instanceof Collection) {
32  			return ((Collection<?>) value).toArray();
33  		}
34  		throw new IllegalArgumentException("not array or collection : " + value);
35  	}
36  
37  	private static String _joinPropertyValue(Object[] beans,
38  			String propertyName, String delim) {
39  		StringBuilder sb = new StringBuilder();
40  		for (int i = 0; i < beans.length; i++) {
41  			Object bean = beans[i];
42  			Object value = property(bean, propertyName);
43  			sb.append(value);
44  			if (i < beans.length - 1) {
45  				sb.append(delim);
46  			}
47  		}
48  		return sb.toString();
49  	}
50  
51  	public static String toAttr(Map<String, Object> map) {
52  		StringBuilder sb = new StringBuilder();
53  		for (Entry<String, Object> entry : map.entrySet()) {
54  			final String key = entry.getKey();
55  			if ("value".equals(key) || "checkedValue".equals(key)) {
56  				continue;
57  			}
58  			sb.append(key);
59  			sb.append("=\"");
60  			sb.append(CubbyFunctions.escapeHtml(entry.getValue()));
61  			sb.append("\" ");
62  		}
63  		return sb.toString();
64  	}
65  
66  	public static String checked(String value, Object values) {
67  		if (value == null || values == null) {
68  			return "";
69  		}
70  		if (isChecked(value, values)) {
71  			return "checked=\"true\"";
72  		} else {
73  			return "";
74  		}
75  	}
76  
77  	public static String selected(String value, Object values) {
78  		if (value == null || values == null) {
79  			return "";
80  		}
81  		if (isChecked(value, values)) {
82  			return "selected=\"true\"";
83  		} else {
84  			return "";
85  		}
86  	}
87  
88  	public static boolean isChecked(String value, Object values) {
89  		if (values instanceof Collection) {
90  			return ((Collection<?>) values).contains(value);
91  		} else if (values.getClass().isArray()) {
92  			for (Object v : (Object[]) values) {
93  				if (equalsValue(v, value)) {
94  					return true;
95  				}
96  			}
97  			return false;
98  		} else {
99  			return equalsValue(values, value);
100 		}
101 	}
102 
103 	private static boolean equalsValue(Object values, Object value) {
104 		if (values == value) {
105 			return true;
106 		} else if (values == null) {
107 			return false;
108 		} else {
109 			return values.toString().equals(value.toString());
110 		}
111 	}
112 
113 	@SuppressWarnings("unchecked")
114 	public static String convertFieldValue(Object source, Object form,
115 			HttpServletRequest request, String propertyName) {
116 		if (form == null || propertyName == null) {
117 			return CubbyFunctions.out(source);
118 		} else {
119 			final Map<String, String> outputValues = (Map<String, String>) request
120 			.getAttribute(ATTR_OUTPUT_VALUES);
121 			final String converted;
122 			if (!(TRUE.equals(request.getAttribute(ATTR_VALIDATION_FAIL))) && outputValues != null) {
123 				final String outputValue = outputValues.get(propertyName);
124 				if (outputValue == null && source != null) {
125 					converted = source.toString();
126 				} else {
127 					converted = outputValues.get(propertyName);
128 				}
129 			} else if (source == null) {
130 				converted = "";
131 			} else {
132 				converted = source.toString();
133 			}
134 			return CubbyFunctions.out(converted);
135 		}
136 	}
137 
138 	public static Object property(Object bean, String property) {
139 		if (StringUtil.isEmpty(property)) {
140 			return bean;
141 		}
142 		BeanDesc beanDesc = BeanDescFactory.getBeanDesc(bean.getClass());
143 		PropertyDesc propertyDesc = beanDesc.getPropertyDesc(property);
144 		return propertyDesc.getValue(bean);
145 	}
146 
147 	@SuppressWarnings("unchecked")
148 	public static void addClassName(Map dyn, String className) {
149 		String classValue = (String) dyn.get("class");
150 		if (StringUtil.isEmpty(classValue)) {
151 			classValue = className;
152 		} else {
153 			classValue = classValue + " " + className;
154 		}
155 		dyn.put("class", classValue);
156 	}
157 	@SuppressWarnings("unchecked")
158 	public static Object formValue(Map dyn, Object form,
159 			JspContext context, String valueParamName) {
160 		Object value = dyn.get(valueParamName);
161 		String name = (String) dyn.get("name");
162 		if (TRUE.equals(context.getAttribute(ATTR_VALIDATION_FAIL, PageContext.REQUEST_SCOPE))) {
163 			if (dyn.containsKey(valueParamName)) {
164 				return value;
165 			} else {
166 				Map<String, Object> params = (Map<String, Object>) context
167 						.getAttribute(ATTR_PARAMS, PageContext.REQUEST_SCOPE);
168 				return CubbyUtils.getParamsValue(params, name);
169 			}
170 		} else {
171 			if (dyn.containsKey(valueParamName) || form == null
172 					|| StringUtil.isEmpty(name)) {
173 				return value;
174 			} else {
175 				return property(form, name);
176 			}
177 		}
178 	}
179 
180 	@SuppressWarnings("unchecked")
181 	public static Object formMultiValue(Map dyn, Object form,
182 			JspContext context, String valueParamName) {
183 		Object value = dyn.get(valueParamName);
184 		String name = (String) dyn.get("name");
185 		if (TRUE.equals(context.getAttribute(ATTR_VALIDATION_FAIL, PageContext.REQUEST_SCOPE))) {
186 			if (dyn.containsKey(valueParamName)) {
187 				return value;
188 			} else {
189 				Map<String, Object> params = (Map<String, Object>) context
190 						.getAttribute(ATTR_PARAMS, PageContext.REQUEST_SCOPE);
191 				return params.get(name);
192 			}
193 		} else {
194 			if (dyn.containsKey(valueParamName) || form == null
195 					|| StringUtil.isEmpty(name)) {
196 				return value;
197 			} else {
198 				return property(form, name);
199 			}
200 		}
201 	}
202 
203 }