Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: info/crossbar/view/taglib/DatagridTag.java


1   /*
2    *  @(#)DatagridTag.java $Revision: 1.3 $ $Date: 2003/06/04 04:55:20 $
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  
42  import java.sql.SQLException;
43  
44  import sun.jdbc.rowset.CachedRowSet;
45  
46  import org.apache.taglibs.standard.lang.support.*;
47  
48  import info.crossbar.view.formatter.FormatterAbstraction;
49  
50  
51  /**
52   * DatagridTag class for use by <a href="http://www.crossbar.info/">Crossbar</a>
53   *
54   * @author     Daniel Kehoe, <a href="http://www.fortuity.com/">Fortuity Consulting</a>
55   * @created    January 23, 2002
56   * @version    <a href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/crossbar/crossbar-sitemap/src/java/info/crossbar/view/taglib/DatagridTag.java">View source, revision history</a>
57   * $Revision: 1.3 $ $Date: 2003/06/04 04:55:20 $
58   * <p>
59   * DESCRIPTION:
60   * The DatagridTag class takes tabular data obtained from a database
61   * and outputs a table using markup (such as HTML) provided by a Formatter.
62   */
63  public class DatagridTag extends TagSupport {
64  
65    /**
66     * Set up logging.
67     */
68    private static Logger log = Logger.getLogger(DatagridTag.class.getName());
69  
70    private CachedRowSet table = null;
71    private String modelReference = null;
72    private String formatterName = null;
73    private String headerstyle = null;
74    private String bodystyle = null;
75    private String footerstyle = null;
76  
77    /**
78     * Method accepts a CachedRowSet. The tabular data can be the result
79     * set from an SQL query, data for a form (field names and default values), a
80     * series of messages, or any other data set organized as fields and rows.
81     *
82     * @param  table  a CachedRowSet object
83     */
84    public void setModelReference(String modelReference) {
85      this.modelReference = modelReference;
86    }
87  
88    /**
89     * A Model can recommend how the tabular data should be displayed (for example,
90     * as a form or messages, or as a table of multiple rows).
91     *
92     * @param  formatter  a formatter class such as &quot;info.crossbar.view.formatter.html.FormatterForForm&quot;
93     */
94    public void setFormatter(String formatter) {
95      if (formatter != null) {
96        this.formatterName = formatter;
97      } else log.fine("formatterName is null");
98    }
99  
100 
101   /**
102    * Accepts a String from a jsp page specifying a CSS style for a table
103    * header.
104    *
105    * @param  align  a String such as &quot;tableheader1&quot;
106    */
107   public void setHeaderstyle(String headerstyle) {
108     this.headerstyle = headerstyle;
109   }
110 
111   /**
112    * Accepts a String from a jsp page specifying a CSS style for table
113    * body cells.
114    *
115    * @param  align  a String such as &quot;tablebody&quot;
116    */
117   public void setBodystyle(String bodystyle) {
118     this.bodystyle = bodystyle;
119   }
120 
121   /**
122    * Accepts a String from a jsp page specifying a CSS style for a table
123    * footer.
124    *
125    * @param  align  a String such as &quot;tablefooter1&quot;
126    */
127   public void setFooterstyle(String footerstyle) {
128     this.footerstyle = footerstyle;
129   }
130 
131   /**
132    * Process the start tag for this instance. See the JSP API specification for details.
133    *
134    * @return    int (see the spec)
135    */
136   public int doStartTag() {
137     log.finest("ENTRY trying to output table using model \"" 
138     + this.modelReference + "\" and formatter \"" + this.formatterName + "\"");
139     try {
140       this.table = (CachedRowSet) ExpressionEvaluatorManager.evaluate("modelReference", 
141         this.modelReference, CachedRowSet.class, this, pageContext);
142       JspWriter out = pageContext.getOut();
143       out.print(markup());
144     } catch (JspException jspe) {
145       log.warning(jspe.getMessage());
146     } catch (IOException ioe) {
147       log.warning(ioe.getMessage());
148     } catch (ServletException se) {
149       log.warning(se.getMessage());
150     }
151     log.finest("RETURN tag is done outputting markup and content");
152     return (SKIP_BODY);
153   }
154 
155   /**
156    * Process the start tag for this instance. See the JSP API specification for details.
157    *
158    * @return    String tabular data with HTML markup
159    */
160   public String markup()
161     throws ServletException {
162     log.finest("ENTRY trying to output table");
163     StringBuffer buffer = new StringBuffer("");
164     if (this.table == null) {
165       // just in case there's a null table:
166       buffer.append("<!-- table is null -->");
167       log.fine("table is null");
168     } else {
169       if (this.formatterName == null) {
170         try {
171           // look for a formatter name hidden in the table title:
172           String title = table.getTableName();
173           if ("TEXT".equals(title.toUpperCase())) {
174             this.formatterName = "info.crossbar.view.formatter.html.FormatterForText";
175           } else if ("MESSAGES".equals(title.toUpperCase())) {
176             this.formatterName = "info.crossbar.view.formatter.html.FormatterForMessages";
177           } else if ("TABLE".equals(title.toUpperCase())) {
178             this.formatterName = "info.crossbar.view.formatter.html.FormatterForTable";
179           } else this.formatterName = "info.crossbar.view.formatter.html.FormatterForTable";
180         } catch (SQLException e) {
181           log.finest("could not get table title!");
182         } 
183       } else if ("TEXT".equals(this.formatterName)) {
184         this.formatterName = "info.crossbar.view.formatter.html.FormatterForText";
185       } else if ("MESSAGES".equals(this.formatterName)) {
186         this.formatterName = "info.crossbar.view.formatter.html.FormatterForMessages";
187       } else if ("TABLE".equals(this.formatterName)) {
188         this.formatterName = "info.crossbar.view.formatter.html.FormatterForTable"; 
189       } // else just use what was given!
190       log.finest("using formatter \"" + this.formatterName + "\"");
191       /*
192        * We load a class dynamically and
193        * instantiate an object based on a parameter passed from the jsp page.
194        * We have an interface named "FormatterAbstraction" which describes possible methods.
195        * Developers can implement FormatterAbstraction and make any new kind of
196        * Formatter which is used to mix markup and content for output.
197        */
198       // the FormatterAbstraction class is an interface that defines a "markup" method:
199       FormatterAbstraction formatter = null;
200       try {
201         // instantiate the Formatter:
202         formatter = (FormatterAbstraction)Class.forName(this.formatterName, true, DatagridTag.class.getClassLoader()).newInstance();
203         // make the formatter do its work:
204         buffer.append(formatter.markup((HttpServletRequest)pageContext.getRequest(), 
205           this.table, this.headerstyle, this.bodystyle, this.footerstyle));
206       } catch (ClassNotFoundException cnfe) {
207         String msg = ("The " + DatagridTag.class.getName() + " class could not "
208           + "find a Formatter: "
209           + cnfe.getMessage());
210         log.fine(msg);
211         throw new ServletException(msg);
212       } catch (InstantiationException ie) {
213         String msg = ("The " + DatagridTag.class.getName() + " class could not "
214           + "instantiate a Formatter: "
215           + ie.getMessage());
216         log.fine(msg);
217         throw new ServletException(msg);
218       } catch (IllegalAccessException iae) {
219         String msg = ("The " + DatagridTag.class.getName() + " class could not "
220           + "access a Formatter: "
221           + iae.getMessage());
222         log.fine(msg);
223         throw new ServletException(msg);
224       }
225     }
226     log.finest("RETURN done marking up content");
227     return buffer.toString();
228   }
229 }
230