KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.idl;
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
24 import java.io.PrintWriter JavaDoc;
25 import java.util.*;
26
27 import org.apache.log.*;
28
29 /**
30  * Base class for all classes of the abstract IDL syntax tree
31  *
32  * @author Gerald Brose
33  * @version $Id: IdlSymbol.java,v 1.36 2004/05/06 12:39:58 nicolas Exp $
34  */

35
36 public class IdlSymbol
37     extends org.jacorb.idl.runtime.symbol
38 {
39     private static int num = 10000;
40     public String JavaDoc pack_name = "";
41     String JavaDoc name = "";
42
43     protected boolean is_pseudo = false; // is this a PIDL spec.?
44
protected boolean included = false;
45     protected boolean inhibitionFlag = false;
46
47     str_token token;
48
49     protected String JavaDoc _id;
50     private String JavaDoc _version;
51
52     protected IdlSymbol enclosing_symbol;
53
54     protected String JavaDoc omg_package_prefix = "";
55     private Hashtable imports = new Hashtable();
56
57     String JavaDoc typeName;
58
59     protected static final char fileSeparator =
60         System.getProperty( "file.separator" ).charAt( 0 );
61
62     Logger logger;
63
64     /** the posizion in the IDL file where this symbol was found by the lexer,
65         needed for better error messages */

66     PositionInfo myPosition = null;
67
68     /**
69      * class constructor
70      */

71
72     public IdlSymbol( int num )
73     {
74         super( num );
75         inhibitionFlag = parser.getInhibitionState();
76         logger = parser.getLogger();
77         myPosition = lexer.getPosition();
78     }
79
80
81    /**
82     * used by the lexer to mark this symbol as included from another
83     * IDL file
84     */

85
86     void set_included( boolean i )
87     {
88         included = i;
89     }
90
91
92     /**
93      * is this a symbol included from another IDL file?
94      * Used to determine if code should be generated or not.
95      */

96
97     public boolean is_included()
98     {
99         return included;
100     }
101
102     /**
103      *
104      */

105
106     public void set_pseudo()
107     {
108         is_pseudo = true;
109     }
110
111     /**
112      * is this a PIDL symbol?
113      */

114
115     public boolean is_pseudo()
116     {
117         return is_pseudo;
118     }
119
120     public void set_token( str_token i )
121     {
122         token = i;
123         if( token != null )
124         {
125             if( token.pragma_prefix.equals( "omg.org" ) )
126             {
127                 omg_package_prefix = "org.omg.";
128             }
129             set_name( token.str_val );
130         }
131     }
132
133     public str_token get_token()
134     {
135         return token;
136     }
137
138
139     /**
140      * get this symbol's name
141      */

142
143     public String JavaDoc name()
144     {
145         return name;
146     }
147
148
149
150     /**
151      * A number of IDL constructs need to have their names
152      * checked for clashes with name reserved by Java or
153      * the Java Language Mapping.
154      */

155
156     public void escapeName()
157     {
158         if( !name.startsWith( "_" ) &&
159             // Not escaping Messaging.ExceptionHolder
160
!pack_name.startsWith( "org.omg.Messaging" ) &&
161             lexer.strictJavaEscapeCheck( name ) )
162         {
163             name = "_" + name;
164         }
165     }
166
167     public boolean isEscaped()
168     {
169         return name().startsWith( "_" );
170     }
171
172     public String JavaDoc deEscapeName()
173     {
174         String JavaDoc tmp = name();
175
176         if( tmp.startsWith( "_" ) )
177         {
178             tmp = tmp.substring( 1 );
179         }
180
181         return tmp;
182     }
183
184     public void setPackage( String JavaDoc s )
185     {
186         s = parser.pack_replace( s );
187         if( pack_name.length() > 0 )
188             pack_name = s + "." + pack_name;
189         else
190             pack_name = s;
191     }
192
193     public void setEnclosingSymbol( IdlSymbol s )
194     {
195         if( enclosing_symbol != null && enclosing_symbol != s )
196             throw new RuntimeException JavaDoc( "Compiler Error: trying to reassign container for " +
197                                         name );
198
199         enclosing_symbol = s;
200     }
201
202     public IdlSymbol getEnclosingSymbol()
203     {
204         return enclosing_symbol;
205     }
206
207     public static int new_num()
208     {
209         return num++;
210     }
211
212     /** the name of this symbol */
213
214     public void set_name( String JavaDoc n )
215     {
216         name = n;
217     }
218
219     /**
220      * @return fully scoped IDL identifier
221      */

222
223     String JavaDoc full_name()
224     {
225         if( name.length() == 0 )
226         {
227             return null;
228         }
229
230         if( pack_name.length() > 0 )
231         {
232             return pack_name + "." + name;
233         }
234         else
235             return name;
236     }
237
238     /**
239      * @return fully scoped Java identifier, only used in
240      * code generation phase
241      */

242
243     String JavaDoc javaName()
244     {
245         if( name.length() == 0 )
246             return null;
247         if( pack_name.length() > 0 )
248         {
249             if( !pack_name.startsWith( "org.omg" ) )
250             {
251                 return omg_package_prefix + pack_name + "." + name;
252             }
253             else
254                 return pack_name + "." + name;
255         }
256         else
257             return name;
258     }
259
260     /**
261      * @return "org.omg." if the symbol has been declared inside a
262      * scope with a pragma prefix of "omg.org".
263      */

264
265     public String JavaDoc omgPrefix()
266     {
267         return omg_package_prefix;
268     }
269
270
271     /** empty parse */
272
273     public void parse()
274             throws ParseException
275     {
276     }
277
278     public void print( PrintWriter JavaDoc ps )
279     {
280         throw new java.lang.RuntimeException JavaDoc( "--abstract--!" );
281     }
282
283     public void printImport( PrintWriter JavaDoc ps )
284     {
285         if( !pack_name.equals( "" ) )
286         {
287             for( Enumeration e = imports.keys(); e.hasMoreElements(); )
288             {
289                 String JavaDoc name = (String JavaDoc)e.nextElement();
290                 ps.println( "import " + name + ";" );
291             }
292             ps.println();
293         }
294     }
295
296     /**
297      * Called by derived classes to potentially add the aliasHelper
298      * name to the generated Java class's import list, which is
299      * necessary in case the mapped code is in the unnamed package.
300      *
301      * @param alias the name of the alias
302      */

303
304     public void addImportedAlias( String JavaDoc alias )
305     {
306         if( logger.isDebugEnabled() )
307             logger.debug( "addImportedAlias " + alias );
308         if( alias.indexOf( '.' ) < 0 && !BaseType.isBasicName( alias ) )
309         {
310             imports.put( alias + "Helper", "" );
311         }
312     }
313
314     /**
315      * Called by derived classes to potentially add the name and the
316      * nameHelper to the generated Java class's import list, which is
317      * necessary in case the mapped code is in the unnamed package.
318      *
319      * @param name
320      */

321
322     public void addImportedName( String JavaDoc name )
323     {
324         // Ensure that we strip [] from names.
325
if ( name != null && name.endsWith( "[]" ) )
326         {
327             name = name.substring( 0, name.length() - 2 );
328         }
329
330         // Only enter this if its an alias.
331
if( name != null && name.indexOf( '.' ) < 0 && !BaseType.isBasicName( name ) )
332         {
333             addImportedName( name, null );
334         }
335     }
336
337     /**
338      * Called by derived classes to potentially add the name and the
339      * nameHelper to the generated Java class's import list, which is
340      * necessary in case the mapped code is in the unnamed package.
341      *
342      * @param name
343      * @param type
344      */

345
346     public void addImportedName( String JavaDoc name, TypeSpec type )
347     {
348         if( name != null && name.indexOf( '.' ) < 0 && !BaseType.isBasicName( name ) )
349         {
350             if( logger.isDebugEnabled() )
351                 logger.debug( "addImportedName " + name );
352
353             // If we have a typedef for a basic type we only want
354
// to import the helper class.
355

356             if( ( type == null ) || !BaseType.isBasicName( type.toString() ) )
357             {
358                 imports.put( name, "" );
359             }
360             imports.put( name + "Helper", "" );
361         }
362     }
363
364     /**
365      * Called by derived classes to potentially add the name, the
366      * nameHelper and nameHolder to the generated Java class's import
367      * list, which is necessary in case the mapped code is in the
368      * unnamed package.
369      *
370      * @param name
371      */

372
373     public void addImportedNameHolder( String JavaDoc name )
374     {
375         if( name.indexOf( '.' ) < 0 && !BaseType.isBasicName( name ) )
376         {
377             if( logger.isDebugEnabled() )
378                 logger.debug( "addImportedNameHolder " + name );
379
380             imports.put( name, "" );
381         }
382     }
383
384     public void setPrintPhaseNames()
385     {
386         if( pack_name.length() > 0 )
387         {
388             typeName = ScopedName.unPseudoName( pack_name + "." + name );
389             if( !typeName.startsWith( "org.omg" ) )
390             {
391                 typeName = omg_package_prefix + typeName;
392             }
393             pack_name = typeName.substring( 0, typeName.lastIndexOf( "." ) );
394         }
395         else
396             typeName = ScopedName.unPseudoName( name );
397
398         if( logger.isDebugEnabled() )
399             logger.debug( "setPrintPhaseNames: pack_name " +
400                           pack_name + ", name " + name +
401                           " typename " + typeName );
402     }
403
404     public void printIdMethod( PrintWriter JavaDoc ps )
405     {
406         ps.println( "\tpublic static String id()" );
407         ps.println( "\t{" );
408         ps.println( "\t\treturn \"" + id() + "\";" );
409         ps.println( "\t}" );
410     }
411
412     /**
413      * @return this symbol's repository Id
414      */

415
416     public String JavaDoc id()
417     {
418         IdlSymbol enc = enclosing_symbol;
419         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
420         ScopeData sd = null;
421         str_token enctoken = null;
422
423         if( logger.isDebugEnabled() )
424             logger.debug( "Id for name " + name );
425
426         if( _id == null )
427         {
428             do
429             {
430                 if (enc != null)
431                 {
432                     // Get enclosing token and check idMap then, if not in
433
// there, determine prefix manually.
434
enctoken = enc.get_token();
435
436                     if (enc instanceof Scope)
437                     {
438                        sd = ((Scope)enc).getScopeData ();
439                        if (sd == null)
440                        {
441                           org.jacorb.idl.parser.fatal_error
442                           (
443                              "ScopeDate null for " + name + " " +
444                              this.getClass().getName(), null
445                           );
446                        }
447                     }
448
449                     if (sd != null && sd.idMap.get (name) != null)
450                     {
451                         _id = (String JavaDoc)sd.idMap.get (name);
452                         break;
453                     }
454                     // Not had a #pragma prefix, attempt to determine using prefix
455
else
456                     {
457                         // Slightly horrible...this says 'if the current token prefix
458
// is blank' then use the enclosing tokens prefix OR
459
// if the current token has a matching prefix to the parent
460
// then also do this (this prevents:
461
// prefix Foo
462
// module F {
463
// prefix X
464
// interface Y {}
465
// }
466
if (token != null &&
467                             (
468                                "".equals (token.pragma_prefix) ||
469                                enctoken.pragma_prefix.equals (token.pragma_prefix)
470                             ))
471                         {
472                             String JavaDoc enclosingName = enc.name;
473                             // if the enclosing symbol is a module, its name
474
// is a package name and might have been modified
475
// by the -i2jpackage switch. We want its unchanged
476
// name as part of the RepositoryId, however.
477
if( enc instanceof Module )
478                             {
479                                 String JavaDoc enclosingModuleName =
480                                     ((Module)enc).originalModuleName ();
481
482                                 if ( !enclosingModuleName.startsWith ("org"))
483                                 {
484                                     enclosingName = ((Module)enc).originalModuleName ();
485                                 }
486
487                                 // remove leading "_" in repository Ids
488
if( enc.isEscaped ())
489                                 {
490                                     enclosingName = enclosingName.substring (1);
491                                 }
492                             }
493                             sb.insert (0, enclosingName + "/");
494                             enc = enc.getEnclosingSymbol ();
495                         }
496                         else
497                         {
498                             break;
499                         }
500                     }
501                 }
502                 // Global Scope
503
else if (parser.scopes.size () == 1 &&
504                          parser.currentScopeData ().idMap.get (name) != null)
505                 {
506                     _id = (String JavaDoc)parser.currentScopeData ().idMap.get (name);
507                     break;
508                 }
509                 else
510                 {
511                     // This is global scope - there is no enclosing symbol and no
512
// defining #pragma. The ID can be built simply from the name
513
break;
514                 }
515             }
516             while (enc != null);
517
518             if (_id == null)
519             {
520                 // There was no #pragma.
521
if( isEscaped() )
522                 {
523                     sb.append( name.substring( 1 ) );
524                 }
525                 else
526                 {
527                     sb.append( name );
528                 }
529                 if( token != null && token.pragma_prefix.length() > 0 )
530                 {
531                     _id =
532                     (
533                         "IDL:" + token.pragma_prefix +
534                         "/" + sb.toString().replace( '.', '/' ) + ":" + version ()
535                     );
536                 }
537                 else
538                 {
539                     _id = "IDL:" + sb.toString().replace( '.', '/' ) + ":" + version();
540                 }
541             }
542         }
543         if( logger.isDebugEnabled() )
544             logger.debug( "Id for name " + name + " is " + _id );
545         return _id;
546     }
547
548     private String JavaDoc version()
549     {
550         IdlSymbol enc = this;
551         String JavaDoc tmp;
552
553         if( _version == null )
554         {
555             while( true )
556             {
557                 while( enc != null && !( enc instanceof Scope ) )
558                 {
559                     enc = enc.getEnclosingSymbol();
560                 }
561                 if( enc != null )
562                 {
563                     ScopeData sd = ( (Scope)enc ).getScopeData();
564                     if( sd == null )
565                     {
566                         org.jacorb.idl.parser.fatal_error( "ScopeData null for " + name + " " +
567                                 this.getClass().getName(), null );
568                     }
569                     Hashtable h = sd.versionMap;
570
571                     // check for version settings in this scope
572
tmp = (String JavaDoc)h.get( name );
573                     if( tmp != null )
574                     {
575                         _version = tmp;
576                         break;
577                     }
578                     enc = enc.getEnclosingSymbol();
579                 }
580                 // Global Scope
581
else if (parser.scopes.size () == 1 &&
582                          parser.currentScopeData ().versionMap.get (name) != null)
583                 {
584                     _version = (String JavaDoc)parser.currentScopeData ().versionMap.get (name);
585                     break;
586                 }
587                 else
588                 {
589                     _version = "1.0";
590                     break;
591                 }
592             }
593
594             // check for further versions (which would be an error!)
595

596             if( enc != null )
597                 enc = enc.getEnclosingSymbol();
598
599             while( true )
600             {
601                 while( enc != null && !( enc instanceof Scope ) )
602                 {
603                     enc = enc.getEnclosingSymbol();
604                 }
605                 if( enc != null )
606                 {
607                     // check for version settings in this scope
608
Hashtable h = ( (Scope)enc ).getScopeData().versionMap;
609                     tmp = (String JavaDoc)h.get( name );
610
611                     if( tmp != null )
612                     {
613                         lexer.emit_error( "Version for " + name +
614                                 " already declared!", enc.get_token() );
615                         break;
616                     }
617                     else
618                         enc = enc.getEnclosingSymbol();
619                 }
620                 else
621                 {
622                     break;
623                 }
624             }
625         }
626         return _version;
627     }
628
629
630     /**
631      * access to parser state (e.g. options)
632      */

633
634     protected boolean generateIncluded()
635     {
636         return parser.generateIncluded() && !( inhibitionFlag );
637     }
638
639     /**
640      * let the visitor pattern do its work...
641      */

642
643     public void accept( IDLTreeVisitor visitor )
644     {
645         // nothing here, all work done in subclasses.
646
}
647 }
648
Popular Tags