1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.isenshi.util;
17
18 import java.util.Iterator;
19 import java.util.Map;
20
21 /***
22 * @author nishioka
23 */
24 public class HtmlBuffer extends XMLStringBuffer {
25
26 public static final String TAG_FORM = "form";
27
28 public static final String TAG_INPUT = "input";
29
30 public static final String TAG_SLECT = "select";
31
32 public static final String TAG_OPTION = "option";
33
34 public static final String TAG_TABLE = "table";
35
36 public static final String TAG_TR = "tr";
37
38 public static final String TAG_TH = "th";
39
40 public static final String TAG_TD = "td";
41
42 public static final String TAG_STYLE = "style";
43
44 public static final String ENTITY_SPACE = " ";
45
46 public HtmlBuffer() {
47 super();
48 }
49
50 public void appendBr() {
51 appendBody("<br/>");
52 }
53
54 public void appendTextArea(String name, String value) {
55 appendTextArea(name, value, -1, -1);
56 }
57
58 public void appendTextArea(String name, String value, int rows, int cols) {
59 setNewline(false);
60 setTab(false);
61 appendStartTag("textarea");
62 appendAttribute("name", name);
63 if (rows > 0) {
64 appendAttribute("rows", String.valueOf(rows));
65 }
66 if (cols > 0) {
67 appendAttribute("cols", String.valueOf(cols));
68 }
69 if (value != null) {
70 appendBody(value);
71 } else {
72 appendBody("");
73 }
74 endTag();
75 setNewline(true);
76 setTab(true);
77 }
78
79 public void appendSubmitButton(String label) {
80 appendStartTag(TAG_INPUT);
81 appendAttribute("type", "submit");
82 appendAttribute("name", "button");
83 appendAttribute("value", label);
84 endTag();
85 }
86
87 public void appendButton(String value, String onclick) {
88 appendStartTag("input");
89 appendAttribute("type", "button");
90 appendAttribute("value", value);
91 appendAttribute("onclick", onclick);
92 endTag();
93 }
94
95 /***
96 * <input type="text">を追加する
97 * @param name
98 */
99 public void appendTextField(String name) {
100 appendTextField(name, null);
101 }
102
103 /***
104 * <input type="text">を追加する
105 * @param name
106 * @param value
107 */
108 public void appendTextField(String name, String value) {
109 appendTextField(name, value, -1, -1);
110 }
111
112 /***
113 * <input type="text">を追加する
114 *
115 * @param name
116 * @param value
117 * @param size
118 */
119 public void appendTextField(String name, String value, int size){
120 appendTextField(name, value, size, -1);
121 }
122
123 /***
124 * <input type="text">を追加する
125 * @param name
126 * @param value
127 * @param size
128 * @param maxlength
129 */
130 public void appendTextField(String name, String value, int size, int maxlength) {
131 appendStartTag(TAG_INPUT);
132 appendAttribute("type", "text");
133 appendAttribute("name", name);
134 if (value != null) {
135 appendAttribute("value", value);
136 }
137 if (size > 0) {
138 appendAttribute("size", String.valueOf(size));
139 }
140 if(maxlength > 0){
141 appendAttribute("maxlength", String.valueOf(maxlength));
142 }
143 endTag();
144 }
145
146 public void appendAnchor(String href, String body, boolean needUrlCheck) {
147
148 if (needUrlCheck) {
149 if (href.indexOf("@") != -1) {
150 href = "mailto:" + href;
151 }
152 }
153 appendAnchor(href, body);
154 }
155
156 public void appendAnchor(String href, String body) {
157 appendAnchor(href, body, null);
158 }
159
160 public void appendAnchor(String href, String body, Map params) {
161 appendStartTag("a");
162
163 if (params != null) {
164 String query = CharUtil.createQuery(params);
165 appendAttribute("href", href + "?" + query);
166 } else {
167 appendAttribute("href", href);
168 }
169 appendBody(body);
170 endTag();
171 }
172
173 public void appendImg(String href, String body, int width, Map params) {
174 appendStartTag("img");
175 if (params != null) {
176 StringBuffer sbuf = new StringBuffer();
177 boolean isFisrt = true;
178 for (Iterator i = params.keySet().iterator(); i.hasNext();) {
179 if (isFisrt) {
180 isFisrt = false;
181 } else {
182 sbuf.append("&");
183 }
184 String key = (String) i.next();
185 sbuf.append(CharUtil.urlEncode(key) + "="
186 + CharUtil.urlEncode((String) params.get(key)));
187 }
188 appendAttribute("src", href + "?" + sbuf.toString());
189 } else {
190 appendAttribute("src", href);
191 }
192 appendAttribute("alt", body);
193 if (width > 0) {
194 appendAttribute("width", "" + width);
195 }
196 endTag();
197 }
198
199 public void appendSelect(String name, String[] label) {
200 appendStartTag("select");
201 appendAttribute("name", name);
202 for (int i = 0; i < label.length; i++) {
203 appendOption(label[i], label[i]);
204 }
205 endTag();
206 }
207
208 public void appendSelectFromMap(String name, Map labelValueMap,
209 String selectedId, boolean nullOption) {
210 appendSelectFromMap(name, labelValueMap, selectedId, nullOption, "-",
211 "-1");
212 }
213
214 public void appendSelectFromMap(String name, Map labelValueMap,
215 String selectedId, boolean nullOption, String nullLabel,
216 String nullValue) {
217 appendStartTag(HtmlBuffer.TAG_SLECT);
218 appendAttribute("name", name);
219 if (nullOption) {
220 appendOption(nullLabel, nullValue);
221 }
222 for (Iterator i = labelValueMap.keySet().iterator(); i.hasNext();) {
223 String label = (String) i.next();
224 String value = (String) labelValueMap.get(label);
225 appendOption(label, value, selectedId);
226 }
227 endTag();
228 }
229
230 public void appendSelect(String name, int start, int end) {
231 appendSelect(name, start, end, null, null, false);
232 }
233
234 public void appendSelect(String name, int start, int end, String selected,
235 String onchange, boolean nullOption) {
236 appendStartTag(TAG_SLECT);
237 appendAttribute("name", name);
238
239 if (onchange != null) {
240 appendAttribute("onchange", onchange);
241 }
242
243 if (nullOption) {
244 appendOption("-", "-1");
245 }
246
247 for (int i = start; i <= end; i++) {
248 String value = String.valueOf(i);
249 appendOption(value, value, selected);
250 }
251 endTag();
252 }
253
254 public void appendOption(String label, String value) {
255 appendOption(label, value, null);
256 }
257
258 public void appendCheckBox(String name, boolean flag) {
259 appendCheckBox(name, null, flag);
260 }
261
262 public void appendCheckBox(String name, String value, boolean flag) {
263 appendStartTag("input");
264 appendAttribute("type", "checkbox");
265 appendAttribute("name", name);
266 if (value != null) {
267 appendAttribute("value", value);
268 }
269 if (flag) {
270 appendAttribute("checked", "checked");
271 }
272 endTag();
273 }
274
275 public void appendRadio(String name, String value) {
276 appendRadio(name, value, false);
277 }
278
279 public void appendRadio(String name, String value, boolean checked) {
280 appendStartTag("input");
281 appendAttribute("type", "radio");
282 appendAttribute("name", name);
283 appendAttribute("value", value);
284 if (checked) {
285 appendAttribute("checked", "checked");
286 }
287 endTag();
288 }
289
290 public void appendOption(String label, String value, String selectedValue) {
291 appendStartTag(HtmlBuffer.TAG_OPTION);
292 appendAttribute("value", value);
293 if (selectedValue != null && selectedValue.equals(value)) {
294 appendAttribute("selected", "selected");
295 }
296 appendBody(label);
297 endTag();
298 }
299
300 public void appendTr(Iterator itr) {
301 appendTr(itr, false);
302 }
303
304 public void appendTr(Iterator itr, boolean heading) {
305 appendStartTag(TAG_TR);
306 while (itr.hasNext()) {
307 appendTd(itr.next().toString(), heading);
308 }
309 endTag();
310 }
311
312 public void appendTh(String str) {
313 appendTd(str, true);
314 }
315
316 public void appendBlankTh() {
317 appendTd(" ", true);
318 }
319
320 public void appendBlankTd() {
321 appendTd(" ", false);
322 }
323
324 public void appendTd(String str, boolean heading) {
325 appendTd(str, null, heading);
326 }
327
328 public void appendTd(String str) {
329 appendTd(str, false);
330 }
331
332 public void appendTd(String str, Map attrs, boolean heading) {
333
334
335
336 if (heading) {
337 appendStartTag(TAG_TH);
338 } else {
339 appendStartTag(TAG_TD);
340 }
341 setNewline(false);
342 setTab(false);
343 if (attrs != null && attrs.keySet() != null) {
344 for (Iterator i = attrs.keySet().iterator(); i.hasNext();) {
345 String key = (String) i.next();
346 String value = (String) attrs.get(key);
347 appendAttribute(key, value);
348 }
349 }
350 appendBody(str);
351 endTag();
352 appendNewLine();
353 setTab(true);
354 setNewline(true);
355 }
356
357 public void appendHeading(int level, String body) {
358 String tag = "h" + level;
359 setTab(false);
360 setNewline(false);
361 appendStartTag(tag);
362 appendBody(body);
363 setNewline(true);
364 endTag();
365 setTab(true);
366 }
367
368 public void appendFieldset(String legend) {
369 appendStartTag("fieldset");
370 appendStartTag("legend");
371 appendBody(legend);
372 endTag();
373 }
374
375 public void appendHidden(String name, String value) {
376 appendStartTag("input");
377 appendAttribute("type", "hidden");
378 appendAttribute("name", name);
379 appendAttribute("value", value);
380 endTag();
381 }
382 }