adams.flow.transformer.PDFInfo.java Source code

Java tutorial

Introduction

Here is the source code for adams.flow.transformer.PDFInfo.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/>.
 */

/*
 * 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&#47;output:<br>
 * - accepts:<br>
 * &nbsp;&nbsp;&nbsp;java.lang.String<br>
 * &nbsp;&nbsp;&nbsp;java.io.File<br>
 * - generates:<br>
 * &nbsp;&nbsp;&nbsp;java.lang.Integer<br>
 * <br><br>
 <!-- flow-summary-end -->
 *
 <!-- options-start -->
 * <pre>-logging-level &lt;OFF|SEVERE|WARNING|INFO|CONFIG|FINE|FINER|FINEST&gt; (property: loggingLevel)
 * &nbsp;&nbsp;&nbsp;The logging level for outputting errors and debugging output.
 * &nbsp;&nbsp;&nbsp;default: WARNING
 * </pre>
 * 
 * <pre>-name &lt;java.lang.String&gt; (property: name)
 * &nbsp;&nbsp;&nbsp;The name of the actor.
 * &nbsp;&nbsp;&nbsp;default: PDFInfo
 * </pre>
 * 
 * <pre>-annotation &lt;adams.core.base.BaseAnnotation&gt; (property: annotations)
 * &nbsp;&nbsp;&nbsp;The annotations to attach to this actor.
 * &nbsp;&nbsp;&nbsp;default: 
 * </pre>
 * 
 * <pre>-skip &lt;boolean&gt; (property: skip)
 * &nbsp;&nbsp;&nbsp;If set to true, transformation is skipped and the input token is just forwarded 
 * &nbsp;&nbsp;&nbsp;as it is.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-stop-flow-on-error &lt;boolean&gt; (property: stopFlowOnError)
 * &nbsp;&nbsp;&nbsp;If set to true, the flow gets stopped in case this actor encounters an error;
 * &nbsp;&nbsp;&nbsp; useful for critical actors.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-silent &lt;boolean&gt; (property: silent)
 * &nbsp;&nbsp;&nbsp;If enabled, then no errors are output in the console; Note: the enclosing 
 * &nbsp;&nbsp;&nbsp;actor handler must have this enabled as well.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-output-array &lt;boolean&gt; (property: outputArray)
 * &nbsp;&nbsp;&nbsp;If enabled, the info items get output as array rather than one-by-one.
 * &nbsp;&nbsp;&nbsp;default: false
 * </pre>
 * 
 * <pre>-type &lt;NUM_PAGES|WIDTH|HEIGHT&gt; (property: type)
 * &nbsp;&nbsp;&nbsp;The type of information to generate.
 * &nbsp;&nbsp;&nbsp;default: NUM_PAGES
 * </pre>
 * 
 * <pre>-page-index &lt;adams.core.Index&gt; (property: pageIndex)
 * &nbsp;&nbsp;&nbsp;The page index to use for generating page-specific information. An index 
 * &nbsp;&nbsp;&nbsp;is a number starting with 1; the following placeholders can be used as well:
 * &nbsp;&nbsp;&nbsp; first, second, third, last_2, last_1, last
 * &nbsp;&nbsp;&nbsp;default: first
 * &nbsp;&nbsp;&nbsp;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;
    }
}