view src/FileUtils.java @ 14:eec2474fdaaa

* src/ClassInfo.java: Added new helper class. * src/FileUtils.java: Added new method for reading contents of text file. Refactored. * src/ReportGenerator.java: New functionality: methods coverage are printed in package list (previously only class coverage were printed). Refactored. * templates/all_packages_template.html: * templates/index.html: Changed width of left column. * templates/style.css: Tables have black borders (they are more visible). * Makefile: Updated according to previous changes.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Fri, 23 Mar 2012 11:55:40 +0100
parents f010a4361c96
children b6c8372f5723
line wrap: on
line source

/*
  Test coverage tool.

   Copyright (C) 2012 Red Hat

This tool 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, or (at your option)
any later version.

This tool 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 tool; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Basic file utilities to avoid boring file handling.
 *
 * @author Pavel Tisnovsky
 */
class FileUtils
{
    /**
     * Default encoding used for all input/output operations.
     */
    private static final String DEFAULT_ENCODING = "UTF-8";

    /**
     * EOF constant used as special return value in some methods.
     */
    private static final int EOF = -1;

    /**
     * Read content of given text file and return it as list of strings. No
     * exception is thrown during reading.
     * 
     * @param fileName
     *            name of file to be read
     * @return list of string containing content of text file
     */
    static List<String> readTextFile(String fileName)
    {
        return readTextFile(fileName, true);
    }

    /**
     * Read content of given text file and return it as list of strings. No
     * exception is thrown during reading.
     * 
     * @param fileName
     *            name of file to be read
     * @param printFileNotFoundException
     *            whether to print an exception when file is not found
     * @return list of string containing content of text file
     */
    static List<String> readTextFile(String fileName, boolean printFileNotFoundException)
    {
        BufferedReader reader = null;
        List<String> out = new LinkedList<String>();
        try
        {
            // try to open file for reading
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), DEFAULT_ENCODING));
            readAllLinesFromTextFile(reader, out);
        }
        catch (FileNotFoundException e)
        {
            // might happen - empty list is returned in this case
            if (printFileNotFoundException)
            {
                // in some cases we don't want to see this exception
                // (because some classes, for example, could not be tested at all)
                e.printStackTrace();
            }
        }
        catch (IOException e)
        {
            // might happen - empty list is returned in this case
            // make sure the list is empty
            out.clear();
            e.printStackTrace();
        }
        finally
        {
            closeBufferedReader(reader);
        }
        // return list containing content of text file
        return out;
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     * 
     * @param fileName
     *            name of file to be read
     * @param lines
     *            of string containing content of text file
     */
    static void writeTextFile(String fileName, List<String> lines)
    {
        BufferedWriter fout = null;
        try
        {
            fout = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), DEFAULT_ENCODING));
            // write all lines to a text file
            writeAllLinesToTextFile(fout, lines);
        }
        catch (IOException e)
        {
            // might happen - empty list is returned in this case
            e.printStackTrace();
        }
        finally
        {
            closeBufferedWriter(fout);
        }
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     *
     * @param reportDirectory
     * @param string
     * @param lines of string containing content of text file
     */
    static void writeTextFile(String reportDirectory, String string, List<String> lines)
    {
        writeTextFile(new File(reportDirectory, string), lines);
    }

    /**
     * Write list of string to a file with given name. No exception is thrown
     * during reading.
     * 
     * @param file
     *            representing file name
     * @param lines
     *            of string containing content of text file
     */
    static void writeTextFile(File file, List<String> lines)
    {
        writeTextFile(file.getPath(), lines);
    }

    /**
     * Try to close buffered reader.
     *
     * @param bufferedReader instance of buffered reader or null
     */
    private static void closeBufferedReader(BufferedReader bufferedReader)
    {
        // try to close the buffered reader
        try
        {
            // for the easier use, it is possible to call this method with null parameter
            if (bufferedReader != null)
            {
                bufferedReader.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Try to close buffered reader.
     * 
     * @param bufferedWriter
     *            instance of buffered writer or null
     */
    private static void closeBufferedWriter(BufferedWriter bufferedWriter)
    {
        // try to close the buffered writer
        try
        {
            // for the easier use, it is possible to call this method with null parameter
            if (bufferedWriter != null)
            {
                bufferedWriter.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Read all lines from text file and add them to a list of strings.
     * 
     * @param bufferedReader
     *            instance of buffered reader
     * @param lines
     *            list of string to be filled
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    private static void readAllLinesFromTextFile(BufferedReader bufferedReader, Collection<String> lines) throws IOException
    {
        String line;
        // read lines from a text file
        while ((line = bufferedReader.readLine()) != null)
        {
            lines.add(line);
        }
    }

    /**
     * Write content of the list of strings to a file.
     * 
     * @param bufferedWriter
     *            instance of buffered writer
     * @param lines
     *            list of string
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    private static void writeAllLinesToTextFile(BufferedWriter bufferedWriter, List<String> lines) throws IOException
    {
        // for all lines
        for (String line : lines)
        {
            bufferedWriter.write(line);
            // new line character should be added after each string
            bufferedWriter.write('\n');
        }
    }

    /**
     * Read one byte from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readOneByte(FileInputStream fileInputStream) throws IOException
    {
        // try to read one byte from the input stream
        int i = fileInputStream.read();
        // -1 means that EOF is reached
        if (i == EOF)
        {
            throw new EOFException("End of file reached!");
        }
        return i;
    }

    /**
     * Read two bytes from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readTwoBytes(FileInputStream fileInputStream) throws IOException
    {
        // try to read two bytes from the input stream
        int i1 = readOneByte(fileInputStream);
        int i2 = readOneByte(fileInputStream);
        // combine all two read bytes into a word
        return (i1 << 8) | (i2);
    }

    /**
     * Read four bytes from given file input stream with check if EOF is
     * reached.
     * 
     * @param fileInputStream instance of already opened FileInputStream
     * @return
     * @throws IOException
     *             thrown if an I/O error occurs
     */
    static int readFourBytes(FileInputStream fileInputStream) throws IOException
    {
        // try to read four bytes from the input stream
        int i1 = readOneByte(fileInputStream);
        int i2 = readOneByte(fileInputStream);
        int i3 = readOneByte(fileInputStream);
        int i4 = readOneByte(fileInputStream);
        // combine all four read bytes into a word
        return (i1 << 24) | (i2 << 16) | (i3 << 8) | (i4);
    }

}