01: /*
02: * Copyright (c) 2006 Your Corporation. All Rights Reserved.
03: */
04:
05: /*
06: * Created by IntelliJ IDEA.
07: * User: Jacques
08: * Date: Jan 1, 2006
09: * Time: 10:59:22 PM
10: */
11: package com.technoetic.xplanner.tags.util;
12:
13: import java.io.IOException;
14: import java.io.Writer;
15: import java.util.Enumeration;
16: import java.util.Iterator;
17: import java.util.Map;
18: import java.util.TreeMap;
19: import javax.servlet.ServletRequest;
20:
21: import org.apache.commons.collections.map.ListOrderedMap;
22:
23: import com.technoetic.xplanner.util.StringUtilities;
24:
25: public class BoxedListTag extends BoxTag {
26: private Map keyValues;
27: public static final String NAME_STYLE_CLASS = "propertyName";
28: public static final String VALUE_STYLE_CLASS = "propertyValue";
29:
30: protected void renderBody(StringBuffer results) {
31: results.append("<ul>");
32: if (bodyContent == null) {
33: for (Iterator i = keyValues.keySet().iterator(); i
34: .hasNext();) {
35: renderRow(results, (String) i.next());
36: }
37: } else {
38: results.append(bodyContent.getString());
39: }
40: results.append("</ul>");
41: }
42:
43: private void renderRow(StringBuffer results, String name) {
44: String key = StringUtilities.htmlEncode(name);
45: Object rawValue = keyValues.get(key);
46: results
47: .append("<li>" + renderProperty(key, rawValue)
48: + "</li>");
49: }
50:
51: public static String renderProperty(String key, Object rawValue) {
52: return span(key, NAME_STYLE_CLASS) + ": "
53: + span(getStringValue(rawValue), VALUE_STYLE_CLASS);
54: }
55:
56: private static String getStringValue(Object rawValue) {
57: String value = "";
58: if (rawValue instanceof String) {
59: value = StringUtilities.htmlEncode((String) rawValue);
60: } else if (rawValue instanceof String[]) {
61: String[] values = (String[]) rawValue;
62: if (values.length > 1)
63: value = "{";
64: for (int i = 0; i < values.length; i++) {
65: value += StringUtilities.htmlEncode(values[i]);
66: if (i < values.length - 1) {
67: value += ", ";
68: }
69: }
70: if (values.length > 1)
71: value += "}";
72: }
73: return value;
74: }
75:
76: private static String span(String key, String styleClass) {
77: return "<span class=\"" + styleClass + "\">" + key + "</span>";
78: }
79:
80: public static Map getRequestAttributeMap(ServletRequest request) {
81: Map map = new TreeMap();
82: Enumeration attributeNames = request.getAttributeNames();
83: while (attributeNames.hasMoreElements()) {
84: String name = (String) attributeNames.nextElement();
85: map.put(name, StringUtilities.htmlEncode(request
86: .getAttribute(name).toString()));
87: }
88: return map;
89: }
90:
91: public void setKeyValues(Map keyValues) {
92: this.keyValues = keyValues;
93: }
94: }
|