ca.qc.cegepoutaouais.tge.pige.server.report.ReportHelper.java Source code

Java tutorial

Introduction

Here is the source code for ca.qc.cegepoutaouais.tge.pige.server.report.ReportHelper.java

Source

/*
 * Copyright 2010, 2011 Renaud Brub
 *
 * This file is part of PIGE.
 *
 * PIGE 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 3 of the License, or
 * (at your option) any later version.
 *
 * PIGE 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 PIGE.  If not, see <http://www.gnu.org/licenses/>.
 */
package ca.qc.cegepoutaouais.tge.pige.server.report;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * Contient de mthodes utilitaires pour la gnration de rapport.
 *
 * @author Renaud Brub
 */
public class ReportHelper {

    /**
     * Instance unique.
     */
    private static final ReportHelper helper = new ReportHelper();

    private ReportHelper() {
    }

    /**
     * Charge un fichier de ressources.
     *
     * @param path d'accs du fichier de ressources.
     * @return un flux d'entr sur le fichier
     */
    public static InputStream loadFileAsStream(String path) {
        if (path == null || path.isEmpty()) {
            throw new IllegalArgumentException("path ne doit pas tre nul ni vide.");
        }
        return helper.getClass().getResourceAsStream(path);
    }

    /**
     * Convertit les donnes d'un flux d'entr en chaine de caractres.
     *
     * @param is flux d'entr  convertir
     * @return chaine de caractres contenant les donnes du flux
     * @throws ReportException
     */
    public static String convertStreamToString(InputStream is) throws ReportException {
        if (is == null) {
            throw new IllegalArgumentException("is ne doit pas tre nul.");
        }
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (IOException ioex) {
            throw new ReportException(ioex);
        } finally {
            try {
                is.close();
            } catch (IOException ignored) {
            }
        }
        return writer.toString();
    }

    /**
     * Applique un style CSS  un document HTML.
     * La section 'head' du document HTML est extraite afin d'y insrer
     * le style CSS.
     *
     * @param htmlDoc document HTML  traiter
     * @param style style CSS  insrer
     * @throws ReportException
     */
    public static void applyStyleToHTML(Document htmlDoc, String style) throws ReportException {
        Element headerTag = (Element) htmlDoc.selectSingleNode("//head");
        Element styleTag = DocumentHelper.createElement("style");
        styleTag.addText(style);
        headerTag.add(styleTag);
    }

}