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