KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > generator > idl > lib > IDL3Generator


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

26
27 package org.objectweb.openccm.generator.idl.lib;
28
29 // Package dependencies.
30
import org.objectweb.openccm.ast.api.AST;
31 import org.objectweb.openccm.ast.api.AnyValue;
32 import org.objectweb.openccm.ast.api.AttributeDecl;
33 import org.objectweb.openccm.ast.api.ComponentBase;
34 import org.objectweb.openccm.ast.api.ComponentDecl;
35 import org.objectweb.openccm.ast.api.ConstantDecl;
36 import org.objectweb.openccm.ast.api.Declaration;
37 import org.objectweb.openccm.ast.api.DeclarationKind;
38 import org.objectweb.openccm.ast.api.EnumDecl;
39 import org.objectweb.openccm.ast.api.ExceptionDecl;
40 import org.objectweb.openccm.ast.api.ExceptionList;
41 import org.objectweb.openccm.ast.api.HomeDecl;
42 import org.objectweb.openccm.ast.api.Initializer;
43 import org.objectweb.openccm.ast.api.InterfaceDecl;
44 import org.objectweb.openccm.ast.api.InterfaceList;
45 import org.objectweb.openccm.ast.api.OperationDecl;
46 import org.objectweb.openccm.ast.api.Parameter;
47 import org.objectweb.openccm.ast.api.Scope;
48 import org.objectweb.openccm.ast.api.StructDecl;
49 import org.objectweb.openccm.ast.api.TypeRef;
50 import org.objectweb.openccm.ast.api.UnionDecl;
51 import org.objectweb.openccm.ast.api.UnionMember;
52 import org.objectweb.openccm.ast.api.UsesDecl;
53 import org.objectweb.openccm.ast.api.ValueDecl;
54 import org.objectweb.openccm.ast.api.ValueMemberDecl;
55 import org.objectweb.openccm.ast.api.WithNameTypeRef;
56 import org.objectweb.openccm.generator.common.lib.GenerationException;
57 import org.objectweb.openccm.generator.translator.ast2idl.api.AST_IDLTranslator;
58
59 import java.util.ArrayList JavaDoc;
60 import java.util.List JavaDoc;
61
62
63 /**
64  * This class visits AST declarations and generate the matching IDL3.
65  *
66  * @author <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
67  * @author <a HREF="mailto:Christophe.Demarey@lifl.fr">Christophe Demarey</a>
68  *
69  * @version 0.1
70  */

