KickJava   Java API By Example, From Geeks To Geeks.

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


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
39 /**
40  * @author yoann
41  */

42 public class SerializableGenerator extends CommonGenerator {
43     /**
44      * This method generates a XSerializableData file corresponding to the pod
45      * parameter in the directory parameter.
46      * @param co The meta object interface.
47      * @param holder The target holder which allows to create files.
48      * @param cp This parameter permits to reach the compilation parameters.
49      */

50         public void generate(Class JavaDoc co,
51                              TargetHolder holder,
52                              JormCompilerParameter cp) throws PException {
53             // Calculate fileName
54
String JavaDoc packName = ((Package JavaDoc) co.getParent()).getName();
55             String JavaDoc packroot = packName;
56             String JavaDoc fileName = co.getName() + "Serializable";
57             if (packName != null && packName.length() > 0) {
58                 fileName = packName + "." + fileName;
59                 fileName = fileName.replace('.', File.separatorChar);
60             }
61             logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class");
62             try {
63                 // Fetch a block with the fileName
64
FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
65                 Context ctx = new VelocityContext();
66                 ctx.put("class", co);
67                 CommonHelper ch = new CommonHelper();
68                 ch.setLogger(logger);
69                 ctx.put("tools", ch);
70                 if (packName.length() > 0) {
71                     ctx.put("packageroot",packroot);
72                 }
73                 ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
74                 if (template == null) {
75                     template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Serializable.vm");
76                 }
77                 template.merge(ctx, fw);
78                 fw.flush();
79                 fw.close();
80             } catch (Exception JavaDoc e) {
81                 throw new PExceptionCompiler(e, "Problem while generating Serializable.");
82             }
83         }
84
85 }
86
Popular Tags