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/>. */ /* * PDFStamp.java * Copyright (C) 2015 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.core.QuickInfoHelper; import adams.core.io.FileUtils; import adams.core.io.PlaceholderFile; import adams.flow.core.Token; import adams.flow.transformer.pdfstamp.AbstractStamper; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import java.io.File; import java.io.FileOutputStream; /** <!-- globalinfo-start --> * Actor for stamping pages in a PDF with a custom overlay. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * java.lang.String<br> * java.io.File<br> * - generates:<br> * java.lang.String<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: PDFStamp * </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. * default: false * </pre> * * <pre>-stamper <adams.flow.transformer.pdfstamp.AbstractStamper> (property: stamper) * The stamping algorithm to apply to the PDF. * default: adams.flow.transformer.pdfstamp.PassThrough * </pre> * * <pre>-output <adams.core.io.PlaceholderFile> (property: output) * The PDF file to save the combined PDF to. * default: ${CWD} * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class PDFStamp extends AbstractTransformer { /** for serialization. */ private static final long serialVersionUID = 5783362940767103716L; /** the stamper to use. */ protected AbstractStamper m_Stamper; /** the output file. */ protected PlaceholderFile m_Output; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Actor for stamping pages in a PDF with a custom overlay."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("stamper", "stamper", new adams.flow.transformer.pdfstamp.PassThrough()); m_OptionManager.add("output", "output", new PlaceholderFile(".")); } /** * Sets the stamper to use. * * @param value the stamper */ public void setStamper(AbstractStamper value) { m_Stamper = value; reset(); } /** * Returns the stamper in use. * * @return the stamper */ public AbstractStamper getStamper() { return m_Stamper; } /** * 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 stamperTipText() { return "The stamping algorithm to apply to the PDF."; } /** * Sets the output file. * * @param value the file */ public void setOutput(PlaceholderFile value) { m_Output = value; reset(); } /** * Returns the output file. * * @return the file */ public PlaceholderFile getOutput() { return m_Output; } /** * 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 outputTipText() { return "The PDF file to save the combined PDF to."; } /** * Returns a quick info about the actor, which will be displayed in the GUI. * * @return null if no info available, otherwise short string */ @Override public String getQuickInfo() { String result; result = QuickInfoHelper.toString(this, "stamper", m_Stamper, "stamper: "); result += QuickInfoHelper.toString(this, "output", m_Output, ", output: "); return result; } /** * Returns the class that the consumer accepts. * * @return <!-- flow-accepts-start -->java.lang.String.class, java.io.File.class<!-- flow-accepts-end --> */ public Class[] accepts() { return new Class[] { String.class, File.class }; } /** * Returns the class of objects that it generates. * * @return <!-- flow-generates-start -->java.lang.String.class<!-- flow-generates-end --> */ public Class[] generates() { return new Class[] { String.class }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; PlaceholderFile file; PdfReader reader; PdfStamper stamper; FileOutputStream fos; result = null; // get files if (m_InputToken.getPayload() instanceof String) file = new PlaceholderFile((String) m_InputToken.getPayload()); else file = new PlaceholderFile((File) m_InputToken.getPayload()); reader = null; stamper = null; fos = null; try { reader = new PdfReader(file.getAbsolutePath()); fos = new FileOutputStream(m_Output.getAbsolutePath()); stamper = new PdfStamper(reader, fos); m_Stamper.stamp(stamper); } catch (Exception e) { result = handleException("Failed to stamp PDF file: " + file, e); } finally { try { if (stamper != null) stamper.close(); } catch (Exception e) { // ignored } try { if (reader != null) reader.close(); } catch (Exception e) { // ignored } FileUtils.closeQuietly(fos); } if (result == null) m_OutputToken = new Token(m_Output.getAbsolutePath()); return result; } }