KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Base class for Java printers.
33  * It defines operations that have a general purpose.
34  *
35  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
36  *
37  * @version 0.3
38  */

39
40 public class JavaPrinterBase
41        extends Printer
42 {
43     // ==================================================================
44
//
45
// Internal state.
46
//
47
// ==================================================================
48

49     /**
50      **
51      **/

52     protected JavaTranslator translator_;
53
54     // ==================================================================
55
//
56
// Constructor.
57
//
58
// ==================================================================
59

60     /**
61      ** The default constructor.
62      **/

63     public
64     JavaPrinterBase(Generator gen,
65                     char call_char,
66                     char comment_char,
67                     java.lang.String JavaDoc indent)
68     throws fr.lifl.cim.jidlscript.lang.JISError
69     {
70         super(gen, call_char, comment_char, indent);
71
72         // init internal state.
73
translator_ = new JavaTranslator();
74     }
75
76     // ==================================================================
77
//
78
// Internal methods.
79
//
80
// ==================================================================
81

82     // ==================================================================
83
//
84
// Public methods that have a general purpose.
85
//
86
// ==================================================================
87

88     /**
89      **
90      **/

91     public java.lang.String JavaDoc
92     open(org.objectweb.ccm.IDL3.Declaration obj,
93          java.lang.String JavaDoc ext)
94     {
95         super.setDirectory(getTranslator().getAsDirectory(obj));
96         super.open(name(obj)+ext);
97         return "";
98     }
99
100     /**
101      **
102      **/

103     public java.lang.String JavaDoc
104     close(org.objectweb.ccm.IDL3.Declaration obj,
105           java.lang.String JavaDoc str)
106     {
107         System.out.println("File \'"+getTranslator().getAsDirectory(obj)+name(obj)+str+"\' has been generated");
108         super.close();
109         return "";
110     }
111
112     /**
113      **
114      **/

115     public java.lang.String JavaDoc
116     visitContained(org.objectweb.ccm.IDL3.Declaration contained,
117                    java.lang.String JavaDoc extension)
118     {
119         java.lang.String JavaDoc id =
120             org.objectweb.ccm.IDL3.DeclarationKind.toString(contained.getDeclKind()).toUpperCase()+
121             extension;
122
123         java.lang.String JavaDoc[] aliases = { "obj" };
124         java.lang.Object JavaDoc[] objs = { contained };
125         super.map(id, aliases, objs);
126
127         return "";
128     }
129
130     /**
131      **
132      **/

133     public java.lang.String JavaDoc
134     visitContainedWithMapping(org.objectweb.ccm.IDL3.Declaration obj,
135                               org.objectweb.ccm.IDL3.Declaration mapobj,
136                               java.lang.String JavaDoc extension)
137     {
138         java.lang.String JavaDoc id =
139             org.objectweb.ccm.IDL3.DeclarationKind.toString(mapobj.getDeclKind()).toUpperCase()+
140             extension;
141
142         java.lang.String JavaDoc[] aliases = { "obj" , "mapobj" };
143         java.lang.Object JavaDoc[] objs = { obj, mapobj };
144         super.map(id, aliases, objs);
145
146         return "";
147     }
148
149     /**
150      **
151      **/

152     public java.lang.String JavaDoc
153     visit(org.objectweb.ccm.IDL3.Declaration obj)
154     {
155         if (obj.getDeclKind()==org.objectweb.ccm.IDL3.DeclarationKind._dk_module)
156             return visitContained(obj, "");
157
158         java.lang.String JavaDoc msg =
159             "error in class org.objectweb.ccm.visitorIDL3.JavaPrinterBase\n"+
160             " operation : visit\n"+
161             " message : root visited object must be a module";
162         System.err.println(msg);
163         return "";
164     }
165
166     /**
167      **
168      **/

169     public java.lang.String JavaDoc
170     name(org.objectweb.ccm.IDL3.Declaration decl)
171     {
172         return getTranslator().checkKeywords(decl.getName());
173     }
174
175     /**
176      **
177      **/

178     public java.lang.String JavaDoc
179     checkName(java.lang.String JavaDoc idl_name)
180     {
181         return getTranslator().checkKeywords(idl_name);
182     }
183
184     /**
185      **
186      **/

187     public java.lang.String JavaDoc
188     type(org.objectweb.ccm.IDL3.TypeRef type)
189     {
190         return getTranslator().toJava(type);
191     }
192
193     /**
194      **
195      **/

196     public java.lang.String JavaDoc
197     isVoid(org.objectweb.ccm.IDL3.TypeRef type)
198     {
199         if (type.getTypeKind()!=org.objectweb.ccm.IDL3.TypeKind._tk_void)
200             return "return ";
201
202         return "";
203     }
204
205     /**
206      **
207      **/

208     public java.lang.String JavaDoc
209     params(org.objectweb.ccm.IDL3.OperationDecl op)
210     {
211         return params(op.getParameters(), op.getContexts());
212     }
213
214     /**
215      **
216      **/

217     public java.lang.String JavaDoc
218     params(org.objectweb.ccm.IDL3.Initializer init)
219     {
220         return params(init.getParameters(), null);
221     }
222
223     /**
224      **
225      **/

226     public java.lang.String JavaDoc
227     params(org.objectweb.ccm.IDL3.ParameterList list,
228            java.lang.String JavaDoc[] ctxs)
229     {
230         java.lang.String JavaDoc[] names = list.getParamNames();
231         org.objectweb.ccm.IDL3.TypeRef[] types = list.getParamTypes();
232         int[] modes = list.getParamModes();
233         java.lang.String JavaDoc res = "";
234
235         for (int i=0;i<names.length;i++)
236             res = res + getTranslator().toJava(types[i], modes[i]) + " " + names[i] + ", ";
237
238         if ((ctxs!=null) && (ctxs.length!=0))
239             res = res + "org.omg.CORBA.Context ctx, ";
240         if (!res.equals(""))
241             res = res.substring(0, res.length()-2);
242
243         return res;
244     }
245
246     /**
247      **
248      **/

249     public java.lang.String JavaDoc
250     paramNames(org.objectweb.ccm.IDL3.OperationDecl op)
251     {
252         return paramNames(op.getParameters(), op.getContexts());
253     }
254
255     /**
256      **
257      **/

258     public java.lang.String JavaDoc
259     paramNames(org.objectweb.ccm.IDL3.Initializer init)
260     {
261         return paramNames(init.getParameters(), null);
262     }
263
264     /**
265      **
266      **/

267     public java.lang.String JavaDoc
268     paramNames(org.objectweb.ccm.IDL3.ParameterList list,
269                java.lang.String JavaDoc[] ctxs)
270     {
271         java.lang.String JavaDoc[] names = list.getParamNames();
272         java.lang.String JavaDoc str = "";
273
274         for (int i=0;i<names.length;i++)
275             str = str + names[i] + ", ";
276
277         if ((ctxs!=null) && (ctxs.length!=0))
278             str = str + "ctx, ";
279         if (!str.equals(""))
280             str = str.substring(0, str.length()-2);
281
282         return str;
283     }
284
285     /**
286      **
287      **/

288     public java.lang.String JavaDoc
289     exceptions(org.objectweb.ccm.IDL3.ExceptionDecl[] excs)
290     {
291         java.lang.String JavaDoc res = "";
292         for (int i=0;i<excs.length;i++)
293             res = res + getTranslator().getAbsoluteName(excs[i]) + ", ";
294
295         if (!res.equals(""))
296             return "throws " + res.substring(0, res.length()-2) + "\\n";
297
298         return "";
299     }
300
301     /**
302      **
303      **/

304     public java.lang.String JavaDoc
305     nullValue(org.objectweb.ccm.IDL3.TypeRef type)
306     {
307         switch(type.getTypeKind())
308         {
309         case org.objectweb.ccm.IDL3.TypeKind._tk_null :
310             return "????";
311         case org.objectweb.ccm.IDL3.TypeKind._tk_void :
312             return "????";
313         case org.objectweb.ccm.IDL3.TypeKind._tk_short :
314         case org.objectweb.ccm.IDL3.TypeKind._tk_ushort :
315             return "0";
316         case org.objectweb.ccm.IDL3.TypeKind._tk_long :
317         case org.objectweb.ccm.IDL3.TypeKind._tk_ulong :
318             return "0";
319         case org.objectweb.ccm.IDL3.TypeKind._tk_longlong :
320         case org.objectweb.ccm.IDL3.TypeKind._tk_ulonglong :
321             return "0";
322         case org.objectweb.ccm.IDL3.TypeKind._tk_char :
323         case org.objectweb.ccm.IDL3.TypeKind._tk_wchar :
324             return "\'\0\'";
325         case org.objectweb.ccm.IDL3.TypeKind._tk_string :
326         case org.objectweb.ccm.IDL3.TypeKind._tk_wstring :
327             return "\"\"";
328         case org.objectweb.ccm.IDL3.TypeKind._tk_float :
329             return "0f";
330         case org.objectweb.ccm.IDL3.TypeKind._tk_double :
331             return "0.0";
332         case org.objectweb.ccm.IDL3.TypeKind._tk_boolean :
333             return "true";
334         case org.objectweb.ccm.IDL3.TypeKind._tk_octet :
335             return "(byte)0";
336             // objects case
337
default :
338             return "null";
339         }
340     }
341
342     /**
343      **
344      **/

345     public org.objectweb.ccm.IDL3.TypeRef
346     getType(org.objectweb.ccm.IDL3.Declaration obj)
347     {
348         switch(obj.getDeclKind())
349         {
350             case org.objectweb.ccm.IDL3.DeclarationKind._dk_alias :
351                 return ((org.objectweb.ccm.IDL3.AliasDecl)obj).getType();
352             case org.objectweb.ccm.IDL3.DeclarationKind._dk_attribute :
353                 return ((org.objectweb.ccm.IDL3.AttributeDecl)obj).getType();
354             case org.objectweb.ccm.IDL3.DeclarationKind._dk_constant :
355                 return ((org.objectweb.ccm.IDL3.ConstantDecl)obj).getType();
356             case org.objectweb.ccm.IDL3.DeclarationKind._dk_emits :
357                 return ((org.objectweb.ccm.IDL3.EmitsDecl)obj).getType();
358             case org.objectweb.ccm.IDL3.DeclarationKind._dk_publishes :
359                 return ((org.objectweb.ccm.IDL3.PublishesDecl)obj).getType();
360             case org.objectweb.ccm.IDL3.DeclarationKind._dk_consumes :
361                 return ((org.objectweb.ccm.IDL3.ConsumesDecl)obj).getType();
362             case org.objectweb.ccm.IDL3.DeclarationKind._dk_operation :
363                 return ((org.objectweb.ccm.IDL3.OperationDecl)obj).getType();
364             case org.objectweb.ccm.IDL3.DeclarationKind._dk_provides :
365                 return ((org.objectweb.ccm.IDL3.ProvidesDecl)obj).getType();
366             case org.objectweb.ccm.IDL3.DeclarationKind._dk_union :
367                 return ((org.objectweb.ccm.IDL3.UnionDecl)obj).getType();
368             case org.objectweb.ccm.IDL3.DeclarationKind._dk_uses :
369                 return ((org.objectweb.ccm.IDL3.UsesDecl)obj).getType();
370             case org.objectweb.ccm.IDL3.DeclarationKind._dk_value_box :
371                 return ((org.objectweb.ccm.IDL3.ValueBoxDecl)obj).getType();
372             case org.objectweb.ccm.IDL3.DeclarationKind._dk_value_member :
373                 return ((org.objectweb.ccm.IDL3.ValueMemberDecl)obj).getType();
374             default :
375                 return null;
376         }
377     }
378
379     /**
380      **
381      **/

382     public JavaTranslator
383     getTranslator()
384     {
385         return translator_;
386     }
387 }
388
Popular Tags