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

Quick Search    Search Deep

Source code: jbreport/render/BasicHTMLRenderer.java


1   /*
2    * $Id: BasicHTMLRenderer.java,v 1.1.1.1 2000/08/31 13:14:38 grantfin Exp $
3    *
4    * jbReport - A reporting library for Java
5    * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package jbreport.render;
22  
23  import java.io.PrintWriter;
24  import java.util.List;
25  
26  import jbreport.Constants;
27  import jbreport.ReportElement;
28  import jbreport.ReportException;
29  import jbreport.ReportStylesheet;
30  import jbreport.Repository;
31  import jbreport.core.AbstractReportElement;
32  import jbreport.core.DefaultReportVisitor;
33  import jbreport.core.ReportVisitorState;
34  
35  /**
36   * This will render the document in a basic html format. This is mainly to be
37   * used for debugging.
38   *
39   * @author Grant Finnemore
40   * @version $Revision: 1.1.1.1 $
41   */
42  public 
43  class BasicHTMLRenderer extends DefaultReportVisitor {
44  
45     /** The no-break space html character */
46     private static final String BR = "&nbsp;";
47  
48     /** The writer that will be used to output formatted html data */
49     private PrintWriter out;
50  
51     /** The offset from the current depth */
52     private int offset = 0;
53  
54     /** The stylesheet for this document */
55     private ReportStylesheet stylesheet;
56  
57     //
58     // Constructors
59     //
60  
61     public BasicHTMLRenderer(PrintWriter out) {
62        this.out = out;
63     }
64  
65     //
66     // Methods overloaded from DefaultReportVisitor
67     //
68  
69     public void visitDocument(ReportElement elem, ReportVisitorState state) 
70        throws ReportException {
71  
72        stylesheet = elem.getStylesheet();
73  
74        if(state.isStart()) {
75           writeln("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 "
76                   + "Transitional//EN\" \"http://www.w3.org/TR/xhtml1/"
77                   + "DTD/xhtml1-transitional.dtd\">", state); 
78           writeln("<html xmlns=\"http://www.w3.org/1999/xhtml\">", state);
79           offset++;
80           writeln("<head>", state);
81           offset++;
82           writeln("<title>Report</title>", state);
83           offset--;
84           writeln("</head>", state);
85           StringBuffer buf = new StringBuffer();
86           buf.append("<body");
87           appendStyleValue(buf, "body", elem, "bgcolor");
88           buf.append(">");
89           writeln(buf, state);
90        }
91        else {
92           writeln("</body>", state);
93           offset--;
94           writeln("</html>", state);
95        }
96     }
97  
98     public void visitFragment(ReportElement elem, ReportVisitorState state) 
99        throws ReportException {
100       
101       if(state.isStart()) {
102          writeln(elem.getString("cdata", BR), state);
103       }
104    }
105 
106    public void visitInclude(ReportElement elem, ReportVisitorState state) 
107       throws ReportException {
108       
109       if(state.isStart()) {
110          Repository repository = ((AbstractReportElement)elem).getRepository();
111          ReportElement fragment = repository.fetchFragment(elem.getName());
112          writeln(fragment.getString("cdata", BR), state);
113       }
114    }
115 
116    public void visitQueryBoundResult(ReportElement elem, 
117                                      ReportVisitorState state) 
118       throws ReportException {
119 
120       if(state.isStart()) {
121          writeln(elem.getString("value", BR), state);
122       }
123    }
124    
125    public void visitTLine(ReportElement elem, ReportVisitorState state) 
126       throws ReportException {
127 
128       if(state.isStart()) {
129          StringBuffer buf = new StringBuffer();
130          buf.append("<table");
131 //           writeln("<table border=\"1\"><tr><td>", state);
132          appendStyleValue(buf, "table", elem, "bgcolor");
133          appendStyleValue(buf, "table", elem, "border");
134          appendStyleValue(buf, "table", elem, "width");
135          appendStyleValue(buf, "table", elem, "cellspacing");
136          appendStyleValue(buf, "table", elem, "cellpadding");
137          buf.append("><tr><td");
138          appendStyleValue(buf, "th", elem, "bgcolor");
139          appendStyleValue(buf, "th", elem, "width");
140          appendStyleValue(buf, "th", elem, "align");
141          buf.append(">");
142          writeln(buf, state);
143       }
144       else {
145          writeln("</td></tr></table>", state);
146       }
147    }
148 
149    public void visitTable(ReportElement elem, ReportVisitorState state) 
150       throws ReportException {
151 
152       if(state.isStart()) {
153          StringBuffer buf = new StringBuffer();
154          buf.append("<table");
155          appendStyleValue(buf, "table", elem, "bgcolor");
156          appendStyleValue(buf, "table", elem, "border");
157          appendStyleValue(buf, "table", elem, "width");
158          appendStyleValue(buf, "table", elem, "cellspacing");
159          appendStyleValue(buf, "table", elem, "cellpadding");
160          buf.append(">");
161          writeln(buf, state);
162       }
163       else {
164          writeln("</table>", state);
165       }
166    }
167 
168    public void visitTableHeaderRow(ReportElement elem, 
169                                    ReportVisitorState state)
170       throws ReportException {
171 
172       if(state.isStart()) {
173          writeln("<tr>", state);
174       }
175       else {
176          writeln("</tr>", state);
177       }
178    }
179 
180    public void visitTableHeaderItem(ReportElement elem, 
181                                     ReportVisitorState state)
182       throws ReportException {
183 
184       StringBuffer buf = new StringBuffer();
185       if(state.isStart()) {
186          buf.append("<th");
187          appendStyleValue(buf, "th", elem, "bgcolor");
188          appendStyleValue(buf, "th", elem, "width");
189          appendStyleValue(buf, "th", elem, "align");
190          buf.append(">");
191          buf.append(elem.getString("cdata", elem.getString("id", BR)));
192          writeln(buf, state);
193       }
194       else {
195          buf.append("</th>");
196          writeln(buf, state);
197       }
198    }
199 
200    public void visitTableRow(ReportElement elem, ReportVisitorState state)
201       throws ReportException {
202 
203       if(state.isStart()) {
204          StringBuffer buf = new StringBuffer();
205          List list = elem.getList(Constants.RE_TABLE_CELLS);
206          if(list.size() > 0) {
207             buf.append("<tr>");
208             for(int i = 0; i < list.size(); i++) {
209                Object o = list.get(i);
210                buf.append("<td");
211 //                 appendStyleValue(buf, "td", elem, "bgcolor");
212 //                 appendStyleValue(buf, "th", elem, "width");
213                ReportElement header = getHeaderForCell(elem, i);
214                if(header != null) {
215                   appendStyleValue(buf, "th", header, "align");
216                }
217                buf.append(">");
218                buf.append(o != null ? o : BR).append("</td>");
219             }
220             buf.append("</tr>");
221          }
222          writeln(buf, state);
223       }
224    }
225 
226    private ReportElement getHeaderForCell(ReportElement tableRow, int cell)
227       throws ReportException {
228       ReportElement table = tableRow.getParent();
229       ReportElement columns = table.getElement(Constants.RE_TABLE_HEADER_ROW);
230       List colItems = columns.getList(Constants.RE_CHILDREN);
231       ReportElement result = null;
232       if(colItems != null && colItems.size() > cell) {
233          result = (ReportElement)colItems.get(cell);
234       }
235       return result;
236    }
237 
238    public void visitAggregate(ReportElement elem, ReportVisitorState state)
239       throws ReportException {
240 
241    }   
242 
243    //
244    // Implementation methods
245    //
246 
247    private void writeln(String data, ReportVisitorState state) {
248       if(data != null) {
249          writeln(new StringBuffer(data), state);
250       }
251    }
252 
253    private void writeln(StringBuffer data, ReportVisitorState state) {
254       for(int i = 0; i < state.getDepth() + offset; i++) {
255          data.insert(0, "  ");
256       }
257       if(state.isEnd()) {
258          data.insert(0, "  ");
259       }
260       out.println(data);
261    }
262 
263    private void appendStyleValue(StringBuffer buf, String objectType,
264                                  ReportElement elem, String paramType)
265       throws ReportException {
266       String value = stylesheet.getStyleValue(objectType,
267                                               elem.getString("class", null),
268                                               paramType);
269       if(value != null) {
270          buf.append(" ").append(paramType).append("=\"").append(value);
271          buf.append("\"");
272       }
273    }
274 }
275