1 package org.jacorb.ir; 2 3 22 23 import java.io.*; 24 25 import org.jacorb.orb.TypeCode; 26 import org.omg.CORBA.TCKind ; 27 import org.omg.DynamicAny.*; 28 29 35 36 public class IdlWriter 37 { 38 private PrintStream ps; 39 private int indent = 0; 40 private org.omg.CORBA.ORB orb ; 41 private org.omg.CORBA.Repository ir = null; 42 private DynAnyFactory factory; 43 44 50 51 public IdlWriter( PrintStream _ps ) 52 { 53 ps = _ps; 54 try 55 { 56 orb = org.omg.CORBA.ORB.init((String [])null, null); 57 ir = org.omg.CORBA.RepositoryHelper.narrow( 58 orb.resolve_initial_references("InterfaceRepository")); 59 factory = 60 org.omg.DynamicAny.DynAnyFactoryHelper.narrow (orb.resolve_initial_references ("DynAnyFactory")); 61 } 62 catch( org.omg.CORBA.ORBPackage.InvalidName e ) 63 {} 64 65 if( ir == null ) 66 { 67 System.err.println("No IR configured! Exiting.."); 68 System.exit(1); 69 } 70 } 71 72 79 80 public IdlWriter( PrintStream _ps, org.omg.CORBA.Repository _ir ) 81 { 82 ps = _ps; 83 try 84 { 85 orb = org.omg.CORBA.ORB.init((String [])null, null); 86 ir = _ir; 87 factory = 88 org.omg.DynamicAny.DynAnyFactoryHelper.narrow (orb.resolve_initial_references ("DynAnyFactory")); 89 } 90 catch( org.omg.CORBA.ORBPackage.InvalidName e ) 91 {} 92 } 93 94 public void close() 95 { 96 ps.flush(); 97 ps.close(); 98 } 99 100 private void indent(int indentation) 101 { 102 indent = indentation; 103 } 104 105 private void print( String s ) 106 { 107 for( int i = 0; i < indent; i++ ) 108 ps.print(" "); 109 ps.print(s); 110 } 111 112 118 119 public void printContained( org.omg.CORBA.Contained c, 120 int indentation ) 121 { 122 org.omg.CORBA.ContainedPackage.Description descr = c.describe(); 123 124 switch (descr.kind.value()) 125 { 126 case org.omg.CORBA.DefinitionKind._dk_Module: 127 { 128 printModule( org.omg.CORBA.ModuleDescriptionHelper.extract( descr.value ), 129 indentation+3 ); 130 break; 131 } 132 case org.omg.CORBA.DefinitionKind._dk_Interface: 133 { 134 org.omg.CORBA.InterfaceDef idef = 135 org.omg.CORBA.InterfaceDefHelper.narrow( 136 ir.lookup_id( 137 org.omg.CORBA.InterfaceDescriptionHelper.extract(descr.value).id )); 138 printInterface( idef, 139 indentation+3 ); 140 break; 141 } 142 case org.omg.CORBA.DefinitionKind._dk_Attribute: 143 { 144 printAttribute( org.omg.CORBA.AttributeDescriptionHelper.extract( descr.value ), 145 indentation+3 ); 146 break; 147 } 148 case org.omg.CORBA.DefinitionKind._dk_Operation: 149 { 150 printOperation( org.omg.CORBA.OperationDescriptionHelper.extract( descr.value ), 151 indentation+3 ); 152 break; 153 } 154 case org.omg.CORBA.DefinitionKind._dk_Exception: 155 { 156 printException( org.omg.CORBA.ExceptionDescriptionHelper.extract( descr.value ), 157 indentation+3 ); 158 break; 159 } 160 case org.omg.CORBA.DefinitionKind._dk_Constant: 161 { 162 printConstant( org.omg.CORBA.ConstantDescriptionHelper.extract( descr.value ), 163 indentation+3 ); 164 break; 165 } 166 case org.omg.CORBA.DefinitionKind._dk_Struct: 167 { 168 printStruct( org.omg.CORBA.TypeDescriptionHelper.extract( descr.value ), 169 indentation+3 ); 170 break; 171 } 172 case org.omg.CORBA.DefinitionKind._dk_Enum: 173 { 174 printEnum( org.omg.CORBA.TypeDescriptionHelper.extract( descr.value ), 175 indentation+3 ); 176 break; 177 } 178 case org.omg.CORBA.DefinitionKind._dk_Union: 179 { 180 printUnion( org.omg.CORBA.TypeDescriptionHelper.extract( descr.value ), 181 indentation+3 ); 182 break; 183 } 184 case org.omg.CORBA.DefinitionKind._dk_Alias: 185 { 186 printAlias( org.omg.CORBA.TypeDescriptionHelper.extract( descr.value ), 187 indentation+3 ); 188 break; 189 } 190 } 191 } 192 193 199 200 public void printModule( org.omg.CORBA.ModuleDescription mdes, 201 int indentation ) 202 { 203 indent( indentation ); 204 205 org.omg.CORBA.ModuleDef mdef = 206 org.omg.CORBA.ModuleDefHelper.narrow( ir.lookup_id( mdes.id )); 207 print("module " + mdef.name() + "\n" ); 208 print("{\n"); 209 org.omg.CORBA.Contained [] contents = 210 mdef.contents( org.omg.CORBA.DefinitionKind.dk_all, true ); 211 212 for( int x = 0; x < contents.length; x++){ 213 printContained( contents[x], indentation ); 214 } 215 216 indent( indentation ); 217 print("};" + "\n\n"); 218 } 219 220 221 224 225 public void printInterface( org.omg.CORBA.InterfaceDef idef, 226 int indentation ) 227 { 228 org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription idfid = 229 idef.describe_interface(); 230 org.omg.CORBA.Contained [] contents = 231 idef.contents( org.omg.CORBA.DefinitionKind.dk_all, true ); 232 233 indent( indentation ); 234 235 StringBuffer inheritanceSb = new StringBuffer (); 236 237 if( idfid.base_interfaces.length > 0 ) 238 inheritanceSb.append(" : " + idfid.base_interfaces[0] ); 239 240 for( int b = 1; b < idfid.base_interfaces.length; b++){ 241 inheritanceSb.append(", " + idfid.base_interfaces[b]); 242 } 243 244 print("interface " + idfid.name + inheritanceSb.toString() + "\n"); 245 print("{" + "\n"); 246 247 for( int x = 0; x < contents.length; x++){ 248 printContained( contents[x], indentation ); 249 } 250 251 indent( indentation ); 252 print("};" + "\n\n" ); 253 } 254 255 256 258 259 public void printException( org.omg.CORBA.ExceptionDescription e, 260 int indentation ) 261 { 262 org.omg.CORBA.ExceptionDef e_def = 263 org.omg.CORBA.ExceptionDefHelper.narrow( ir.lookup_id( e.id )); 264 265 if( e_def != null ) 266 { 267 org.omg.CORBA.StructMember [] members = e_def.members(); 268 indent( indentation ); 269 print( "exception " + e.name + " {" + "\n" ); 270 indent( indentation + 3 ); 271 for( int i = 0; i< members.length; i++){ 272 print( TypeCode.idlTypeName( members[i].type ) + " " + members[i].name + 273 ";" + "\n" ); 274 } 275 indent( indentation ); 276 print( "};" + "\n\n" ); 277 } 278 else 279 System.err.println("Error, could not find excpetion " + e.id + " in IR "); 280 } 281 282 284 285 public void printStruct( org.omg.CORBA.TypeDescription t, int indentation ) 286 { 287 org.omg.CORBA.StructDef s_def = 288 org.omg.CORBA.StructDefHelper.narrow(ir.lookup_id( t.id )); 289 290 if( s_def != null ) 291 { 292 org.omg.CORBA.StructMember [] members = s_def.members(); 293 org.omg.CORBA.Contained [] contents = 294 s_def.contents(org.omg.CORBA.DefinitionKind.dk_all, false); 295 296 indent( indentation ); 297 print( "struct " + s_def.name() + " {" + "\n" ); 298 indent( indentation + 3 ); 299 300 for( int i = 0; i < members.length; i++) 301 { 302 print( TypeCode.idlTypeName( members[i].type ) + " " + 303 members[i].name + ";" + "\n" ); 304 } 305 306 for( int i = 0; i< contents.length; i++) 307 { 308 printContained( contents[i], indentation ); 309 } 310 311 indent( indentation ); 312 print( "};" + "\n\n" ); 313 } 314 else 315 System.err.println("Error, could not find struct " + t.id + " in IR "); 316 317 } 318 319 320 322 323 public void printConstant( org.omg.CORBA.ConstantDescription c, 324 int indentation ) 325 { 326 indent( indentation ); 327 StringBuffer sb = 328 new StringBuffer ( "const " + TypeCode.idlTypeName( c.type ) 329 + " " + c.name + " = " ); 330 switch ( c.type.kind().value() ) 331 { 332 case org.omg.CORBA.TCKind._tk_string: 333 sb.append( "\"" + c.value.extract_string() + "\"" ); 334 break; 335 case org.omg.CORBA.TCKind._tk_wstring: 336 sb.append( "\"" + c.value.extract_wstring() + "\"" ); 337 break; 338 case org.omg.CORBA.TCKind._tk_boolean: 339 sb.append( c.value.extract_boolean() ); 340 break; 341 case org.omg.CORBA.TCKind._tk_long: 342 sb.append( c.value.extract_long() ); 343 break; 344 case org.omg.CORBA.TCKind._tk_ulong: 345 sb.append( c.value.extract_ulong() ); 346 break; 347 case org.omg.CORBA.TCKind._tk_longlong: 348 sb.append( c.value.extract_longlong() ); 349 break; 350 case org.omg.CORBA.TCKind._tk_ulonglong: 351 sb.append( c.value.extract_ulonglong() ); 352 break; 353 case org.omg.CORBA.TCKind._tk_short: 354 sb.append( c.value.extract_short() ); 355 break; 356 case org.omg.CORBA.TCKind._tk_ushort: 357 sb.append( c.value.extract_ushort() ); 358 break; 359 case org.omg.CORBA.TCKind._tk_float: 360 sb.append( c.value.extract_float() ); 361 break; 362 case org.omg.CORBA.TCKind._tk_octet: 363 sb.append( c.value.extract_octet() ); 364 break; 365 case org.omg.CORBA.TCKind._tk_char: 366 sb.append( "\'" + c.value.extract_char() + "\'" ); 367 break; 368 case org.omg.CORBA.TCKind._tk_wchar: 369 sb.append( "\'" + c.value.extract_wchar() + "\'" ); 370 break; 371 case org.omg.CORBA.TCKind._tk_fixed: 372 sb.append( c.value.extract_fixed() ); 373 break; 374 } 375 376 print( sb.toString()+";\n\n"); 377 } 378 379 380 382 383 public void printAttribute( org.omg.CORBA.AttributeDescription a, 384 int indentation ) 385 { 386 indent( indentation ); 387 String mode = ""; 388 if( a.mode.equals( org.omg.CORBA.AttributeMode.ATTR_READONLY )) 389 mode = "readonly "; 390 print( mode + "attribute " + TypeCode.idlTypeName(a.type) 391 + " " + a.name + ";" + "\n" ); 392 } 393 394 395 397 398 public void printEnum( org.omg.CORBA.TypeDescription t, 399 int indentation ) 400 { 401 org.omg.CORBA.EnumDef e_def = 402 org.omg.CORBA.EnumDefHelper.narrow( ir.lookup_id( t.id )); 403 if( e_def != null ) 404 { 405 String [] members = e_def.members(); 406 indent( indentation ); 407 StringBuffer vals = new StringBuffer (); 408 if( members.length > 0 ) 409 vals.append( members[0] ); 410 for( int i = 1; i< members.length; i++){ 411 vals.append( "," + members[i] ); 412 } 413 print( "enum " + e_def.name() + " {" + vals + "};" + "\n\n" ); 414 } 415 else 416 System.err.println("Error, could not find enum " + t.id + " in IR "); 417 418 } 419 420 422 423 public void printUnion( org.omg.CORBA.TypeDescription t, 424 int indentation ) 425 { 426 org.omg.CORBA.UnionDef u_def = 427 org.omg.CORBA.UnionDefHelper.narrow( ir.lookup_id( t.id )); 428 if( u_def != null ) 429 { 430 org.omg.CORBA.UnionMember [] members = u_def.members(); 431 indent( indentation ); 432 print( "union " + u_def.name() + " switch ( " + 433 TypeCode.idlTypeName(u_def.discriminator_type()) 434 + " )\n"); 435 print("{\n" ); 436 indent( indentation + 4 ); 437 int def_idx = -1; 438 for( int i = 0; i < members.length; i++ ) 439 { 440 if( members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_octet && 441 ( members[i].label.extract_octet() == (byte)0 )) 442 { 443 def_idx = i; 444 } 445 else if( members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_char ) 446 { 447 print("case \'" + members[i].label.extract_char() + "\' : " + 448 TypeCode.idlTypeName(members[i].type) + " " 449 + members[i].name + ";" + "\n"); 450 } 451 else if( members[i].label.type().kind() == org.omg.CORBA.TCKind.tk_enum ) 452 { 453 try 455 { 456 DynEnum dEnum = 457 DynEnumHelper.narrow( 458 factory.create_dyn_any( members[i].label )); 459 460 print("case " + dEnum.get_as_string() + " : " + 462 TypeCode.idlTypeName(members[i].type) + " " + 463 members[i].name + ";" + "\n"); 464 } 465 catch( Exception bk ) 466 { 467 bk.printStackTrace(); 468 } 469 } 470 else 471 print("case " + members[i].label.type() + " : " + 472 TypeCode.idlTypeName(members[i].type) + " " + 473 members[i].name + ";" + "\n"); 474 } 475 if( def_idx != -1 ) 476 { 477 print("default : " + TypeCode.idlTypeName(members[def_idx].type) + " " + 478 members[def_idx].name +";" + "\n" ); 479 } 480 indent( indentation ); 481 print( "};" + "\n\n"); 482 } 483 else 484 System.err.println("Error, could not find union " + 485 t.id + " in IR "); 486 } 487 488 491 492 public void printAlias( org.omg.CORBA.TypeDescription t, int indentation ) 493 { 494 org.omg.CORBA.AliasDef adef = 495 org.omg.CORBA.AliasDefHelper.narrow( ir.lookup_id( t.id )); 496 indent( indentation ); 497 498 String originalTypeName = TypeCode.idlTypeName( adef.original_type_def().type()); 499 500 print("typedef " + originalTypeName + 501 " " + adef.name() + ";\n\n"); 502 503 } 504 505 508 509 public void printOperation(org.omg.CORBA.OperationDescription op, 510 int indentation ) 511 { 512 indent( indentation ); 513 514 String mode = ""; 515 if( op.mode.equals(org.omg.CORBA.OperationMode.OP_ONEWAY )) 516 mode = "oneway "; 517 print( mode + TypeCode.idlTypeName(op.result) + " " + op.name + "("); 518 519 indent(0); 520 521 for( int i = 0; i < op.parameters.length-1; i++){ 522 printParameter(op.parameters[i], ","); 523 } 524 525 if( op.parameters.length > 0 ) 526 printParameter(op.parameters[op.parameters.length -1], ""); 527 print(")"); 528 529 if( op.exceptions.length > 0 ){ 530 print(" raises ("); 531 print( TypeCode.idlTypeName(op.exceptions[0].type ) ); 532 for( int i = 1; i < op.exceptions.length; i++){ 533 print( TypeCode.idlTypeName(op.exceptions[0].type ) + ","); 534 } 535 print(")"); 536 } 537 print( ";" + "\n" ); 538 indent( indentation ); 539 } 540 541 542 public void printParameter( org.omg.CORBA.ParameterDescription p, 543 String separator ) 544 { 545 if( p.mode.equals( org.omg.CORBA.ParameterMode.PARAM_OUT) ) 546 print("out "); 547 else if ( p.mode.equals( org.omg.CORBA.ParameterMode.PARAM_INOUT )) 548 print("inout "); 549 else 550 print("in "); 551 print( TypeCode.idlTypeName(p.type) + " " + p.name ); 552 print( separator ); 553 } 554 555 } 556 | Popular Tags |