KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > translator > ast2idl > lib > AST_IDLTranslator


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.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.generator.translator.ast2idl.lib;
28
29 import org.objectweb.openccm.ast.api.TypeKind;
30 import org.objectweb.openccm.ast.api.Declaration;
31
32 /**
33  * Utility interface to translate IDL types in string format.
34  *
35  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
36  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
37  *
38  * @version 0.5
39  */

40
41 public class AST_IDLTranslator
42   implements org.objectweb.openccm.generator.translator.ast2idl.api.AST_IDLTranslator
43 {
44     // ==================================================================
45
//
46
// Internal state.
47
//
48
// ==================================================================
49

50     /**
51      * Java keywords table.
52      */

53     protected java.util.Set JavaDoc keywords_;
54
55     // ==================================================================
56
//
57
// Constructor.
58
//
59
// ==================================================================
60

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

64     public
65     AST_IDLTranslator()
66     {
67         keywords_ = new java.util.HashSet JavaDoc();
68         keywords_.add("abstract");
69         keywords_.add("any");
70         keywords_.add("attribute");
71         keywords_.add("boolean");
72         keywords_.add("case");
73         keywords_.add("char");
74         keywords_.add("const");
75         keywords_.add("context");
76         keywords_.add("custom");
77         keywords_.add("default");
78         keywords_.add("double");
79         keywords_.add("exception");
80         keywords_.add("enum");
81         keywords_.add("factory");
82         keywords_.add("FALSE");
83         keywords_.add("fixed");
84         keywords_.add("float");
85         keywords_.add("in");
86         keywords_.add("inout");
87         keywords_.add("interface");
88         keywords_.add("local");
89         keywords_.add("long");
90         keywords_.add("module");
91         keywords_.add("native");
92         keywords_.add("Object");
93         keywords_.add("octet");
94         keywords_.add("oneway");
95         keywords_.add("out");
96         keywords_.add("private");
97         keywords_.add("public");
98         keywords_.add("raises");
99         keywords_.add("readonly");
100         keywords_.add("sequence");
101         keywords_.add("short");
102         keywords_.add("string");
103         keywords_.add("struct");
104         keywords_.add("supports");
105         keywords_.add("switch");
106         keywords_.add("TRUE");
107         keywords_.add("truncatable");
108         keywords_.add("typedef");
109         keywords_.add("unsigned");
110         keywords_.add("union");
111         keywords_.add("ValueBase");
112         keywords_.add("valuetype");
113         keywords_.add("void");
114         keywords_.add("wchar");
115         keywords_.add("wstring");
116     }
117
118     // ==================================================================
119
//
120
// Internal methods.
121
//
122
// ==================================================================
123

124     // ==================================================================
125
//
126
// Public methods.
127
//
128
// ==================================================================
129

130     /**
131      * Translate a type into its idl format.
132      *
133      * @param typeref - The type reference to translate.
134      *
135      * @return The resulting string.
136      **/

137     public String JavaDoc
138     toIDL(org.objectweb.openccm.ast.api.TypeRef typeref)
139     {
140         TypeKind tk = typeref.getTypeKind();
141
142         if (tk == TypeKind.tk_null)
143             return "null";
144         else if (tk == TypeKind.tk_void)
145             return "void";
146         else if (tk == TypeKind.tk_short)
147             return "short";
148         else if (tk == TypeKind.tk_ushort)
149             return "unsigned short";
150         else if (tk == TypeKind.tk_long)
151             return "long";
152         else if (tk == TypeKind.tk_ulong)
153             return "unsigned long";
154         else if (tk == TypeKind.tk_longlong)
155             return "long long";
156         else if (tk == TypeKind.tk_ulonglong)
157             return "unsigned long long";
158         else if (tk == TypeKind.tk_float)
159             return "float";
160         else if (tk == TypeKind.tk_double)
161             return "double";
162         else if (tk == TypeKind.tk_longdouble)
163             return "long double";
164         else if (tk == TypeKind.tk_boolean)
165             return "boolean";
166         else if (tk == TypeKind.tk_char)
167             return "char";
168         else if (tk == TypeKind.tk_wchar)
169             return "wchar";
170         else if (tk == TypeKind.tk_octet)
171             return "octet";
172         else if (tk == TypeKind.tk_any)
173             return "any";
174         else if (tk == TypeKind.tk_Principal)
175             return "::CORBA::Principal";
176         else if (tk == TypeKind.tk_TypeCode)
177             return "::CORBA::TypeCode";
178
179         else if (tk == TypeKind.tk_objref)
180             return "Object";
181
182         else if ( (tk == TypeKind.tk_struct) ||
183                   (tk == TypeKind.tk_union) ||
184                   (tk == TypeKind.tk_enum) ||
185                   (tk == TypeKind.tk_exception) ||
186                   (tk == TypeKind.tk_alias) ||
187                   (tk == TypeKind.tk_value_box) ||
188                   (tk == TypeKind.tk_value) ||
189                   (tk == TypeKind.tk_native) ||
190                   (tk == TypeKind.tk_abstract_interface) ||
191                   (tk == TypeKind.tk_local_interface) ||
192                   (tk == TypeKind.tk_interface) ||
193                   (tk == TypeKind.tk_component) ||
194                   (tk == TypeKind.tk_event) ||
195                   (tk == TypeKind.tk_home) )
196         {
197                 Declaration decl = (Declaration)typeref;
198                 return getAbsoluteName(decl);
199         }
200         else if (tk == TypeKind.tk_string)
201         {
202                 int l = ((org.objectweb.openccm.ast.api.TypeRefWithLength)typeref).getLength();
203                 if (l==0)
204                     return "string";
205                 return "string<"+l+">";
206         }
207         else if (tk == TypeKind.tk_wstring)
208         {
209                 int l = ((org.objectweb.openccm.ast.api.TypeRefWithLength)typeref).getLength();
210                 if (l==0)
211                     return "wstring";
212                 return "wstring<"+l+">";
213         }
214         else if (tk == TypeKind.tk_sequence)
215         {
216                 org.objectweb.openccm.ast.api.TypeRefWithContent type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref;
217                 int l = type.getLength();
218                 String JavaDoc str = toIDL(type.getContentType());
219                 if (l==0)
220                     return "sequence<"+str+">";
221                 return "sequence<"+str+","+l+">";
222         }
223         else if (tk == TypeKind.tk_array)
224         {
225                 org.objectweb.openccm.ast.api.TypeRefWithContent type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref;
226                 String JavaDoc str = toIDL(type.getContentType());
227                 return str;
228         }
229         else if (tk == TypeKind.tk_fixed)
230         {
231                 org.objectweb.openccm.ast.api.FixedTypeRef type = (org.objectweb.openccm.ast.api.FixedTypeRef)typeref;
232                 return "fixed<"+type.getDigits()+","+type.getScale()+">";
233         }
234         else
235                 return "";
236     }
237
238     /**
239      * Add postfix to the IDL name if typeref is an array.
240      *
241      * @param typeref - The array to translate.
242      *
243      * @return The resulting string.
244      **/

245     public String JavaDoc
246     toIDLPostfix(org.objectweb.openccm.ast.api.TypeRef typeref)
247     {
248         String JavaDoc res = "";
249
250         if ( typeref.getTypeKind() == TypeKind.tk_array )
251         {
252                 org.objectweb.openccm.ast.api.TypeRefWithContent type = null;
253                 type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref;
254                 res = toIDLPostfix(type.getContentType())+"["+type.getLength()+"]";
255         }
256         return res;
257     }
258
259     /**
260      * Get the string value of an Any.
261      *
262      * @param anyvalue - The Any to translate.
263      * @param typeref - The value's type.
264      *
265      * @return The resulting string.
266      **/

267     public String JavaDoc
268     toString(org.objectweb.openccm.ast.api.AnyValue anyvalue,
269              org.objectweb.openccm.ast.api.TypeRef typeref)
270     {
271         //
272
// TODO : add escaped sequencesto strings, eg '\n' => "\\n"
273
//
274
if (anyvalue.isInteger())
275             return java.lang.String.valueOf(anyvalue.getAsInteger());
276         else if (anyvalue.isString())
277             return "\""+anyvalue.getAsString()+"\"";
278         else if (anyvalue.isWString())
279             return "L\""+anyvalue.getAsWString()+"\"";
280         else if (anyvalue.isChar())
281             return "\'"+java.lang.String.valueOf(anyvalue.getAsChar())+"\'";
282         else if (anyvalue.isWChar())
283             return "L\'"+java.lang.String.valueOf(anyvalue.getAsWChar())+"\'";
284         else if (anyvalue.isFloating())
285             return java.lang.String.valueOf(anyvalue.getAsFloating());
286         else if (anyvalue.isFixed())
287             return anyvalue.getAsFixed();
288         else if (anyvalue.isBoolean())
289         {
290             if (anyvalue.getAsBoolean())
291                 return "TRUE";
292             else
293                 return "FALSE";
294         }
295         else if (anyvalue.isEnum())
296         {
297             // build enum member absolute name
298
String JavaDoc abs_name = getAbsoluteName( ((org.objectweb.openccm.ast.api.EnumDecl)typeref).getParent() );
299             return abs_name + "::" + anyvalue.getAsEnum();
300         }
301         else
302             return "";
303     }
304
305     /**
306      * Check if id is an idl keyword.
307      *
308      * @param id - The string to check
309      *
310      * @return A valid string.
311      */

312     public java.lang.String JavaDoc
313     checkKeywords(String JavaDoc id)
314     {
315         if (keywords_.contains( id.toLowerCase() ))
316            return "_"+id;
317
318         return id;
319     }
320
321     /**
322      * Get a valid absolute name from a declaration,
323      * i.e. check keywords.
324      *
325      * @param decl - The declaration to get the name.
326      *
327      * @return A valid absolute name.
328      */

329     public String JavaDoc
330     getAbsoluteName(Declaration decl)
331     {
332         String JavaDoc result = "";
333
334         if (decl.getParent() != null)
335             result = getAbsoluteName( decl.getParent() );
336
337         if ( (decl.getName() != null) &&
338              !(decl.getName().equals("")) )
339             result = result + "::" + checkKeywords(decl.getName());
340
341         return result;
342     }
343
344 }
345
Popular Tags