CLI: supprt combination of --input-file and --system-id (to choose proper input module for given SystemId but actually read stream from the file)
3 * Copyright © 2014 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package cz.frantovo.alt2xml.cli;
21 import java.io.FileInputStream;
22 import java.io.FileNotFoundException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Properties;
28 import java.util.logging.Level;
29 import java.util.logging.LogRecord;
30 import java.util.logging.Logger;
31 import org.xml.sax.InputSource;
34 * Holds options from command line, validates them, combines with configuration and provides derived
37 * @author Ing. František Kučera (frantovo.cz)
39 public class CLIOptions {
41 private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
43 private File inputFile;
44 private InputStream inputStream;
46 private String systemId;
47 private final Properties readerProperties = new Properties();
49 private OutputStream outputStream;
50 private String action;
51 private final Properties actionProperties = new Properties();
52 private final List<String> actionData = new ArrayList<>();
54 public void validate() throws InvalidOptionsException {
55 InvalidOptionsException e = new InvalidOptionsException();
57 if (inputFile == null && systemId == null) {
58 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
64 if (e.hasProblems()) {
69 public void setInputFile(File inputFile) {
70 this.inputFile = inputFile;
73 public void setInputStream(InputStream inputStream) {
74 this.inputStream = inputStream;
77 public void setSystemId(String systemId) {
78 this.systemId = systemId;
81 public InputSource getInputSource() {
82 InputSource is = new InputSource();
84 if (systemId != null) {
85 is.setSystemId(systemId);
88 if (inputFile != null) {
89 if (systemId == null) {
90 is.setSystemId(inputFile.toURI().toASCIIString());
93 is.setByteStream(new FileInputStream(inputFile));
94 } catch (FileNotFoundException e) {
95 LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
96 message.setParameters(new Object[]{inputFile, systemId});
103 if (inputStream != null) {
104 is.setByteStream(inputStream);
110 public void setOutputStream(OutputStream outputStream) {
111 this.outputStream = outputStream;
114 public OutputStream getOutputStream() {
118 public void setAction(String action) {
119 this.action = action;
122 public String getAction() {
126 public void addReaderProperty(String name, String value) {
127 readerProperties.put(name, value);
130 public Properties getReaderProperties() {
131 return readerProperties;
134 public void addActionProperty(String name, String value) {
135 actionProperties.put(name, value);
138 public Properties getActionProperties() {
139 return actionProperties;
142 public void addActionData(String value) {
143 actionData.add(value);
146 public List<String> getActionData() {