KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > idl > InitDecl


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

20
21 package org.jacorb.idl;
22
23 import java.io.PrintWriter JavaDoc;
24 import java.util.*;
25
26 /**
27  * @author Gerald Brose
28  * @version $Id: InitDecl.java,v 1.10 2004/05/06 12:39:58 nicolas Exp $
29  */

30
31 class InitDecl
32     extends Declaration
33 {
34     public Vector paramDecls;
35     public IdlSymbol myValue;
36
37     /** new in CORBA 3.0, factory methods may raise exceptions */
38     public RaisesExpr raisesExpr;
39
40     public InitDecl( int num )
41     {
42         super( num );
43         paramDecls = new Vector();
44     }
45
46     public void setPackage( String JavaDoc s )
47     {
48         s = parser.pack_replace( s );
49
50         if( pack_name.length() > 0 )
51             pack_name = s + "." + pack_name;
52         else
53             pack_name = s;
54
55         for( Enumeration e = paramDecls.elements();
56              e.hasMoreElements();
57              ( (ParamDecl)e.nextElement() ).setPackage( s )
58                 )
59             ;
60         raisesExpr.setPackage( s );
61     }
62
63     public void setEnclosingSymbol( IdlSymbol s )
64     {
65         if( enclosing_symbol != null && enclosing_symbol != s )
66             throw new RuntimeException JavaDoc( "Compiler Error: trying to reassign container for "
67                     + name );
68         enclosing_symbol = s;
69         raisesExpr.setEnclosingSymbol( s );
70     }
71
72     public void parse()
73     {
74         myValue = enclosing_symbol;
75
76         try
77         {
78             NameTable.define( full_name(), "factory" );
79         }
80         catch( NameAlreadyDefined nad )
81         {
82             parser.error( "Factory " + full_name() + " already defined", token );
83         }
84
85         for( Enumeration e = paramDecls.elements(); e.hasMoreElements(); )
86         {
87             ParamDecl param = (ParamDecl)e.nextElement();
88             param.parse();
89             try
90             {
91                 NameTable.define( full_name() + "." +
92                         param.simple_declarator.name(),
93                         "argument" );
94             }
95             catch( NameAlreadyDefined nad )
96             {
97                 parser.error( "Argument " + param.simple_declarator.name() +
98                         " already defined in operation " + full_name(),
99                         token );
100             }
101         }
102         raisesExpr.parse();
103     }
104
105     /**
106      * Prints the method's signature, for inclusion in the
107      * factory interface.
108      */

109     public void print( PrintWriter JavaDoc ps, String JavaDoc type_name )
110     {
111         ps.print( "\t" + type_name + " " + name + "( " );
112
113         Enumeration e = paramDecls.elements();
114
115         if( e.hasMoreElements() )
116             ( (ParamDecl)e.nextElement() ).print( ps );
117
118         for( ; e.hasMoreElements(); )
119         {
120             ps.print( ", " );
121             ( (ParamDecl)e.nextElement() ).print( ps );
122         }
123         ps.print( ")" );
124         raisesExpr.print( ps );
125         ps.println( ";" );
126     }
127
128     /**
129      * Prints the Helper method that corresponds to this factory method.
130      */

131     public void printHelperMethod( PrintWriter JavaDoc ps, String JavaDoc type_name )
132     {
133         ps.print( "\tpublic static " + type_name + " " + name + "( " );
134         ps.print( "org.omg.CORBA.ORB orb" );
135
136         for ( Enumeration e = paramDecls.elements();
137               e.hasMoreElements(); )
138         {
139             ps.print( ", " );
140             ( (ParamDecl)e.nextElement() ).print( ps );
141         }
142         ps.println (" )");
143         
144         ps.println ("\t{");
145         ps.println ("\t\t" + type_name + "ValueFactory f = "
146                     + "( " + type_name + "ValueFactory )"
147                     + "((org.omg.CORBA_2_3.ORB)orb).lookup_value_factory(id());");
148         ps.println ("\t\tif (f == null)");
149         ps.println ("\t\t\tthrow new org.omg.CORBA.MARSHAL( "
150                     + "1, org.omg.CORBA.CompletionStatus.COMPLETED_NO );");
151         ps.print ("\t\treturn f." + name + "( ");
152         
153         for ( Enumeration e = paramDecls.elements();
154               e.hasMoreElements(); )
155         {
156             ps.print (( (ParamDecl)e.nextElement() ).simple_declarator );
157             if (e.hasMoreElements()) ps.print (", ");
158         }
159
160         ps.println (" );");
161
162         ps.println ("\t}");
163     }
164
165     public String JavaDoc name()
166     {
167         return name;
168     }
169
170
171     public String JavaDoc opName()
172     {
173         return name();
174     }
175
176
177 }
178
179
180
181
182
183
184
185
Popular Tags