001: /*
002: * JacORB - a free Java ORB
003: *
004: * Copyright (C) 1997-2004 Gerald Brose.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Library General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Library General Public License for more details.
015: *
016: * You should have received a copy of the GNU Library General Public
017: * License along with this library; if not, write to the Free
018: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
019: */
020:
021: package org.jacorb.idl;
022:
023: /**
024: * JacORB IDL compiler classes
025: *
026: * @author Gerald Brose
027: * @version $Id: Module.java,v 1.19 2006/06/20 09:57:40 alphonse.bendt Exp $
028: */
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.PrintWriter;
033:
034: /**
035: * Note: a module's name is its package name!
036: */
037:
038: public class Module extends Declaration implements Scope {
039: public Definitions spec;
040:
041: private ScopeData scopeData;
042: private String unreplacedName = null;
043:
044: public Module(int num) {
045: super (num);
046: pack_name = "";
047: }
048:
049: public void setScopeData(ScopeData data) {
050: scopeData = data;
051: }
052:
053: public ScopeData getScopeData() {
054: return scopeData;
055: }
056:
057: public void setPackage(String s) {
058: if (unreplacedName == null)
059: unreplacedName = s;
060: s = parser.pack_replace(s);
061:
062: if (pack_name.length() > 0) {
063: pack_name = s + "." + pack_name;
064: spec.setPackage(s);
065: } else {
066: pack_name = s;
067:
068: if (lexer.needsJavaEscape(this ))
069: pack_name = "_" + s;
070:
071: name = pack_name;
072: spec.setPackage(pack_name);
073: }
074: }
075:
076: String full_name() {
077: return pack_name;
078: }
079:
080: public void set_included(boolean i) {
081: included = i;
082: spec.set_included(i);
083: }
084:
085: public void setEnclosingSymbol(IdlSymbol s) {
086: if (enclosing_symbol != null && enclosing_symbol != s)
087: throw new RuntimeException(
088: "Compiler Error: trying to reassign container for "
089: + name);
090: enclosing_symbol = s;
091: spec.setEnclosingSymbol(this );
092: }
093:
094: public void parse() {
095: try {
096: NameTable.define(full_name(), "module");
097: } catch (NameAlreadyDefined nad) {
098: parser.error("Module name " + full_name()
099: + " already defined", token);
100: }
101: spec.parse();
102: }
103:
104: public void print(PrintWriter ps) {
105: if (parser.generateIR) {
106: try {
107: // Java Interface file
108:
109: String path = parser.out_dir + fileSeparator
110: + pack_name.replace('.', fileSeparator);
111: File dir = new File(path);
112: if (!dir.exists()) {
113: if (!dir.mkdirs()) {
114: org.jacorb.idl.parser.fatal_error(
115: "Unable to create " + path, null);
116: }
117: }
118:
119: File f = new File(dir, "_" + originalModuleName()
120: + "Module.java");
121: if (GlobalInputStream.isMoreRecentThan(f)) {
122:
123: PrintWriter moduleWriter = new PrintWriter(
124: new java.io.FileWriter(f));
125: moduleWriter
126: .println("package " + pack_name + ";\n");
127: moduleWriter
128: .println("/** \n * IR module information, generated by the JacORB IDL compiler \n */");
129: moduleWriter.println("public class _"
130: + originalModuleName() + "Module {}");
131: moduleWriter.close();
132: }
133: } catch (IOException io) {
134: logger.warn("Exception: ", io);
135: }
136: }
137: spec.print(ps);
138: }
139:
140: /**
141: * @return the original, unreplaced module name
142: * (needed to build a repositoryID that is untouched by
143: * the compiler option -i2jpackage
144: */
145:
146: public String originalModuleName() {
147: return unreplacedName;
148: }
149:
150: public Definitions getDefinitions() {
151: return spec;
152: }
153:
154: /**
155: */
156:
157: public void accept(IDLTreeVisitor visitor) {
158: visitor.visitModule(this );
159: }
160:
161: public String toString() {
162: return "module " + name;
163: }
164: }
|