Search in sources :

Example 1 with ReportFactory

use of eu.esdihumboldt.hale.common.core.report.ReportFactory in project hale by halestudio.

the class ReportWriter method write.

/**
 * Writes all {@link Report}s to a {@link File}.
 *
 * @param file target file
 * @param reports reports to be saved
 * @param append if the reports should be appended to the file instead of
 *            overwriting it
 *
 * @return true on success
 *
 * @throws IOException if IO fails
 */
public static boolean write(File file, Collection<Report<?>> reports, boolean append) throws IOException {
    // check if the file exists
    if (!file.exists()) {
        // and create the file
        if (!file.createNewFile()) {
            _log.error("Logfile could not be created!");
            return false;
        }
        // make it writable
        file.setWritable(true);
    }
    // check if it's writable
    if (!file.canWrite()) {
        _log.error("Report could not be saved. No write permission!");
        return false;
    }
    // create PrintStream
    PrintStream p = new PrintStream(new BufferedOutputStream(new FileOutputStream(file, append)));
    try {
        // get an instance of ReportFactory
        ReportFactory rf = ReportFactory.getInstance();
        MessageFactory mf = MessageFactory.getInstance();
        // iterate through all reports
        for (Report<?> r : reports) {
            // write them to the file
            p.print(rf.asString(r));
            for (Message m : r.getErrors()) {
                p.println("!ERROR");
                p.print(mf.asString(m));
            }
            for (Message m : r.getWarnings()) {
                p.println("!WARN");
                p.print(mf.asString(m));
            }
            for (Message m : r.getInfos()) {
                p.println("!INFO");
                p.print(mf.asString(m));
            }
        }
        // new line at end of file to allow appending
        p.println();
        p.flush();
    } finally {
        // close stream
        p.close();
    }
    return true;
}
Also used : PrintStream(java.io.PrintStream) MessageFactory(eu.esdihumboldt.hale.common.core.report.MessageFactory) Message(eu.esdihumboldt.hale.common.core.report.Message) FileOutputStream(java.io.FileOutputStream) ReportFactory(eu.esdihumboldt.hale.common.core.report.ReportFactory) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

Message (eu.esdihumboldt.hale.common.core.report.Message)1 MessageFactory (eu.esdihumboldt.hale.common.core.report.MessageFactory)1 ReportFactory (eu.esdihumboldt.hale.common.core.report.ReportFactory)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1