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.HashMap;
27 import java.util.List;
29 import java.util.Properties;
30 import java.util.logging.Level;
31 import java.util.logging.LogRecord;
32 import java.util.logging.Logger;
33 import org.xml.sax.InputSource;
36 * Holds options from command line, validates them, combines with configuration and provides derived
39 * @author Ing. František Kučera (frantovo.cz)
41 public class CLIOptions {
43 private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
45 private File inputFile;
46 private InputStream inputStream;
48 private String systemId;
49 private final Properties readerProperties = new Properties();
50 private final Map<String, Boolean> readerFeatures = new HashMap<>();
52 private OutputStream outputStream;
53 private String action;
54 private final Properties actionProperties = new Properties();
55 private final List<String> actionData = new ArrayList<>();
57 public void validate() throws InvalidOptionsException {
58 InvalidOptionsException e = new InvalidOptionsException();
60 if (inputFile == null && systemId == null) {
61 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
67 if (e.hasProblems()) {
72 public void setInputFile(File inputFile) {
73 this.inputFile = inputFile;
76 public void setInputStream(InputStream inputStream) {
77 this.inputStream = inputStream;
80 public void setSystemId(String systemId) {
81 this.systemId = systemId;
84 public InputSource getInputSource() {
85 InputSource is = new InputSource();
87 if (systemId != null) {
88 is.setSystemId(systemId);
91 if (inputFile != null) {
92 if (systemId == null) {
93 is.setSystemId(inputFile.toURI().toASCIIString());
96 is.setByteStream(new FileInputStream(inputFile));
97 } catch (FileNotFoundException e) {
98 LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
99 message.setParameters(new Object[]{inputFile, systemId});
100 message.setThrown(e);
106 if (inputStream != null) {
107 is.setByteStream(inputStream);
113 public void setOutputStream(OutputStream outputStream) {
114 this.outputStream = outputStream;
117 public OutputStream getOutputStream() {
121 public void setAction(String action) {
122 this.action = action;
125 public String getAction() {
129 public void addReaderProperty(String name, String value) {
130 readerProperties.put(name, value);
133 public Properties getReaderProperties() {
134 return readerProperties;
137 public void addReaderFeature(String name, boolean value) {
138 readerFeatures.put(name, value);
141 public Map<String, Boolean> getReaderFeatures() {
142 return readerFeatures;
145 public void addActionProperty(String name, String value) {
146 actionProperties.put(name, value);
149 public Properties getActionProperties() {
150 return actionProperties;
153 public void addActionData(String value) {
154 actionData.add(value);
157 public List<String> getActionData() {