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: * @author Gerald Brose
025: * @version $Id: ConstDecl.java,v 1.31 2006/06/20 10:52:57 alphonse.bendt Exp $
026: */
027:
028: import java.io.File;
029: import java.io.PrintWriter;
030: import java.util.*;
031:
032: public class ConstDecl extends Declaration {
033: private static Hashtable values = new Hashtable();
034: private static Hashtable declarations = new Hashtable();
035:
036: private ScopedName t = new ScopedName(new_num());
037: private int pos_int_const = 0;
038: private boolean int_const_set = false;
039:
040: public ConstExpr const_expr;
041: public ConstType const_type;
042:
043: public ConstDecl(int num) {
044: super (num);
045: }
046:
047: public static void init() {
048: values.clear();
049: declarations.clear();
050: }
051:
052: public static String namedValue(ScopedName sn) {
053: String resolvedName = sn.resolvedName();
054: if (values.containsKey(resolvedName)) {
055: return (String) values.get(resolvedName);
056: }
057: return resolvedName;
058: }
059:
060: public void setPackage(String s) {
061: s = parser.pack_replace(s);
062: super .setPackage(s);
063: const_type.setPackage(s);
064: const_expr.setPackage(s);
065: t.typeName = name;
066: t.setPackage(s);
067: }
068:
069: public void parse() {
070: const_expr.setDeclaration(this );
071: try {
072: NameTable.define(full_name(), "constant");
073: } catch (NameAlreadyDefined p) {
074: parser.error(
075: "Constant " + full_name() + " already defined",
076: token);
077: }
078: const_type.parse();
079: const_expr.parse();
080: t.typeName = name;
081: values.put(t.resolvedName() + (contained() ? "" : ".value"),
082: const_expr.value());
083:
084: if (logger.isDebugEnabled()) {
085: logger.debug("ConstDecl.parse, put value: "
086: + t.resolvedName() + (contained() ? "" : ".value")
087: + " , " + const_expr.value());
088: }
089:
090: declarations.put(t.resolvedName(), this );
091: }
092:
093: static ConstDecl getDeclaration(String resolvedName) {
094: return (ConstDecl) declarations.get(resolvedName);
095: }
096:
097: int pos_int_const() {
098: if (!int_const_set) {
099: pos_int_const = const_expr.pos_int_const();
100: int_const_set = true;
101: }
102: return pos_int_const;
103: }
104:
105: /**
106: * prints a constant declaration as part of an enclosing
107: * interface
108: */
109:
110: public void printContained(PrintWriter ps) {
111: ps.print("\t" + const_type + " " + name + " = ");
112: ps.print(getValue());
113: ps.println(";");
114: }
115:
116: boolean contained() {
117:
118: boolean result = false;
119: IdlSymbol enc = getEnclosingSymbol();
120:
121: while (enc != null) {
122: if (enc instanceof Interface) {
123: result = true;
124: break;
125: }
126: enc = enc.getEnclosingSymbol();
127: }
128: if (logger.isDebugEnabled())
129: logger.debug("ConstDecl.contained()? " + full_name()
130: + " returns " + result);
131: return result;
132: }
133:
134: /** prints a constant declaration outside of an enclosing interface
135: * into a separate interface
136: */
137:
138: public void print(PrintWriter ps) {
139: if (contained() || (included && !generateIncluded()))
140: return;
141:
142: try {
143: String fullName = ScopedName.unPseudoName(full_name());
144: String className;
145: if (fullName.indexOf('.') > 0) {
146: pack_name = fullName.substring(0, fullName
147: .lastIndexOf('.'));
148: className = fullName.substring(fullName
149: .lastIndexOf('.') + 1);
150: } else {
151: pack_name = "";
152: className = fullName;
153: }
154:
155: String path = parser.out_dir + fileSeparator
156: + pack_name.replace('.', fileSeparator);
157: File dir = new File(path);
158: if (!dir.exists()) {
159: if (!dir.mkdirs()) {
160: org.jacorb.idl.parser.fatal_error(
161: "Unable to create " + path, null);
162: }
163: }
164:
165: String fname = className + ".java";
166: File f = new File(dir, fname);
167:
168: if (GlobalInputStream.isMoreRecentThan(f)) {
169: PrintWriter pw = new PrintWriter(
170: new java.io.FileWriter(f));
171:
172: if (logger.isDebugEnabled())
173: logger.debug("ConstDecl.print " + fname);
174:
175: if (Environment.JAVA14 && pack_name.equals(""))
176: lexer
177: .emit_warn("No package defined for "
178: + className
179: + " - illegal in JDK1.4", token);
180: if (!pack_name.equals(""))
181: pw.println("package " + pack_name + ";");
182:
183: printClassComment("const", className, pw);
184:
185: pw.println("public interface " + className);
186: pw.println("{");
187:
188: pw.print("\t" + const_type.toString() + " value = ");
189:
190: pw.print(getValue());
191: pw.println(";");
192:
193: pw.println("}");
194: pw.close();
195: }
196: } catch (java.io.IOException i) {
197: throw new RuntimeException("File IO error" + i);
198: }
199: }
200:
201: private String getValue() {
202: TypeSpec ts = const_type.symbol.typeSpec();
203: while (ts instanceof AliasTypeSpec) {
204: ts = ((AliasTypeSpec) ts).originalType();
205: }
206:
207: if (logger.isDebugEnabled()) {
208: logger.debug("ConstDecl(" + name + ": " + ts.getClass()
209: + ") = " + const_type.toString());
210: }
211:
212: if (ts instanceof ShortType) {
213: // short constant values have to be cast explicitly
214: return ("(short)(" + const_expr.toString() + ")");
215: } else if (ts instanceof FloatType) {
216: // float constant values have to be cast explicitly
217: return ("(float)(" + const_expr.toString() + ")");
218: } else if (ts instanceof OctetType) {
219: // byte constant values have to be cast explicitly
220: return ("(byte)(" + const_expr.toString() + ")");
221: } else if (ts instanceof FixedPointConstType
222: || ts instanceof FixedPointType) {
223: return ("new java.math.BigDecimal ("
224: + const_expr.toString() + ")");
225: } else {
226: return const_expr.toString();
227: }
228: }
229: }
|