1 2 24 package org.enhydra.tool.codegen; 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.Replacement; 31 import org.enhydra.tool.common.ReplacementSet; 32 import org.enhydra.tool.common.ResUtil; 33 import org.enhydra.tool.common.ToolException; 34 import org.enhydra.tool.common.Template; 35 import org.enhydra.tool.common.TemplateTool; 36 import org.enhydra.tool.common.TemplateFilter; 37 import org.enhydra.tool.common.event.TemplateListener; 38 import org.enhydra.tool.configure.ConfigTool; 39 40 import java.io.File ; 42 import java.util.Properties ; 43 import java.util.ResourceBundle ; 44 import java.util.Vector ; 45 import javax.swing.JFrame ; 46 import java.net.URL ; 47 48 53 abstract public class TemplateDrivenGenerator implements Generator, 54 TemplateListener, Constants { 55 public static ResourceBundle res = 56 ResourceBundle.getBundle("org.enhydra.tool.codegen.Res"); 58 private Properties properties = new Properties (); 60 private File rootDir = null; 61 private Vector fileVector = new Vector (); 62 private ReplacementSet replacementSet = null; 63 private OptionSet optionSet = null; 64 private boolean overwrite = false; 65 private boolean echo = true; 66 private boolean swing = true; 67 68 abstract public TemplateFilter createTemplateFilter(TemplateTool tool) 70 throws GeneratorException; 71 abstract public TemplateFilter createCopyFilter(TemplateTool tool) 72 throws GeneratorException; 73 abstract public String getDestination() throws GeneratorException; 74 abstract public void initReplacementSet() throws GeneratorException; 75 abstract public Replacement[] createReplacementsForDirectory(PathHandle path) 76 throws GeneratorException; 77 78 85 public String toString() { 86 return getDisplayName(); 87 } 88 89 97 public Properties getProperties() { 98 return properties; 99 } 100 101 109 public void setProperties(Properties p) throws GeneratorException { 110 Template root = null; 111 112 properties = p; 113 root = getTemplateRoot(); 114 if (root == null) { 115 throw new GeneratorException(res.getString("Root_path_is_not")); 116 } else if (!root.isDirectory()) { 117 throw new GeneratorException(ResUtil.format(res.getString("Invalid_template_0"), 118 root.toString())); 119 } 120 121 GeneratorOption[] options = getOptionSet().toArray(); 123 124 for (int i = 0; i < options.length; i++) { 125 String name = options[i].getName(); 126 String value = options[i].getValue(); 127 128 value = properties.getProperty(getCommandName() + '.' + name, 129 value); 130 if (value != null) { 131 if (options[i].isRequired()) { 132 if (value.trim().length() > 0) { 133 getOptionSet().lookup(name).setValue(value); 134 } 135 } else { 136 getOptionSet().lookup(name).setValue(value); 137 } 138 } 139 } 140 } 141 142 148 public Template getTemplateRoot() { 149 StringBuffer buf = new StringBuffer (); 150 String path = new String (); 151 URL url = null; 152 Template root = null; 153 154 buf.append(DIR_TEMPLATE); 155 buf.append('/'); 156 buf.append(getCommandName()); 157 buf.append('/'); 158 url = this.getClass().getClassLoader().getResource(buf.toString()); if (url == null) { 161 System.err.println(""); 162 System.err.println("Failed to load resource: " + buf.toString()); 163 root = null; 164 } else { 165 root = new Template(url, buf.toString()); 166 } 167 return root; 168 } 169 170 public boolean isOverwrite() { 171 return overwrite; 172 } 173 174 public void setOverwrite(boolean b) { 175 overwrite = b; 176 } 177 178 public boolean isEcho() { 179 return echo; 180 } 181 182 public void setEcho(boolean b) { 183 echo = b; 184 } 185 186 public boolean isSwing() { 187 return swing; 188 } 189 190 public void setSwing(boolean b) { 191 swing = b; 192 } 193 194 202 public ReplacementSet getReplacementSet() { 203 return replacementSet; 204 } 205 206 214 public void setReplacementSet(ReplacementSet replacementSet) { 215 this.replacementSet = replacementSet; 216 } 217 218 225 public OptionSet getOptionSet() { 226 return optionSet; 227 } 228 229 public void setOptionSet(OptionSet set) { 230 optionSet = set; 231 } 232 233 245 public File [] generate() throws GeneratorException { 246 initReplacementSet(); 247 File [] files = new File [0]; 248 249 rootDir = new File (getDestination()); 250 fileVector = new Vector (); 251 if (getTemplateRoot() == null) { 252 throw new GeneratorException(res.getString("Template_null")); 253 } 254 try { 255 recurseFolders(getTemplateRoot(), rootDir); 256 } catch (ToolException e) { 257 String mess = new String (); 258 259 mess = ResUtil.format(res.getString("Failed_to_copy"), 260 getTemplateRoot().toString(), rootDir); 261 throw new GeneratorException(e, mess); 262 } 263 storeProperties(); 264 files = (File []) fileVector.toArray(files); 265 fileVector.removeAllElements(); 266 return files; 267 } 268 269 273 278 public void storeProperties() { 279 try { 280 Properties tbProps = ToolBoxInfo.loadProperties(); 281 GeneratorOption[] options = getOptionSet().toArray(); 282 283 for (int i = 0; i < options.length; i++) { 284 if (options[i].isPersistent()) { 285 tbProps.setProperty(getCommandName() + '.' 286 + options[i].getName(), options[i].getValue()); 287 } 288 } 289 ToolBoxInfo.storeProperties(tbProps); 290 } catch (ToolException e) { 291 292 e.printStackTrace(System.err); 294 } 295 } 296 297 protected String [] getInputIncludes() { 298 String [] includes = new String [1]; 299 300 if (ToolBoxInfo.isEnhydra3()) { 301 includes[0] = "3"; 302 } else { 303 includes[0] = "4"; 304 } 305 return includes; 306 } 307 308 317 private void recurseFolders(Template source, File dest) 318 throws ToolException, GeneratorException { 319 Template[] toolList = new Template[0]; 320 Template[] copyList = new Template[0]; 321 File [] output = new File [0]; 322 ConfigTool tool = null; 323 String destPath = null; 324 325 tool = new ConfigTool(); 326 tool.addTemplateListener(this); 327 tool.setReplacements(replacementSet.toArray()); 328 tool.setOverwrite(isOverwrite()); 329 tool.setEcho(isEcho()); 330 if (isSwing()) { 331 tool.setOwner(new JFrame ()); 332 } 333 toolList = source.listTemplates(createTemplateFilter(tool)); 334 copyList = source.listTemplates(createCopyFilter(tool)); 335 destPath = dest.getAbsolutePath(); 336 destPath = tool.lineSearchAndReplace(destPath); 337 dest = new File (destPath); 338 339 if (copyList != null) { 341 for (int i = 0; i < copyList.length; i++) { 342 if (copyList[i].isDirectory()) { 343 File nextDest = null; 344 345 nextDest = new File (dest, copyList[i].getName()); 346 recurseFolders(copyList[i], nextDest); 347 } else { 348 dest.mkdirs(); 349 File outCopy = null; 350 outCopy = FileUtil.copy(copyList[i], dest); 351 if (isEcho()) { 352 PathHandle path = 353 PathHandle.createPathHandle(outCopy); 354 355 System.out.println("Copying file to: " 356 + path.getPath()); 357 } 358 fileVector.addElement(outCopy); 359 } 360 } 361 } 362 363 if (toolList != null && toolList.length > 0) { 365 tool.setTemplates(toolList); 366 Replacement[] pathReplacements = new Replacement[0]; 367 String outPath = new String (); 368 369 if (tool.getTemplates()[0].getOutput().isDirectory()) { 370 outPath = 371 tool.getTemplates()[0].getOutput().getAbsolutePath(); 372 } else { 373 outPath = tool.getTemplates()[0].getOutput().getParent(); 374 } 375 pathReplacements = 376 createReplacementsForDirectory(PathHandle.createPathHandle(outPath)); 377 tool.setReplacements(pathReplacements); 378 output = tool.createOutput(); 379 overwrite = tool.isOverwrite(); 380 for (int i = 0; i < output.length; i++) { 381 fileVector.addElement(output[i]); 382 } 383 for (int i = 0; i < toolList.length; i++) { 384 if (toolList[i].isDirectory()) { 385 File nextDest = null; 386 387 nextDest = new File (dest, toolList[i].getName()); 388 recurseFolders(toolList[i], nextDest); 389 } 390 } 391 } 392 } 393 394 } 395 | Popular Tags |