1 13 package info.magnolia.cms.taglibs.util; 14 15 import java.io.IOException ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.JspWriter ; 22 import javax.servlet.jsp.tagext.BodyTagSupport ; 23 24 import org.apache.commons.lang.StringUtils; 25 import org.slf4j.Logger; 26 import org.slf4j.LoggerFactory; 27 28 29 33 public class TableTag extends BodyTagSupport { 34 35 38 private static Logger log = LoggerFactory.getLogger(TableTag.class); 39 40 43 private static final long serialVersionUID = 1L; 44 45 private boolean header; 46 47 private Map htmlAttributes = new HashMap (); 48 49 53 public void setHeader(boolean header) { 54 this.header = header; 55 } 56 57 61 public void setClass(String value) { 62 this.htmlAttributes.put("class", value); 63 } 64 65 69 public void setStyle(String value) { 70 this.htmlAttributes.put("style", value); 71 } 72 73 77 public void setId(String value) { 78 this.htmlAttributes.put("id", value); 79 } 80 81 85 public void setCellspacing(String value) { 86 this.htmlAttributes.put("cellspacing", value); 87 } 88 89 93 public void setCellpadding(String value) { 94 this.htmlAttributes.put("cellpadding", value); 95 } 96 97 100 public int doEndTag() throws JspException { 101 String data = getBodyContent().getString(); 102 JspWriter 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 [] rows = data.split("\n"); 114 115 int startingRow = 0; 116 117 if (header && rows.length > 0) { 118 startingRow = 1; out.print("<thead>\n"); 120 out.print("<tr>\n"); 121 122 String [] 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 [] 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 e) { 161 log.debug(e.getMessage(), e); 163 } 164 165 return EVAL_PAGE; 166 } 167 168 172 private void writeAttributes(JspWriter out, Map attributes) throws IOException { 173 for (Iterator iter = attributes.keySet().iterator(); iter.hasNext();) { 174 String name = (String ) iter.next(); 175 String value = (String ) 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 188 public void release() { 189 super.release(); 190 header = false; 191 htmlAttributes.clear(); 192 } 193 } 194 | Popular Tags |