adams.data.io.output.AbstractSimpleJsonReportWriter.java Source code

Java tutorial

Introduction

Here is the source code for adams.data.io.output.AbstractSimpleJsonReportWriter.java

Source

/*
 *   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 3 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * AbstractJsonReportWriter.java
 * Copyright (C) 2018 University of Waikato, Hamilton, New Zealand
 */

package adams.data.io.output;

import adams.core.io.FileUtils;
import adams.core.io.PrettyPrintingSupporter;
import adams.data.io.input.AbstractSimpleJsonReportReader;
import adams.data.report.Report;
import adams.data.report.ReportJsonUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

/**
 * Abstract ancestor for writing reports in Json format.
 *
 * @author  fracpete (fracpete at waikato dot ac dot nz)
 * @param <T> the type of report to handle
 */
public abstract class AbstractSimpleJsonReportWriter<T extends Report> extends AbstractReportWriter<T>
        implements PrettyPrintingSupporter {

    /** for serialization. */
    private static final long serialVersionUID = 1068874780353203514L;

    /** whether to use pretty-printing. */
    protected boolean m_PrettyPrinting;

    /**
     * Returns a string describing the object.
     *
     * @return          a description suitable for displaying in the gui
     */
    @Override
    public String globalInfo() {
        return "Writes reports in JSON file format.";
    }

    /**
     * Returns a string describing the format (used in the file chooser).
     *
     * @return          a description suitable for displaying in the
     *             file chooser
     */
    @Override
    public String getFormatDescription() {
        return "JSON file format";
    }

    /**
     * Returns the extension(s) of the format.
     *
     * @return          the extension(s) (without the dot!)
     */
    @Override
    public String[] getFormatExtensions() {
        return new String[] { AbstractSimpleJsonReportReader.FILE_EXTENSION };
    }

    /**
     * Adds options to the internal list of options.
     */
    public void defineOptions() {
        super.defineOptions();

        m_OptionManager.add("pretty-printing", "prettyPrinting", false);
    }

    /**
     * Sets whether to use pretty-printing or not.
     *
     * @param value   true if to use pretty-printing
     */
    public void setPrettyPrinting(boolean value) {
        m_PrettyPrinting = value;
        reset();
    }

    /**
     * Returns whether pretty-printing is used or not.
     *
     * @return      true if to use pretty-printing
     */
    public boolean getPrettyPrinting() {
        return m_PrettyPrinting;
    }

    /**
     * Returns the tip text for this property.
     *
     * @return       tip text for this property suitable for
     *          displaying in the GUI or for listing the options.
     */
    public String prettyPrintingTipText() {
        return "If enabled, the output is printed in a 'pretty' format.";
    }

    /**
     * Performs the actual writing.
     *
     * @param data   the data to write
     * @return      true if successfully written
     */
    @Override
    protected boolean writeData(T data) {
        boolean result;
        GsonBuilder builder;
        Gson gson;
        JsonObject jobj;

        jobj = ReportJsonUtils.toJson(data);
        builder = new GsonBuilder();
        if (m_PrettyPrinting)
            builder.setPrettyPrinting();
        gson = builder.create();
        result = FileUtils.writeToFile(m_Output.getAbsolutePath(), gson.toJson(jobj), false);
        if (!result)
            getLogger().severe("Error writing report #" + data.getDatabaseID() + " to " + m_Output);

        return result;
    }
}