1 2 24 package org.enhydra.tool.configure; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.common.FileUtil; 29 import org.enhydra.tool.common.PathHandle; 30 import org.enhydra.tool.common.ResUtil; 31 import org.enhydra.tool.common.ExtensionFilter; 32 import org.enhydra.tool.common.ToolException; 33 34 import java.io.File ; 36 import java.util.ResourceBundle ; 37 import java.util.ArrayList ; 38 import java.util.Arrays ; 39 40 public class Main implements Constants { 42 static ResourceBundle res = 43 ResourceBundle.getBundle("org.enhydra.tool.configure.Res"); 45 private final String PARAM_JAVA = "-java"; private final String PARAM_ENHYDRA = "-enhydra"; private final String PARAM_APP = "-app"; private final String PARAM_ALL = "-all"; 51 private ConfigTool tool = null; 53 private boolean copyNonTemplates = false; 54 55 public Main() { 56 System.out.println("Kelp Configuration Tool " + 57 ToolBoxInfo.getToolBoxVersion()); 58 System.out.println(ToolBoxInfo.getCopyright()); 59 tool = new ConfigTool(); 60 tool.setOwner(null); 61 tool.setEcho(true); 62 } 63 64 public static void main(String [] args) { 65 Main main = new Main(); 66 67 main.init(args); 68 } 69 70 private void init(String [] args) { 73 try { 74 parseArgs(args); 75 tool.configure(); 76 if (copyNonTemplates) { 77 doCopy(); 78 } 79 System.out.println(new String ()); 80 System.out.println(ResUtil.format(res.getString("Enhydra_home_0_"), 81 tool.getEnhydraRoot())); 82 System.out.println(ResUtil.format(res.getString("Java_home_0_"), 83 tool.getJavaPath())); 84 if (tool.getAppPath() != null) { 85 System.out.println(ResUtil.format(res.getString("Application_home_0_"), 86 tool.getAppPath())); 87 } 88 System.out.println(new String ()); 89 System.out.println(res.getString("Configuration")); 90 System.out.println(new String ()); 91 } catch (ParameterException e) { 92 showUsage(); 93 System.out.println(ResUtil.format(res.getString("Error_0_"), 94 e.getMessage())); 95 } catch (ConfigException e) { 96 System.out.println(ResUtil.format(res.getString("Error_0_"), 97 e.getMessage())); 98 } 99 } 100 101 private void doCopy() { 102 File sourceDir = null; 103 ExtensionFilter copyFilter = null; 104 105 sourceDir = new File (tool.getAppPath() + File.separator + DIR_INPUT); 106 copyFilter = new ExtensionFilter(); 107 copyFilter.addExclusion(".in"); 108 copyFilter.setExcludeOnly(true); 109 recurseCopy(sourceDir, copyFilter); 110 } 111 112 private void recurseCopy(File sourceDir, ExtensionFilter copyFilter) { 113 File [] sourceFiles = new File [0]; 114 PathHandle dest = null; 115 116 sourceFiles = sourceDir.listFiles(copyFilter); 117 for (int i = 0; i < sourceFiles.length; i++) { 118 if (sourceFiles[i].isDirectory()) { 119 recurseCopy(sourceFiles[i], copyFilter); 120 } else { 121 dest = getDestPath(sourceFiles[i]); 122 try { 123 FileUtil.copy(sourceFiles[i], dest.getFile()); 124 System.out.println(res.getString("Copying_input_to") 125 + dest.getPath()); 126 } catch (ToolException e) { 127 e.printStackTrace(); 128 } 129 } 130 } 131 } 132 133 private PathHandle getDestPath(File inFile) { 134 PathHandle inputPath = null; 135 PathHandle filePath = null; 136 PathHandle rootPath = null; 137 PathHandle passPath = null; 138 StringBuffer buf = new StringBuffer (); 139 String suffix = new String (); 140 141 inputPath = PathHandle.createPathHandle(tool.getAppPath() 142 + File.separator + DIR_INPUT); 143 filePath = PathHandle.createPathHandle(inFile); 144 rootPath = PathHandle.createPathHandle(tool.getAppPath() 145 + File.separator + DIR_OUTPUT); 146 buf.append(rootPath.getPath()); 147 if (inputPath.parentOf(filePath)) { 148 suffix = 149 filePath.getPath().substring(inputPath.getPath().length()); 150 } else { 151 suffix = filePath.getFile().getName(); 152 } 153 if ((suffix.length() == 0) || (suffix.charAt(0) != '/')) { 154 buf.append('/'); 155 } 156 buf.append(suffix); 157 passPath = PathHandle.createPathHandle(buf.toString()); 158 return passPath; 159 } 160 161 private String [] extractAllOption(String [] args) { 162 boolean all = false; 163 boolean app = false; 164 ArrayList list = null; 165 166 list = new ArrayList (Arrays.asList(args)); 167 for (int i = 0; i < args.length; i++) { 168 if (args[i].equalsIgnoreCase(PARAM_ALL)) { 169 all = true; 170 list.remove(args[i]); 171 } 172 if (args[i].equalsIgnoreCase(PARAM_APP)) { 173 app = true; 174 } 175 } 176 copyNonTemplates = app && all; 177 list.trimToSize(); 178 args = new String [list.size()]; 179 args = (String []) list.toArray(args); 180 return args; 181 } 182 183 private void parseArgs(String [] args) throws ParameterException { 184 args = extractAllOption(args); 185 if (args.length == 0) { 186 187 } else if ((args.length % 2) == 1) { 189 throw new ParameterException(res.getString("Wrong_number_of")); 190 } else if (args.length > 6) { 191 throw new ParameterException(res.getString("Too_many_parameters")); 192 } else { 193 for (int i = 0; i < args.length; i += 2) { 194 if (!isKey(args[i])) { 195 throw new ParameterException(ResUtil.format(res.getString("Invalid_parameter_0_"), 196 args[i])); 197 } 198 int dirIndex = i + 1; 199 200 if (dirIndex < args.length) { 201 if ((!FileUtil.isDirectory(args[dirIndex]))) { 202 throw new ParameterException(ResUtil.format(res.getString("Directory_not_found_0"), 203 args[dirIndex])); 204 } else { 205 initProperty(args[i], args[dirIndex]); 206 } 207 } 208 } 209 } 210 } 211 212 private void initProperty(String key, String in) { 213 String path = FileUtil.toCanonicalPath(in); 214 215 path = FileUtil.toJavaPath(path); 216 if (key.equalsIgnoreCase(PARAM_JAVA)) { 217 tool.setJavaPath(path); 218 } else if (key.equalsIgnoreCase(PARAM_ENHYDRA)) { 219 tool.setEnhydraRoot(path); 220 } else if (key.equalsIgnoreCase(PARAM_APP)) { 221 tool.setAppPath(path); 222 } 223 } 224 225 private boolean isKey(String key) { 226 return (key.equalsIgnoreCase(PARAM_JAVA) 227 || key.equalsIgnoreCase(PARAM_ENHYDRA) 228 || key.equalsIgnoreCase(PARAM_APP)); 229 } 230 231 private void showUsage() { 232 System.out.println(res.getString("Usage01")); 233 System.out.println(res.getString("Usage02")); 234 System.out.println(res.getString("Usage03")); 235 System.out.println(new String ()); 236 System.out.println(ResUtil.format(res.getString("Usage04"), 237 PARAM_APP)); 238 System.out.println(res.getString("Usage05")); 239 System.out.println(new String ()); 240 System.out.println(res.getString("Usage06")); 241 System.out.println(res.getString("Usage07")); 242 System.out.print(ResUtil.format(res.getString("Usage08"), 243 PARAM_JAVA)); 244 System.out.print(ResUtil.format(res.getString("Usage09"), 245 PARAM_ENHYDRA)); 246 System.out.print(ResUtil.format(res.getString("Usage10"), PARAM_APP, 247 PARAM_ALL)); 248 System.out.println(new String ()); 249 System.out.println(new String ()); 250 } 251 252 private class ParameterException extends ConfigException { 253 public ParameterException(String msg) { 254 super(msg); 255 } 256 257 } 258 } 259 | Popular Tags |