KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > generator > lib > AccessorGenerator


1 /**
2  * JORM: an implementation of a generic mapping system for persistent Java
3  * objects. Two mapping are supported: to RDBMS and to binary files.
4  * Copyright (C) 2001-2003 France Telecom R&D - INRIA
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Contact: jorm-team@objectweb.org
21  *
22  */

23
24 package org.objectweb.jorm.generator.lib;
25
26 import org.apache.velocity.VelocityContext;
27 import org.apache.velocity.context.Context;
28 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
29 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
30 import org.objectweb.jorm.metainfo.api.Class;
31 import org.objectweb.jorm.metainfo.api.Package;
32 import org.objectweb.jorm.util.io.api.TargetHolder;
33 import org.objectweb.jorm.api.PException;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.io.File JavaDoc;
37 import java.io.FileWriter JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 /**
41  * This class is a generator of XXXAccessor objects. It is generic either the
42  * mapper type. This generator use the velocity tools. The used template is
43  * Accessor.vm . Associated to this template this generator builds a velocity
44  * context which contains the following information:<br/>
45  * <table border=1>
46  * <tr><td>Key</td><td>Value</td></tr>
47  * <tr><td>"class"</td><td>The reference to the Class meta object</td></tr>
48  * <tr>
49  * <td>"tools"</td>
50  * <td>The reference to the <a HREF="CommonHelper.html">CommonHelper<a/></td>
51  * </tr>
52  * <tr>
53  * <td>"header"</td>
54  * <td>The file name of the common template which contains the header of
55  * the generate files.</td>
56  * </tr>
57  * </table>
58  */

59 public class AccessorGenerator extends CommonGenerator {
60     /**
61      * This method generates a XAccessor file corresponding to the pod
62      * parameter in the directory parameter.
63      * @param co The meta object interface.
64      * @param holder The target holder which allows to create files.
65      * @param cp This parameter permits to reach the compilation parameters.
66      */

67     public void generate(Class JavaDoc co,
68                          TargetHolder holder,
69                          JormCompilerParameter cp) throws PException {
70         // Calculate fileName
71
String JavaDoc packName = ((Package JavaDoc) co.getParent()).getName();
72         String JavaDoc fileName = co.getName() + "Accessor";
73         if (packName != null && packName.length() > 0) {
74             fileName = packName + "." + fileName;
75             fileName = fileName.replace('.', File.separatorChar);
76         }
77         logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " interface");
78         Context ctx = new VelocityContext();
79         ctx.put("class", co);
80         CommonHelper ch = new CommonHelper();
81         ch.setLogger(logger);
82         ctx.put("tools", ch);
83         ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
84
85         StringBuffer JavaDoc extension = new StringBuffer JavaDoc("PAccessor");
86         Iterator JavaDoc it = co.getSuperClasses().iterator();
87         if (it.hasNext()) {
88             while(it.hasNext()) {
89                 extension.append(", ");
90                 extension.append(((Class JavaDoc) it.next()).getFQName());
91                 extension.append("Accessor");
92             }
93         }
94         ctx.put("extension", extension);
95
96         try {
97             // Fetch a block with the fileName
98
FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
99             //load only once the template
100
if (template == null) {
101                 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Accessor.vm");
102             }
103             template.merge(ctx, fw);
104             fw.flush();
105             fw.close();
106         } catch (Exception JavaDoc e) {
107             throw new PExceptionCompiler(e, "Problem while generating PAccessor.");
108         }
109     }
110 }
111
Popular Tags