# HG changeset patch # User František Kučera # Date 1404340658 -7200 # Node ID c703fb7f088f330a937adc1e1c1fd4960b3b3da3 # Parent 3d5cc308e268fbcced2c7ac5c7b0ed873793d0e9 CLIOptions, CLIParser – first working version diff -r 3d5cc308e268 -r c703fb7f088f java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java Thu Jul 03 00:18:35 2014 +0200 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java Thu Jul 03 00:37:38 2014 +0200 @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; +import org.xml.sax.InputSource; /** * Holds options from command line, validates them, combines with configuration and provides derived @@ -34,7 +35,6 @@ private File inputFile; private InputStream inputStream; - private String inputUrl; private String systemId; private final Properties readerProperties = new Properties(); @@ -48,7 +48,7 @@ InvalidOptionsException e = new InvalidOptionsException(); /** - * TODO: validace + * TODO: validate */ if (e.hasProblems()) { throw e; @@ -63,22 +63,41 @@ this.inputStream = inputStream; } - public void setInputUrl(String inputUrl) { - this.inputUrl = inputUrl; - } - public void setSystemId(String systemId) { this.systemId = systemId; } - + + public InputSource getInputSource() { + InputSource is = new InputSource(); + + if (inputFile != null) { + is.setSystemId(inputFile.toURI().toASCIIString()); + } + + if (inputStream != null) { + is.setByteStream(inputStream); + is.setSystemId(systemId); + } + + return is; + } + public void setOutputStream(OutputStream outputStream) { this.outputStream = outputStream; } - + + public OutputStream getOutputStream() { + return outputStream; + } + public void setAction(String action) { this.action = action; } + public String getAction() { + return action; + } + public void addReaderProperty(String name, String value) { readerProperties.put(name, value); } diff -r 3d5cc308e268 -r c703fb7f088f java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java Thu Jul 03 00:18:35 2014 +0200 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java Thu Jul 03 00:37:38 2014 +0200 @@ -18,7 +18,6 @@ package cz.frantovo.alt2xml.cli; import java.io.File; -import java.io.InputStream; /** * Converts command line arguments from String array to object. @@ -29,22 +28,28 @@ */ public class CLIParser { - public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException { + public CLIOptions parseOptions(String[] args) throws CLIParserException { CLIOptions options = new CLIOptions(); for (int i = 0; i < args.length; i++) { String arg = args[i]; + boolean matches = false; + for (Token t : Token.values()) { if (t.matches(arg)) { - t.parse(args, i, options); + int parsedArgs = t.parse(args, i, options); + i = i + parsedArgs; + matches = true; } } - throw new CLIParserException("Unknown option: " + arg); + if (!matches) { + throw new CLIParserException("Unknown option: " + arg); + } } - // Default output: STDOUT + // Default output: options.setOutputStream(System.out); return options; @@ -75,14 +80,6 @@ return 0; } }, - INPUT_URL("--input-url") { - @Override - public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { - int originalIndex = index; - options.setInputUrl(fetchNext(args, ++index)); - return index - originalIndex; - } - }, SYSTEM_ID("--system-id") { @Override public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { diff -r 3d5cc308e268 -r c703fb7f088f java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Thu Jul 03 00:18:35 2014 +0200 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIStarter.java Thu Jul 03 00:37:38 2014 +0200 @@ -43,9 +43,8 @@ public static void main(String[] args) { try { - File inputFile = new File(args[0]); - String actionCode = args[1]; - OutputStream outputStream = System.out; + CLIParser cliParser = new CLIParser(); + CLIOptions cliOptions = cliParser.parseOptions(args); Map actionFactories = new HashMap<>(); @@ -55,19 +54,20 @@ log.log(Level.CONFIG, "Discovered output module: {0} = {1}", new Object[]{code, f.getClass().getName()}); } + String actionCode = cliOptions.getAction(); ActionFactory actionFactory = actionFactories.get(actionCode); if (actionFactory == null) { log.log(Level.SEVERE, "No such output action with code: {0}", actionCode); } else { - ActionContext actionContext = new ActionContext(outputStream); + ActionContext actionContext = new ActionContext(cliOptions.getOutputStream()); Action action = actionFactory.getAction(actionContext); SAXParserFactory t = SAXParserFactory.newInstance(); SAXParser p = t.newSAXParser(); - action.run(p, new InputSource(inputFile.toURI().toASCIIString())); + action.run(p, cliOptions.getInputSource()); } } catch (Exception e) {