KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > newbean > JopeUpdateBean


1 package com.bull.eclipse.newbean;
2
3
4 import java.io.FileWriter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.util.Vector JavaDoc;
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 /**
20  * This class is a “bean generator”. It uses Velocity and
21  * a set of template files to generate some files that can serve as a
22  * startup point for the developpement of a new bean.
23  */

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 JavaDoc pkgListe = null;
32     private JonasProject prj = null;
33
34
35     public JopeUpdateBean(Vector JavaDoc pkgListe, JonasProject prj) {
36         this.pkgListe = pkgListe;
37         this.prj = prj;
38     }
39
40
41     /**
42      * Generates files for this new bean.
43      */

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 JavaDoc e) {
54                 fatalError("unable to initilise Velocity engine (" + e + ")");
55             }
56
57             // Creates a Velocity context and populates it by walking
58
// through the parameter set
59
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 JavaDoc e) {
64             error(e.toString());
65         }
66     
67     }
68
69
70
71     /**
72      * Generates a file from the specified template.
73      * @param templateFileName the name of the template file
74      * @param targetFileName the name of the generated file
75      */

76     private void generate(String JavaDoc templateFileName,
77                           String JavaDoc targetFileName) throws Exception JavaDoc, IOException JavaDoc, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
78         FileWriter JavaDoc fileWriter = null;
79         fileWriter = new FileWriter JavaDoc(targetFileName);
80         vEngine.mergeTemplate(templateFileName, vContext, fileWriter);
81         fileWriter.close();
82     }
83
84
85     /**
86      * Display the specified error message.
87      * @param errMsg the error message to display
88      */

89     static void error(String JavaDoc errMsg) {
90         System.err.println("NewBean error: " + errMsg);
91     }
92
93
94     /**
95      * Display the specified error message and exits with an
96      * EXIT_FAILURE status.
97      * @param errMsg the error message to display
98      */

99     static void fatalError(String JavaDoc errMsg) {
100         System.err.println("NewBean fatal error: " + errMsg);
101         System.exit(EXIT_FAILURE);
102     }
103
104 }
105
Popular Tags