KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > ir > IdlWriter


1 package org.jacorb.ir;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.io.*;
24
25 import org.jacorb.orb.TypeCode;
26 import org.omg.CORBA.TCKind JavaDoc;
27 import org.omg.DynamicAny.*;
28
29 /**
30  * This class prints IDL from IR-Descriptions to PrintStreams
31  *
32  * @author (c) Gerald Brose, FU Berlin 2000
33  * @version $Id: IdlWriter.java,v 1.11 2005/01/28 14:49:45 andre.spiegel Exp $
34  */

35
36 public class IdlWriter
37 {
38     private PrintStream ps;
39     private int indent = 0;
40     private org.omg.CORBA.ORB JavaDoc orb ;
41     private org.omg.CORBA.Repository JavaDoc ir = null;
42     private DynAnyFactory factory;
43
44     /**
45      * create a new IdlWriter for the default JacORB IR
46      * which writes to a specific PrintStream
47      *
48      * @param _ps a PrintStream
49      */

50
51     public IdlWriter( PrintStream _ps )
52     {
53         ps = _ps;
54         try
55         {
56             orb = org.omg.CORBA.ORB.init((String JavaDoc[])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 JavaDoc e )
63         {}
64
65         if( ir == null )
66         {
67             System.err.println("No IR configured! Exiting..");
68             System.exit(1);
69         }
70     }
71
72     /**
73      * create a new IdlWriter for a specific IR which writes
74      * to a specific PrintStream
75      *
76      * @param _ps a PrintStream
77      * @param _ir a Repository
78      */

79
80     public IdlWriter( PrintStream _ps, org.omg.CORBA.Repository JavaDoc _ir )
81     {
82         ps = _ps;
83         try
84         {
85             orb = org.omg.CORBA.ORB.init((String JavaDoc[])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 JavaDoc 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 JavaDoc s )
106     {
107         for( int i = 0; i < indent; i++ )
108             ps.print(" ");
109         ps.print(s);
110     }
111
112     /**
113      * print the IDL definition for a contained objec
114      *
115      * @param c the contained object
116      * @param indentation how many spaces to use for indentation
117      */

118
119     public void printContained( org.omg.CORBA.Contained JavaDoc 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 JavaDoc 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     /**
194      * print the IDL definition for a module
195      *
196      * @param mdes the module description
197      * @param indentation how many spaces to use for indentation
198      */

199
200     public void printModule( org.omg.CORBA.ModuleDescription JavaDoc mdes,
201                              int indentation )
202     {
203         indent( indentation );
204
205         org.omg.CORBA.ModuleDef JavaDoc 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 JavaDoc[] 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     /**
222      * print an IDL interface
223      */

224
225     public void printInterface( org.omg.CORBA.InterfaceDef JavaDoc idef,
226                                 int indentation )
227     {
228         org.omg.CORBA.InterfaceDefPackage.FullInterfaceDescription idfid =
229             idef.describe_interface();
230         org.omg.CORBA.Contained JavaDoc[] contents =
231             idef.contents( org.omg.CORBA.DefinitionKind.dk_all, true );
232
233         indent( indentation );
234
235         StringBuffer JavaDoc inheritanceSb = new StringBuffer JavaDoc();
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     /** print an IDL exception def
257      */

258
259     public void printException( org.omg.CORBA.ExceptionDescription JavaDoc e,
260                                 int indentation )
261     {
262         org.omg.CORBA.ExceptionDef JavaDoc 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 JavaDoc [] 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     /** print an IDL struct def
283      */

284
285     public void printStruct( org.omg.CORBA.TypeDescription JavaDoc t, int indentation )
286     {
287         org.omg.CORBA.StructDef JavaDoc 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 JavaDoc [] members = s_def.members();
293             org.omg.CORBA.Contained JavaDoc [] 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     /** print an IDL const
321      */

322
323     public void printConstant( org.omg.CORBA.ConstantDescription JavaDoc c,
324                                int indentation )
325     {
326         indent( indentation );
327         StringBuffer JavaDoc sb =
328             new StringBuffer JavaDoc ( "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     /** print an IDL attribute
381      */

382
383     public void printAttribute( org.omg.CORBA.AttributeDescription JavaDoc a,
384                                 int indentation )
385     {
386         indent( indentation );
387         String JavaDoc 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     /** print an IDL Enum
396      */

397
398     public void printEnum( org.omg.CORBA.TypeDescription JavaDoc t,
399                            int indentation )
400     {
401         org.omg.CORBA.EnumDef JavaDoc e_def =
402             org.omg.CORBA.EnumDefHelper.narrow( ir.lookup_id( t.id ));
403         if( e_def != null )
404         {
405             String JavaDoc [] members = e_def.members();
406             indent( indentation );
407             StringBuffer JavaDoc vals = new StringBuffer JavaDoc();
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     /** print an IDL Union
421      */

422
423     public void printUnion( org.omg.CORBA.TypeDescription JavaDoc t,
424                             int indentation )
425     {
426         org.omg.CORBA.UnionDef JavaDoc u_def =
427             org.omg.CORBA.UnionDefHelper.narrow( ir.lookup_id( t.id ));
428         if( u_def != null )
429         {
430             org.omg.CORBA.UnionMember JavaDoc [] 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                     // int val = members[i].label.extract_long();
454
try
455                     {
456                         DynEnum dEnum =
457                             DynEnumHelper.narrow(
458                                   factory.create_dyn_any( members[i].label ));
459
460                         // print("case " + members[i].label.type().member_name(val) + " : " +
461
print("case " + dEnum.get_as_string() + " : " +
462                               TypeCode.idlTypeName(members[i].type) + " " +
463                               members[i].name + ";" + "\n");
464                     }
465                     catch( Exception JavaDoc 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     /**
489      * print an IDL alias
490      */

491
492     public void printAlias( org.omg.CORBA.TypeDescription JavaDoc t, int indentation )
493     {
494         org.omg.CORBA.AliasDef JavaDoc adef =
495             org.omg.CORBA.AliasDefHelper.narrow( ir.lookup_id( t.id ));
496         indent( indentation );
497
498         String JavaDoc originalTypeName = TypeCode.idlTypeName( adef.original_type_def().type());
499
500         print("typedef " + originalTypeName +
501                   " " + adef.name() + ";\n\n");
502
503     }
504
505     /**
506      * print an IDL operation
507      */

508
509     public void printOperation(org.omg.CORBA.OperationDescription JavaDoc op,
510                                int indentation )
511     {
512         indent( indentation );
513
514         String JavaDoc 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 JavaDoc p,
543                                 String JavaDoc 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