Source code: info/crossbar/view/taglib/SitemapTag.java
1 /*
2 * @(#)SitemapTag.java $Revision: 1.3 $ $Date: 2003/06/04 04:55:19 $
3 *
4 * Copyright 2002 by Daniel Kehoe <kehoe@fortuity.com>
5 * All Rights Reserved
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28 package info.crossbar.view.taglib;
29
30 import java.util.*;
31 import java.util.logging.Logger;
32
33 import java.io.*;
34
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
37 import javax.servlet.ServletException;
38
39 import javax.servlet.jsp.*;
40 import javax.servlet.jsp.tagext.*;
41 import javax.servlet.jsp.jstl.core.Config;
42
43 import java.sql.SQLException;
44
45 import sun.jdbc.rowset.CachedRowSet;
46
47 import org.apache.taglibs.standard.lang.support.*;
48
49 import info.crossbar.state.App;
50 import info.crossbar.state.Sitemap;
51
52
53 /**
54 * SitemapTag class for use by <a href="http://www.crossbar.info/">Crossbar</a>
55 *
56 * @author Daniel Kehoe, <a href="http://www.fortuity.com/">Fortuity Consulting</a>
57 * @created January 23, 2002
58 * @version <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/crossbar/crossbar-sitemap/src/java/info/crossbar/view/taglib/SitemapTag.java">View source, revision history</a>
59 * $Revision: 1.3 $ $Date: 2003/06/04 04:55:19 $
60 * <p>
61 * DESCRIPTION:
62 * The SitemapTag class takes tabular data obtained from a database
63 * and outputs a table using markup (such as HTML) provided by a Formatter.
64 */
65 public class SitemapTag extends TagSupport {
66
67 /**
68 * Set up logging.
69 */
70 private static Logger log = Logger.getLogger(SitemapTag.class.getName());
71
72 private CachedRowSet table = null;
73 private String modelReference = null;
74
75 /**
76 * Method accepts a CachedRowSet. The tabular data can be the result
77 * set from an SQL query, data for a form (field names and default values), a
78 * series of messages, or any other data set organized as fields and rows.
79 *
80 * @param table a CachedRowSet object
81 */
82 public void setModelReference(String modelReference) {
83 this.modelReference = modelReference;
84 }
85
86 /**
87 * Process the start tag for this instance. See the JSP API specification for details.
88 *
89 * @return int (see the spec)
90 */
91 public int doStartTag() {
92 log.finest("ENTRY trying to output table using model \""
93 + this.modelReference + "\"");
94 try {
95 this.table = (CachedRowSet) ExpressionEvaluatorManager.evaluate("modelReference",
96 this.modelReference, CachedRowSet.class, this, pageContext);
97 JspWriter out = pageContext.getOut();
98 out.print(markup());
99 } catch (JspException jspe) {
100 log.warning(jspe.getMessage());
101 } catch (IOException ioe) {
102 log.warning(ioe.getMessage());
103 } catch (ServletException se) {
104 log.warning(se.getMessage());
105 }
106 log.finest("RETURN tag is done outputting markup and content");
107 return (SKIP_BODY);
108 }
109
110 /**
111 * Process the start tag for this instance. See the JSP API specification for details.
112 *
113 * @return String tabular data with HTML markup
114 */
115 public String markup()
116 throws ServletException {
117 log.finest("ENTRY trying to output table");
118 // cast the table to an instance of the Sitemap class:
119 Sitemap sitemap = (Sitemap) this.table;
120 StringBuffer buffer = new StringBuffer("");
121 /*
122 * The columnCount value is needed for iterating through each field of
123 * the CachedRowSet. The colspan value may be needed if some header or
124 * footer gets placed around the table. The colspan may be equal to the
125 * columnCount or it may be less if one or more columns are marked with
126 * "DELETE".
127 */
128 int columnCount = 0;
129 int colspan = 0;
130 if (sitemap == null) {
131 // just in case there's a null table:
132 buffer.append("<!-- table is null -->");
133 log.fine("table is null");
134 } else {
135 // usually the table object will contain data:
136 try {
137 // determine how many columns in the table:
138 columnCount = sitemap.getMetaData().getColumnCount();
139 } catch (SQLException e) {
140 log.finer("could not get column count!");
141 buffer.append("<!-- could not get column count! -->");
142 return buffer.toString();
143 } catch (NullPointerException npe) {
144 log.finer("could not get column count!");
145 buffer.append("<!-- could not get column count! -->");
146 return buffer.toString();
147 }
148 // begin concatenating markup and content:
149 buffer.append("<table width=\"500\" border=\"0\" frame=\"border\" "
150 + "cellpadding=\"3\" cellspacing=\"0\" bordercolor=\"#000000\" "
151 + "summary=\"table for a sitemap\">\n");
152 String locale = "en_US";
153 // get the locale from a JSTL standard session attribute:
154 if (Config.find(pageContext, Config.FMT_LOCALE) != null) locale = Config.find(pageContext, Config.FMT_LOCALE).toString();
155 if("en".equals(locale)) locale = "en_US";
156 log.fine("locale for menu is \"" + locale + "\"");
157 String columnName;
158 String columnLabel;
159 String fieldValue;
160 String location;
161 String description;
162 try {
163 for (int col = 1; col < columnCount + 1; col++) {
164 sitemap.beforeFirst();
165 int rowspan = 1;
166 while (sitemap.next()) {
167 fieldValue = sitemap.getString(col);
168 if (fieldValue != null) {
169 buffer.append("<TR align=\"left\">");
170 // add the column name:
171 columnName = (String)sitemap.getMetaData().getColumnName(col);
172 columnLabel = (String)sitemap.getMetaData().getColumnLabel(col); // not used
173 buffer.append("<TD NOWRAP>");
174 if (rowspan == 1) {
175 // location = sitemap.getLocationOfMenuCategory(columnName);
176 location = "/" + App.context.getServletContextName().toLowerCase() + sitemap.getLocationOfMenuCategory(columnName);
177 columnName = sitemap.getDisplayNameOfCategory(locale, columnName);
178 buffer.append("<a href=\"" + location + "\">" + columnName + "</a>");
179 } else {
180 buffer.append(" ");
181 }
182 buffer.append("</TD>");
183 // add the field value:
184 buffer.append("<TD NOWRAP>");
185 // location = sitemap.getLocationOfMenuItem(fieldValue);
186 location = "/" + App.context.getServletContextName().toLowerCase() + sitemap.getLocationOfMenuItem(fieldValue);
187 description = sitemap.getDescriptionOfMenuItem(locale, fieldValue);
188 fieldValue = sitemap.getDisplayNameOfMenuItem(locale, fieldValue);
189 buffer.append("<a href=\"" + location + "\">" + fieldValue + "</a>");
190 buffer.append("</TD>");
191 if (description != null) {
192 buffer.append("<TD>");
193 buffer.append(description);
194 buffer.append("</TD>");
195 }
196 buffer.append("</TR>\n");
197 }
198 rowspan++;
199 }
200 }
201 buffer.append("</table>");
202 } catch (SQLException e) {
203 buffer.append("<TR><TD>SQLException: " + e.toString() + "</TD></TR>\n");
204 }
205 }
206 log.finest("RETURN trying to output table");
207 return buffer.toString();
208 }
209 }
210