KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
25  * @author Gerald Brose
26  * @version $Id: ConstDecl.java,v 1.28 2004/05/06 12:39:58 nicolas Exp $
27  */

28
29 import java.io.File JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.*;
32
33 class ConstDecl extends Declaration
34 {
35     private static Hashtable values = new Hashtable();
36     private static Hashtable declarations = new Hashtable();
37
38     private ScopedName t = new ScopedName(new_num());
39     private int pos_int_const = 0;
40     private boolean int_const_set = false;
41
42     public ConstExpr const_expr;
43     public ConstType const_type;
44
45     public ConstDecl(int num)
46     {
47         super(num);
48     }
49
50     public static void init()
51     {
52         values.clear();
53         declarations.clear();
54     }
55
56     public static String JavaDoc namedValue(ScopedName sn)
57     {
58         String JavaDoc resolvedName = sn.resolvedName();
59         if (values.containsKey(resolvedName))
60         {
61             return (String JavaDoc)values.get(resolvedName);
62         }
63         else
64         {
65             return resolvedName;
66         }
67     }
68
69     public void setPackage(String JavaDoc s)
70     {
71         s = parser.pack_replace(s);
72         super.setPackage(s);
73         const_type.setPackage(s);
74         const_expr.setPackage(s);
75         t.typeName = name;
76         t.setPackage(s);
77     }
78
79     public void parse()
80     {
81         const_expr.setDeclaration(this);
82         try
83         {
84             NameTable.define(full_name(), "constant");
85         }
86         catch (NameAlreadyDefined p)
87         {
88             parser.error("Constant " + full_name() +
89                          " already defined", token);
90         }
91         const_type.parse();
92         const_expr.parse();
93         t.typeName = name;
94         values.put(t.resolvedName() +
95                    (contained() ? "" : ".value"),
96                    const_expr.value());
97
98         if (logger.isDebugEnabled())
99         {
100             logger.debug("ConstDecl.parse, put value: " + t.resolvedName() +
101                          (contained() ? "" : ".value") + " , " +
102                          const_expr.value());
103         }
104
105         declarations.put(t.resolvedName(), this);
106     }
107
108     static ConstDecl getDeclaration(String JavaDoc resolvedName)
109     {
110         return (ConstDecl)declarations.get(resolvedName);
111     }
112
113     int pos_int_const()
114     {
115         if (!int_const_set)
116         {
117             pos_int_const = const_expr.pos_int_const();
118             int_const_set = true;
119         }
120         return pos_int_const;
121     }
122
123     /**
124      * prints a constant declaration as part of an enclosing
125      * interface
126      */

127
128     public void printContained(PrintWriter JavaDoc ps)
129     {
130         TypeSpec ts = const_type.symbol.typeSpec();
131         boolean alias = ts instanceof AliasTypeSpec;
132
133         if (ts instanceof AliasTypeSpec)
134         {
135             ts = ((AliasTypeSpec)ts).originalType();
136         }
137
138         ps.print("\t" + const_type + " " + name + " = ");
139         if (ts instanceof IntType && ((IntType)ts).type_spec instanceof ShortType)
140         {
141             // short constant values have to be cast explicitly
142
ps.print("(short)(");
143             const_expr.print(ps);
144             ps.println(");");
145         }
146         else if (ts instanceof LongLongType)
147         {
148             ps.print (const_expr.toString () + ';');
149         }
150         else if (ts instanceof FloatType)
151         {
152             // float constant values have to be cast explicitly
153
ps.print("(float)(");
154             const_expr.print(ps);
155             ps.println(");");
156         }
157         else if (ts instanceof FixedPointConstType)
158         {
159             // fixed point values have to be created explicitly
160
ps.print("new java.math.BigDecimal(");
161             const_expr.print(ps);
162             ps.println(");");
163         }
164         else if (ts instanceof OctetType)
165         {
166             // float constant values have to be cast explicitly
167
ps.print("(byte)(");
168             const_expr.print(ps);
169             ps.println(");");
170         }
171         else
172         {
173             const_expr.print(ps);
174             ps.println(";");
175         }
176     }
177
178     boolean contained()
179     {
180
181         boolean result = false;
182         IdlSymbol enc = getEnclosingSymbol();
183
184         while(enc != null)
185         {
186             if (enc instanceof Interface)
187             {
188                 result = true;
189                 break;
190             }
191             enc = enc.getEnclosingSymbol();
192         }
193         if (logger.isDebugEnabled())
194             logger.debug("ConstDecl.contained()? " + full_name()
195                          + " returns " + result);
196         return result;
197     }
198
199     /** prints a constant declaration outside of an enclosing interface
200      * into a separate interface
201      */

202
203     public void print(PrintWriter JavaDoc ps)
204     {
205         if (contained() || (included && !generateIncluded()))
206             return;
207
208         try
209         {
210             String JavaDoc fullName = ScopedName.unPseudoName(full_name());
211             String JavaDoc className;
212             if (fullName.indexOf('.') > 0)
213             {
214                 pack_name = fullName.substring(0, fullName.lastIndexOf('.'));
215                 className = fullName.substring(fullName.lastIndexOf('.') + 1);
216             }
217             else
218             {
219                 pack_name = "";
220                 className = fullName;
221             }
222
223             String JavaDoc path = parser.out_dir + fileSeparator +
224                 pack_name.replace('.', fileSeparator);
225             File JavaDoc dir = new File JavaDoc(path);
226             if (!dir.exists())
227             {
228                 if (!dir.mkdirs())
229                 {
230                     org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
231                 }
232             }
233
234             String JavaDoc fname = className + ".java";
235             File JavaDoc f = new File JavaDoc(dir, fname);
236
237             if (GlobalInputStream.isMoreRecentThan(f))
238             {
239                 PrintWriter JavaDoc pw = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
240
241                 if (logger.isDebugEnabled())
242                     logger.debug("ConstDecl.print " + fname);
243
244                 if (Environment.JAVA14 && pack_name.equals(""))
245                     lexer.emit_warn
246                         ("No package defined for " + className + " - illegal in JDK1.4", token);
247                 if (!pack_name.equals(""))
248                     pw.println("package " + pack_name + ";");
249
250                 pw.println("/**");
251                 pw.println(" * Automatically generated from IDL const definition ");
252                 pw.println(" * @author JacORB IDL compiler ");
253                 pw.println(" */\n");
254
255                 pw.println("public interface " + className);
256                 pw.println("{");
257
258                 TypeSpec ts = const_type.symbol.typeSpec();
259                 if (ts instanceof AliasTypeSpec)
260                 {
261                     ts = ((AliasTypeSpec)ts).originalType();
262                 }
263
264                 pw.print("\t" + const_type.toString() + " value = ");
265
266                 if (logger.isWarnEnabled())
267                     logger.warn("ConstDecl, ts " +
268                                 const_type.toString() + " " + ts.getClass());
269
270                 if (ts instanceof ShortType)
271                 {
272                     // short constant values have to be cast explicitly
273
pw.print("(short)(" + const_expr.toString() + ");");
274                 }
275                 else if (ts instanceof LongLongType)
276                 {
277                     pw.print (const_expr.toString () + ';');
278                 }
279                 else if (ts instanceof FloatType)
280                 {
281                     // float constant values have to be cast explicitly
282
pw.println("(float)(" + const_expr.toString() + ");");
283                 }
284                 else if (ts instanceof OctetType)
285                 {
286                     // float constant values have to be cast explicitly
287
pw.println("(byte)(" + const_expr.toString() + ");");
288                 }
289                 else if (ts instanceof FixedPointConstType ||
290                          ts instanceof FixedPointType)
291                 {
292                     pw.println("new java.math.BigDecimal (" + const_expr.toString() + ");");
293                 }
294                 else
295                 {
296                     pw.println(const_expr.toString() + ";");
297                 }
298                 pw.println("}");
299                 pw.close();
300             }
301         }
302         catch (java.io.IOException JavaDoc i)
303         {
304             throw new RuntimeException JavaDoc("File IO error" + i);
305         }
306     }
307 }
308
Popular Tags