franta-hg@54
|
1 |
/**
|
franta-hg@54
|
2 |
* Alt2XML
|
franta-hg@54
|
3 |
* Copyright © 2014 František Kučera (frantovo.cz)
|
franta-hg@54
|
4 |
*
|
franta-hg@54
|
5 |
* This program is free software: you can redistribute it and/or modify
|
franta-hg@54
|
6 |
* it under the terms of the GNU General Public License as published by
|
franta-hg@111
|
7 |
* the Free Software Foundation, version 3 of the License.
|
franta-hg@54
|
8 |
*
|
franta-hg@54
|
9 |
* This program is distributed in the hope that it will be useful,
|
franta-hg@54
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
franta-hg@54
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
franta-hg@54
|
12 |
* GNU General Public License for more details.
|
franta-hg@54
|
13 |
*
|
franta-hg@54
|
14 |
* You should have received a copy of the GNU General Public License
|
franta-hg@54
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
franta-hg@54
|
16 |
*/
|
franta-hg@54
|
17 |
package cz.frantovo.alt2xml.cli;
|
franta-hg@54
|
18 |
|
franta-hg@54
|
19 |
import java.io.File;
|
franta-hg@90
|
20 |
import java.io.FileInputStream;
|
franta-hg@90
|
21 |
import java.io.FileNotFoundException;
|
franta-hg@54
|
22 |
import java.io.InputStream;
|
franta-hg@54
|
23 |
import java.io.OutputStream;
|
franta-hg@54
|
24 |
import java.util.ArrayList;
|
franta-hg@98
|
25 |
import java.util.HashMap;
|
franta-hg@54
|
26 |
import java.util.List;
|
franta-hg@98
|
27 |
import java.util.Map;
|
franta-hg@54
|
28 |
import java.util.Properties;
|
franta-hg@90
|
29 |
import java.util.logging.Level;
|
franta-hg@90
|
30 |
import java.util.logging.LogRecord;
|
franta-hg@90
|
31 |
import java.util.logging.Logger;
|
franta-hg@55
|
32 |
import org.xml.sax.InputSource;
|
franta-hg@54
|
33 |
|
franta-hg@54
|
34 |
/**
|
franta-hg@54
|
35 |
* Holds options from command line, validates them, combines with configuration and provides derived
|
franta-hg@54
|
36 |
* objects.
|
franta-hg@54
|
37 |
*
|
franta-hg@54
|
38 |
* @author Ing. František Kučera (frantovo.cz)
|
franta-hg@54
|
39 |
*/
|
franta-hg@54
|
40 |
public class CLIOptions {
|
franta-hg@54
|
41 |
|
franta-hg@90
|
42 |
private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
|
franta-hg@90
|
43 |
|
franta-hg@54
|
44 |
private File inputFile;
|
franta-hg@54
|
45 |
private InputStream inputStream;
|
franta-hg@54
|
46 |
|
franta-hg@54
|
47 |
private String systemId;
|
franta-hg@54
|
48 |
private final Properties readerProperties = new Properties();
|
franta-hg@98
|
49 |
private final Map<String, Boolean> readerFeatures = new HashMap<>();
|
franta-hg@54
|
50 |
|
franta-hg@54
|
51 |
private OutputStream outputStream;
|
franta-hg@54
|
52 |
private String action;
|
franta-hg@54
|
53 |
private final Properties actionProperties = new Properties();
|
franta-hg@54
|
54 |
private final List<String> actionData = new ArrayList<>();
|
franta-hg@54
|
55 |
|
franta-hg@54
|
56 |
public void validate() throws InvalidOptionsException {
|
franta-hg@54
|
57 |
InvalidOptionsException e = new InvalidOptionsException();
|
franta-hg@64
|
58 |
|
franta-hg@58
|
59 |
if (inputFile == null && systemId == null) {
|
franta-hg@58
|
60 |
e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
|
franta-hg@58
|
61 |
}
|
franta-hg@54
|
62 |
|
franta-hg@54
|
63 |
/**
|
franta-hg@55
|
64 |
* TODO: validate
|
franta-hg@54
|
65 |
*/
|
franta-hg@54
|
66 |
if (e.hasProblems()) {
|
franta-hg@54
|
67 |
throw e;
|
franta-hg@54
|
68 |
}
|
franta-hg@54
|
69 |
}
|
franta-hg@54
|
70 |
|
franta-hg@54
|
71 |
public void setInputFile(File inputFile) {
|
franta-hg@54
|
72 |
this.inputFile = inputFile;
|
franta-hg@54
|
73 |
}
|
franta-hg@54
|
74 |
|
franta-hg@54
|
75 |
public void setInputStream(InputStream inputStream) {
|
franta-hg@54
|
76 |
this.inputStream = inputStream;
|
franta-hg@54
|
77 |
}
|
franta-hg@54
|
78 |
|
franta-hg@54
|
79 |
public void setSystemId(String systemId) {
|
franta-hg@54
|
80 |
this.systemId = systemId;
|
franta-hg@54
|
81 |
}
|
franta-hg@55
|
82 |
|
franta-hg@55
|
83 |
public InputSource getInputSource() {
|
franta-hg@55
|
84 |
InputSource is = new InputSource();
|
franta-hg@55
|
85 |
|
franta-hg@90
|
86 |
if (systemId != null) {
|
franta-hg@90
|
87 |
is.setSystemId(systemId);
|
franta-hg@90
|
88 |
}
|
franta-hg@90
|
89 |
|
franta-hg@55
|
90 |
if (inputFile != null) {
|
franta-hg@90
|
91 |
if (systemId == null) {
|
franta-hg@90
|
92 |
is.setSystemId(inputFile.toURI().toASCIIString());
|
franta-hg@90
|
93 |
} else {
|
franta-hg@90
|
94 |
try {
|
franta-hg@90
|
95 |
is.setByteStream(new FileInputStream(inputFile));
|
franta-hg@90
|
96 |
} catch (FileNotFoundException e) {
|
franta-hg@90
|
97 |
LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
|
franta-hg@90
|
98 |
message.setParameters(new Object[]{inputFile, systemId});
|
franta-hg@90
|
99 |
message.setThrown(e);
|
franta-hg@90
|
100 |
log.log(message);
|
franta-hg@90
|
101 |
}
|
franta-hg@90
|
102 |
}
|
franta-hg@55
|
103 |
}
|
franta-hg@55
|
104 |
|
franta-hg@55
|
105 |
if (inputStream != null) {
|
franta-hg@55
|
106 |
is.setByteStream(inputStream);
|
franta-hg@56
|
107 |
}
|
franta-hg@56
|
108 |
|
franta-hg@55
|
109 |
return is;
|
franta-hg@55
|
110 |
}
|
franta-hg@55
|
111 |
|
franta-hg@54
|
112 |
public void setOutputStream(OutputStream outputStream) {
|
franta-hg@54
|
113 |
this.outputStream = outputStream;
|
franta-hg@54
|
114 |
}
|
franta-hg@55
|
115 |
|
franta-hg@55
|
116 |
public OutputStream getOutputStream() {
|
franta-hg@55
|
117 |
return outputStream;
|
franta-hg@55
|
118 |
}
|
franta-hg@55
|
119 |
|
franta-hg@54
|
120 |
public void setAction(String action) {
|
franta-hg@54
|
121 |
this.action = action;
|
franta-hg@54
|
122 |
}
|
franta-hg@54
|
123 |
|
franta-hg@55
|
124 |
public String getAction() {
|
franta-hg@55
|
125 |
return action;
|
franta-hg@55
|
126 |
}
|
franta-hg@55
|
127 |
|
franta-hg@54
|
128 |
public void addReaderProperty(String name, String value) {
|
franta-hg@54
|
129 |
readerProperties.put(name, value);
|
franta-hg@54
|
130 |
}
|
franta-hg@54
|
131 |
|
franta-hg@64
|
132 |
public Properties getReaderProperties() {
|
franta-hg@64
|
133 |
return readerProperties;
|
franta-hg@64
|
134 |
}
|
franta-hg@64
|
135 |
|
franta-hg@98
|
136 |
public void addReaderFeature(String name, boolean value) {
|
franta-hg@98
|
137 |
readerFeatures.put(name, value);
|
franta-hg@98
|
138 |
}
|
franta-hg@98
|
139 |
|
franta-hg@98
|
140 |
public Map<String, Boolean> getReaderFeatures() {
|
franta-hg@98
|
141 |
return readerFeatures;
|
franta-hg@98
|
142 |
}
|
franta-hg@98
|
143 |
|
franta-hg@54
|
144 |
public void addActionProperty(String name, String value) {
|
franta-hg@54
|
145 |
actionProperties.put(name, value);
|
franta-hg@54
|
146 |
}
|
franta-hg@54
|
147 |
|
franta-hg@64
|
148 |
public Properties getActionProperties() {
|
franta-hg@64
|
149 |
return actionProperties;
|
franta-hg@64
|
150 |
}
|
franta-hg@64
|
151 |
|
franta-hg@54
|
152 |
public void addActionData(String value) {
|
franta-hg@54
|
153 |
actionData.add(value);
|
franta-hg@54
|
154 |
}
|
franta-hg@54
|
155 |
|
franta-hg@64
|
156 |
public List<String> getActionData() {
|
franta-hg@64
|
157 |
return actionData;
|
franta-hg@64
|
158 |
}
|
franta-hg@54
|
159 |
}
|