1 package com.bull.eclipse.newbean; 2 3 4 import java.io.FileWriter ; 5 import java.io.IOException ; 6 import java.util.Vector ; 7 8 import org.apache.velocity.VelocityContext; 9 import org.apache.velocity.app.VelocityEngine; 10 import org.apache.velocity.exception.MethodInvocationException; 11 import org.apache.velocity.exception.ParseErrorException; 12 import org.apache.velocity.exception.ResourceNotFoundException; 13 14 import com.bull.eclipse.jonas.JonasProject; 15 import com.bull.eclipse.jonas.utils.TemplateDirUtil; 16 17 18 19 24 public class JopeUpdateBean { 25 26 private static final int EXIT_SUCCESS = 0; 27 private static final int EXIT_FAILURE = 1; 28 29 private VelocityEngine vEngine = null; 30 private VelocityContext vContext = null; 31 private Vector pkgListe = null; 32 private JonasProject prj = null; 33 34 35 public JopeUpdateBean(Vector pkgListe, JonasProject prj) { 36 this.pkgListe = pkgListe; 37 this.prj = prj; 38 } 39 40 41 44 public void generate() { 45 try { 46 vEngine = new VelocityEngine(); 47 vEngine.setProperty(VelocityEngine.VM_LIBRARY, ""); 48 vEngine.setProperty(VelocityEngine.RESOURCE_LOADER, "file"); 49 vEngine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, 50 TemplateDirUtil.getTemplateDir().concat("/newbean")); 51 try { 52 vEngine.init(); 53 } catch (Exception e) { 54 fatalError("unable to initilise Velocity engine (" + e + ")"); 55 } 56 57 vContext = new VelocityContext(); 60 vContext.put("pkgListe", pkgListe); 61 generate("buildgene.vm", prj.getProject().getLocation().append("components").append("build.xml").toOSString()); 62 System.out.println("Your bean files have been created. You can now customize them."); 63 } catch (Exception e) { 64 error(e.toString()); 65 } 66 67 } 68 69 70 71 76 private void generate(String templateFileName, 77 String targetFileName) throws Exception , IOException , ResourceNotFoundException, ParseErrorException, MethodInvocationException { 78 FileWriter fileWriter = null; 79 fileWriter = new FileWriter (targetFileName); 80 vEngine.mergeTemplate(templateFileName, vContext, fileWriter); 81 fileWriter.close(); 82 } 83 84 85 89 static void error(String errMsg) { 90 System.err.println("NewBean error: " + errMsg); 91 } 92 93 94 99 static void fatalError(String errMsg) { 100 System.err.println("NewBean fatal error: " + errMsg); 101 System.exit(EXIT_FAILURE); 102 } 103 104 } 105 | Popular Tags |