java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
author František Kučera <franta-hg@frantovo.cz>
Thu, 02 Jan 2014 19:59:33 +0100
branchv_0
changeset 113 575a8c6b91ad
parent 107 8189a4a28cd8
child 155 eb3676c6929b
permissions -rw-r--r--
Relax NG schema for XML configuration
     1 /**
     2  * SQL-DK
     3  * Copyright © 2013 František Kučera (frantovo.cz)
     4  *
     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, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk;
    19 
    20 import info.globalcode.sql.dk.InfoLister.InfoType;
    21 import info.globalcode.sql.dk.configuration.Property;
    22 import java.util.ArrayList;
    23 import java.util.HashMap;
    24 import java.util.List;
    25 import java.util.Map;
    26 
    27 /**
    28  *
    29  * @author Ing. František Kučera (frantovo.cz)
    30  */
    31 public class CLIParser {
    32 
    33 	public static final String TYPE_NAME_SEPARATOR = ":";
    34 
    35 	public CLIOptions parseOptions(String[] args) throws CLIParserException {
    36 		CLIOptions options = new CLIOptions();
    37 
    38 		List<SQLType> numberedTypes = new ArrayList<>();
    39 		Map<String, SQLType> namedTypes = new HashMap<>();
    40 
    41 		for (int i = 0; i < args.length; i++) {
    42 			String arg = args[i];
    43 			switch (arg) {
    44 				case Tokens.TYPES:
    45 					String typesString = fetchNext(args, ++i);
    46 
    47 					for (String oneType : typesString.split(",")) {
    48 						int sepatratorIndex = oneType.indexOf(TYPE_NAME_SEPARATOR);
    49 						if (sepatratorIndex == -1) {
    50 							numberedTypes.add(getType(oneType.toUpperCase()));
    51 						} else {
    52 							String namePart = oneType.substring(0, sepatratorIndex).trim();
    53 							String typePart = oneType.substring(sepatratorIndex + TYPE_NAME_SEPARATOR.length(), oneType.length());
    54 							namedTypes.put(namePart, getType(typePart.toUpperCase()));
    55 						}
    56 					}
    57 					break;
    58 				case Tokens.NAME_PREFIX:
    59 					options.setNamePrefix(fetchNext(args, ++i));
    60 					break;
    61 				case Tokens.NAME_SUFFIX:
    62 					options.setNameSuffix(fetchNext(args, ++i));
    63 					break;
    64 				case Tokens.DB:
    65 					options.setDatabaseName(fetchNext(args, ++i));
    66 					break;
    67 				case Tokens.SQL:
    68 					options.setSql(fetchNext(args, ++i));
    69 					break;
    70 				case Tokens.BATCH:
    71 					options.setBatch(true);
    72 					break;
    73 				case Tokens.DATA: // --data is the last option
    74 					for (i++; i < args.length; i++) {
    75 						arg = args[i];
    76 						Parameter parameter;
    77 						if (numberedTypes.isEmpty()) {
    78 							parameter = new Parameter(arg, null);
    79 						} else {
    80 							int paramIndex = options.getNumberedParameters().size();
    81 							SQLType paramType;
    82 							try {
    83 								paramType = numberedTypes.get(paramIndex);
    84 							} catch (IndexOutOfBoundsException e) {
    85 								throw new CLIParserException("Missing type for parameter #" + paramIndex, e);
    86 							} catch (NullPointerException e) {
    87 								throw new CLIParserException("Invalid type definition for parameter #" + paramIndex, e);
    88 							}
    89 							parameter = new Parameter(arg, paramType);
    90 						}
    91 						options.addNumberedParameter(parameter);
    92 					}
    93 					break;
    94 				case Tokens.DATA_NAMED:
    95 					for (i++; i < args.length; i++) {
    96 						String paramName = args[i];
    97 						String paramValue = fetchNext(args, ++i);
    98 						options.addNamedParameter(new NamedParameter(paramName, paramValue, namedTypes.get(paramName)));
    99 					}
   100 					break;
   101 				case Tokens.FORMATTER:
   102 					options.setFormatterName(fetchNext(args, ++i));
   103 					break;
   104 				case Tokens.DB_PROPERTY:
   105 					options.addDatabaseProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
   106 					break;
   107 				case Tokens.FORMATTER_PROPERTY:
   108 					options.addFormatterProperty(new Property(fetchNext(args, ++i), fetchNext(args, ++i)));
   109 					break;
   110 				case Tokens.INFO_HELP:
   111 					options.addShowInfo(InfoType.HELP);
   112 					break;
   113 				case Tokens.INFO_FORMATTERS:
   114 					options.addShowInfo(InfoType.FORMATTERS);
   115 					break;
   116 				case Tokens.INFO_LICENSE:
   117 					options.addShowInfo(InfoType.LICENSE);
   118 					break;
   119 				case Tokens.INFO_TYPES:
   120 					options.addShowInfo(InfoType.TYPES);
   121 					break;
   122 				case Tokens.INFO_VERSION:
   123 					options.addShowInfo(InfoType.VERSION);
   124 					break;
   125 				case Tokens.INFO_DATABASES:
   126 					options.addShowInfo(InfoType.DATABASES);
   127 					break;
   128 				case Tokens.INFO_CONNECTION:
   129 					options.addShowInfo(InfoType.CONNECTION);
   130 					options.addDatabaseNameToTest(fetchNext(args, ++i));
   131 					break;
   132 				default:
   133 					throw new CLIParserException("Unknown option: " + arg);
   134 			}
   135 		}
   136 		return options;
   137 	}
   138 
   139 	private String fetchNext(String[] args, int index) throws CLIParserException {
   140 		if (index < args.length) {
   141 			return args[index];
   142 		} else {
   143 			throw new CLIParserException("Expecting value for option: " + args[index - 1]);
   144 		}
   145 	}
   146 
   147 	public static class Tokens {
   148 
   149 		// bash-completion:options:
   150 		public static final String DB = "--db"; // bash-completion:option // help: database name
   151 		public static final String DB_PROPERTY = "--db-property"; // bash-completion:option // help: name and value
   152 		public static final String SQL = "--sql"; // bash-completion:option // help: SQL query/command
   153 		public static final String BATCH = "--batch"; // bash-completion:option // help: batch mode (no argument)
   154 		public static final String DATA = "--data"; // bash-completion:option // help: list of ordinal parameters
   155 		public static final String DATA_NAMED = "--data-named"; // bash-completion:option // help: list of named parameters
   156 		public static final String NAME_PREFIX = "--name-prefix"; // bash-completion:option // help: parameter name prefix – regular expression
   157 		public static final String NAME_SUFFIX = "--name-suffix"; // bash-completion:option // help: parameter name suffix – regular expression
   158 		public static final String TYPES = "--types"; // bash-completion:option // help: comma separated list of parameter types
   159 		public static final String FORMATTER = "--formatter"; // bash-completion:option // help: name of the output formatter
   160 		public static final String FORMATTER_PROPERTY = "--formatter-property"; // bash-completion:option // help: name and value
   161 		public static final String INFO_HELP = "--help"; // bash-completion:option // help: print this help
   162 		public static final String INFO_VERSION = "--version"; // bash-completion:option // help: print version info
   163 		public static final String INFO_LICENSE = "--license"; // bash-completion:option // help: print license
   164 		public static final String INFO_FORMATTERS = "--list-formatters"; // bash-completion:option // help: print list of available formatters
   165 		public static final String INFO_TYPES = "--list-types"; // bash-completion:option // help: print list of available data types
   166 		public static final String INFO_DATABASES = "--list-databases"; // bash-completion:option // help: print list of configured databases
   167 		public static final String INFO_CONNECTION = "--test-connection"; // bash-completion:option // help: test connection to particular database
   168 
   169 		private Tokens() {
   170 		}
   171 	}
   172 
   173 	private SQLType getType(String typeString) throws CLIParserException {
   174 		try {
   175 			return SQLType.valueOf(typeString.trim());
   176 		} catch (IllegalArgumentException e) {
   177 			throw new CLIParserException("Unsupported type: " + typeString, e);
   178 		}
   179 	}
   180 }