KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.generator.lib;
19
20 import org.objectweb.jorm.metainfo.api.Class;
21 import org.objectweb.jorm.metainfo.api.Package;
22 import org.objectweb.jorm.metainfo.api.NameDef;
23 import org.objectweb.jorm.metainfo.api.ClassProject;
24 import org.objectweb.jorm.metainfo.api.Mapping;
25 import org.objectweb.jorm.metainfo.api.ClassMapping;
26 import org.objectweb.jorm.util.io.api.TargetHolder;
27 import org.objectweb.jorm.compiler.api.JormCompilerParameter;
28 import org.objectweb.jorm.compiler.api.PExceptionCompiler;
29 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator;
30 import org.objectweb.jorm.api.PException;
31 import org.objectweb.jorm.api.PMapper;
32 import org.objectweb.jorm.generator.api.MOP;
33 import org.objectweb.util.monolog.api.BasicLevel;
34 import org.apache.velocity.context.Context;
35 import org.apache.velocity.VelocityContext;
36
37 import java.io.FileWriter JavaDoc;
38 import java.io.File JavaDoc;
39 import java.util.Collection JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * Mapping generator for abstract classes
46  *
47  * @author S.Chassande-Barrioz
48  */

49 public class AbstractMappingGenerator extends CommonGenerator {
50
51     /**
52      * The mapper generation specializations (some MOP objects)
53      * key = mapper name
54      * value = a MOP
55      */

56     protected HashMap JavaDoc mmops = new HashMap JavaDoc();
57
58     /**
59      * This constrcutor permits to specify which BindingMOP must be used
60      */

61     public AbstractMappingGenerator(JormCompilerConfigurator jcc,
62                                     JormCompilerParameter cp) throws PException {
63         super();
64         Iterator JavaDoc it = jcc.knownMappers();
65         while (it.hasNext()) {
66             String JavaDoc mn = (String JavaDoc) it.next();
67             mmops.put(mn, jcc.getMOPFactory(mn).createMappingMOP(cp, mn));
68         }
69     }
70
71     public String JavaDoc[][] getTemplateLibraries() {
72         String JavaDoc[][] res = new String JavaDoc[mmops.size()][];
73         Iterator JavaDoc it = mmops.values().iterator();
74         int i = 0;
75         while (it.hasNext()) {
76             res[i] = ((MOP) it.next()).getTemplateLibraries();
77             i++;
78         }
79         return res;
80     }
81
82     public void generate(Class JavaDoc clazz,
83                          TargetHolder holder,
84                          JormCompilerParameter cp,
85                          JormCompilerConfigurator jcc) throws PException {
86         if (!clazz.isAbstract()) {
87             return;
88         }
89         Collection JavaDoc nds = clazz.getNameDefs();
90         Class JavaDoc classWithNd = clazz;
91         while(nds.size() < 1 && classWithNd.getInheritedClassNumber() > 0) {
92             classWithNd = (Class JavaDoc) classWithNd.getSuperClasses().iterator().next();
93             nds = classWithNd.getNameDefs();
94         }
95         if (nds.size() < 1) {
96             logger.log(BasicLevel.WARN, "AbstractMapping: No NameDef defined for the class " + clazz.getFQName());
97             return;
98         }
99         if (nds.size() == 1) {
100             generate(clazz, holder, cp, (NameDef) nds.iterator().next(), null);
101         } else {
102             Iterator JavaDoc it = nds.iterator();
103             while(it.hasNext()) {
104                 NameDef nd = (NameDef) it.next();
105                 generate(clazz, holder, cp, nd,
106                     (nd.getName() != null && nd.getName().length() == 0
107                     ? null
108                     : nd.getName()));
109             }
110         }
111
112     }
113     public void generate(Class JavaDoc clazz,
114                          TargetHolder holder,
115                          JormCompilerParameter cp,
116                          NameDef nd,
117                          String JavaDoc ndName) throws PException {
118
119         String JavaDoc packName = ((Package JavaDoc) clazz.getParent()).getName();
120         // File name
121
String JavaDoc fileName = clazz.getName() + PMapper.PCLASSMAPPINGAPPENDER;
122         if (ndName != null) {
123             if (packName != null && packName.length() > 0) {
124                 packName += "." + ndName;
125             } else {
126                 packName = ndName;
127             }
128         }
129         if (packName.length() > 0) {
130             fileName = (packName + '.').replace('.', File.separatorChar) + fileName;
131         }
132         logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " class");
133         Context ctx = new VelocityContext();
134         ctx.put("class", clazz);
135         if (packName.length() > 0) {
136             ctx.put("package", packName);
137         }
138         ctx.put("cparam", cp);
139         CommonHelper ch = new CommonHelper();
140         ch.setLogger(logger);
141         ctx.put("tools", ch);
142         ctx.put("mappername", "");
143         ctx.put("noBindingCreation", Boolean.TRUE);
144         ctx.put("abstractMapping", Boolean.TRUE);
145         ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
146         ctx.put("mappingTools", this);
147         ClassProject classproject = clazz.getClassProject(cp.getProjectName());
148         ArrayList JavaDoc dependencies = new ArrayList JavaDoc();
149         if (classproject != null) {
150             Collection JavaDoc ms = classproject.getMappings();
151             if (!ms.isEmpty()) {
152                 Iterator JavaDoc it = ms.iterator();
153                 while(it.hasNext()) {
154                     Mapping m = (Mapping) it.next();
155                     ClassMapping cm = m.getClassMapping();
156                     if (cm != null) {
157                         dependencies.addAll(cm.getDependencies());
158                     }
159                 }
160             }
161         }
162         //mmop.initContext(ctx, clazz, cp.getProjectName(), null);
163
ctx.put("dependencies", dependencies);
164         try {
165             // Fetch a block with the fileName
166
FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
167             velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "Mapping.vm").merge(ctx, fw);
168             fw.flush();
169             fw.close();
170         } catch (Exception JavaDoc e) {
171             throw new PExceptionCompiler(e, "Problem while writing Mapping java file: " + fileName + ".java");
172         }
173
174     }
175 }
176
Popular Tags