KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
31 import org.objectweb.jorm.metainfo.api.Package;
32 import org.objectweb.jorm.api.PException;
33 import org.objectweb.jorm.util.io.api.TargetHolder;
34 import org.objectweb.util.monolog.api.BasicLevel;
35
36 import java.io.File JavaDoc;
37 import java.io.FileWriter JavaDoc;
38
39 /**
40  * This class is a generator of XXXPNG objects. It is generic either the
41  * mapper type. This generator use the velocity tools. The used template is
42  * PNG.vm . Associated to this template this generator builds a velocity
43  * context which contains the following information:<br/>
44  * <table border=1>
45  * <tr><td>Key</td><td>Value</td></tr>
46  * <tr><td>"compositename"</td><td>The reference to the compositename meta object</td></tr>
47  * <tr>
48  * <td>"tools"</td>
49  * <td>The reference to the <a HREF="CommonHelper.html">CommonHelper<a/></td>
50  * </tr>
51  * <tr>
52  * <td>"header"</td>
53  * <td>The file name of the common template which contains the header of
54  * the generate files.</td>
55  * </tr>
56  * </table>
57  */

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

66     public void generate(CompositeName co,
67                          TargetHolder holder,
68                          JormCompilerParameter cp) throws PException {
69         // Calculate fileName
70
String JavaDoc packName = ((Package JavaDoc) co.getParent()).getName();
71         String JavaDoc fileName = co.getName() + "PNG";
72         if (packName != null && packName.length() > 0) {
73             fileName = packName + "." + fileName;
74             fileName = fileName.replace('.', File.separatorChar);
75         }
76         logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " interface");
77         try {
78             // Fetch a block with the fileName
79
FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
80             Context ctx = new VelocityContext();
81             ctx.put("compositename", co);
82             CommonHelper helper = new CommonHelper();
83             helper.setLogger(logger);
84             ctx.put("tools", helper);
85             ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
86             logger.log(BasicLevel.DEBUG, "Merging for PNG");
87             if (template == null) {
88                 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Png.vm");
89             }
90             template.merge(ctx, fw);
91             logger.log(BasicLevel.DEBUG, "Merge done for PNG");
92             fw.flush();
93             fw.close();
94         } catch (Exception JavaDoc e) {
95             logger.log(BasicLevel.ERROR, "", e);
96             throw new PExceptionCompiler(e, "Problem while generating PNameGetter.");
97         }
98     }
99 }
100
Popular Tags