1/** 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.latin.dicttool; 18 19import java.util.Arrays; 20import java.util.HashMap; 21 22public class Dicttool { 23 24 public static abstract class Command { 25 protected String[] mArgs; 26 public void setArgs(String[] args) throws IllegalArgumentException { 27 mArgs = args; 28 } 29 abstract public String getHelp(); 30 abstract public void run() throws Exception; 31 } 32 static HashMap<String, Class<? extends Command>> sCommands = 33 new HashMap<String, Class<? extends Command>>(); 34 static { 35 CommandList.populate(); 36 AdditionalCommandList.populate(); 37 } 38 public static void addCommand(final String commandName, final Class<? extends Command> cls) { 39 sCommands.put(commandName, cls); 40 } 41 42 private static Command getCommandInstance(final String commandName) { 43 try { 44 return sCommands.get(commandName).newInstance(); 45 } catch (InstantiationException e) { 46 throw new RuntimeException(commandName + " is not installed"); 47 } catch (IllegalAccessException e) { 48 throw new RuntimeException(commandName + " is not installed"); 49 } 50 } 51 52 private static void help() { 53 System.out.println("Syntax: dicttool <command [arguments]>\nAvailable commands:\n"); 54 for (final String commandName : sCommands.keySet()) { 55 System.out.println("*** " + commandName); 56 System.out.println(getCommandInstance(commandName).getHelp()); 57 System.out.println(""); 58 } 59 } 60 61 private static boolean isCommand(final String commandName) { 62 return sCommands.containsKey(commandName); 63 } 64 65 private Command getCommand(final String[] arguments) { 66 final String commandName = arguments[0]; 67 if (!isCommand(commandName)) { 68 throw new RuntimeException("Unknown command : " + commandName); 69 } 70 final Command command = getCommandInstance(commandName); 71 final String[] argsArray = Arrays.copyOfRange(arguments, 1, arguments.length); 72 command.setArgs(argsArray); 73 return command; 74 } 75 76 private void execute(final String[] arguments) { 77 final Command command = getCommand(arguments); 78 try { 79 command.run(); 80 } catch (Exception e) { 81 System.out.println("Exception while processing command " 82 + command.getClass().getSimpleName() + " : " + e); 83 return; 84 } 85 } 86 87 public static void main(final String[] arguments) { 88 if (0 == arguments.length) { 89 help(); 90 return; 91 } 92 new Dicttool().execute(arguments); 93 } 94} 95