KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > TableTag


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import java.io.IOException JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspWriter JavaDoc;
22 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 /**
30  * @author Fabrizio Giustina
31  * @version $Revision $ ($Author $)
32  */

33 public class TableTag extends BodyTagSupport JavaDoc {
34
35     /**
36      * logger.
37      */

38     private static Logger log = LoggerFactory.getLogger(TableTag.class);
39
40     /**
41      * Stable serialVersionUID.
42      */

43     private static final long serialVersionUID = 1L;
44
45     private boolean header;
46
47     private Map JavaDoc htmlAttributes = new HashMap JavaDoc();
48
49     /**
50      * Setter for <code>header</code>.
51      * @param header show header
52      */

53     public void setHeader(boolean header) {
54         this.header = header;
55     }
56
57     /**
58      * Setter for <code>class</code>.
59      * @param value html attribute
60      */

61     public void setClass(String JavaDoc value) {
62         this.htmlAttributes.put("class", value);
63     }
64
65     /**
66      * Setter for <code>style</code>.
67      * @param value html attribute
68      */

69     public void setStyle(String JavaDoc value) {
70         this.htmlAttributes.put("style", value);
71     }
72
73     /**
74      * Setter for <code>id</code>.
75      * @param value html attribute
76      */

77     public void setId(String JavaDoc value) {
78         this.htmlAttributes.put("id", value);
79     }
80
81     /**
82      * Setter for <code>cellspacing</code>.
83      * @param value html attribute
84      */

85     public void setCellspacing(String JavaDoc value) {
86         this.htmlAttributes.put("cellspacing", value);
87     }
88
89     /**
90      * Setter for <code>cellpadding</code>.
91      * @param value html attribute
92      */

93     public void setCellpadding(String JavaDoc value) {
94         this.htmlAttributes.put("cellpadding", value);
95     }
96
97     /**
98      * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
99      */

100     public int doEndTag() throws JspException JavaDoc {
101         String JavaDoc data = getBodyContent().getString();
102         JspWriter JavaDoc out = pageContext.getOut();
103
104         if (StringUtils.isEmpty(data)) {
105             return EVAL_PAGE;
106         }
107
108         try {
109             out.print("<table cellspacing=\"0\" ");
110             writeAttributes(out, htmlAttributes);
111             out.print(">\n");
112
113             String JavaDoc[] rows = data.split("\n");
114
115             int startingRow = 0;
116
117             if (header && rows.length > 0) {
118                 startingRow = 1; // for body
119
out.print("<thead>\n");
120                 out.print("<tr>\n");
121
122                 String JavaDoc[] cols = StringUtils.split(rows[0], "\t");
123                 for (int col = 0; col < cols.length; col++) {
124                     out.print("<th>");
125                     out.print(cols[col]);
126                     out.print("</th>\n");
127                 }
128
129                 out.print("</tr>\n");
130                 out.print("</thead>\n");
131
132             }
133
134             if (rows.length > startingRow) {
135                 out.print("<tbody>\n");
136
137                 for (int row = startingRow; row < rows.length; row++) {
138
139                     out.print("<tr");
140
141                     out.print(" class=\"");
142                     out.print(row % 2 == 0 ? "even" : "odd");
143
144                     out.print("\">\n");
145
146                     String JavaDoc[] cols = StringUtils.split(rows[row], "\t");
147
148                     for (int col = 0; col < cols.length; col++) {
149                         out.print("<td>");
150                         out.print(cols[col]);
151                         out.print("</td>\n");
152                     }
153                     out.print("</tr>\n");
154
155                 }
156                 out.print("</tbody>\n");
157             }
158             out.print("</table>\n");
159         }
160         catch (IOException JavaDoc e) {
161             // should never happen
162
log.debug(e.getMessage(), e);
163         }
164
165         return EVAL_PAGE;
166     }
167
168     /**
169      * @param out
170      * @throws IOException
171      */

172     private void writeAttributes(JspWriter JavaDoc out, Map JavaDoc attributes) throws IOException JavaDoc {
173         for (Iterator JavaDoc iter = attributes.keySet().iterator(); iter.hasNext();) {
174             String JavaDoc name = (String JavaDoc) iter.next();
175             String JavaDoc value = (String JavaDoc) attributes.get(name);
176             if (StringUtils.isNotBlank(value)) {
177                 out.write(name);
178                 out.write("=\"");
179                 out.write(value);
180                 out.write("\" ");
181             }
182         }
183     }
184
185     /**
186      * @see javax.servlet.jsp.tagext.TagSupport#release()
187      */

188     public void release() {
189         super.release();
190         header = false;
191         htmlAttributes.clear();
192     }
193 }
194
Popular Tags