71
72 public class IDL3Generator
73      extends org.objectweb.openccm.generator.common.lib.GeneratorBase
74   implements org.objectweb.openccm.generator.idl.api.IDL3Generator
75 {
76
77     // ===========================================================
78
//
79
// Internal State.
80
//
81
// ===========================================================
82

83     /**
84      * Utility class to convert types in IDL
85      */

86     public AST_IDLTranslator translator_;
87
88     /**
89      * The last prefix defined in the file
90      */

91     protected String JavaDoc last_prefix_;
92
93     /**
94      * Print the macro directive ?
95      */

96     protected boolean print_macro_;
97
98
99     // ===========================================================
100
//
101
// Constructors.
102
//
103
// ===========================================================
104

105     /**
106      * The default constructor.
107      *
108      * @param ast - The Abstract Syntax Tree.
109      */

110
111     public IDL3Generator(AST ast)
112     {
113         // Call the GeneratorBase constructor
114
super(ast);
115
116         // Init internal states
117
translator_ = new org.objectweb.openccm.generator.translator.ast2idl.lib.AST_CIDLTranslator();
118         last_prefix_ = "";
119         print_macro_ = true;
120     }
121
122     // ===========================================================
123
//
124
// Internal methods.
125
//
126
// ===========================================================
127

128     /**
129      * Get the Macro Name from a declaration.
130      *
131      * @param target_decl - The scope to visit.
132      *
133      * @return The macro name.
134      */

135     protected String JavaDoc
136     get_macro(Scope target_decl)
137     {
138         int idx, idx2;
139         String JavaDoc abs_name = null,
140                res = "";
141         try{
142             abs_name = target_decl.getAbsoluteName();
143
144             idx = abs_name.indexOf(':', 2);
145             idx2 = 2;
146             while (idx!=-1)
147             {
148                 res = res + abs_name.substring(idx2, idx) + '_';
149                 idx2 = idx+2;
150                 idx = abs_name.indexOf(':', idx2);
151             }
152             res = res + abs_name.substring(idx2);
153             res = "__" + res.toUpperCase() + "__";
154         }catch(Exception JavaDoc e){
155             res = "__NO_NAME__";
156             print_macro_ = false;
157         }
158         return res;
159     }
160
161     /**
162      * Method called after generation.
163      * Generally used to close files.
164      **/

165     protected void
166     after_generation()
167     {
168         close("out");
169     }
170
171
172     // ===========================================================
173
//
174
// Public methods.
175
//
176
// ===========================================================
177

178     /**
179      * Initialize the generator.
180      *
181      * @param outputfile - The name where declarations will be generated.
182      * @param imports - IR declarations to import.
183      * @param app_name - The application's name.
184      */

185     public void
186     initialize( String JavaDoc outputfile,
187                 ArrayList JavaDoc imports,
188                 String JavaDoc app_name )
189     {
190         List list = new java.util.ArrayList JavaDoc();
191
192         /** Initialize environment **/
193
194         // Templates to use.
195
list.clear();
196         list.add("org/objectweb/openccm/generator/common/common.vm");
197         list.add("org/objectweb/openccm/generator/idl/idl3.vm");
198         setLibrary(list);
199
200         // The output file
201
open(outputfile, "out");
202
203         // call the common generator init method
204
super.init();
205
206         /** Initialize velocity context **/
207         put("which", app_name);
208         put("gen", this);
209         put("declarator", "");
210         put("limited_types" , new Long JavaDoc(org.objectweb.openccm.ast.api.DeclarationKind.dk_all));
211         put("imports", imports);
212     }
213
214     /**
215      * Generates contents of a scope.
216      * name must be a valid declaration name.
217      *
218      * @param name - The name of the declaration to visit.
219      *
220      * @throws GenerationException
221      */

222     public void
223     generate(String JavaDoc name)
224     throws GenerationException
225     {
226         Scope target_decl = null;
227
228         try{
229             target_decl = (Scope)getDeclaration(name);
230         }catch(ClassCastException JavaDoc ex){
231             String JavaDoc msg = name + "is not a scope!";
232             throw new GenerationException(msg);
233         }
234         generate(target_decl);
235     }
236
237     /**
238      * Generates contents of a scope.
239      *
240      * @param target_decl - The scope to visit.
241      *
242      * @throws GenerationException
243      */

244     public void
245     generate(Scope target_decl)
246     throws GenerationException
247     {
248         target_decl_ = target_decl;
249
250         // System.out.println("### Visiting scope (generate) : " + target_decl.getAbsoluteName());
251
put("obj", target_decl);
252         put("FILE_MACRO", get_macro(target_decl));
253         map("FILE_HEADER");
254         visit(target_decl);
255         after_generation();
256     }
257
258     /**
259      * Generates contents of the root scope.
260      * Declarations types must match to limited types.
261      *
262      * @param target_decl - The scope to visit.
263      * @param limited_types - A logical combination of DeclarationKind.
264      *
265      * @throws GenerationException
266      */

267     public void
268     generate(Scope target_decl, long limited_types)
269     throws GenerationException
270     {
271         List vect = null;
272
273         vect = getDeclarations(target_decl, limited_types);
274         put("limited_types" , new Long JavaDoc(limited_types));
275         put("FILE_MACRO", get_macro(target_decl));
276         map("FILE_HEADER");
277         forward(vect);
278         map("END_LINE");
279         visit(vect);
280         after_generation();
281     }
282
283     /**
284      * Close an idl stream.
285      *
286      * @param id - The identifiant of the file to close.
287      */

288     public void
289     close(String JavaDoc id)
290     {
291         if ( print_macro_ )
292             print("\n#endif", id);
293         super.close(id);
294     }
295
296     // ===========================================================
297
//
298
// Useful methods for IDL3 Mapping.
299
//
300
// ===========================================================
301

302     /**
303      * Check if id is an idl keyword.
304      *
305      * @param id - The string to check
306      *
307      * @return A valid name.
308      **/

309     public String JavaDoc
310     checkName(String JavaDoc id)
311     {
312         return translator_.checkKeywords(id);
313     }
314
315     /**
316      * Get a valid absolute name from a declaration,
317      * i.e. check keywords.
318      *
319      * @param decl - The declaration to get the name.
320      *
321      * @return A valid absolute name.
322      */

323     public String JavaDoc
324     getAbsoluteName(Declaration decl)
325     {
326         return translator_.getAbsoluteName(decl);
327     }
328
329     /**
330      * Utility method to check forward declarations.
331      *
332      * @return The associated search mask.
333      **/

334     public long
335     forward_check()
336     {
337         return (DeclarationKind.dk_interface +
338                 DeclarationKind.dk_local_interface +
339                 DeclarationKind.dk_abstract_interface +
340                 DeclarationKind.dk_component +
341                 DeclarationKind.dk_value +
342                 DeclarationKind.dk_struct +
343                 DeclarationKind.dk_union +
344                 DeclarationKind.dk_event);
345     }
346
347     /**
348      * Utility method to check interface declarations.
349      *
350      * @return The associated search mask.
351      **/

352     public long
353     interface_check()
354     {
355         return (DeclarationKind.dk_attribute +
356                 DeclarationKind.dk_constant +
357                 DeclarationKind.dk_exception +
358                 DeclarationKind.dk_operation +
359                 DeclarationKind.dk_alias +
360                 DeclarationKind.dk_struct +
361                 DeclarationKind.dk_union +
362                 DeclarationKind.dk_enum +
363                 DeclarationKind.dk_native);
364     }
365
366     /**
367      * Utility method to check component declarations.
368      *
369      * @return The associated search mask.
370      **/

371     public long
372     component_check()
373     {
374         return (DeclarationKind.dk_provides +
375                 DeclarationKind.dk_uses +
376                 DeclarationKind.dk_emits +
377                 DeclarationKind.dk_publishes +
378                 DeclarationKind.dk_consumes +
379                 DeclarationKind.dk_attribute);
380     }
381
382     /**
383      * Utility method to check home declarations.
384      *
385      * @return The associated search mask.
386      **/

387     public long
388     home_check()
389     {
390         return (DeclarationKind.dk_factory +
391                 DeclarationKind.dk_finder +
392                 interface_check());
393     }
394
395     /**
396      * Utility method to check value declarations.
397      *
398      * @return The associated search mask.
399      **/

400     public long
401     value_check()
402     {
403         return (DeclarationKind.dk_value_member +
404                 interface_check());
405     }
406
407     /**
408      * Get an attribute of the class DeclarationKind.
409      *
410      * @param att - The attribute to get.
411      *
412      * @return The double value of the attribute.
413      **/

414     public long
415     getDeclarationKind(String JavaDoc att)
416     {
417         java.lang.reflect.Field JavaDoc field = null;
418         long res = 0;
419
420         try{
421             field = Class.forName("org.objectweb.openccm.ast.api.DeclarationKind").getField(att);
422             res = field.getLong(field);
423         }catch(Exception JavaDoc e){
424             e.printStackTrace();
425         }
426
427         return res;
428     }
429
430     // ===========================================================
431
//
432
// Methods for IDL3 Mapping.
433
//
434
// ===========================================================
435

436     /**
437      ** Generate a macro if necessary
438      **/

439     public String JavaDoc
440     macro(Declaration obj)
441     {
442         String JavaDoc res = "";
443         String JavaDoc version = obj.getVersion();
444         String JavaDoc id = obj.getId();
445         String JavaDoc prefix = obj.getPrefix();
446
447         // System.err.println("Macro for " + obj.getAbsoluteName() + " : " + prefix);
448
if (!prefix.equals(last_prefix_))
449         {
450             res = res + "#pragma prefix \""+prefix+"\"\n";
451             last_prefix_ = prefix;
452         }
453         else
454         {
455             String JavaDoc p_id = obj.getParent().getId();
456             int idx1 = p_id.indexOf(':');
457             int idx2 = p_id.lastIndexOf(':');
458             if (id.indexOf(p_id.substring(idx1, idx2))==-1)
459                 res = res + "#pragma id "+obj.getName()+" \""+id+"\"\n";
460         }
461         if (!version.equals("1.0"))
462             res = res + "#pragma version "+obj.getName()+" "+version+"\n";
463
464         return res;
465    }
466
467     /**
468      * Generate structured members contained in a declaration
469      * (i.e. a struct, an exception or a declaration).
470      *
471      * @param obj - The declaration to visit
472      **/

473     public void
474     struct_members(Declaration obj, String JavaDoc obj_declarator)
475     {
476         boolean is_inner = false;
477         int multiple_idx = -1;
478         WithNameTypeRef[] members = new WithNameTypeRef[0];
479
480         // Retrieve members of the declaration
481
try
482         {
483             members = ((ExceptionDecl) obj).getMemberList().getStructMembers();
484         }catch(ClassCastException JavaDoc e){
485             try{
486                 members = ((StructDecl) obj).getMemberList().getStructMembers();
487             }catch(ClassCastException JavaDoc ex){
488                 members = ((UnionDecl) obj).getMemberList().getUnionMembers();
489             }
490         }
491 // System.err.println("members de struct_members : " + members);
492

493         // Compute each member
494
for (int i=0; i<members.length; i++)
495         {
496             String JavaDoc name = checkName( members[i].getName() );
497             TypeRef type = members[i].getType();
498
499             // check for inner struct, union or enum members
500
if ((type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_struct) ||
501                 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_union) ||
502                 (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_enum))
503             {
504                 Declaration decl = null;
505                 decl = (Declaration)type;
506                 if (decl.getParent().getId().equals(obj.getId()))
507                     is_inner = true;
508                 else
509                     is_inner = false;
510             }
511             else
512                 is_inner = false;
513
514
515             // compute the declarator
516
String JavaDoc declarator = "";
517             // check for multiple declarators
518
// e.g. long l1, l2, l3;
519
if ( (i<members.length-1) && (type==members[i+1].getType()) )
520                 multiple_idx = i;
521             while ( (i<members.length-1) && (type==members[i+1].getType()) )
522                 i++;
523
524             if (multiple_idx==-1)
525                 declarator = name + translator_.toIDLPostfix(type);
526             else
527             {
528                 for (int j=multiple_idx; j<i+1; j++)
529                     declarator = declarator + checkName(members[j].getName())
530                                  + translator_.toIDLPostfix(members[j].getType()) + ", ";
531                 declarator = declarator.substring(0, declarator.length()-2);
532                 multiple_idx = -1;
533             }
534
535             if (is_inner)
536             {
537                 // inline struct, union or enum
538
Declaration decl = null;
539                 String JavaDoc id = "";
540
541                 decl = (Declaration)type;
542                 id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(decl.getDeclKind()).toUpperCase()+ "_INLINED";
543
544                 put("obj", decl);
545                 put("declarator", declarator);
546                 map(id);
547             }
548             else
549             {
550                 put("type_spec", translator_.toIDL(type) );
551                 put("name", declarator);
552                 map("STRUCT_MEMBER");
553             }
554         }//end for
555

556         put("declarator",obj_declarator);
557         put("obj", obj);
558     }
559
560
561     /**
562      * Generate union members for an union.
563      *
564      * @param union - The union to visit
565      **/

