001/* 
002 * Copyright (C) 2016 Hobrasoft s.r.o.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU Affero General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU Affero General Public License for more details.
013 *
014 * You should have received a copy of the GNU Affero General Public License
015 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
016 */
017package cz.hobrasoft.pdfmu.operation.args;
018
019import com.itextpdf.text.pdf.PdfReader;
020import cz.hobrasoft.pdfmu.PdfmuUtils;
021import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_CLOSE;
022import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_NOT_FOUND;
023import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_NOT_VALID_PDF;
024import cz.hobrasoft.pdfmu.operation.OperationException;
025import java.io.File;
026import java.io.FileInputStream;
027import java.io.FileNotFoundException;
028import java.io.IOException;
029import java.io.InputStream;
030import java.util.logging.Logger;
031import net.sourceforge.argparse4j.impl.Arguments;
032import net.sourceforge.argparse4j.inf.ArgumentParser;
033import net.sourceforge.argparse4j.inf.Namespace;
034
035public class InPdfArgs implements ArgsConfiguration, AutoCloseable {
036
037    private final String name = "in";
038    private final String help = "input PDF document";
039    private final String metavar;
040
041    private static final Logger logger = Logger.getLogger(InPdfArgs.class.getName());
042
043    public InPdfArgs(String metavar) {
044        this.metavar = metavar;
045    }
046
047    public InPdfArgs() {
048        this("IN.pdf");
049    }
050
051    @Override
052    public void addArguments(ArgumentParser parser) {
053        parser.addArgument(name)
054                .help(help)
055                .metavar(metavar)
056                .type(Arguments.fileType().acceptSystemIn());
057    }
058
059    private File file = null;
060
061    public File getFile() {
062        return file;
063    }
064
065    private InputStream is = null;
066    private PdfReader pdfReader = null;
067
068    @Override
069    public void setFromNamespace(Namespace namespace) {
070        file = namespace.get(name);
071        assert file != null; // Required argument (because it is positional)
072    }
073
074    public PdfReader open() throws OperationException {
075        assert file != null;
076        assert is == null;
077        assert pdfReader == null;
078
079        logger.info(String.format("Input file: %s", file));
080
081        // Open the input stream
082        try {
083            is = new FileInputStream(file);
084        } catch (FileNotFoundException ex) {
085            throw new OperationException(INPUT_NOT_FOUND, ex,
086                    PdfmuUtils.sortedMap(new String[]{"file"}, new Object[]{file}));
087        }
088
089        // Open the PDF reader
090        try {
091            pdfReader = new PdfReader(is);
092        } catch (IOException ex) {
093            throw new OperationException(INPUT_NOT_VALID_PDF, ex,
094                    PdfmuUtils.sortedMap(new String[]{"file"}, new Object[]{file}));
095        }
096
097        return pdfReader;
098    }
099
100    @Override
101    public void close() throws OperationException {
102        if (pdfReader != null) {
103            // Close the PDF reader
104            pdfReader.close();
105            pdfReader = null;
106        }
107
108        if (is != null) {
109            // Close the input stream
110            try {
111                is.close(); // May throw IOException
112            } catch (IOException ex) {
113                throw new OperationException(INPUT_CLOSE, ex);
114            }
115            is = null;
116        }
117    }
118
119    public PdfReader getPdfReader() {
120        return pdfReader;
121    }
122
123}