View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.util;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.Date;
22  import java.util.Map;
23  
24  import org.seasar.framework.util.StringUtil;
25  
26  public class CubbyFunctions {
27  
28  	public static Boolean contains(Object c, Object value) {
29  		if (c instanceof Collection) {
30  			return _contains((Collection<?>) c, value);
31  		} else if (c != null && c.getClass().isArray()) {
32  			return _contains(Arrays.asList((Object[]) c), value);
33  		} else {
34  			return false;
35  		}
36  	}
37  
38  	public static Boolean _contains(Collection<?> c, Object value) {
39  		return c.contains(value);
40  	}
41  
42  	public static Boolean containsKey(Map<?, ?> m, Object value) {
43  		return m.containsKey(value);
44  	}
45  
46  	public static Boolean containsValue(Map<?, ?> m, Object value) {
47  		return m.containsValue(value);
48  	}
49  
50  	public static String odd(Integer index, String classnames) {
51  		String[] c = classnames.split(",");
52  		return c[index % c.length];
53  	}
54  
55  	public static String out(Object value) {
56  		return value == null ? "" : escapeHtml(value.toString());
57  	}
58  
59  	public static String escapeHtml(Object value) {
60  		if (value == null) {
61  			return "";
62  		}
63  		String text;
64  		if (value instanceof String) {
65  			text = (String) value;
66  		} else {
67  			text = value.toString();
68  		}
69  		text = StringUtil.replace(text, "&", "&amp;");
70  		text = StringUtil.replace(text, "<", "&lt;");
71  		text = StringUtil.replace(text, ">", "&gt;");
72  		text = StringUtil.replace(text, "\"", "&quot;");
73  		text = StringUtil.replace(text, "'", "&#39;");
74  		return text;
75  	}
76  
77  	public static String dateFormat(Object date, String pattern) {
78  		if (date instanceof Date) {
79  			SimpleDateFormat format = new SimpleDateFormat(pattern);
80  			return format.format(date);
81  		} else {
82  			return "";
83  		}
84  	}
85  
86  }