KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JacORB IDL compiler classes
25  *
26  * @author Gerald Brose
27  * @version $Id: Module.java,v 1.17 2004/05/06 12:39:58 nicolas Exp $
28  */

29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33
34 /**
35  * Note: a module's name is its package name!
36  */

37
38 public class Module
39     extends Declaration
40     implements Scope
41 {
42     public Definitions spec;
43
44     private ScopeData scopeData;
45     private String JavaDoc unreplacedName = null;
46
47     public Module(int num)
48     {
49         super(num);
50         pack_name = "";
51     }
52
53     public void setScopeData(ScopeData data)
54     {
55         scopeData = data;
56     }
57
58     public ScopeData getScopeData()
59     {
60         return scopeData;
61     }
62
63     public void setPackage(String JavaDoc s)
64     {
65         if (unreplacedName == null)
66             unreplacedName = s;
67         s = parser.pack_replace(s);
68
69         if (pack_name.length() > 0)
70         {
71             pack_name = s + "." + pack_name;
72             spec.setPackage(s);
73         }
74         else
75         {
76             pack_name = s;
77
78             if (lexer.needsJavaEscape(this))
79                 pack_name = "_" + s;
80
81             name = pack_name;
82             spec.setPackage(pack_name);
83         }
84     }
85
86     String JavaDoc full_name()
87     {
88         return pack_name;
89     }
90
91     public void set_included(boolean i)
92     {
93         included = i;
94         spec.set_included(i);
95     }
96
97     public void setEnclosingSymbol(IdlSymbol s)
98     {
99         if (enclosing_symbol != null && enclosing_symbol != s)
100             throw new RuntimeException JavaDoc("Compiler Error: trying to reassign container for " + name);
101         enclosing_symbol = s;
102         spec.setEnclosingSymbol(this);
103     }
104
105     public void parse()
106     {
107         try
108         {
109             NameTable.define(full_name(), "module");
110         }
111         catch (NameAlreadyDefined nad)
112         {
113             parser.error("Module name " + full_name() + " already defined", token);
114         }
115         spec.parse();
116     }
117
118     public void print(PrintWriter JavaDoc ps)
119     {
120         if (parser.generateIR)
121         {
122             try
123             {
124                 // Java Interface file
125

126                 String JavaDoc path =
127                     parser.out_dir + fileSeparator + pack_name.replace('.', fileSeparator);
128                 File JavaDoc dir = new File JavaDoc(path);
129                 if (!dir.exists())
130                 {
131                     if (!dir.mkdirs())
132                     {
133                         org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
134                     }
135                 }
136
137                 File JavaDoc f = new File JavaDoc(dir, "_" + name + "Module.java");
138                 if (GlobalInputStream.isMoreRecentThan(f))
139                 {
140
141                     PrintWriter JavaDoc moduleWriter = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f ));
142                     moduleWriter.println("package " + pack_name + ";\n");
143                     moduleWriter.println("/** \n * IR module information, generated by the JacORB IDL compiler \n */");
144                     moduleWriter.println("public class _" + name + "Module {}");
145                     moduleWriter.close();
146                 }
147             }
148             catch (IOException JavaDoc io)
149             {
150                 if (logger.isWarnEnabled())
151                     logger.warn("Exception: ", io);
152             }
153         }
154         spec.print(ps);
155     }
156
157     /**
158      * @return the original, unreplaced module name
159      * (needed to build a repositoryID that is untouched by
160      * the compiler option -i2jpackage
161      */

162
163     public String JavaDoc originalModuleName()
164     {
165         return unreplacedName;
166     }
167
168     public Definitions getDefinitions()
169     {
170         return spec;
171     }
172
173     /**
174      */

175
176     public void accept(IDLTreeVisitor visitor)
177     {
178         visitor.visitModule(this);
179     }
180
181
182 }
183
Popular Tags