com.hotaviano.tableexporter.csv.CSVExporter.java Source code

Java tutorial

Introduction

Here is the source code for com.hotaviano.tableexporter.csv.CSVExporter.java

Source

/*
 * This software is a html table text convert to conventional formats
 * Copyright (C) 2014  Melo, Hotaviano
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package com.hotaviano.tableexporter.csv;

import com.hotaviano.tableexporter.DefaultStringTableParser;
import com.hotaviano.tableexporter.ExporterInterface;
import com.hotaviano.tableexporter.StringTableParser;
import com.lowagie.text.DocumentException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;

/**
 * Implementation of CSV exporter
 * 
 * @author Melo, Hotaviano <melo.hotaviano@gmail.com>
 */
public class CSVExporter implements ExporterInterface {

    @Override
    public byte[] export(String htmlTable) throws DocumentException {
        Document document = createDocument(htmlTable);
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        PrintWriter writer = new PrintWriter(output);

        createHeader(document, writer);
        createBody(document, writer);

        writer.close();

        return output.toByteArray();
    }

    private void createBody(Document doc, PrintWriter writer) {
        Element tbody = doc.getRootElement().getChild("tbody");

        for (Element tr : tbody.getChildren()) {
            String line = "";
            for (Element td : tr.getChildren()) {
                line += td.getText() + ",";
            }

            writer.println(line.substring(0, line.length() - 1));
        }
    }

    private void createHeader(Document doc, PrintWriter writer) {
        Element thead = doc.getRootElement().getChild("thead").getChildren().get(0);

        String header = "";
        for (Element elem : thead.getChildren()) {
            header += elem.getText() + ",";
        }

        writer.println(header.subSequence(0, header.length() - 1));
    }

    private Document createDocument(String html) throws DocumentException {
        StringTableParser parser = new DefaultStringTableParser();

        try {
            return parser.parse(html);
        } catch (JDOMException | IOException ex) {
            throw new DocumentException(ex);
        }
    }

}