Java tutorial
/* * 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/>. */ /* * JsonFileWriter.java * Copyright (C) 2013-2016 University of Waikato, Hamilton, New Zealand */ package adams.flow.sink; import adams.core.io.PrettyPrintingSupporter; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import net.minidev.json.JSONAware; import adams.core.io.FileUtils; /** <!-- globalinfo-start --> * Writes a JSON object/array to a file. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * net.minidev.json.JSONAware<br> * <br><br> <!-- flow-summary-end --> * <!-- options-start --> * <pre>-logging-level <OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST> (property: loggingLevel) * The logging level for outputting errors and debugging output. * default: WARNING * </pre> * * <pre>-name <java.lang.String> (property: name) * The name of the actor. * default: JsonFileWriter * </pre> * * <pre>-annotation <adams.core.base.BaseAnnotation> (property: annotations) * The annotations to attach to this actor. * default: * </pre> * * <pre>-skip <boolean> (property: skip) * If set to true, transformation is skipped and the input token is just forwarded * as it is. * default: false * </pre> * * <pre>-stop-flow-on-error <boolean> (property: stopFlowOnError) * If set to true, the flow gets stopped in case this actor encounters an error; * useful for critical actors. * default: false * </pre> * * <pre>-silent <boolean> (property: silent) * If enabled, then no errors are output in the console; Note: the enclosing * actor handler must have this enabled as well. * default: false * </pre> * * <pre>-output <adams.core.io.PlaceholderFile> (property: outputFile) * The name of the output file. * default: ${CWD} * </pre> * * <pre>-pretty-printing <boolean> (property: prettyPrinting) * If enabled, the output is printed in a 'pretty' format. * default: false * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class JsonFileWriter extends AbstractFileWriter implements PrettyPrintingSupporter { /** for serialization. */ private static final long serialVersionUID = 3613018887562327088L; /** 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 a JSON object/array to a file."; } /** * Adds options to the internal list of options. */ public void defineOptions() { super.defineOptions(); m_OptionManager.add("pretty-printing", "prettyPrinting", false); } /** * Returns the tip text for this property. * * @return tip text for this property suitable for * displaying in the GUI or for listing the options. */ @Override public String outputFileTipText() { return "The name of the output file."; } /** * 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."; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->net.minidev.json.JSONAware.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { JSONAware.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; String content; Gson gson; JsonParser jp; JsonElement je; result = null; content = ((JSONAware) m_InputToken.getPayload()).toJSONString(); if (m_PrettyPrinting) { gson = new GsonBuilder().setPrettyPrinting().create(); jp = new JsonParser(); je = jp.parse(content); content = gson.toJson(je); } if (!FileUtils.writeToFile(m_OutputFile.getAbsolutePath(), content, false)) result = "Failed to write JSON file: " + m_OutputFile; return result; } }