KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > rules > codeGenerator > GenerateMacro


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Pierre Carpentier.
23 Contributor(s): Philippe Merle.
24
25 ---------------------------------------------------------------------
26 $Id: GenerateMacro.java,v 1.1 2004/05/26 11:25:35 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.rules.codeGenerator;
30
31 import java.io.Writer JavaDoc;
32
33 import org.apache.velocity.app.Velocity;
34
35 import ispuml.mdaTransformation.RuleContext;
36 import ispuml.mdaTransformation.TransformationException;
37 import ispuml.mdaTransformation.rules.codeGenerator.GenerateAction;
38 import ispuml.mdaTransformation.rules.codeGenerator.VelocityContext;
39 import ispuml.mdaTransformation.rules.codeGenerator.VelocityTemplateDirectives;
40
41 /**
42  * This action allows to call a Velocity macro (in template) to generate a file.
43  * The template must be in the classpath.
44  */

45 public class GenerateMacro extends GenerateAction {
46
47     /**
48      * The Velocimacro to call.
49      */

50     protected String JavaDoc macro;
51
52     /**
53      * Gets the Velocimacro name.
54      * @return The Velocimacro name.
55      */

56     public String JavaDoc getMacro() {
57         return macro;
58     }
59
60     /**
61      * Sets the Velocimacro name.
62      * @param string The name.
63      */

64     public void setMacro(String JavaDoc macro) {
65         this.macro = macro;
66     }
67
68     /**
69      * Calling to the Velocity macro.
70      * @param bean The current bean to generate (with Velocity Template).
71      * @param request The context.
72      * @return Object The bean.
73      * @throws TransformationException
74      */

75     public Object JavaDoc execute(Object JavaDoc bean, RuleContext request) throws TransformationException {
76         // Get or create the writer.
77
// Also remember if we should close the writer at the end.
78
boolean shouldCloseWriter = false;
79
80         // First Check if there is a writer in the context
81
Writer JavaDoc writer = (Writer JavaDoc) request.getAttribute(WRITER_ATTRIBUTE_NAME);
82         // Create the writer if none is found or if the create flag is set
83
if (alwaysCreateWriter || writer == null) {
84             writer = createWriter(bean, request);
85             request.putAttribute(WRITER_ATTRIBUTE_NAME, writer);
86             // We should close the writer if it is created from a filename.
87
shouldCloseWriter = (filename != null);
88         } // end if
89

90         // Generate output from the template
91
try {
92             VelocityContext context = new VelocityTemplateDirectives(bean, request);
93             if (template != null) {
94                 if (macro != null) {
95                     // Velocity properties to remove the log file and to search the templates in the classpath.
96
org.apache.velocity.app.VelocityEngine engine_ = new org.apache.velocity.app.VelocityEngine();
97                     engine_.setProperty(org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
98                                         "org.apache.velocity.runtime.log.NullLogSystem" );
99                     engine_.setProperty(org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER, "classpath");
100                     engine_.setProperty("classpath." + org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER + ".class",
101                                         org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader.class.getName());
102                     engine_.setProperty(org.apache.velocity.app.VelocityEngine.VM_LIBRARY, template);
103                     engine_.init();
104                     boolean res = engine_.invokeVelocimacro(macro, template, new String JavaDoc[0], context, writer);
105                 } else {
106                     org.apache.velocity.runtime.RuntimeSingleton.setProperty(org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS,
107                                                                              "org.apache.velocity.runtime.log.NullLogSystem" );
108                     Velocity.init();
109                     Velocity.mergeTemplate(template, "ISO-8859-1", context, writer);
110                 }
111             } else if (templateString != null) {
112                 Velocity.evaluate(context, writer, "template", templateString);
113             } // end if
114

115             // Close the writer if we have created it.
116
if (shouldCloseWriter)
117                 writer.close();
118         } catch (java.io.IOException JavaDoc ex) {
119             throw new TransformationException("Can't close writer.", ex);
120         } catch (Exception JavaDoc ex) {
121             throw new TransformationException("Template exception.", ex);
122         }
123
124         return bean;
125     }
126
127 }
128
Popular Tags