KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
24  * @author Gerald Brose
25  * @version $Id: TypeMap.java,v 1.21 2004/05/06 12:39:59 nicolas Exp $
26  */

27
28 import java.util.*;
29
30 class TypeMap
31 {
32     static Hashtable typemap = new Hashtable( 5000 );
33
34     public static void init()
35     {
36         typemap.clear();
37         typemap.put( "org.omg.CORBA.Object", new ObjectTypeSpec( IdlSymbol.new_num() ) );
38         typemap.put( "org.omg.CORBA.TypeCode", new TypeCodeTypeSpec( IdlSymbol.new_num() ) );
39         typemap.put( "CORBA.Object", new ObjectTypeSpec( IdlSymbol.new_num() ) );
40         typemap.put( "CORBA.TypeCode", new TypeCodeTypeSpec( IdlSymbol.new_num() ) );
41     }
42
43     // return the type spec associated with a name, if any
44

45     public static TypeSpec map( String JavaDoc name )
46     {
47         return (TypeSpec)typemap.get( name );
48     }
49
50     /**
51      * define a new name for a type spec
52      */

53
54     public static void typedef( String JavaDoc name, TypeSpec type )
55         throws NameAlreadyDefined
56     {
57         if( parser.getLogger().isInfoEnabled() )
58             parser.getLogger().info( "Typedef'ing " + name +
59                                           " , hash: " + type.hashCode() );
60
61         if( typemap.containsKey( name ) )
62         {
63             // actually throw new NameAlreadyDefined()
64
// but we get better error messages if we leave
65
// this to later stages
66
if( parser.getLogger().isDebugEnabled() )
67                 parser.getLogger().info( "Typedef'ing " + name +
68                                          " already in type map!" );
69         }
70         else
71         {
72             if( type.typeSpec() instanceof ScopedName )
73             {
74                 if( ( (ScopedName)type.typeSpec() ).resolvedTypeSpec() != null )
75                     typemap.put( name, ( (ScopedName)type.typeSpec() ).resolvedTypeSpec() );
76                 else
77                     typemap.put( name, type.typeSpec() );
78
79                 if( parser.getLogger().isInfoEnabled() )
80                     parser.getLogger().info(" resolved " +
81                                                  ((ScopedName)type.typeSpec()).resolvedTypeSpec());
82             }
83             else
84             {
85                 typemap.put( name, type.typeSpec() );
86
87                 if( parser.getLogger().isInfoEnabled() )
88                     parser.getLogger().info( " (not a resolved scoped name) "
89                                                   + type.typeSpec().full_name() );
90             }
91         }
92     }
93
94
95     /**
96      * remove the definition of a type with a give name, used when
97      * inherited definitions are overwritten, called from NameTable only!
98      */

99
100     static void removeDefinition( String JavaDoc name )
101     {
102        if( typemap.containsKey( name ) )
103        {
104            typemap.remove( name );
105        }
106        else
107            throw new RuntimeException JavaDoc( "Could not find definition of : " + name );
108     }
109
110
111     public static void replaceForwardDeclaration( String JavaDoc name,
112                                                   TypeSpec type )
113     {
114         if( typemap.containsKey( name ) )
115         {
116             typemap.remove( name );
117             try
118             {
119                 typedef( name, type );
120             }
121             catch( NameAlreadyDefined nad )
122             {
123                 // serious error, should never happen
124
parser.getLogger().debug("TypeMap.replaceForwardDeclaration, serious error!", nad);
125                 parser.fatal_error( "TypeMap.replaceForwardDeclaration, serious error!", null );
126             }
127         }
128         else
129             throw new RuntimeException JavaDoc( "Could not find forward declaration!" );
130     }
131
132
133 }
134
Popular Tags