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