All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csv.impl.HtmlWriter Maven / Gradle / Ivy

Go to download

A library for easily accessing CSV, Excel and and other table-like data from Java

There is a newer version: 4.3.4
Show newest version
/*
 * This file is part of CSV package.
 *
 *  CSV is free software: you can redistribute it 
 *  and/or modify it under the terms of version 3 of the GNU 
 *  Lesser General Public  License as published by the Free Software 
 *  Foundation.
 *  
 *  CSV is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with CSV.  If not, see 
 *  .
 */
package csv.impl;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * Provides implementation for writing HTML table.
 * The HTML table written is controlled by separate templates for each individual tags.
 * 

* Please note! This class is mainly for convinience when aou need to create * static HTML files. You'd rather use JSP pages when you are in a JSP environment (unless you * have good reasons to use this class). *

* @author ralph * */ public class HtmlWriter extends AbstractStreamTableWriter { public static final String DEFAULT_TABLE_TEMPLATE = "\n|
\n"; // Header templates public static final String DEFAULT_THEAD_TEMPLATE = "\t\n|\t\n"; public static final String DEFAULT_THEAD_TR_TEMPLATE = "\t\t\n|\t\t\n"; public static final String DEFAULT_THEAD_TH_TEMPLATE = "\t\t\t|\n"; public static final String DEFAULT_THEAD_TH_TEMPLATE2 = "\t\t\t|\n"; // Body templates public static final String DEFAULT_TBODY_TEMPLATE = "\t\n|\t\n"; public static final String DEFAULT_TBODY_TR_TEMPLATE = "\t\t\n|\t\t\n"; public static final String DEFAULT_TBODY_TD_TEMPLATE = "\t\t\t|\n"; public static final String DEFAULT_TBODY_TR_TEMPLATE2 = "\t\t\n|\t\t\n"; public static final String DEFAULT_TBODY_TD_TEMPLATE2 = "\t\t\t|\n"; /** General template (table) */ private String tableTemplate = DEFAULT_TABLE_TEMPLATE; /** Header Template (thead) */ private String headerTemplate = DEFAULT_THEAD_TEMPLATE; /** Header Row Template (tr/even rows) */ private String headerRowTemplate = DEFAULT_THEAD_TR_TEMPLATE; /** Header Column Template (th/even columns) */ private String headerColumnTemplate = DEFAULT_THEAD_TH_TEMPLATE; /** Header Column Template (th/odd columns) */ private String headerColumnTemplate2 = DEFAULT_THEAD_TH_TEMPLATE2; /** Header Template (tbody) */ private String bodyTemplate = DEFAULT_TBODY_TEMPLATE; /** Header Row Template (tr/even rows) */ private String bodyRowTemplate = DEFAULT_TBODY_TR_TEMPLATE; /** Header Row Template (tr/odd rows) */ private String bodyRowTemplate2 = DEFAULT_TBODY_TR_TEMPLATE2; /** Header Column Template (td/even columns) */ private String dataColumnTemplate = DEFAULT_TBODY_TD_TEMPLATE; /** Header Column Template (th/odd columns) */ private String dataColumnTemplate2 = DEFAULT_TBODY_TD_TEMPLATE2; /** whether table header was written */ private boolean tableHeaderWritten = false; /** whether table header was written */ private boolean tableBodyHeaderWritten = false; /** current row index */ private int currentRowIndex = 0; /** has header row? */ private boolean hasHeaderRow = true; /** * Default Constructor. */ public HtmlWriter() { } /** * @param out output stream */ public HtmlWriter(OutputStream out) { super(out); } /** * @param file output file * @throws IOException when the file cannot be written */ public HtmlWriter(File file) throws IOException { super(file); } /** * @param file output filename * @throws IOException when the file cannot be written */ public HtmlWriter(String file) throws IOException { super(file); } /** * Closes the writer by printing the footer of the table. * @see csv.impl.AbstractStreamTableWriter#close() */ @Override public void close() { printTableBodyFooter(); printTableFooter(); super.close(); } /** * @see csv.impl.AbstractTableWriter#init() */ @Override protected void init() { currentRowIndex = 0; tableHeaderWritten = false; super.init(); } /** * Prints the row. * if the writer was configured to have a header row then first call will write a HTML header row * @param columns columns to be written * @see csv.TableWriter#printRow(java.lang.Object[]) */ @Override public void printRow(Object[] columns) throws IOException { printTableHeader(); if ((getRowCount() == 0) && isHasHeaderRow()) { // print the header row printTableHeadHeader(); printHeaderRow(columns); printTableHeadFooter(); } else { printTableBodyHeader(); printDataRow(columns, currentRowIndex); currentRowIndex++; } incrementRowCount(); getWriter().flush(); } /** * Prints the header row into the stream. * @param columns columns to be written */ public void printHeaderRow(Object columns[]) { printTableHeadRowHeader(); for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy