KickJava   Java API By Example, From Geeks To Geeks.

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


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-2004 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 java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.util.Collection JavaDoc;
29
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.context.Context;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
34 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
35 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
36 import org.objectweb.jorm.metainfo.api.Class;
37 import org.objectweb.jorm.metainfo.api.NameDef;
38 import org.objectweb.jorm.metainfo.api.Package;
39 import org.objectweb.jorm.util.io.api.TargetHolder;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * This class is a generator of XXXBinding objects. This generator use the
44  * velocity tools. The used template is Binding.vm . Associated to this template
45  * this generator builds a velocity context which contains the following
46  * information:<br/>
47  * <table border=1>
48  * <tr><td>Key</td><td>Value</td></tr>
49  * <tr><td>"class"</td><td>The reference to the Class meta object</td></tr>
50  * <tr><td>"classND"</td><td>The reference to the used NameDef</td></tr>
51  * <tr><td>"tools"</td>
52  * <td>The reference to the <a HREF="CommonHelper.html">CommonHelper<a/></td>
53  * </tr>
54  * <tr><td>"header"</td>
55  * <td>The file name of the common template which contains the header of
56  * the generate files.</td>
57  * </tr>
58  * <tr><td>"cparam"</td>
59  * <td>The reference to a CompilerParameter instance. This instance
60  * describes the user compilation parameter.</td>
61  * </tr>
62  * </table>
63  */

64 public class BindingGenerator extends CommonGenerator {
65
66     /**
67      * This method generates a XPBinding file corresponding to the pod
68      * parameter in the directory parameter
69      * @param clazz the class meta object interface
70      * @param holder The target holder which allows to create files
71      * @param cp This parameter permits to reach the compilation parameters
72      * @param jcc the JormCompilerConfigurator
73      */

74     public void generate(Class JavaDoc clazz,
75             TargetHolder holder,
76             JormCompilerParameter cp,
77             JormCompilerConfigurator jcc) throws PException {
78         String JavaDoc pack = ((Package JavaDoc) clazz.getParent()).getName();
79         // File name
80
String JavaDoc fileName = clazz.getName() + "Binding";
81         if (pack.length() > 0) {
82             fileName = (pack + '.').replace('.', File.separatorChar) + fileName;
83         }
84         logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class");
85         Collection JavaDoc nds = clazz.getNameDefs();
86         Class JavaDoc classWithNd = clazz;
87         while(nds.size() < 1 && classWithNd.getInheritedClassNumber() > 0) {
88             classWithNd = (Class JavaDoc) classWithNd.getSuperClasses().iterator().next();
89             nds = classWithNd.getNameDefs();
90         }
91         if (nds.size() < 1) {
92             logger.log(BasicLevel.WARN, "Binding generation: No NameDef defined for the class " + clazz.getFQName());
93             return;
94         }
95         NameDef nd = (NameDef) nds.iterator().next();
96         Context ctx = new VelocityContext();
97         ctx.put("class", clazz);
98         ctx.put("classND", nd);
99         if (pack.length() > 0) {
100             ctx.put("package", pack);
101         }
102         ctx.put("cparam", cp);
103         ctx.put("tools", this);
104         ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
105         try {
106             FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
107             velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Binding.vm").merge(ctx, fw);
108             fw.flush();
109             fw.close();
110         } catch (Exception JavaDoc e) {
111             throw new PExceptionCompiler(e,
112                     "Problem while writing Binding java file: "
113                     + e.getMessage());
114         }
115     }
116 }
117
Popular Tags