1 2 24 package org.enhydra.tool.common; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 29 import java.io.File ; 31 import java.util.Arrays ; 32 import java.util.ArrayList ; 33 import java.util.ResourceBundle ; 34 public class Main { 35 36 static ResourceBundle res = 38 ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 40 private final String PARAM_ARCHIVE = "-archive"; private final String PARAM_CONFIG = "-config"; private final String PARAM_CODEGEN = "-codegen"; private final String PARAM_INFO = "-info"; 46 private boolean archive = false; 48 private boolean config = false; 49 private boolean codeGen = false; 50 private boolean info = false; 51 52 public Main() { 53 } 54 55 public static void main(String [] args) { 56 Main main = new Main(); 57 58 main.init(args); 59 } 60 61 private String [] getToolArgs(String [] args) { 62 ArrayList list = null; 63 String [] toolArgs = new String [0]; 64 65 if (args.length >= 1) { 66 list = new ArrayList (Arrays.asList(args)); 67 list.remove(0); 68 list.trimToSize(); 69 toolArgs = new String [list.size()]; 70 toolArgs = (String []) list.toArray(toolArgs); 71 } 72 return toolArgs; 73 } 74 75 private void init(String [] args) { 76 try { 77 parseArgs(args); 78 if (isArchive()) { 79 org.enhydra.tool.archive.Main.main(getToolArgs(args)); 80 } else if (isCodeGen()) { 81 org.enhydra.tool.codegen.Main.main(getToolArgs(args)); 82 } else if (isConfig()) { 83 org.enhydra.tool.configure.Main.main(getToolArgs(args)); 84 } else if (isInfo()) { 85 org.enhydra.tool.ToolBoxInfo.main(getToolArgs(args)); 86 } 87 } catch (ArgumentException e) { 88 System.out.println(ResUtil.format(res.getString("Error_0_"), 89 e.getMessage())); 90 showUsage(); 91 } 92 } 93 94 private void parseArgs(String [] args) throws ArgumentException { 95 if (args.length < 1) { 96 throw new ArgumentException(res.getString("Too_few_arguments")); 97 } else if (!isValidTool(args[0])) { 98 throw new ArgumentException(ResUtil.format(res.getString("Tool_not_found_0_"), 99 args[0])); 100 } 101 } 102 103 private boolean isArchive() { 104 return archive; 105 } 106 107 private void setArchive(boolean b) { 108 archive = b; 109 } 110 111 private boolean isConfig() { 112 return config; 113 } 114 115 private void setConfig(boolean b) { 116 config = b; 117 } 118 119 private boolean isCodeGen() { 120 return codeGen; 121 } 122 123 private void setCodeGen(boolean b) { 124 codeGen = b; 125 } 126 127 private boolean isInfo() { 128 return info; 129 } 130 131 private void setInfo(boolean b) { 132 info = b; 133 } 134 135 private boolean isValidTool(String tool) { 136 boolean valid = false; 137 138 if (tool == null) { 139 140 } else if (tool.equalsIgnoreCase(PARAM_ARCHIVE)) { 142 setArchive(true); 143 valid = true; 144 } else if (tool.equalsIgnoreCase(PARAM_CONFIG)) { 145 setConfig(true); 146 valid = true; 147 } else if (tool.equalsIgnoreCase(PARAM_CODEGEN)) { 148 setCodeGen(true); 149 valid = true; 150 } else if (tool.equalsIgnoreCase(PARAM_INFO)) { 151 setInfo(true); 152 valid = true; 153 } 154 return valid; 155 } 156 157 private void showUsage() { 158 System.out.println(res.getString("Kelp_ToolBox") + ' ' 159 + ToolBoxInfo.getToolBoxVersion()); 160 System.out.println(ToolBoxInfo.getCopyright()); 161 System.out.println(res.getString("Usage1")); 162 System.out.println(res.getString("Usage2")); 163 System.out.println(res.getString("Usage3")); 164 } 165 166 class ArgumentException extends Exception { 167 public ArgumentException(String msg) { 168 super(msg); 169 } 170 171 } 172 } 173 | Popular Tags |