View Javadoc

1   /*
2    * Copyright 2004-2009 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.tags;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.text.SimpleDateFormat;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Date;
23  import java.util.Map;
24  
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.seasar.cubby.internal.controller.ThreadContext;
28  import org.seasar.cubby.internal.util.URLBodyEncoder;
29  
30  /**
31   * JSP functions を提供します。
32   * 
33   * @author baba
34   */
35  public class CubbyFunctions {
36  
37  	/**
38  	 * 配列やコレクションに指定したオブジェクトが含まれるかどうかを判定します。
39  	 * 
40  	 * @param collection
41  	 *            配列や{@link Collection コレクション}
42  	 * @param obj
43  	 *            配列やコレクションにあるかどうかを調べる要素
44  	 * @return 配列やコレクションに指定したオブジェクトが含まれる場合は <code>true</code>、そうでない場合は
45  	 *         <code>false</code>
46  	 */
47  	public static Boolean contains(final Object collection, final Object obj) {
48  		if (collection instanceof Collection<?>) {
49  			return _contains((Collection<?>) collection, obj);
50  		} else if (collection != null && collection.getClass().isArray()) {
51  			return _contains(Arrays.asList((Object[]) collection), obj);
52  		} else {
53  			return false;
54  		}
55  	}
56  
57  	/**
58  	 * 指定された要素が{@link Collection}内にあるかどうかを示します。
59  	 * 
60  	 * @param collection
61  	 *            コレクション
62  	 * @param obj
63  	 *            コレクションにあるかどうかを調べる要素
64  	 * @return 指定された要素が{@link Collection}内にある場合は <code>true</code>、そうでない場合は
65  	 *         <code>false</code>
66  	 */
67  	private static Boolean _contains(final Collection<?> collection,
68  			final Object obj) {
69  		return collection.contains(obj);
70  	}
71  
72  	/**
73  	 * {@link Map}に指定したキーが含まれるかどうかを判定します。
74  	 * 
75  	 * @param map
76  	 *            マップ
77  	 * @param key
78  	 *            マップにあるかどうかが判定されるキー
79  	 * @return {@link Map}に指定したキーが含まれる場合は <code>true</code>、そうでない場合は
80  	 *         <code>false</code>
81  	 */
82  	public static Boolean containsKey(final Map<?, ?> map, final Object key) {
83  		return map.containsKey(key);
84  	}
85  
86  	/**
87  	 * {@link Map}に指定した値が含まれるかどうかを判定します。
88  	 * 
89  	 * @param map
90  	 *            マップ
91  	 * @param value
92  	 *            マップにあるかどうかを判定される値
93  	 * @return {@link Map}に指定した値が含まれる場合は <code>true</code>、そうでない場合は
94  	 *         <code>false</code>
95  	 */
96  	public static Boolean containsValue(final Map<?, ?> map, final Object value) {
97  		return map.containsValue(value);
98  	}
99  
100 	/**
101 	 * 指定したカンマ区切りの文字列をインデックス値でサイクルして出力します。
102 	 * <p>
103 	 * 主に行毎に色分けする場合に CSS のクラス名を出力する場合に使用します。
104 	 * </p>
105 	 * 
106 	 * @param index
107 	 *            インデックス
108 	 * @param classNames
109 	 *            カンマ区切りの文字列
110 	 * @return 指定したインデックスに対応する文字列
111 	 */
112 	public static String odd(final Integer index, final String classNames) {
113 		final String[] c = classNames.split(",");
114 		return c[index % c.length].trim();
115 	}
116 
117 	/**
118 	 * HTMLをエスケープします。
119 	 * <p>
120 	 * JSTLのoutタグの代わりに使用します。EL式で出力された文字列はエスケープされないため、
121 	 * エスケープを行いたい場合はこのfunctionを使用します。
122 	 * </p>
123 	 * 
124 	 * @param str
125 	 *            エスケープする文字列
126 	 * @return エスケープされた HTML
127 	 */
128 	public static String out(final Object str) {
129 		return str == null ? "" : TagUtils.escapeHtml(str.toString());
130 	}
131 
132 	/**
133 	 * {@link Date}型のオブジェクトをフォーマットして出力します。
134 	 * <p>
135 	 * JSTL の dateFormat タグの代わりに使用します。
136 	 * </p>
137 	 * 
138 	 * @param date
139 	 *            日付/時刻文字列にフォーマットする日付/時刻値
140 	 * @param pattern
141 	 *            日付と時刻のフォーマットを記述するパターン
142 	 * @return フォーマットされた日付/時刻文字列
143 	 */
144 	public static String dateFormat(final Object date, final String pattern) {
145 		if (date instanceof Date) {
146 			final SimpleDateFormat format = new SimpleDateFormat(pattern);
147 			return format.format(date);
148 		} else {
149 			return "";
150 		}
151 	}
152 
153 	/**
154 	 * 指定された条件によって属性を出力するかしないかを制御します。
155 	 * <p>
156 	 * <code>condition</code> が <code>true</code> のときは <code>value</code>
157 	 * を属性の値として出力し、 <code>condition</code> が <code>false</code> のときは属性自体を出力しません。
158 	 * 条件によって disabled や checked などの属性の出力する・しないを制御したい場合に使用します。
159 	 * 出力する・しないの制御はカスタムタグで行うので、t:input/t:select/t:textarea と組み合わせて使用してください。
160 	 * </p>
161 	 * <p>
162 	 * 
163 	 * <pre>
164 	 * &lt;t:input name=&quot;foo&quot; disabled=&quot;${f:render(cond == true, \&quot;disabled\&quot;)} /&gt;
165 	 * </pre>
166 	 * 
167 	 * 上記の例では、<code>cond == true</code> の場合には input
168 	 * タグの属性に「disabled="disabled"」が出力され、 <code>cond == false</code> の場合には
169 	 * disabled 属性が出力されません。
170 	 * </p>
171 	 * 
172 	 * @param condition
173 	 *            属性を出力する条件
174 	 * @param value
175 	 * @return condition が <code>true</code> の場合は
176 	 *         value、そうでない場合は属性を出力しないことを示すオブジェクト
177 	 */
178 	public static Object ifrender(final Boolean condition, final Object value) {
179 		if (condition) {
180 			return value;
181 		}
182 		return TagUtils.REMOVE_ATTRIBUTE;
183 	}
184 
185 	/**
186 	 * 文字列を Base64 でエンコードします。
187 	 * <p>
188 	 * JSTL の url タグの代わりに使用します。 {@code
189 	 * HttpServletRequest#getCharacterEncoding()}で取得した文字コードでエンコードされます。
190 	 * </p>
191 	 * <p>
192 	 * 例:<br/>
193 	 * ${f:url('abc あいう'))} -> abc+%E3%81%82%E3%81%84%E3%81%86
194 	 * </p>
195 	 * 
196 	 * @param str
197 	 *            エンコードする文字列
198 	 * @return エンコードされた文字列
199 	 * @see HttpServletRequest#setCharacterEncoding(String)
200 	 * @see HttpServletRequest#getCharacterEncoding()
201 	 * @throws UnsupportedEncodingException
202 	 */
203 	public static String url(final Object str)
204 			throws UnsupportedEncodingException {
205 		if (str == null) {
206 			return "";
207 		}
208 		final String characterEncoding = ThreadContext.getRequest()
209 				.getCharacterEncoding();
210 		return URLBodyEncoder.encode(str.toString(), characterEncoding);
211 	}
212 
213 }