1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java Thu Jul 03 00:18:35 2014 +0200
1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java Thu Jul 03 00:37:38 2014 +0200
1.3 @@ -23,6 +23,7 @@
1.4 import java.util.ArrayList;
1.5 import java.util.List;
1.6 import java.util.Properties;
1.7 +import org.xml.sax.InputSource;
1.8
1.9 /**
1.10 * Holds options from command line, validates them, combines with configuration and provides derived
1.11 @@ -34,7 +35,6 @@
1.12
1.13 private File inputFile;
1.14 private InputStream inputStream;
1.15 - private String inputUrl;
1.16
1.17 private String systemId;
1.18 private final Properties readerProperties = new Properties();
1.19 @@ -48,7 +48,7 @@
1.20 InvalidOptionsException e = new InvalidOptionsException();
1.21
1.22 /**
1.23 - * TODO: validace
1.24 + * TODO: validate
1.25 */
1.26 if (e.hasProblems()) {
1.27 throw e;
1.28 @@ -63,22 +63,41 @@
1.29 this.inputStream = inputStream;
1.30 }
1.31
1.32 - public void setInputUrl(String inputUrl) {
1.33 - this.inputUrl = inputUrl;
1.34 - }
1.35 -
1.36 public void setSystemId(String systemId) {
1.37 this.systemId = systemId;
1.38 }
1.39 -
1.40 +
1.41 + public InputSource getInputSource() {
1.42 + InputSource is = new InputSource();
1.43 +
1.44 + if (inputFile != null) {
1.45 + is.setSystemId(inputFile.toURI().toASCIIString());
1.46 + }
1.47 +
1.48 + if (inputStream != null) {
1.49 + is.setByteStream(inputStream);
1.50 + is.setSystemId(systemId);
1.51 + }
1.52 +
1.53 + return is;
1.54 + }
1.55 +
1.56 public void setOutputStream(OutputStream outputStream) {
1.57 this.outputStream = outputStream;
1.58 }
1.59 -
1.60 +
1.61 + public OutputStream getOutputStream() {
1.62 + return outputStream;
1.63 + }
1.64 +
1.65 public void setAction(String action) {
1.66 this.action = action;
1.67 }
1.68
1.69 + public String getAction() {
1.70 + return action;
1.71 + }
1.72 +
1.73 public void addReaderProperty(String name, String value) {
1.74 readerProperties.put(name, value);
1.75 }
2.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java Thu Jul 03 00:18:35 2014 +0200
2.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java Thu Jul 03 00:37:38 2014 +0200
2.3 @@ -18,7 +18,6 @@
2.4 package cz.frantovo.alt2xml.cli;
2.5
2.6 import java.io.File;
2.7 -import java.io.InputStream;
2.8
2.9 /**
2.10 * Converts command line arguments from String array to object.
2.11 @@ -29,22 +28,28 @@
2.12 */
2.13 public class CLIParser {
2.14
2.15 - public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
2.16 + public CLIOptions parseOptions(String[] args) throws CLIParserException {
2.17 CLIOptions options = new CLIOptions();
2.18
2.19 for (int i = 0; i < args.length; i++) {
2.20 String arg = args[i];
2.21
2.22 + boolean matches = false;
2.23 +
2.24 for (Token t : Token.values()) {
2.25 if (t.matches(arg)) {
2.26 - t.parse(args, i, options);
2.27 + int parsedArgs = t.parse(args, i, options);
2.28 + i = i + parsedArgs;
2.29 + matches = true;
2.30 }
2.31 }
2.32
2.33 - throw new CLIParserException("Unknown option: " + arg);
2.34 + if (!matches) {
2.35 + throw new CLIParserException("Unknown option: " + arg);
2.36 + }
2.37 }
2.38
2.39 - // Default output: STDOUT
2.40 + // Default output:
2.41 options.setOutputStream(System.out);
2.42
2.43 return options;
2.44 @@ -75,14 +80,6 @@
2.45 return 0;
2.46 }
2.47 },
2.48 - INPUT_URL("--input-url") {
2.49 - @Override
2.50 - public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
2.51 - int originalIndex = index;
2.52 - options.setInputUrl(fetchNext(args, ++index));
2.53 - return index - originalIndex;
2.54 - }
2.55 - },
2.56 SYSTEM_ID("--system-id") {
2.57 @Override
2.58 public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
3.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Thu Jul 03 00:18:35 2014 +0200
3.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Thu Jul 03 00:37:38 2014 +0200
3.3 @@ -43,9 +43,8 @@
3.4 public static void main(String[] args) {
3.5
3.6 try {
3.7 - File inputFile = new File(args[0]);
3.8 - String actionCode = args[1];
3.9 - OutputStream outputStream = System.out;
3.10 + CLIParser cliParser = new CLIParser();
3.11 + CLIOptions cliOptions = cliParser.parseOptions(args);
3.12
3.13 Map<String, ActionFactory> actionFactories = new HashMap<>();
3.14
3.15 @@ -55,19 +54,20 @@
3.16 log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()});
3.17 }
3.18
3.19 + String actionCode = cliOptions.getAction();
3.20 ActionFactory actionFactory = actionFactories.get(actionCode);
3.21
3.22 if (actionFactory == null) {
3.23 log.log(Level.SEVERE, "No such output action with code: {0}", actionCode);
3.24 } else {
3.25
3.26 - ActionContext actionContext = new ActionContext(outputStream);
3.27 + ActionContext actionContext = new ActionContext(cliOptions.getOutputStream());
3.28 Action action = actionFactory.getAction(actionContext);
3.29
3.30 SAXParserFactory t = SAXParserFactory.newInstance();
3.31 SAXParser p = t.newSAXParser();
3.32
3.33 - action.run(p, new InputSource(inputFile.toURI().toASCIIString()));
3.34 + action.run(p, cliOptions.getInputSource());
3.35 }
3.36
3.37 } catch (Exception e) {