566     public void
567     union_members(UnionDecl union, String JavaDoc obj_declarator)
568     {
569         UnionMember[] members = null;
570         boolean is_default = false;
571         boolean print = true;
572         boolean is_inner = false;
573         int di = union.getDefaultIndex();
574
575         members = union.getMemberList().getUnionMembers();
576
577 //union.getContents(true, DeclarationKind.dk_all);
578
//System.err.println("default index:"+di);
579
for (int i=0; i<members.length; i++)
580         {
581             String JavaDoc name = checkName( members[i].getName() );
582             TypeRef type = members[i].getType();
583             AnyValue val = members[i].getAnyValue();
584
585             // check that the i and i+1 element spec are different.
586
// else just print the label
587
if ( ( i<members.length-1 ) &&
588                  ( name.equals( checkName(members[i+1].getName()) ) )
589                 )
590                 print = false;
591             else
592                 print = true;
593
594 // TODO : Correct the bug with default index (always -1)
595
// if (di == i)
596
if (translator_.toString(val, union.getDiscriminator()).equals(""))
597                 is_default = true;
598             else
599                 is_default = false;
600 //System.err.println("Default="+is_default+", i:"+i);
601

602             // check for inner struct, union or enum members
603
if ( (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_struct) ||
604                  (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_union) ||
605                  (type.getTypeKind() == org.objectweb.openccm.ast.api.TypeKind.tk_enum))
606             {
607                 Declaration decl = null;
608                 decl = (Declaration)type;
609                 if (decl.getParent().getId().equals(union.getId()))
610                     is_inner = true;
611                 else
612                     is_inner = false;
613             }
614             else
615                 is_inner = false;
616
617             if (!print)
618             {
619                 // print the label
620
if (is_default)
621                     print("default :\n");
622                 else
623                 {
624                     String JavaDoc value = translator_.toString(val, union.getDiscriminator());
625                     print("case " + value + " :\n");
626                 }
627             }
628             else if (is_inner)
629             {
630                 // print the label first
631
if (is_default)
632                     print("default :\n");
633                 else
634                 {
635                     String JavaDoc value = translator_.toString(val, union.getDiscriminator());
636                     print("case " + value + " :\n");
637                 }
638
639                 // inline struct, union or enum
640
Declaration decl = null;
641                 String JavaDoc id = "";
642
643                 decl = (Declaration)type;
644                 id = org.objectweb.openccm.ast.lib.DeclarationKindImpl.toString(decl.getDeclKind()).toUpperCase()+
645                     "_INLINED";
646
647                 put("obj", decl);
648                 put("declarator", name+translator_.toIDLPostfix(type));
649                 // reajust the tabulation
650
//print("/inc");
651
map(id);
652                 //print("/dec");
653
}
654             else
655             {
656                 put("value", translator_.toString(val, union.getDiscriminator()));
657                 put("type_spec",translator_.toIDL(type));
658                 put("name", name+translator_.toIDLPostfix(type));
659                 if (is_default)
660                     map("UNION_DEFAULT_MEMBER");
661                 else
662                     map("UNION_MEMBER");
663             }
664         }//end for
665
put("declarator",obj_declarator);
666         put("obj", union);
667    }
668
669
670     /**
671      * Generate members of an enumeration
672      *
673      * @param enumDecl - The enumeration.
674      **/

