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/>. */ /* * PDFInfo.java * Copyright (C) 2016 University of Waikato, Hamilton, New Zealand */ package adams.flow.transformer; import adams.core.Index; import adams.core.QuickInfoHelper; import adams.core.Utils; import adams.core.io.PlaceholderFile; import adams.flow.core.DataInfoActor; import com.itextpdf.text.pdf.PdfReader; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; /** <!-- globalinfo-start --> * Outputs information on a PDF file. * <br><br> <!-- globalinfo-end --> * <!-- flow-summary-start --> * Input/output:<br> * - accepts:<br> * java.lang.String<br> * java.io.File<br> * - generates:<br> * java.lang.Integer<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: PDFInfo * </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-array <boolean> (property: outputArray) * If enabled, the info items get output as array rather than one-by-one. * default: false * </pre> * * <pre>-type <NUM_PAGES|WIDTH|HEIGHT> (property: type) * The type of information to generate. * default: NUM_PAGES * </pre> * * <pre>-page-index <adams.core.Index> (property: pageIndex) * The page index to use for generating page-specific information. An index * is a number starting with 1; the following placeholders can be used as well: * first, second, third, last_2, last_1, last * default: first * example: An index is a number starting with 1; the following placeholders can be used as well: first, second, third, last_2, last_1, last * </pre> * <!-- options-end --> * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public class PDFInfo extends AbstractArrayProvider implements DataInfoActor { /** for serialization. */ private static final long serialVersionUID = -3019442578354930841L; /** * The type of information to generate. * * @author fracpete (fracpete at waikato dot ac dot nz) * @version $Revision$ */ public enum InfoType { /** the number of pages. */ NUM_PAGES, /** the width. */ WIDTH, /** the height. */ HEIGHT, } /** the type of information to generate. */ protected InfoType m_Type; /** the index of the page to get the information for. */ protected Index m_PageIndex; /** * Returns a string describing the object. * * @return a description suitable for displaying in the gui */ @Override public String globalInfo() { return "Outputs information on a PDF file."; } /** * Adds options to the internal list of options. */ @Override public void defineOptions() { super.defineOptions(); m_OptionManager.add("type", "type", InfoType.NUM_PAGES); m_OptionManager.add("page-index", "pageIndex", new Index(Index.FIRST)); } /** * Initializes the members. */ @Override protected void initialize() { super.initialize(); m_PageIndex = new Index(); } /** * 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; HashSet<InfoType> types; result = QuickInfoHelper.toString(this, "type", m_Type); types = new HashSet<>(Arrays.asList(new InfoType[] { InfoType.NUM_PAGES, })); if (!types.contains(m_Type) || QuickInfoHelper.hasVariable(this, "type")) result += QuickInfoHelper.toString(this, "pageIndex", m_PageIndex, ", page: "); return result; } /** * 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 outputArrayTipText() { return "If enabled, the info items get output as array rather than one-by-one."; } /** * Sets the type of information to generate. * * @param value the type */ public void setType(InfoType value) { m_Type = value; reset(); } /** * Returns the type of information to generate. * * @return the type */ public InfoType getType() { return m_Type; } /** * 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 typeTipText() { return "The type of information to generate."; } /** * Sets the page index to use for page specific information. * * @param value the 1-based index */ public void setPageIndex(Index value) { m_PageIndex = value; reset(); } /** * Returns the page index to use for page specific information. * * @return the 1-based index */ public Index getPageIndex() { return m_PageIndex; } /** * 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 pageIndexTipText() { return "The page index to use for generating page-specific information. " + m_PageIndex.getExample(); } /** * Returns the base class of the items. * * @return the class */ @Override protected Class getItemClass() { switch (m_Type) { case NUM_PAGES: return Integer.class; case WIDTH: case HEIGHT: return Double.class; default: throw new IllegalStateException("Unhandled info type: " + m_Type); } } /** * 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 }; } /** * Executes the flow item. * * @return null if everything is fine, otherwise error message */ @Override protected String doExecute() { String result; File file; PdfReader reader; result = null; m_Queue = new ArrayList(); file = null; if (m_InputToken.getPayload() instanceof String) file = new PlaceholderFile((String) m_InputToken.getPayload()); else if (m_InputToken.getPayload() instanceof File) file = new PlaceholderFile((File) m_InputToken.getPayload()); else result = "Unhandled input type: " + Utils.classToString(m_InputToken.getPayload()); reader = null; if (result == null) { try { reader = new PdfReader(file.getAbsolutePath()); } catch (Exception e) { result = handleException("Failed to open PDF file: " + file, e); } } if (result == null) { m_PageIndex.setMax(reader.getNumberOfPages()); switch (m_Type) { case NUM_PAGES: m_Queue.add(reader.getNumberOfPages()); break; case WIDTH: m_Queue.add(new Double(reader.getPageSize(m_PageIndex.getIntIndex() + 1).getWidth())); break; case HEIGHT: m_Queue.add(new Double(reader.getPageSize(m_PageIndex.getIntIndex() + 1).getHeight())); break; default: result = "Unhandled info type: " + m_Type; } } if (reader != null) reader.close(); return result; } }