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