675     public void
676     enum_members(EnumDecl enumDecl)
677     {
678         String JavaDoc[] members = enumDecl.getMemberList().getStrings();
679         String JavaDoc res = "";
680         int i;
681
682         for (i=0; i<members.length-1; i++)
683             print( members[i] + ",\n");
684
685         if (i<members.length)
686             print( members[i] );
687     }
688
689
690     /**
691      * Generate the modifier of a ValueDecl
692      *
693      * @param value - The ValueDecl concerned.
694      **/

695     public void
696     modifier(ValueDecl value)
697     {
698         if (value.isCustom())
699             print("custom ");
700         else if (value.isAbstract())
701             print("abstract ");
702     }
703
704
705     /**
706      * Generate inherited component for a component.
707      *
708      * @param comp - The component concerned.
709      *
710      * @return The resulting string.
711      **/

712     public String JavaDoc
713     base(ComponentDecl comp)
714     {
715         ComponentDecl inh = comp.getBaseComponent();
716         if (inh != null)
717             return (" : " + getAbsoluteName(inh));
718         return "";
719    }
720
721     /**
722      * Generate inherited home.
723      *
724      * @param home - The home concerned.
725      *
726      * @return The resulting string.
727      **/

728     public String JavaDoc
729     base(HomeDecl home)
730     {
731         HomeDecl inh = home.getBaseHome();
732         if (inh != null)
733             return (" : "+ getAbsoluteName(inh));
734         return "";
735    }
736
737
738     /**
739      * Generate supported interfaces.
740      *
741      * @param comp - The list of interaces concerned.
742      *
743      * @return The resulting string.
744      **/

