KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > visitorIDL3 > java > JavaTranslator


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2002 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Mathieu Vadet.
23 Contributor(s): .
24
25 ====================================================================*/

26
27 package org.objectweb.ccm.visitorIDL3.java;
28
29 import org.objectweb.ccm.visitorIDL3.common.*;
30
31 /**
32  * .
33  *
34  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
35  *
36  * @version 0.3
37  */

38
39 public class JavaTranslator
40 {
41     // ==================================================================
42
//
43
// Internal state.
44
//
45
// ==================================================================
46

47     /**
48      **
49      **/

50     protected org.objectweb.ccm.util.Table abs_names_;
51
52     /**
53      ** Java keywords table.
54      **/

55      protected org.objectweb.ccm.util.Table keywords_;
56     
57
58     // ==================================================================
59
//
60
// Constructor.
61
//
62
// ==================================================================
63

64     /**
65      ** The default constructor.
66      **/

67     public
68     JavaTranslator()
69     {
70         abs_names_ = new org.objectweb.ccm.util.Table();
71         keywords_ = new org.objectweb.ccm.util.Table();
72         keywords_.put("public", new Object JavaDoc());
73         keywords_.put("protected", new Object JavaDoc());
74         keywords_.put("private", new Object JavaDoc());
75         keywords_.put("class", new Object JavaDoc());
76         keywords_.put("interface", new Object JavaDoc());
77         keywords_.put("abstract", new Object JavaDoc());
78         keywords_.put("boolean", new Object JavaDoc());
79         keywords_.put("break", new Object JavaDoc());
80         keywords_.put("byte", new Object JavaDoc());
81         keywords_.put("case", new Object JavaDoc());
82         keywords_.put("catch", new Object JavaDoc());
83         keywords_.put("char", new Object JavaDoc());
84         keywords_.put("const", new Object JavaDoc());
85         keywords_.put("continue", new Object JavaDoc());
86         keywords_.put("default", new Object JavaDoc());
87         keywords_.put("do", new Object JavaDoc());
88         keywords_.put("double", new Object JavaDoc());
89         keywords_.put("else", new Object JavaDoc());
90         keywords_.put("extends", new Object JavaDoc());
91         keywords_.put("final", new Object JavaDoc());
92         keywords_.put("finally", new Object JavaDoc());
93         keywords_.put("float", new Object JavaDoc());
94         keywords_.put("for", new Object JavaDoc());
95         keywords_.put("goto", new Object JavaDoc());
96         keywords_.put("if", new Object JavaDoc());
97         keywords_.put("implements", new Object JavaDoc());
98         keywords_.put("import", new Object JavaDoc());
99         keywords_.put("instanceof", new Object JavaDoc());
100         keywords_.put("int", new Object JavaDoc());
101         keywords_.put("long", new Object JavaDoc());
102         keywords_.put("native", new Object JavaDoc());
103         keywords_.put("new", new Object JavaDoc());
104         keywords_.put("package", new Object JavaDoc());
105         keywords_.put("return", new Object JavaDoc());
106         keywords_.put("short", new Object JavaDoc());
107         keywords_.put("static", new Object JavaDoc());
108         keywords_.put("super", new Object JavaDoc());
109         keywords_.put("switch", new Object JavaDoc());
110         keywords_.put("synchronized", new Object JavaDoc());
111         keywords_.put("this", new Object JavaDoc());
112         keywords_.put("throw", new Object JavaDoc());
113         keywords_.put("throws", new Object JavaDoc());
114         keywords_.put("transient", new Object JavaDoc());
115         keywords_.put("try", new Object JavaDoc());
116         keywords_.put("void", new Object JavaDoc());
117         keywords_.put("volatile", new Object JavaDoc());
118         keywords_.put("while", new Object JavaDoc());
119         keywords_.put("true", new Object JavaDoc());
120         keywords_.put("false", new Object JavaDoc());
121         keywords_.put("null", new Object JavaDoc());
122         keywords_.put("clone", new Object JavaDoc());
123         keywords_.put("equals", new Object JavaDoc());
124         keywords_.put("finalize", new Object JavaDoc());
125         keywords_.put("getClass", new Object JavaDoc());
126         keywords_.put("hashCode", new Object JavaDoc());
127         keywords_.put("notify", new Object JavaDoc());
128         keywords_.put("notifyAll", new Object JavaDoc());
129         keywords_.put("toString", new Object JavaDoc());
130         keywords_.put("wait", new Object JavaDoc());
131     }
132
133     // ==================================================================
134
//
135
// Internal methods.
136
//
137
// ==================================================================
138

139     // ==================================================================
140
//
141
// Public methods.
142
//
143
// ==================================================================
144

145     /**
146      **
147      **/

148     public java.lang.String JavaDoc
149     toJava(org.objectweb.ccm.IDL3.TypeRef typeref)
150     {
151         switch(typeref.getTypeKind())
152         {
153             case org.objectweb.ccm.IDL3.TypeKind._tk_null :
154                 return "null";
155             case org.objectweb.ccm.IDL3.TypeKind._tk_void :
156                 return "void";
157             case org.objectweb.ccm.IDL3.TypeKind._tk_short :
158                 return "short";
159             case org.objectweb.ccm.IDL3.TypeKind._tk_ushort :
160                 return "short";
161             case org.objectweb.ccm.IDL3.TypeKind._tk_long :
162                 return "int";
163             case org.objectweb.ccm.IDL3.TypeKind._tk_ulong :
164                 return "int";
165             case org.objectweb.ccm.IDL3.TypeKind._tk_longlong :
166                 return "long";
167             case org.objectweb.ccm.IDL3.TypeKind._tk_ulonglong :
168                 return "long";
169             case org.objectweb.ccm.IDL3.TypeKind._tk_float :
170                 return "float";
171             case org.objectweb.ccm.IDL3.TypeKind._tk_double :
172                 return "double";
173             case org.objectweb.ccm.IDL3.TypeKind._tk_longdouble :
174                 return "double";
175             case org.objectweb.ccm.IDL3.TypeKind._tk_boolean :
176                 return "boolean";
177             case org.objectweb.ccm.IDL3.TypeKind._tk_char :
178                 return "char";
179             case org.objectweb.ccm.IDL3.TypeKind._tk_wchar :
180                 return "char";
181             case org.objectweb.ccm.IDL3.TypeKind._tk_octet :
182                 return "byte";
183             case org.objectweb.ccm.IDL3.TypeKind._tk_any :
184                 return "org.omg.CORBA.Any";
185             case org.objectweb.ccm.IDL3.TypeKind._tk_Principal :
186                 return "org.omg.CORBA.Principal";
187             case org.objectweb.ccm.IDL3.TypeKind._tk_TypeCode :
188                 return "org.omg.CORBA.TypeCode";
189             case org.objectweb.ccm.IDL3.TypeKind._tk_objref :
190                 return "org.omg.CORBA.Object";
191             case org.objectweb.ccm.IDL3.TypeKind._tk_string :
192                 return "java.lang.String";
193             case org.objectweb.ccm.IDL3.TypeKind._tk_wstring :
194                 return "java.lang.String";
195             case org.objectweb.ccm.IDL3.TypeKind._tk_fixed :
196                 return "java.Math.BigDecimal";
197
198             case org.objectweb.ccm.IDL3.TypeKind._tk_struct :
199             case org.objectweb.ccm.IDL3.TypeKind._tk_union :
200             case org.objectweb.ccm.IDL3.TypeKind._tk_enum :
201             case org.objectweb.ccm.IDL3.TypeKind._tk_exception :
202             case org.objectweb.ccm.IDL3.TypeKind._tk_value_box :
203             case org.objectweb.ccm.IDL3.TypeKind._tk_value :
204             case org.objectweb.ccm.IDL3.TypeKind._tk_native :
205             case org.objectweb.ccm.IDL3.TypeKind._tk_abstract_interface :
206             case org.objectweb.ccm.IDL3.TypeKind._tk_local_interface :
207             case org.objectweb.ccm.IDL3.TypeKind._tk_interface :
208             case org.objectweb.ccm.IDL3.TypeKind._tk_component :
209             case org.objectweb.ccm.IDL3.TypeKind._tk_home :
210             case org.objectweb.ccm.IDL3.TypeKind._tk_event :
211                 org.objectweb.ccm.IDL3.Declaration decl = (org.objectweb.ccm.IDL3.Declaration)typeref;
212                 return getAbsoluteName(decl);
213
214             case org.objectweb.ccm.IDL3.TypeKind._tk_alias :
215                 org.objectweb.ccm.IDL3.AliasDecl alias = (org.objectweb.ccm.IDL3.AliasDecl)typeref;
216                 return toJava(alias.getType());
217
218             case org.objectweb.ccm.IDL3.TypeKind._tk_sequence :
219             case org.objectweb.ccm.IDL3.TypeKind._tk_array :
220                 org.objectweb.ccm.IDL3.TypeRefImpl impl= (org.objectweb.ccm.IDL3.TypeRefImpl)typeref;
221                 java.lang.String JavaDoc str = toJava(impl.getContentType());
222                 return str+"[]";
223             default :
224                 return "";
225         }
226     }
227
228     /**
229      **
230      **/

231     public java.lang.String JavaDoc
232     toJava(org.objectweb.ccm.IDL3.TypeRef type,
233            int mode)
234     {
235         if (mode==org.objectweb.ccm.IDL3.OperationDecl.PARAM_IN)
236             return toJava(type);
237
238         switch(type.getTypeKind())
239         {
240             case org.objectweb.ccm.IDL3.TypeKind._tk_null :
241             case org.objectweb.ccm.IDL3.TypeKind._tk_void :
242                 throw new Error JavaDoc("IDL null and void types could not be used for out or inout parameters!!!");
243
244             case org.objectweb.ccm.IDL3.TypeKind._tk_short :
245                 return "org.omg.CORBA.ShortHolder";
246
247             case org.objectweb.ccm.IDL3.TypeKind._tk_ushort :
248                 return "org.omg.CORBA.ShortHolder";
249
250             case org.objectweb.ccm.IDL3.TypeKind._tk_long :
251                 return "org.omg.CORBA.IntHolder";
252
253             case org.objectweb.ccm.IDL3.TypeKind._tk_ulong :
254                 return "org.omg.CORBA.IntHolder";
255
256             case org.objectweb.ccm.IDL3.TypeKind._tk_longlong :
257                 return "org.omg.CORBA.LongHolder";
258
259             case org.objectweb.ccm.IDL3.TypeKind._tk_ulonglong :
260                 return "org.omg.CORBA.LongHolder";
261
262             case org.objectweb.ccm.IDL3.TypeKind._tk_float :
263                 return "org.omg.CORBA.FloatHolder";
264
265             case org.objectweb.ccm.IDL3.TypeKind._tk_double :
266                 return "org.omg.CORBA.DoubleHolder";
267
268             case org.objectweb.ccm.IDL3.TypeKind._tk_longdouble :
269                 throw new Error JavaDoc("Java mapping for IDL longdouble is unspecified!!!");
270
271             case org.objectweb.ccm.IDL3.TypeKind._tk_boolean :
272                 return "org.omg.CORBA.BooleanHolder";
273
274             case org.objectweb.ccm.IDL3.TypeKind._tk_char :
275                 return "org.omg.CORBA.CharHolder";
276
277             case org.objectweb.ccm.IDL3.TypeKind._tk_wchar :
278                 return "org.omg.CORBA.CharHolder";
279
280             case org.objectweb.ccm.IDL3.TypeKind._tk_octet :
281                 return "org.omg.CORBA.ByteHolder";
282
283             case org.objectweb.ccm.IDL3.TypeKind._tk_any :
284                 return "org.omg.CORBA.AnyHolder";
285
286             case org.objectweb.ccm.IDL3.TypeKind._tk_Principal :
287                 return "org.omg.CORBA.PrincipalHolder";
288
289             case org.objectweb.ccm.IDL3.TypeKind._tk_TypeCode :
290                 return "org.omg.CORBA.TypeCodeHolder";
291
292             case org.objectweb.ccm.IDL3.TypeKind._tk_objref :
293                 return "org.omg.CORBA.ObjectHolder";
294
295             case org.objectweb.ccm.IDL3.TypeKind._tk_string :
296                 return "org.omg.CORBA.StringHolder";
297
298             case org.objectweb.ccm.IDL3.TypeKind._tk_wstring :
299                 return "org.omg.CORBA.StringHolder";
300
301             case org.objectweb.ccm.IDL3.TypeKind._tk_fixed :
302                 return "org.omg.CORBA.FixedHolder";
303
304             case org.objectweb.ccm.IDL3.TypeKind._tk_alias :
305         {
306                 org.objectweb.ccm.IDL3.TypeRef ct =
307                     ((org.objectweb.ccm.IDL3.AliasDecl)type).getType();
308                 if ((ct.getTypeKind()==org.objectweb.ccm.IDL3.TypeKind._tk_array) ||
309                     (ct.getTypeKind()==org.objectweb.ccm.IDL3.TypeKind._tk_sequence))
310                     return getAbsoluteName((org.objectweb.ccm.IDL3.AliasDecl)type) + "Holder";
311                 else
312                     return toJava(ct, mode);
313             }
314
315             case org.objectweb.ccm.IDL3.TypeKind._tk_sequence :
316             case org.objectweb.ccm.IDL3.TypeKind._tk_array :
317                 throw new Error JavaDoc("Not implemented!!!");
318
319             case org.objectweb.ccm.IDL3.TypeKind._tk_struct :
320             case org.objectweb.ccm.IDL3.TypeKind._tk_union :
321             case org.objectweb.ccm.IDL3.TypeKind._tk_enum :
322             case org.objectweb.ccm.IDL3.TypeKind._tk_exception :
323             case org.objectweb.ccm.IDL3.TypeKind._tk_value_box :
324             case org.objectweb.ccm.IDL3.TypeKind._tk_value :
325             case org.objectweb.ccm.IDL3.TypeKind._tk_native :
326             case org.objectweb.ccm.IDL3.TypeKind._tk_abstract_interface :
327             case org.objectweb.ccm.IDL3.TypeKind._tk_local_interface :
328             case org.objectweb.ccm.IDL3.TypeKind._tk_interface :
329             case org.objectweb.ccm.IDL3.TypeKind._tk_component :
330             case org.objectweb.ccm.IDL3.TypeKind._tk_home :
331             case org.objectweb.ccm.IDL3.TypeKind._tk_event :
332                 return getAbsoluteName((org.objectweb.ccm.IDL3.Declaration)type) + "Holder";
333
334             default :
335                 throw new Error JavaDoc("Other cases are not supported!!!");
336         }
337     }
338
339     /**
340      **
341      **/

342     public java.lang.String JavaDoc
343     checkKeywords(java.lang.String JavaDoc id)
344     {
345
346         if (keywords_.containsKey(id))
347            return "_"+id;
348
349         return id;
350     }
351
352     /**
353      **
354      **/

355     public java.lang.String JavaDoc
356     getAbsoluteName(org.objectweb.ccm.IDL3.Declaration decl)
357     {
358         if (decl==null)
359             return "";
360
361         // check if the absolute name has already been generated.
362
java.lang.String JavaDoc res = null;
363         res = (java.lang.String JavaDoc)abs_names_.get(decl.getId());
364         if (res!=null)
365             return res;
366
367         // translate the IDL prefix into a java prefix.
368
res = "";
369         java.lang.String JavaDoc prefix = decl.getPrefix();
370         int idx, idx2;
371         if (!prefix.equals(""))
372         {
373             idx = prefix.indexOf('.');
374             idx2 = 0;
375             while (idx!=-1)
376             {
377                 res = prefix.substring(idx2, idx+1) + res;
378                 idx2 = idx+1;
379                 idx = prefix.indexOf('.', idx2);
380             }
381             res = prefix.substring(idx2) + '.' + res;
382         }
383
384         // translate the IDL scoped name into a Java absolute name
385
java.lang.String JavaDoc abs_name = decl.getAbsoluteName();
386         idx = abs_name.indexOf(':', 2);
387         idx2 = 2;
388         while (idx!=-1)
389         {
390             res = res + abs_name.substring(idx2, idx) + '.';
391             idx2 = idx+2;
392             idx = abs_name.indexOf(':', idx2);
393         }
394         res = res + abs_name.substring(idx2);
395
396         // check special cases ie nested types within interfaces
397
org.objectweb.ccm.IDL3.Scope parent = decl.getParent();
398         int parent_kind = parent.getDeclKind();
399         int decl_kind = decl.getDeclKind();
400         if (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_constant)
401         {
402             if ((parent_kind!=org.objectweb.ccm.IDL3.DeclarationKind._dk_interface) &&
403                 (parent_kind!=org.objectweb.ccm.IDL3.DeclarationKind._dk_abstract_interface) &&
404                 (parent_kind!=org.objectweb.ccm.IDL3.DeclarationKind._dk_local_interface))
405                 res = res + ".value";
406         }
407         else if ((decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_exception) ||
408                  (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_alias) ||
409                  (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_struct) ||
410                  (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_union) ||
411                  (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_enum) ||
412                  (decl_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_native))
413         {
414             if ((parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_interface) ||
415                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_abstract_interface) ||
416                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_local_interface) ||
417                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_home) ||
418                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_value) ||
419                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_event) ||
420                 (parent_kind==org.objectweb.ccm.IDL3.DeclarationKind._dk_component))
421             {
422                 idx = res.lastIndexOf('.');
423                 res = res.substring(0, idx) + "Package" + res.substring(idx);
424             }
425         }
426
427         // store the computed absolute name
428
abs_names_.put(decl.getId(), res);
429         return res;
430     }
431
432     /**
433      **
434      **/

435     public java.lang.String JavaDoc
436     getPackage(org.objectweb.ccm.IDL3.Declaration decl)
437     {
438         java.lang.String JavaDoc abs_name = getAbsoluteName(decl);
439         int idx = abs_name.lastIndexOf('.');
440         if (idx==-1)
441             return "";
442
443         return abs_name.substring(0, idx);
444     }
445
446     /**
447      **
448      **/

449     public java.lang.String JavaDoc
450     getAsDirectory(org.objectweb.ccm.IDL3.Declaration decl)
451     {
452         java.lang.String JavaDoc abs_name = getAbsoluteName(decl);
453         int idx = abs_name.indexOf('.');
454         if (idx==-1)
455             return "./";
456
457         java.lang.String JavaDoc dir = "";
458         int idx2 = 0;
459         while (idx!=-1)
460         {
461             dir = dir + abs_name.substring(idx2, idx) + '/';
462             idx2 = idx+1;
463             idx = abs_name.indexOf('.', idx2);
464         }
465
466         return dir;
467     }
468 }
469
Popular Tags