View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu.operation.args;
18  
19  import com.itextpdf.text.pdf.PdfReader;
20  import cz.hobrasoft.pdfmu.PdfmuUtils;
21  import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_CLOSE;
22  import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_NOT_FOUND;
23  import static cz.hobrasoft.pdfmu.error.ErrorType.INPUT_NOT_VALID_PDF;
24  import cz.hobrasoft.pdfmu.operation.OperationException;
25  import java.io.File;
26  import java.io.FileInputStream;
27  import java.io.FileNotFoundException;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.util.logging.Logger;
31  import net.sourceforge.argparse4j.impl.Arguments;
32  import net.sourceforge.argparse4j.inf.ArgumentParser;
33  import net.sourceforge.argparse4j.inf.Namespace;
34  
35  public class InPdfArgs implements ArgsConfiguration, AutoCloseable {
36  
37      private final String name = "in";
38      private final String help = "input PDF document";
39      private final String metavar;
40  
41      private static final Logger logger = Logger.getLogger(InPdfArgs.class.getName());
42  
43      public InPdfArgs(String metavar) {
44          this.metavar = metavar;
45      }
46  
47      public InPdfArgs() {
48          this("IN.pdf");
49      }
50  
51      @Override
52      public void addArguments(ArgumentParser parser) {
53          parser.addArgument(name)
54                  .help(help)
55                  .metavar(metavar)
56                  .type(Arguments.fileType().acceptSystemIn());
57      }
58  
59      private File file = null;
60  
61      public File getFile() {
62          return file;
63      }
64  
65      private InputStream is = null;
66      private PdfReader pdfReader = null;
67  
68      @Override
69      public void setFromNamespace(Namespace namespace) {
70          file = namespace.get(name);
71          assert file != null; // Required argument (because it is positional)
72      }
73  
74      public PdfReader open() throws OperationException {
75          assert file != null;
76          assert is == null;
77          assert pdfReader == null;
78  
79          logger.info(String.format("Input file: %s", file));
80  
81          // Open the input stream
82          try {
83              is = new FileInputStream(file);
84          } catch (FileNotFoundException ex) {
85              throw new OperationException(INPUT_NOT_FOUND, ex,
86                      PdfmuUtils.sortedMap(new String[]{"file"}, new Object[]{file}));
87          }
88  
89          // Open the PDF reader
90          try {
91              pdfReader = new PdfReader(is);
92          } catch (IOException ex) {
93              throw new OperationException(INPUT_NOT_VALID_PDF, ex,
94                      PdfmuUtils.sortedMap(new String[]{"file"}, new Object[]{file}));
95          }
96  
97          return pdfReader;
98      }
99  
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 }