745     public String JavaDoc
746     supports(InterfaceDecl[] itfs)
747     {
748         String JavaDoc res = "";
749
750         if ( (itfs!=null) && (itfs.length>0) )
751         {
752             res += "\n supports ";
753             int i;
754             for (i=0; i<itfs.length-1; i++)
755                 res += getAbsoluteName(itfs[i]) + ", ";
756             res += getAbsoluteName(itfs[i]);
757         }
758         return res;
759   }
760
761     /**
762      * Generate supported interfaces for a component base.
763      *
764      * @param comp - The component base concerned.
765      *
766      * @return The resulting string.
767      **/

768     public String JavaDoc
769     supported(ComponentBase comp)
770     {
771         InterfaceList list = null;
772
773         list = comp.getSupportedInterfaceList();
774         if (list != null)
775             return supports( list.getInterfaces() );
776         return "";
777     }
778
779     /**
780      * Generate supported interfaces for a value declaration.
781      *
782      * @param comp - The value declaration concerned.
783      *
784      * @return The resulting string.
785      **/

786     public String JavaDoc
787     supported(ValueDecl value)
788     {
789         InterfaceList list = null;
790
791         list = value.getSupportedInterfaceList();
792         if (list != null)
793             return supports( list.getInterfaces() );
794         return "";
795     }
796
797     /**
798      * Generate inherited interfaces for an interface.
799      *
800      * @param itf - The interface concerned.
801      *
802      * @return The string format.
803      **/

