Java tutorial
/* * DatpassCLI - command line utility for generating passwords * Copyright (C) 2013 - 2014 Alexandru Geana (alegen) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package net.alegen.datpass.cli.input; import net.alegen.datpass.library.configure.Configurator; import net.alegen.datpass.library.configure.XmlConfigurator; import org.apache.commons.cli.Options; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public abstract class Command { private static final String PROFILE_COMMAND = "profile"; private static final String EXIT_COMMAND = "exit"; private static final String PASSWORD_COMMAND = "password"; private static final String SET_COMMAND = "set"; private static final String GET_COMMAND = "get"; private static final Map<String, String> aliases; private static final Map<String, String> substitutions; static { aliases = new HashMap<String, String>(); aliases.put("profiles", "profile -list"); aliases.put("pfs", "profile -list"); aliases.put("prs", "profile -list"); substitutions = new HashMap<String, String>(); substitutions.put("pf", "profile"); substitutions.put("pr", "profile"); substitutions.put("pass", "password"); substitutions.put("pwd", "password"); substitutions.put("pw", "password"); substitutions.put("quit", "exit"); } public static Command create(String input) { if (aliases.containsKey(input)) input = aliases.get(input); List args = split(input); Command newCommand; if (substitutions.containsKey(args.get(0))) args.set(0, substitutions.get(args.get(0))); if (args.get(0).equals(PROFILE_COMMAND)) newCommand = new ProfileCommand(); else if (args.get(0).equals(EXIT_COMMAND)) newCommand = new ExitCommand(); else if (args.get(0).equals(PASSWORD_COMMAND)) newCommand = new PasswordCommand(); else if (args.get(0).equals(SET_COMMAND) || args.get(0).equals(GET_COMMAND)) newCommand = new SettingsCommand(); else newCommand = new ErrorCommand(); newCommand.args = args; return newCommand; } private static List<String> split(String input) { List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"); Matcher regexMatcher = regex.matcher(input); while (regexMatcher.find()) { if (regexMatcher.group(1) != null) matchList.add(regexMatcher.group(1)); // Add double-quoted string without the quotes else if (regexMatcher.group(2) != null) matchList.add(regexMatcher.group(2)); // Add single-quoted string without the quotes else matchList.add(regexMatcher.group()); // Add unquoted word } return matchList; } protected Options options; protected List<String> args; public abstract void execute(); }