1 /**
2 * Licensed under the Artistic License; you may not use this file
3 * except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://displaytag.sourceforge.net/license.html
7 *
8 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 */
12 package org.displaytag.export;
13
14 import java.io.OutputStream;
15
16 import javax.servlet.jsp.JspException;
17
18 import org.displaytag.Messages;
19 import org.displaytag.exception.BaseNestableJspTagException;
20 import org.displaytag.exception.SeverityEnum;
21 import org.displaytag.model.TableModel;
22 import org.displaytag.render.ItextTableWriter;
23
24 import com.lowagie.text.Document;
25 import com.lowagie.text.DocumentException;
26 import com.lowagie.text.PageSize;
27 import com.lowagie.text.Table;
28
29
30 /**
31 * Exporter using iText: subclasses export to any of the iText document types, such as PDF and RTF.
32 * @author Jorge L. Barroso
33 * @version $Revision$ ($Author$)
34 */
35 public abstract class DefaultItextExportView implements BinaryExportView
36 {
37
38 /**
39 * TableModel to render.
40 */
41 private TableModel model;
42
43 /**
44 * @see org.displaytag.export.ExportView#setParameters(TableModel, boolean, boolean, boolean)
45 */
46 public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
47 boolean decorateValues)
48 {
49 this.model = tableModel;
50 }
51
52 /**
53 * @see org.displaytag.export.BaseExportView#getMimeType() Meant to be overwritten by subclasses.
54 * @return null
55 */
56 public String getMimeType()
57 {
58 return null;
59 }
60
61 /**
62 * @see org.displaytag.export.BinaryExportView#doExport(OutputStream)
63 */
64 public void doExport(OutputStream out) throws JspException
65 {
66 try
67 {
68 Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40);
69 this.initItextWriter(document, out);
70 document.open();
71 Table table = new Table(this.model.getNumberOfColumns());
72 ItextTableWriter writer = new ItextTableWriter(table, document);
73 writer.writeTable(this.model, "-1");
74 document.add(table);
75 document.close();
76 }
77 catch (Exception e)
78 {
79 throw new ItextGenerationException(e);
80 }
81 }
82
83 /**
84 * Initializes the iText writer used by export view to write iText document, such as PDF or RTF iText writer.
85 * @param document The iText document to be written.
86 * @param out The output stream to which the document is written.
87 * @throws DocumentException If something goes wrong during initialization.
88 */
89 protected abstract void initItextWriter(Document document, OutputStream out) throws DocumentException;
90
91 /**
92 * Wraps iText-generated exceptions.
93 * @author Fabrizio Giustina
94 * @version $Revision$ ($Author$)
95 */
96 static class ItextGenerationException extends BaseNestableJspTagException
97 {
98
99 /**
100 * D1597A17A6.
101 */
102 private static final long serialVersionUID = 899149338534L;
103
104 /**
105 * Instantiate a new PdfGenerationException with a fixed message and the given cause.
106 * @param cause Previous exception
107 */
108 public ItextGenerationException(Throwable cause)
109 {
110 super(DefaultItextExportView.class, Messages.getString("DefaultItextExportView.errorexporting"), cause); //$NON-NLS-1$
111 this.initCause(cause);
112 }
113
114 /**
115 * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
116 */
117 public SeverityEnum getSeverity()
118 {
119 return SeverityEnum.ERROR;
120 }
121 }
122 }