Clover coverage report - Maven Clover report
Coverage timestamp: Sun Jan 3 2010 19:08:46 CET
file stats: LOC: 277   Methods: 10
NCLOC: 143   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
PdfView.java 80% 92.7% 80% 89.3%
coverage coverage
 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.awt.Color;
 15    import java.io.OutputStream;
 16    import java.util.Iterator;
 17   
 18    import javax.servlet.jsp.JspException;
 19   
 20    import org.apache.commons.lang.ObjectUtils;
 21    import org.apache.commons.lang.StringUtils;
 22    import org.displaytag.Messages;
 23    import org.displaytag.exception.BaseNestableJspTagException;
 24    import org.displaytag.exception.SeverityEnum;
 25    import org.displaytag.model.Column;
 26    import org.displaytag.model.ColumnIterator;
 27    import org.displaytag.model.HeaderCell;
 28    import org.displaytag.model.Row;
 29    import org.displaytag.model.RowIterator;
 30    import org.displaytag.model.TableModel;
 31    import org.displaytag.util.TagConstants;
 32   
 33    import com.lowagie.text.BadElementException;
 34    import com.lowagie.text.Cell;
 35    import com.lowagie.text.Chunk;
 36    import com.lowagie.text.Document;
 37    import com.lowagie.text.Element;
 38    import com.lowagie.text.Font;
 39    import com.lowagie.text.FontFactory;
 40    import com.lowagie.text.HeaderFooter;
 41    import com.lowagie.text.PageSize;
 42    import com.lowagie.text.Phrase;
 43    import com.lowagie.text.Rectangle;
 44    import com.lowagie.text.Table;
 45    import com.lowagie.text.pdf.PdfWriter;
 46   
 47   
 48    /**
 49    * PDF exporter using IText. This class is provided more as an example than as a "production ready" class: users
 50    * probably will need to write a custom export class with a specific layout.
 51    * @author Ivan Markov
 52    * @author Fabrizio Giustina
 53    * @version $Revision: 1081 $ ($Author: fgiust $)
 54    */
 55    public class PdfView implements BinaryExportView
 56    {
 57   
 58    /**
 59    * TableModel to render.
 60    */
 61    private TableModel model;
 62   
 63    /**
 64    * export full list?
 65    */
 66    private boolean exportFull;
 67   
 68    /**
 69    * include header in export?
 70    */
 71    private boolean header;
 72   
 73    /**
 74    * decorate export?
 75    */
 76    private boolean decorated;
 77   
 78    /**
 79    * This is the table, added as an Element to the PDF document. It contains all the data, needed to represent the
 80    * visible table into the PDF
 81    */
 82    private Table tablePDF;
 83   
 84    /**
 85    * The default font used in the document.
 86    */
 87    private Font smallFont;
 88   
 89    /**
 90    * @see org.displaytag.export.ExportView#setParameters(TableModel, boolean, boolean, boolean)
 91    */
 92  4 public void setParameters(TableModel tableModel, boolean exportFullList, boolean includeHeader,
 93    boolean decorateValues)
 94    {
 95  4 this.model = tableModel;
 96  4 this.exportFull = exportFullList;
 97  4 this.header = includeHeader;
 98  4 this.decorated = decorateValues;
 99    }
 100   
 101    /**
 102    * Initialize the main info holder table.
 103    * @throws BadElementException for errors during table initialization
 104    */
 105  4 protected void initTable() throws BadElementException
 106    {
 107  4 tablePDF = new Table(this.model.getNumberOfColumns());
 108  4 tablePDF.setDefaultVerticalAlignment(Element.ALIGN_TOP);
 109  4 tablePDF.setCellsFitPage(true);
 110  4 tablePDF.setWidth(100);
 111   
 112  4 tablePDF.setPadding(2);
 113  4 tablePDF.setSpacing(0);
 114   
 115  4 smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL, new Color(0, 0, 0));
 116   
 117    }
 118   
 119    /**
 120    * @see org.displaytag.export.BaseExportView#getMimeType()
 121    * @return "application/pdf"
 122    */
 123  4 public String getMimeType()
 124    {
 125  4 return "application/pdf"; //$NON-NLS-1$
 126    }
 127   
 128    /**
 129    * The overall PDF table generator.
 130    * @throws JspException for errors during value retrieving from the table model
 131    * @throws BadElementException IText exception
 132    */
 133  4 protected void generatePDFTable() throws JspException, BadElementException
 134    {
 135  4 if (this.header)
 136    {
 137  4 generateHeaders();
 138    }
 139  4 tablePDF.endHeaders();
 140  4 generateRows();
 141    }
 142   
 143    /**
 144    * @see org.displaytag.export.BinaryExportView#doExport(OutputStream)
 145    */
 146  4 public void doExport(OutputStream out) throws JspException
 147    {
 148  4 try
 149    {
 150    // Initialize the table with the appropriate number of columns
 151  4 initTable();
 152   
 153    // Initialize the Document and register it with PdfWriter listener and the OutputStream
 154  4 Document document = new Document(PageSize.A4.rotate(), 60, 60, 40, 40);
 155  4 document.addCreationDate();
 156  4 HeaderFooter footer = new HeaderFooter(new Phrase(TagConstants.EMPTY_STRING, smallFont), true);
 157  4 footer.setBorder(Rectangle.NO_BORDER);
 158  4 footer.setAlignment(Element.ALIGN_CENTER);
 159   
 160  4 PdfWriter.getInstance(document, out);
 161   
 162    // Fill the virtual PDF table with the necessary data
 163  4 generatePDFTable();
 164  4 document.open();
 165  4 document.setFooter(footer);
 166  4 document.add(this.tablePDF);
 167  4 document.close();
 168   
 169    }
 170    catch (Exception e)
 171    {
 172  0 throw new PdfGenerationException(e);
 173    }
 174    }
 175   
 176    /**
 177    * Generates the header cells, which persist on every page of the PDF document.
 178    * @throws BadElementException IText exception
 179    */
 180  4 protected void generateHeaders() throws BadElementException
 181    {
 182  4 Iterator iterator = this.model.getHeaderCellList().iterator();
 183   
 184  4 while (iterator.hasNext())
 185    {
 186  4 HeaderCell headerCell = (HeaderCell) iterator.next();
 187   
 188  4 String columnHeader = headerCell.getTitle();
 189   
 190  4 if (columnHeader == null)
 191    {
 192  0 columnHeader = StringUtils.capitalize(headerCell.getBeanPropertyName());
 193    }
 194   
 195  4 Cell hdrCell = getCell(columnHeader);
 196  4 hdrCell.setGrayFill(0.9f);
 197  4 hdrCell.setHeader(true);
 198  4 tablePDF.addCell(hdrCell);
 199   
 200    }
 201    }
 202   
 203    /**
 204    * Generates all the row cells.
 205    * @throws JspException for errors during value retrieving from the table model
 206    * @throws BadElementException errors while generating content
 207    */
 208  4 protected void generateRows() throws JspException, BadElementException
 209    {
 210    // get the correct iterator (full or partial list according to the exportFull field)
 211  4 RowIterator rowIterator = this.model.getRowIterator(this.exportFull);
 212    // iterator on rows
 213  4 while (rowIterator.hasNext())
 214    {
 215  12 Row row = rowIterator.next();
 216   
 217    // iterator on columns
 218  12 ColumnIterator columnIterator = row.getColumnIterator(this.model.getHeaderCellList());
 219   
 220  12 while (columnIterator.hasNext())
 221    {
 222  12 Column column = columnIterator.nextColumn();
 223   
 224    // Get the value to be displayed for the column
 225  12 Object value = column.getValue(this.decorated);
 226   
 227  12 Cell cell = getCell(ObjectUtils.toString(value));
 228  12 tablePDF.addCell(cell);
 229    }
 230    }
 231    }
 232   
 233    /**
 234    * Returns a formatted cell for the given value.
 235    * @param value cell value
 236    * @return Cell
 237    * @throws BadElementException errors while generating content
 238    */
 239  16 private Cell getCell(String value) throws BadElementException
 240    {
 241  16 Cell cell = new Cell(new Chunk(StringUtils.trimToEmpty(value), smallFont));
 242  16 cell.setVerticalAlignment(Element.ALIGN_TOP);
 243  16 cell.setLeading(8);
 244  16 return cell;
 245    }
 246   
 247    /**
 248    * Wraps IText-generated exceptions.
 249    * @author Fabrizio Giustina
 250    * @version $Revision: 1081 $ ($Author: fgiust $)
 251    */
 252    static class PdfGenerationException extends BaseNestableJspTagException
 253    {
 254   
 255    /**
 256    * D1597A17A6.
 257    */
 258    private static final long serialVersionUID = 899149338534L;
 259   
 260    /**
 261    * Instantiate a new PdfGenerationException with a fixed message and the given cause.
 262    * @param cause Previous exception
 263    */
 264  0 public PdfGenerationException(Throwable cause)
 265    {
 266  0 super(PdfView.class, Messages.getString("PdfView.errorexporting"), cause); //$NON-NLS-1$
 267    }
 268   
 269    /**
 270    * @see org.displaytag.exception.BaseNestableJspTagException#getSeverity()
 271    */
 272  0 public SeverityEnum getSeverity()
 273    {
 274  0 return SeverityEnum.ERROR;
 275    }
 276    }
 277    }