//
// Project "Portal"
//
// Michael Meyling
// Stoltenstrasse 38
// 22119 Hamburg
// Germany

package com.meyling.portal;

import java.io.*;

/**
 * This class wrapps the class <code>FileWriter</code> and gives
 * some convenient methods for generating an HTML file.
 * Especially there will be written a file header automatically.
 *
 * @version $Revision: 1.3 $
 * @author  Michael Meyling
 */
class HtmlFileWriter {

    /**
     * file that is written
     */
    private FileWriter out = null;

    /**
     * name of the output file
     */
    private String fileName = null;

    /**
     * Contstructs a <code>FileWriter</code> object.
     * @param   outputName  output file name without ".html"
     * @throws  IOException if the construction failed
     */
    HtmlFileWriter(String outputName) throws IOException {
        fileName = outputName + ".html";
        out = new FileWriter(fileName);
        header();
    }


    /**
     * Prints a line into the output file.
     * @param   line        line to print
     * @throws  IOException if a writing exception occurs
     */
    public void println(String line) throws IOException {
        print(line + "\n");
    }


    /**
     * Prints an empty line into the output file.
     * @throws  IOException if a writing exception occurs
     */
    public void println() throws IOException {
        print("\n");
    }


    /**
     * Prints a text into the output file
     * @param   text        text to print
     * @throws  IOException if a writing exception occurs
     */
    public void print(String text) throws IOException {
        if (out == null) {
            throw new IOException("Coulnd't write to closed file");
        }
        out.write(text);
        out.flush();
    }


    /**
     * Closes output.
     * @throws  IOException if an writing exception occured
     */
    public void close() throws IOException {
        out.close();
        out = null;
    }


    /**
     * Writes the file header.
     * @throws  IOException bei Schreibfehlern
     */
    private void header() throws IOException {
    }


    /**
     * Returns name of output file (including ".html").
     * @return name of output file
     */
     String getFileName() {
        return fileName;
     }


}
