KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > translator > idl2java > lib > CommonTranslator


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@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, Christophe Demarey.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.translator.idl2java.lib;
28
29 // Package dependencies.
30
import org.objectweb.openccm.ast.api.Declaration;
31 import org.objectweb.openccm.ast.api.DeclarationKind;
32 import org.objectweb.openccm.ast.api.TypeKind;
33
34 /**
35  * This class offers some IDL translations such as IDL names to Java names, etc.
36  *
37  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
38  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
39  *
40  * @version 0.1
41  */

42
43 public class CommonTranslator
44   implements org.objectweb.openccm.generator.translator.idl2java.api.CommonTranslator
45 {
46     // ==================================================================
47
//
48
// Internal state.
49
//
50
// ==================================================================
51

52     /**
53      * Table with declaration absolute names translated into java
54      */

55     protected java.util.Map JavaDoc abs_names_;
56
57     /**
58      * Java keywords table.
59      */

60     protected java.util.Set JavaDoc keywords_;
61
62
63     // ==================================================================
64
//
65
// Constructor.
66
//
67
// ==================================================================
68

69     /**
70      * The default constructor.
71      */

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

144     // ==================================================================
145
//
146
// Public methods for org.objectweb.openccm.generator.translator.idl2java.api.CommonTranslator.
147
//
148
// ==================================================================
149

150     /**
151      * Translate a TypeRef into its java mapping.
152      *
153      * @param typeref - The TypeRef to translate.
154      *
155      * @return The resulting mapping.
156      **/

157     public java.lang.String JavaDoc
158     toJava(org.objectweb.openccm.ast.api.TypeRef typeref)
159     {
160         TypeKind tk = typeref.getTypeKind();
161
162         if (tk == TypeKind.tk_null)
163             return "null";
164         else if (tk == TypeKind.tk_void)
165             return "void";
166         else if (tk == TypeKind.tk_short)
167             return "short";
168         else if (tk == TypeKind.tk_ushort)
169             return "short";
170         else if (tk == TypeKind.tk_long)
171             return "int";
172         else if (tk == TypeKind.tk_ulong)
173             return "int";
174         else if (tk == TypeKind.tk_longlong)
175             return "long";
176         else if (tk == TypeKind.tk_ulonglong)
177             return "long";
178         else if (tk == TypeKind.tk_float)
179             return "float";
180         else if (tk == TypeKind.tk_double)
181             return "double";
182         else if (tk == TypeKind.tk_longdouble)
183             return "double";
184         else if (tk == TypeKind.tk_boolean)
185             return "boolean";
186         else if (tk == TypeKind.tk_char)
187             return "char";
188         else if (tk == TypeKind.tk_wchar)
189             return "char";
190         else if (tk == TypeKind.tk_octet)
191             return "byte";
192         else if (tk == TypeKind.tk_any)
193             return "org.omg.CORBA.Any";
194         else if (tk == TypeKind.tk_Principal)
195             return "org.omg.CORBA.Principal";
196         else if (tk == TypeKind.tk_TypeCode)
197             return "org.omg.CORBA.TypeCode";
198         else if (tk == TypeKind.tk_objref)
199             return "org.omg.CORBA.Object";
200         else if (tk == TypeKind.tk_string)
201             return "String";
202         else if (tk == TypeKind.tk_wstring)
203             return "String";
204         else if (tk == TypeKind.tk_fixed)
205             return "java.Math.BigDecimal";
206
207         else if ( tk == TypeKind.tk_struct ||
208                   tk == TypeKind.tk_union ||
209                   tk == TypeKind.tk_enum ||
210                   tk == TypeKind.tk_exception ||
211                   tk == TypeKind.tk_value_box ||
212                   tk == TypeKind.tk_value ||
213                   tk == TypeKind.tk_native ||
214                   tk == TypeKind.tk_abstract_interface ||
215                   tk == TypeKind.tk_local_interface ||
216                   tk == TypeKind.tk_interface ||
217                   tk == TypeKind.tk_component ||
218                   tk == TypeKind.tk_home ||
219                   tk == TypeKind.tk_event )
220             {
221                 org.objectweb.openccm.ast.api.Declaration decl;
222                 decl = (org.objectweb.openccm.ast.api.Declaration)typeref;
223                 return getAbsoluteName(decl);
224             }
225
226         else if (tk == TypeKind.tk_alias)
227         {
228             org.objectweb.openccm.ast.api.AliasDecl alias;
229             alias = (org.objectweb.openccm.ast.api.AliasDecl)typeref;
230             return toJava(alias.getType());
231         }
232
233         else if ( tk == TypeKind.tk_sequence ||
234                   tk == TypeKind.tk_array )
235         {
236             org.objectweb.openccm.ast.api.TypeRefWithContent content;
237             content = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref;
238             java.lang.String JavaDoc str = toJava(content.getContentType());
239             return str+"[]";
240         }
241         else
242             return "";
243     }
244
245     /**
246      * Translate a parameter into its java mapping.
247      *
248      * @param typeref - The TypeRef to translate.
249      * @param mode - The parameter mode (in, out or inout).
250      *
251      * @return The resulting mapping.
252      **/

253     public java.lang.String JavaDoc
254     toJava(org.objectweb.openccm.ast.api.TypeRef type,
255            int mode)
256     {
257         if (mode==org.omg.CORBA.ParameterMode._PARAM_IN)
258             return toJava(type);
259
260         // mode is 'out' or 'inout'
261
TypeKind tk = type.getTypeKind();
262         if ( tk == TypeKind.tk_null ||
263              tk == TypeKind.tk_void )
264                 throw new Error JavaDoc("IDL null and void types could not be used for out or inout parameters!!!");
265
266         else if (tk == TypeKind.tk_short )
267             return "org.omg.CORBA.ShortHolder";
268         else if (tk == TypeKind.tk_ushort )
269             return "org.omg.CORBA.ShortHolder";
270         else if (tk == TypeKind.tk_long )
271             return "org.omg.CORBA.IntHolder";
272         else if (tk == TypeKind.tk_ulong )
273             return "org.omg.CORBA.IntHolder";
274         else if (tk == TypeKind.tk_longlong )
275             return "org.omg.CORBA.LongHolder";
276         else if (tk == TypeKind.tk_ulonglong )
277             return "org.omg.CORBA.LongHolder";
278         else if (tk == TypeKind.tk_float )
279             return "org.omg.CORBA.FloatHolder";
280         else if (tk == TypeKind.tk_double )
281             return "org.omg.CORBA.DoubleHolder";
282         else if (tk == TypeKind.tk_longdouble )
283             throw new Error JavaDoc("Java mapping for IDL longdouble is unspecified!!!");
284         else if (tk == TypeKind.tk_boolean )
285             return "org.omg.CORBA.BooleanHolder";
286         else if (tk == TypeKind.tk_char )
287             return "org.omg.CORBA.CharHolder";
288         else if (tk == TypeKind.tk_wchar )
289             return "org.omg.CORBA.CharHolder";
290         else if (tk == TypeKind.tk_octet )
291             return "org.omg.CORBA.ByteHolder";
292         else if (tk == TypeKind.tk_any )
293             return "org.omg.CORBA.AnyHolder";
294         else if (tk == TypeKind.tk_Principal )
295             return "org.omg.CORBA.PrincipalHolder";
296         else if (tk == TypeKind.tk_TypeCode )
297             return "org.omg.CORBA.TypeCodeHolder";
298         else if (tk == TypeKind.tk_objref )
299             return "org.omg.CORBA.ObjectHolder";
300         else if (tk == TypeKind.tk_string )
301             return "org.omg.CORBA.StringHolder";
302         else if (tk == TypeKind.tk_wstring )
303             return "org.omg.CORBA.StringHolder";
304         else if (tk == TypeKind.tk_fixed )
305             return "org.omg.CORBA.FixedHolder";
306         else if (tk == TypeKind.tk_alias )
307             {
308                 org.objectweb.openccm.ast.api.TypeRef ct;
309                 ct = ((org.objectweb.openccm.ast.api.AliasDecl)type).getType();
310                 if ((ct.getTypeKind()==TypeKind.tk_array) ||
311                     (ct.getTypeKind()==TypeKind.tk_sequence))
312                     return getAbsoluteName((org.objectweb.openccm.ast.api.AliasDecl)type) + "Holder";
313                 else
314                     return toJava(ct, mode);
315             }
316         else if ( tk == TypeKind.tk_sequence ||
317                   tk == TypeKind.tk_array )
318             throw new Error JavaDoc("Not implemented!!!");
319         else if ( tk == TypeKind.tk_struct ||
320                   tk == TypeKind.tk_union ||
321                   tk == TypeKind.tk_enum ||
322                   tk == TypeKind.tk_exception ||
323                   tk == TypeKind.tk_value_box ||
324                   tk == TypeKind.tk_value ||
325                   tk == TypeKind.tk_native ||
326                   tk == TypeKind.tk_abstract_interface ||
327                   tk == TypeKind.tk_local_interface ||
328                   tk == TypeKind.tk_interface ||
329                   tk == TypeKind.tk_component ||
330                   tk == TypeKind.tk_home ||
331                   tk == TypeKind.tk_event )
332             return getAbsoluteName((org.objectweb.openccm.ast.api.Declaration)type) + "Holder";
333         else
334             throw new Error JavaDoc("Other cases are not supported!!!");
335     }
336
337     /**
338      * Check if id is a java keyword.
339      *
340      * @param id - The string to check
341      *
342      * @return A valid string.
343      **/

344     public java.lang.String JavaDoc
345     checkKeywords(java.lang.String JavaDoc id)
346     {
347         if (keywords_.contains(id))
348            return "_"+id;
349
350         return id;
351     }
352
353     /**
354      * Get the java absolute name of a declaration
355      * ( i.e. package.decl_name ).
356      *
357      * @param decl - The declaration to translate
358      *
359      * @return The java absolute name.
360      **/

361     public java.lang.String JavaDoc
362     getAbsoluteName(Declaration decl)
363     {
364         if (decl==null)
365             return "";
366
367         // check if the absolute name has already been generated.
368
java.lang.String JavaDoc res = null;
369         res = (java.lang.String JavaDoc)abs_names_.get(decl.getId());
370         if (res!=null)
371             return res;
372
373         // translate the IDL prefix into a java prefix.
374
res = "";
375         java.lang.String JavaDoc prefix = decl.getPrefix();
376         int idx, idx2;
377         if (!prefix.equals(""))
378         {
379             idx = prefix.indexOf('.');
380             idx2 = 0;
381             while (idx!=-1)
382             {
383                 res = prefix.substring(idx2, idx+1) + res;
384                 idx2 = idx+1;
385                 idx = prefix.indexOf('.', idx2);
386             }
387             res = prefix.substring(idx2) + '.' + res;
388         }
389
390         // translate the IDL scoped name into a Java absolute name
391
java.lang.String JavaDoc abs_name = decl.getAbsoluteName();
392         idx = abs_name.indexOf(':', 2);
393         idx2 = 2;
394         while (idx!=-1)
395         {
396             res = res + abs_name.substring(idx2, idx) + '.';
397             idx2 = idx+2;
398             idx = abs_name.indexOf(':', idx2);
399         }
400         res = res + abs_name.substring(idx2);
401
402         // check special cases ie nested types within interfaces
403
org.objectweb.openccm.ast.api.Scope parent = decl.getParent();
404         long parent_kind = parent.getDeclKind();
405         long decl_kind = decl.getDeclKind();
406         if (decl_kind==DeclarationKind.dk_constant)
407         {
408             if ((parent_kind!=DeclarationKind.dk_interface) &&
409                 (parent_kind!=DeclarationKind.dk_abstract_interface) &&
410                 (parent_kind!=DeclarationKind.dk_local_interface))
411                 res = res + ".value";
412         }
413         else if ((decl_kind==DeclarationKind.dk_exception) ||
414                  (decl_kind==DeclarationKind.dk_alias) ||
415                  (decl_kind==DeclarationKind.dk_struct) ||
416                  (decl_kind==DeclarationKind.dk_union) ||
417                  (decl_kind==DeclarationKind.dk_enum) ||
418                  (decl_kind==DeclarationKind.dk_native))
419         {
420             if ((parent_kind==DeclarationKind.dk_interface) ||
421                 (parent_kind==DeclarationKind.dk_abstract_interface) ||
422                 (parent_kind==DeclarationKind.dk_local_interface) ||
423                 (parent_kind==DeclarationKind.dk_home) ||
424                 (parent_kind==DeclarationKind.dk_value) ||
425                 (parent_kind==DeclarationKind.dk_event) ||
426                 (parent_kind==DeclarationKind.dk_component))
427             {
428                 idx = res.lastIndexOf('.');
429                 res = res.substring(0, idx) + "Package" + res.substring(idx);
430             }
431         }
432
433         // store the computed absolute name
434
abs_names_.put(decl.getId(), res);
435         return res;
436     }
437
438     /**
439      * Get the package of a declaration.
440      *
441      * @param decl - The declaration to translate
442      *
443      * @return The java package name.
444      **/

445     public java.lang.String JavaDoc
446     getPackage(Declaration decl)
447     {
448         java.lang.String JavaDoc abs_name = getAbsoluteName(decl);
449         int idx = abs_name.lastIndexOf('.');
450         if (idx==-1)
451             return "";
452
453         return abs_name.substring(0, idx);
454     }
455
456     /**
457      * Get the java path of a declaration.
458      * This don't include the declaration's java name.
459      * The package name is translated to a path.
460      *
461      * @param decl - The declaration to translate
462      *
463      * @return The java package name.
464      **/

465     public String JavaDoc
466     getAsDirectory(String JavaDoc package_name)
467     {
468         if (package_name == null)
469             return "./";
470
471         return package_name.replace('.', java.io.File.separatorChar);
472     }
473
474     /**
475      * Get the java path of a declaration.
476      * This don't include the declaration's java name.
477      * The package name is translated to a path.
478      *
479      * @param decl - The declaration to translate
480      *
481      * @return The java package name.
482      **/

483     public java.lang.String JavaDoc
484     getAsDirectory(Declaration decl)
485     {
486         java.lang.String JavaDoc abs_name = getAbsoluteName(decl);
487         int idx = abs_name.indexOf('.');
488         if (idx==-1)
489             return "./";
490
491         java.lang.String JavaDoc dir = "";
492         int idx2 = 0;
493         while (idx!=-1)
494         {
495             dir = dir + abs_name.substring(idx2, idx) + '/';
496             idx2 = idx+1;
497             idx = abs_name.indexOf('.', idx2);
498         }
499
500         return dir;
501     }
502
503     /**
504      * Get the null value for an IDL3 TypeRef
505      *
506      * @param typeref - The TypeRef to translate.
507      *
508      * @return The null value for this typeref.
509      **/

510     public java.lang.String JavaDoc
511     nullValue(org.objectweb.openccm.ast.api.TypeRef typeref)
512     {
513         TypeKind tk = typeref.getTypeKind();
514
515         if ( (tk == TypeKind.tk_null) ||
516              (tk == TypeKind.tk_void) )
517             return "NO_VALUE";
518         else if ( (tk == TypeKind.tk_short) ||
519                   (tk == TypeKind.tk_ushort) )
520             return "0";
521         else if ( (tk == TypeKind.tk_long) ||
522                   (tk == TypeKind.tk_ulong) )
523             return "0";
524         else if ( (tk == TypeKind.tk_longlong) ||
525                   (tk == TypeKind.tk_ulonglong) )
526             return "0";
527         else if ( (tk == TypeKind.tk_char) ||
528                   (tk == TypeKind.tk_wchar) )
529             return "\'\0\'";
530         else if ( (tk == TypeKind.tk_string) ||
531                   (tk == TypeKind.tk_wstring) )
532             return "\"\"";
533         else if (tk == TypeKind.tk_float)
534             return "0f";
535         else if (tk == TypeKind.tk_double)
536             return "0.0";
537         else if (tk == TypeKind.tk_boolean)
538             return "true";
539         else if (tk == TypeKind.tk_octet)
540             return "(byte)0";
541         // objects case
542
else
543             return "null";
544     }
545
546     // ==================================================================
547
//
548
// Public methods.
549
//
550
// ==================================================================
551

552 }
553
Popular Tags