java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java
changeset 54 3d5cc308e268
child 55 c703fb7f088f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIParser.java	Thu Jul 03 00:18:35 2014 +0200
     1.3 @@ -0,0 +1,159 @@
     1.4 +/**
     1.5 + * Alt2XML
     1.6 + * Copyright © 2014 František Kučera (frantovo.cz)
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, either version 3 of the License, or
    1.11 + * (at your option) any later version.
    1.12 + *
    1.13 + * This program is distributed in the hope that it will be useful,
    1.14 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    1.16 + * GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License
    1.19 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +package cz.frantovo.alt2xml.cli;
    1.22 +
    1.23 +import java.io.File;
    1.24 +import java.io.InputStream;
    1.25 +
    1.26 +/**
    1.27 + * Converts command line arguments from String array to object.
    1.28 + * Checks basic constraints (if only supported options are used and if they have correct number of
    1.29 + * parameters)
    1.30 + *
    1.31 + * @author Ing. František Kučera (frantovo.cz)
    1.32 + */
    1.33 +public class CLIParser {
    1.34 +
    1.35 +	public CLIOptions parseOptions(String[] args, InputStream in) throws CLIParserException {
    1.36 +		CLIOptions options = new CLIOptions();
    1.37 +
    1.38 +		for (int i = 0; i < args.length; i++) {
    1.39 +			String arg = args[i];
    1.40 +
    1.41 +			for (Token t : Token.values()) {
    1.42 +				if (t.matches(arg)) {
    1.43 +					t.parse(args, i, options);
    1.44 +				}
    1.45 +			}
    1.46 +
    1.47 +			throw new CLIParserException("Unknown option: " + arg);
    1.48 +		}
    1.49 +
    1.50 +		// Default output: STDOUT
    1.51 +		options.setOutputStream(System.out);
    1.52 +
    1.53 +		return options;
    1.54 +	}
    1.55 +
    1.56 +	private static String fetchNext(String[] args, int index) throws CLIParserException {
    1.57 +		if (index < args.length) {
    1.58 +			return args[index];
    1.59 +		} else {
    1.60 +			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
    1.61 +		}
    1.62 +	}
    1.63 +
    1.64 +	private static enum Token {
    1.65 +
    1.66 +		INPUT_FILE("--input-file") {
    1.67 +					@Override
    1.68 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.69 +						int originalIndex = index;
    1.70 +						options.setInputFile(new File(fetchNext(args, ++index)));
    1.71 +						return index - originalIndex;
    1.72 +					}
    1.73 +				},
    1.74 +		INPUT_STDIN("--input-stdin") {
    1.75 +					@Override
    1.76 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.77 +						options.setInputStream(System.in);
    1.78 +						return 0;
    1.79 +					}
    1.80 +				},
    1.81 +		INPUT_URL("--input-url") {
    1.82 +					@Override
    1.83 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.84 +						int originalIndex = index;
    1.85 +						options.setInputUrl(fetchNext(args, ++index));
    1.86 +						return index - originalIndex;
    1.87 +					}
    1.88 +				},
    1.89 +		SYSTEM_ID("--system-id") {
    1.90 +					@Override
    1.91 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
    1.92 +						int originalIndex = index;
    1.93 +						options.setSystemId(fetchNext(args, ++index));
    1.94 +						return index - originalIndex;
    1.95 +					}
    1.96 +				},
    1.97 +		READER_PROPERTY("--reader-property") {
    1.98 +					@Override
    1.99 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   1.100 +						int originalIndex = index;
   1.101 +						String name = fetchNext(args, ++index);
   1.102 +						String value = fetchNext(args, ++index);
   1.103 +						options.addReaderProperty(name, value);
   1.104 +						return index - originalIndex;
   1.105 +					}
   1.106 +				},
   1.107 +		ACTION("--action") {
   1.108 +					@Override
   1.109 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   1.110 +						int originalIndex = index;
   1.111 +						options.setAction(fetchNext(args, ++index));
   1.112 +						return index - originalIndex;
   1.113 +					}
   1.114 +				},
   1.115 +		ACTION_PROPERTY("--action-property") {
   1.116 +					@Override
   1.117 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   1.118 +						int originalIndex = index;
   1.119 +						String name = fetchNext(args, ++index);
   1.120 +						String value = fetchNext(args, ++index);
   1.121 +						options.addActionProperty(name, value);
   1.122 +						return index - originalIndex;
   1.123 +					}
   1.124 +				},
   1.125 +		ACTION_DATA("--") {
   1.126 +					@Override
   1.127 +					public int parse(String[] args, int index, CLIOptions options) throws CLIParserException {
   1.128 +						int originalIndex = index;
   1.129 +						for (index++; index < args.length; index++) {
   1.130 +							options.addActionData(args[index]);
   1.131 +						}
   1.132 +						return index - originalIndex;
   1.133 +					}
   1.134 +				};
   1.135 +
   1.136 +		private final String option;
   1.137 +
   1.138 +		private Token(String option) {
   1.139 +			this.option = option;
   1.140 +		}
   1.141 +
   1.142 +		/**
   1.143 +		 * @param option e.g. „--input-file“
   1.144 +		 * @return whether option is this token
   1.145 +		 */
   1.146 +		public boolean matches(String option) {
   1.147 +			return this.option.equals(option);
   1.148 +		}
   1.149 +
   1.150 +		/**
   1.151 +		 * Parse String arguments and fill values into the options object.
   1.152 +		 *
   1.153 +		 * @param args CLI arguments
   1.154 +		 * @param index index of the option matched by this token, like „--input-file“
   1.155 +		 * @param options object to be filled
   1.156 +		 * @return number of parsed arguments – if option has no arguments (just boolean flag),
   1.157 +		 * return 0, otherwise return positive integer: number of eaten arguments.
   1.158 +		 * @throws CLIParserException
   1.159 +		 */
   1.160 +		public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException;
   1.161 +	}
   1.162 +}