1 26 27 package org.objectweb.openccm.generator.translator.idl2java.lib; 28 29 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 42 43 public class CommonTranslator 44 implements org.objectweb.openccm.generator.translator.idl2java.api.CommonTranslator 45 { 46 52 55 protected java.util.Map abs_names_; 56 57 60 protected java.util.Set keywords_; 61 62 63 69 72 public 73 CommonTranslator() 74 { 75 abs_names_ = new java.util.HashMap (); 76 keywords_ = new java.util.HashSet (); 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 144 150 157 public java.lang.String 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 str = toJava(content.getContentType()); 239 return str+"[]"; 240 } 241 else 242 return ""; 243 } 244 245 253 public java.lang.String 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 TypeKind tk = type.getTypeKind(); 262 if ( tk == TypeKind.tk_null || 263 tk == TypeKind.tk_void ) 264 throw new Error ("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 ("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 ("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 ("Other cases are not supported!!!"); 335 } 336 337 344 public java.lang.String 345 checkKeywords(java.lang.String id) 346 { 347 if (keywords_.contains(id)) 348 return "_"+id; 349 350 return id; 351 } 352 353 361 public java.lang.String 362 getAbsoluteName(Declaration decl) 363 { 364 if (decl==null) 365 return ""; 366 367 java.lang.String res = null; 369 res = (java.lang.String )abs_names_.get(decl.getId()); 370 if (res!=null) 371 return res; 372 373 res = ""; 375 java.lang.String 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 java.lang.String 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 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 abs_names_.put(decl.getId(), res); 435 return res; 436 } 437 438 445 public java.lang.String 446 getPackage(Declaration decl) 447 { 448 java.lang.String 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 465 public String 466 getAsDirectory(String package_name) 467 { 468 if (package_name == null) 469 return "./"; 470 471 return package_name.replace('.', java.io.File.separatorChar); 472 } 473 474 483 public java.lang.String 484 getAsDirectory(Declaration decl) 485 { 486 java.lang.String abs_name = getAbsoluteName(decl); 487 int idx = abs_name.indexOf('.'); 488 if (idx==-1) 489 return "./"; 490 491 java.lang.String 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 510 public java.lang.String 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 else 543 return "null"; 544 } 545 546 552 } 553 | Popular Tags |