KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > generator > lib > AbstractVelocityGenerator


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
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  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27
28 package org.objectweb.speedo.generation.generator.lib;
29
30 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
31 import org.objectweb.speedo.generation.generator.api.SpeedoGenerationException;
32 import org.objectweb.speedo.generation.generator.api.VelocityGenerator;
33 import org.objectweb.speedo.api.SpeedoException;
34 import org.objectweb.speedo.metadata.SpeedoClass;
35 import org.objectweb.speedo.metadata.SpeedoIdentity;
36 import org.objectweb.jorm.metainfo.api.Class;
37 import org.objectweb.jorm.metainfo.api.TypedElement;
38 import org.apache.velocity.Template;
39 import org.apache.velocity.app.VelocityEngine;
40
41 import java.util.Collection JavaDoc;
42 import java.io.File JavaDoc;
43
44 /**
45  * @author S.Chassande-Barrioz
46  */

47 public abstract class AbstractVelocityGenerator
48         extends AbstractGeneratorComponent
49         implements VelocityGenerator {
50
51     public final static String JavaDoc TEMPLATE_DIR
52             = "org.objectweb.speedo.generation.generator";
53
54     protected VelocityEngine ve = null;
55
56     protected static char fs = File.separatorChar;
57
58     protected Template template = null;
59
60     protected void computeTemplate(String JavaDoc templateFN) throws SpeedoException {
61         try {
62             if (template == null) {
63                 try {
64                     template = ve.getTemplate(templateFN);
65                 } catch (Exception JavaDoc e1) {
66                     template = null;
67                 }
68                 if (template == null) {
69                     templateFN = templateFN.replace(fs, '/');
70                     template = ve.getTemplate(templateFN);
71                     if (template == null) {
72                         throw new SpeedoException("The Speedo template "
73                                 + templateFN
74                                 + " is not availlable in the classpath");
75                     }
76                 }
77             }
78         } catch (Exception JavaDoc e) {
79             throw new SpeedoGenerationException(
80                     "Impossible to fetch the template " + templateFN, e);
81         }
82     }
83     
84     /**
85      * This method indicates if the type is a boolean, a char, a short, an int,
86      * a long, a float, a double or a String.
87      *
88      * @param type the type to determine.
89      * @return true if the String is one of those above.
90      */

91     protected static boolean isClassicalType(String JavaDoc type) {
92         return type.equals("boolean")
93                 || type.equals("char")
94                 || type.equals("short")
95                 || type.equals("int")
96                 || type.equals("long")
97                 || type.equals("float")
98                 || type.equals("double")
99                 || type.equals("String")
100                 || type.equals("byte");
101     }
102
103     public boolean isContainerIdField(Class JavaDoc clazz, TypedElement te, SpeedoClass sc) {
104         return sc.identityType == SpeedoIdentity.CONTAINER_ID
105                 && clazz
106                 .getNameDef(scp.projectName)
107                 .getNameRef().getProjection().containsValue(te.getName());
108     }
109
110
111
112     // IMPLEMENTATION OF THE VelocityGenerator INTERFACE //
113
//---------------------------------------------------//
114

115     public VelocityEngine getVelocityEngine() {
116         return ve;
117     }
118
119     public void setVelocityEngine(VelocityEngine ve) {
120         this.ve = ve;
121     }
122
123     public Collection JavaDoc getExternalsTemplate() {
124         return null;
125     }
126
127     /**
128      * generate the file name corresponding to the Speedo meta object given in
129      * parameter.
130      * @param sClass is the speedo meta object which represents a persistent class
131      * @param fileName is the generated file name
132      * @throws SpeedoException
133      */

134     public abstract void generate(SpeedoClass sClass, String JavaDoc fileName)
135             throws SpeedoException;
136
137     // IMPLEMENTATION OF THE GeneratorComponent INTERFACE //
138
//----------------------------------------------------//
139

140     public abstract boolean init() throws SpeedoException;
141
142     public final void process() throws SpeedoException {
143         throw new SpeedoException("Do not use this method");
144     }
145 }
146
Popular Tags