804     public String JavaDoc
805     bases(InterfaceDecl itf)
806     {
807         String JavaDoc res = "";
808         Declaration[] itfs = itf.getInheritedInterfaceList().getInterfaces();
809
810         if (itfs.length>0)
811             res += "\n : ";
812
813         for (int i=0; i<itfs.length; i++)
814         {
815             res += getAbsoluteName(itfs[i]);
816             if (i<itfs.length-1)
817                 res += ",\n " ;
818         }
819         return res;
820     }
821
822     /**
823      * Generate mode for an operation.
824      *
825      * @param op - The operation concerned.
826      *
827      * @return The string format.
828      **/

829     public String JavaDoc
830     mode(OperationDecl op)
831     {
832         if (op.isOneway())
833             return("oneway ");
834         else
835             return "";
836     }
837
838     /**
839      * Convert a parameter mode in String format
840      *
841      * @param param - The parameter concerned.
842      *
843      * @return The string format.
844      **/

845     public String JavaDoc
846     toStringMode(Parameter param)
847     {
848         if ( param.isIn() )
849             return "in ";
850         else if ( param.isOut() )
851             return "out ";
852         else
853             return "inout ";
854     }
855
856     /**
857      * Generate type for a type reference.
858      *
859      * @param obj - The type reference concerned.
860      *
861      * @return The string format.
862      **/

