KickJava   Java API By Example, From Geeks To Geeks.

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


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

63 public class PNameGenerator extends CommonGenerator {
64     public CommonHelper helper = null;
65
66     /**
67      * This method generates a XPName file corresponding to the pod
68      * parameter in the directory parameter.
69      * @param co The 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      */

73     public void generate(CompositeName co,
74                          TargetHolder holder,
75                          JormCompilerParameter cp) throws PException {
76         // Calculate fileName
77
String JavaDoc packName = ((Package JavaDoc) co.getParent()).getName();
78         String JavaDoc fileName = co.getName() + "PName";
79         if (packName != null && packName.length() > 0) {
80             fileName = packName + "." + fileName;
81             fileName = fileName.replace('.', File.separatorChar);
82         }
83         logger.log(BasicLevel.DEBUG, "Generate the " + fileName + " PName");
84         Context ctx = new VelocityContext();
85         ctx.put("compositename", co);
86         helper = new CommonHelper();
87         helper.setLogger(logger);
88         ctx.put("tools", helper);
89         ctx.put("nametools", this);
90         Boolean JavaDoc hasca = Boolean.FALSE;
91         Boolean JavaDoc hasba = Boolean.FALSE;
92         StringBuffer JavaDoc constructParam = new StringBuffer JavaDoc();
93         Iterator JavaDoc it = co.getAllFields().iterator();
94         while (it.hasNext()) {
95             TypedElement te = (TypedElement) it.next();
96             int tc = te.getType().getTypeCode();
97             if (tc == PType.TYPECODE_BYTEARRAY) {
98                 hasba = Boolean.TRUE;
99             } else if (tc == PType.TYPECODE_CHARARRAY) {
100                 hasca = Boolean.TRUE;
101             }
102             constructParam.append(te.getType().getJavaName());
103             constructParam.append(" ");
104             constructParam.append(te.getName());
105             constructParam.append(", ");
106         }
107         constructParam.append("PNameManager pnc");
108         ctx.put("consparam", constructParam.toString());
109
110         ctx.put("containsCharArray", hasca);
111         ctx.put("containsByteArray", hasba);
112         ctx.put("header", GEN_TEMPLATE_DIR + "Header.vm");
113         try {
114             // Fetch a block with the fileName
115
FileWriter JavaDoc fw = holder.getFileWriter(fileName + ".java");
116             if (template == null) {
117                 template = velocityEngine.getTemplate(GEN_TEMPLATE_DIR + "PName.vm");
118             }
119             template.merge(ctx, fw);
120             fw.flush();
121             fw.close();
122         } catch (Exception JavaDoc e) {
123             throw new PExceptionCompiler(e, "Problem while generating PName.");
124         }
125     }
126
127     /**
128      * It compute the hashcode method for a composite pname.
129      */

130     public String JavaDoc getCnHashCode(CompositeName cn) throws PException {
131         String JavaDoc result = "0", plus = " + ";
132         Iterator JavaDoc fs = cn.getAllFields().iterator();
133         // compositename type can be : byte | char | short | int | long | string
134
// tthe hashcode value for the pname is equal to the hashcode of the first field belonging to the pname.
135
while (fs.hasNext()) {
136             ScalarField f = (ScalarField) fs.next();
137             result += plus;
138             switch (f.getType().getTypeCode()) {
139             case PType.TYPECODE_BYTE:
140             case PType.TYPECODE_CHAR:
141             case PType.TYPECODE_SHORT:
142             case PType.TYPECODE_INT:
143                 result += f.getName();
144                 break;
145             case PType.TYPECODE_LONG:
146                 result += "((int) " + f.getName() + ")";
147                 break;
148             case PType.TYPECODE_OBJCHAR:
149             case PType.TYPECODE_OBJBYTE:
150             case PType.TYPECODE_OBJSHORT:
151             case PType.TYPECODE_OBJINT:
152             case PType.TYPECODE_OBJLONG:
153             case PType.TYPECODE_DATE:
154             case PType.TYPECODE_BIGDECIMAL:
155             case PType.TYPECODE_BIGINTEGER:
156             case PType.TYPECODE_STRING:
157                 result += "(" + f.getName() + " == null ? 0 :" + f.getName() + ".hashCode())";
158                 break;
159             case PType.TYPECODE_BYTEARRAY:
160             case PType.TYPECODE_CHARARRAY:
161             default:
162                 result += "0";
163             }
164         }
165         return result;
166     }
167
168     /**
169      * It compute the equality test for the equals method for pname.
170      */

171     public String JavaDoc getCnEquals(CompositeName cn) throws PException {
172         String JavaDoc result = "", and = " && ";
173         boolean addAnd = false;
174         Iterator JavaDoc fs = cn.getAllFields().iterator();
175         while (fs.hasNext()) {
176             ScalarField f = (ScalarField) fs.next();
177             if (addAnd) {
178                 result += and;
179             } else {
180                 addAnd = true;
181             }
182             switch (f.getType().getTypeCode()) {
183             case PType.TYPECODE_BYTE:
184             case PType.TYPECODE_CHAR:
185             case PType.TYPECODE_SHORT:
186             case PType.TYPECODE_INT:
187             case PType.TYPECODE_LONG:
188                 result += "(" + f.getName() + " == pn." + f.getName() + ")";
189                 break;
190             case PType.TYPECODE_OBJBYTE:
191             case PType.TYPECODE_OBJCHAR:
192             case PType.TYPECODE_OBJSHORT:
193             case PType.TYPECODE_OBJINT:
194             case PType.TYPECODE_OBJLONG:
195             case PType.TYPECODE_DATE:
196             case PType.TYPECODE_BIGDECIMAL:
197             case PType.TYPECODE_BIGINTEGER:
198             case PType.TYPECODE_STRING:
199                 result += "(" + f.getName() + " == null" +
200                         " ? (pn." + f.getName() + "== null)" +
201                         " : " + f.getName() + ".equals(pn." + f.getName() + "))";
202                 break;
203             case PType.TYPECODE_BYTEARRAY:
204             case PType.TYPECODE_CHARARRAY:
205                 result += "equal(" + f.getName() + ", pn." + f.getName() + ")";
206                 break;
207             default:
208                 throw new PExceptionTyping(
209                         "Unsupported PType: " + f.getType().getJormName());
210             }
211         }
212         return result;
213     }
214
215     /**
216      * It compute the test used to know if a pname is null.
217      */

218     public String JavaDoc getPNameNullTest(CompositeName cn) throws PException {
219         String JavaDoc result = "";
220         Iterator JavaDoc fs = cn.getAllFields().iterator();
221         while (fs.hasNext()) {
222             ScalarField f = (ScalarField) fs.next();
223             String JavaDoc nullvalue = getNullValue(cn, f);
224             if (helper.canBeNullValue(f.getType())) {
225                 //To avoid NullPointerException we must check if the field name
226
// is not null
227
result += "(" + f.getName() + " == null"
228                         + "? null == " + nullvalue
229                         + ": " + f.getName() + ".equals(" + nullvalue + "))";
230             } else {
231                 result += f.getName() + " == " + nullvalue;
232             }
233             if (fs.hasNext()) result += " && ";
234         }
235         return result;
236     }
237
238     /**
239      * It compute the constructor parameter used to instanciate a null pname.
240      */

241     public String JavaDoc getPNameNullValue(CompositeName cn, String JavaDoc pncVal) throws PException {
242         String JavaDoc result = "";
243         Iterator JavaDoc fs = cn.getAllFields().iterator();
244         while (fs.hasNext()) {
245             ScalarField f = (ScalarField) fs.next();
246             result = result + getNullValue(cn, f) + ", ";
247         }
248         return result + pncVal;
249     }
250
251     public String JavaDoc getNullValue(CompositeName cn, ScalarField f)
252             throws PException {
253         String JavaDoc nullValue = f.getNullValue();
254         if (nullValue == null) {
255             nullValue = (helper.canBeNullValue(f.getType())
256                     ? "NULLOVALUE"
257                     : "NULLVALUE");
258         } else {
259             if (f.getType().getTypeCode() == PType.TYPECODE_STRING)
260                 nullValue = "\"" + nullValue + "\"";
261             else if (f.getType().getTypeCode() == PType.TYPECODE_CHAR)
262                 nullValue = "\'" + nullValue + "\'";
263         }
264         return "(" + f.getType().getJavaName() + ")" + nullValue;
265     }
266 }
267
Popular Tags