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, version 3 of the License.
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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package cz.frantovo.alt2xml.cli;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.Properties;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31 import java.util.logging.Logger;
32 import org.xml.sax.InputSource;
35 * Holds options from command line, validates them, combines with configuration and provides derived
38 * @author Ing. František Kučera (frantovo.cz)
40 public class CLIOptions {
42 private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
44 private File inputFile;
45 private InputStream inputStream;
47 private String systemId;
48 private final Properties readerProperties = new Properties();
49 private final Map<String, Boolean> readerFeatures = new HashMap<>();
51 private OutputStream outputStream;
52 private String action;
53 private final Properties actionProperties = new Properties();
54 private final List<String> actionData = new ArrayList<>();
56 public void validate() throws InvalidOptionsException {
57 InvalidOptionsException e = new InvalidOptionsException();
59 if (inputFile == null && systemId == null) {
60 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
66 if (e.hasProblems()) {
71 public void setInputFile(File inputFile) {
72 this.inputFile = inputFile;
75 public void setInputStream(InputStream inputStream) {
76 this.inputStream = inputStream;
79 public void setSystemId(String systemId) {
80 this.systemId = systemId;
83 public InputSource getInputSource() {
84 InputSource is = new InputSource();
86 if (systemId != null) {
87 is.setSystemId(systemId);
90 if (inputFile != null) {
91 if (systemId == null) {
92 is.setSystemId(inputFile.toURI().toASCIIString());
95 is.setByteStream(new FileInputStream(inputFile));
96 } catch (FileNotFoundException e) {
97 LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
98 message.setParameters(new Object[]{inputFile, systemId});
105 if (inputStream != null) {
106 is.setByteStream(inputStream);
112 public void setOutputStream(OutputStream outputStream) {
113 this.outputStream = outputStream;
116 public OutputStream getOutputStream() {
120 public void setAction(String action) {
121 this.action = action;
124 public String getAction() {
128 public void addReaderProperty(String name, String value) {
129 readerProperties.put(name, value);
132 public Properties getReaderProperties() {
133 return readerProperties;
136 public void addReaderFeature(String name, boolean value) {
137 readerFeatures.put(name, value);
140 public Map<String, Boolean> getReaderFeatures() {
141 return readerFeatures;
144 public void addActionProperty(String name, String value) {
145 actionProperties.put(name, value);
148 public Properties getActionProperties() {
149 return actionProperties;
152 public void addActionData(String value) {
153 actionData.add(value);
156 public List<String> getActionData() {