863     public String JavaDoc
864     type(TypeRef obj)
865     {
866         return translator_.toIDL(obj);
867     }
868
869     /**
870      * Generate parameters for an operation.
871      *
872      * @param op - The operation concerned.
873      *
874      * @return The string format.
875      **/

876     public String JavaDoc
877     params(OperationDecl op)
878     {
879         String JavaDoc res = "";
880         Parameter[] par = op.getParameterList().getParameters();
881
882         for (int i=0; i<par.length; i++)
883             res += toStringMode(par[i]) + translator_.toIDL(par[i].getType())
884                    + " " + checkName(par[i].getName()) + ", ";
885
886         if (!res.equals(""))
887             res = res.substring(0, res.length()-2);
888
889         return res;
890     }
891
892     /**
893      * Generate exceptions raises.
894      *
895      * @param exc - The list of exceptions concerned.
896      *
897      * @return The string format.
898      **/

899     public String JavaDoc
900     raise(String JavaDoc raises_format, ExceptionList list)
901     {
902         ExceptionDecl[] excs = list.getExceptions();
903         String JavaDoc res = "";
904
905         if ( (excs != null) && (excs.length>0))
906         {
907             int i;
908
909             res += ("\n " + raises_format + "(");
910             for (i=0; i<excs.length-1; i++)
911                 res += getAbsoluteName(excs[i]) + ", ";
912             res += getAbsoluteName(excs[i]) + ")";
913         }
914         return res;
915     }
916
917     /**
918      * Generate exceptions raises for an attribute.
919      *
920      * @param attr - The attribute concerned.
921      *
922      * @return the string format.
923      **/

924     public String JavaDoc
925     raises(AttributeDecl attr)
926     {
927         return raise("raises", attr.getExceptionList());
928     }
929
930     /**
931      * Generate getexceptions raises for an attribute.
932      *
933      * @param attr - The attribute concerned.
934      *
935      * @return The string format.
936      **/

937     public String JavaDoc
938     getraises(AttributeDecl attr)
939     {
940         return raise("getraises", attr.getGetExceptionList());
941     }
942
943     /**
944      * Generate getexceptions raises for an attribute.
945      *
946      * @param attr - The attribute concerned.
947      *
948      * @return the string format.
949      **/

950     public String JavaDoc
951     setraises(AttributeDecl attr)
952     {
953         return raise("setraises", attr.getSetExceptionList());
954     }
955
956     /**
957      * Generate access mode for an attribute.
958      *
959      * @param attr - The attribute concerned.
960      *
961      * @return the string format.
962      **/

963     public String JavaDoc
964     access(AttributeDecl attr)
965     {
966         if (attr.isReadonly())
967             return "readonly ";
968         return "";
969     }
970
971     /**
972      * Generate multiple use declaration.
973      *
974      * @param uses - The use declaration concerned.
975      *
976      * @return The string format.
977      **/

978     public String JavaDoc
979     multiple(UsesDecl uses)
980     {
981         if (uses.isMultiple())
982             return "multiple ";
983         else
984             return "";
985     }
986
987     /**
988      * Generate primary key for a home.
989      *
990      * @param home - The home concerned.
991      *
992      * @return The string format.
993      **/

994     public String JavaDoc
995     primary_key(HomeDecl home)
996     {
997         Declaration pk = home.getPrimaryKey();
998
999         if (pk == null)
1000            return "";
1001        else
1002            return ("\n primary key " + getAbsoluteName(pk));
1003   }
1004
1005    /**
1006     * Generate base value for a value declaration.
1007     *
1008     * @param value - The value declaration concerned.
1009     *
1010     * @return the string format.
1011     **/

