KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > axis > JVelocity


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  *
19  * Initial Developer : Guillaume Sauthier
20  * --------------------------------------------------------------------------
21  * $Id: JVelocity.java,v 1.4 2004/12/16 09:46:37 benoitf Exp $
22  * --------------------------------------------------------------------------
23  */

24
25 package org.objectweb.jonas_ws.wsgen.generator.axis;
26
27 import java.io.File JavaDoc;
28 import java.io.FileWriter JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import org.apache.velocity.Template;
32 import org.apache.velocity.VelocityContext;
33 import org.apache.velocity.app.VelocityEngine;
34 import org.apache.velocity.runtime.RuntimeConstants;
35
36 import org.objectweb.jonas_lib.I18n;
37
38 import org.objectweb.jonas_ws.wsgen.WsGenException;
39
40 import org.objectweb.jonas.common.Log;
41
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44
45 /**
46  * Wrapper around Velocity tool.
47  *
48  * @author Guillaume Sauthier
49  */

50 public class JVelocity {
51
52     /** i18n */
53     private static I18n i18n = I18n.getInstance(JVelocity.class);
54
55     /**
56      * logger
57      */

58     private static Logger logger = Log.getLogger(Log.JONAS_WSGEN_PREFIX);
59
60     /** Velocity Engine */
61     private VelocityEngine vEngine;
62
63     /** Velocity Template */
64     private Template template;
65
66     /**
67      * Creates a new JVelocity instance using the given template.
68      *
69      * @param tmplName the template filename
70      *
71      * @exception WsGenException when error occurs during Velocity
72      * initialization.
73      */

74     public JVelocity(String JavaDoc tmplName) throws WsGenException {
75         // Prepare Velocity Engine
76
String JavaDoc jonasRoot = System.getProperty("install.root");
77
78         if (jonasRoot == null) {
79             String JavaDoc err = i18n.getMessage("JVelocity.constr.notset");
80             throw new WsGenException(err);
81         }
82
83         String JavaDoc path2Tmpl = new String JavaDoc(jonasRoot + File.separatorChar + "templates" + File.separatorChar + "wsgen"
84                 + File.separatorChar + "generator" + File.separatorChar + "axis");
85
86         // instanciate the engine
87
vEngine = new VelocityEngine();
88         vEngine.setProperty(RuntimeConstants.VM_LIBRARY, "");
89         vEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
90         vEngine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path2Tmpl);
91
92         try {
93             vEngine.init();
94         } catch (Exception JavaDoc e) {
95             String JavaDoc err = i18n.getMessage("JVelocity.constr.initFailure");
96             throw new WsGenException(err, e);
97         }
98
99         // Create the Template
100
try {
101             template = vEngine.getTemplate(tmplName);
102         } catch (Exception JavaDoc e) {
103             String JavaDoc err = i18n.getMessage("JVelocity.constr.tmplError", tmplName);
104             throw new WsGenException(err, e);
105         }
106     }
107
108     /**
109      * Generate the given file with the given VelocityContext.
110      *
111      * @param fs the output file
112      * @param context VelocityContext
113      *
114      * @throws WsGenException when velocity generation fails
115      */

116     public void generate(File JavaDoc fs, VelocityContext context) throws WsGenException {
117         FileWriter JavaDoc fwriter = null;
118
119         try {
120             // Create before the parent directory
121
File JavaDoc fdir = fs.getParentFile();
122
123             if (fdir != null) {
124                 if (!fdir.exists()) {
125                     if (!fdir.mkdirs()) {
126                         String JavaDoc err = i18n.getMessage("JVelocity.generate.directories", fdir.getPath());
127                         throw new WsGenException(err);
128                     }
129                 }
130             }
131
132             fwriter = new FileWriter JavaDoc(fs);
133         } catch (IOException JavaDoc e) {
134             String JavaDoc err = i18n.getMessage("JVelocity.generate.file", fs);
135             throw new WsGenException(err, e);
136         }
137
138         try {
139             template.merge(context, fwriter);
140         } catch (Exception JavaDoc e) {
141             String JavaDoc err = i18n.getMessage("JVelocity.generate.cannot", fs);
142             throw new WsGenException(err, e);
143         }
144
145         try {
146             fwriter.flush();
147             fwriter.close();
148         } catch (IOException JavaDoc e) {
149             // do nothing, just a warn
150
String JavaDoc err = i18n.getMessage("JVelocity.generate.close", fs);
151             logger.log(BasicLevel.WARN, err);
152         }
153     }
154 }
Popular Tags