1012    public String JavaDoc
1013    base_value(ValueDecl value)
1014    {
1015        ValueDecl base = value.getBaseValue();
1016        if (base!=null)
1017        {
1018            if (value.isTruncatable())
1019                return " : truncatable " + getAbsoluteName(base);
1020            else
1021                return " : " + getAbsoluteName(base);
1022        }
1023        return "";
1024   }
1025
1026
1027    /**
1028     * Generate abstract values for a value declaration.
1029     *
1030     * @param value - The value declaration concerned.
1031     *
1032     * @return The resulting string.
1033     **/

1034    public String JavaDoc
1035    abstract_values(ValueDecl value)
1036    {
1037        ValueDecl[] bases = value.getAbstractValueList().getValues();
1038        String JavaDoc res = "";
1039
1040        if (bases.length>0)
1041        {
1042            if (value.getBaseValue() == null)
1043                res += " : ";
1044            else
1045                res += ", ";
1046            int i;
1047            for (i=0; i<bases.length-1; i++)
1048                res += getAbsoluteName(bases[i]) + ", ";
1049            res += getAbsoluteName(bases[i]);
1050        }
1051        return res;
1052    }
1053
1054    /**
1055     * Generate visibility for a value member.
1056     *
1057     * @param vm - The value member concerned.
1058     *
1059     * @return The string format.
1060     **/

1061    public String JavaDoc
1062    visibility(ValueMemberDecl vm)
1063    {
1064        if (vm.isPrivate())
1065            return "private";
1066        else
1067            return "public";
1068    }
1069
1070    /**
1071     * Translate an array into its idl format.
1072     *
1073     * @param obj - The array to translate.
1074     *
1075     * @return The resulting string.
1076     **/

1077    public String JavaDoc
1078    array(TypeRef obj)
1079    {
1080        return translator_.toIDLPostfix(obj);
1081    }
1082
1083    /**
1084     * Generate contexts for an operation.
1085     *
1086     * @param op - The operation concerned.
1087     *
1088     * @return The string format.
1089     **/

1090    public String JavaDoc
1091    contexts(OperationDecl op)
1092    {
1093        String JavaDoc[] ctxs = op.getContextList().getStrings();
1094        String JavaDoc res = "";
1095
1096        if (ctxs.length > 0)
1097        {
1098            res += " context(";
1099            for (int i=0; i<ctxs.length; i++)
1100                res += "\"" + ctxs[i] + "\", ";
1101            res += ")";
1102        }
1103
1104        return res;
1105    }
1106
1107
1108    /**
1109     * Generate value for a constant.
1110     *
1111     * @param constant - The constant declaration concerned.
1112     *
1113     * @return The string format.
1114     **/

1115    public String JavaDoc
1116    value(ConstantDecl constant)
1117    {
1118        return translator_.toString(constant.getAnyValue(), constant.getType());
1119    }
1120
1121
1122    /**
1123     * Generate initializers parameters for an initializer.
1124     *
1125     * @param obj - The initializer concerned.
1126     *
1127     * @return The resulting string.
1128     **/

1129    public String JavaDoc
1130    initializer_params(Initializer obj)
1131    {
1132        Parameter[] params = null;
1133        params = obj.getParameterList().getParameters();
1134        String JavaDoc res = "";
1135
1136        for (int i=0; i<params.length; i++)
1137        {
1138            res += toStringMode(params[i]);
1139            res += translator_.toIDL(params[i].getType());
1140            res += " " + checkName(params[i].getName()) + ", ";
1141        }
1142
1143        if (!res.equals(""))
1144            res = res.substring(0, res.length()-2);
1145
1146        return res;
1147    }
1148
1149
1150    /**
1151     * Generate initializers (i.e. factory) for a value Declaration.
1152     *
1153     * @param value - The value declaration concerned.
1154     **/

1155    public void
1156    initializers(ValueDecl value)
1157    {
1158        Initializer[] ops = value.getInitializerList().getInitializers();
1159
1160        for (int i=0;i<ops.length;i++)
1161        {
1162            put("init", ops[i]);
1163            map("INITIALIZER");
1164        }
1165    }
1166}
1167
Popular Tags