KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jacorb.idl;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23
24 import java.io.File JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26
27 import java.util.*;
28
29 /**
30  * @author Gerald Brose
31  * @version $Id: ValueBoxDecl.java,v 1.29 2005/03/08 11:49:36 andre.spiegel Exp $
32  */

33
34 public class ValueBoxDecl
35     extends Value
36 {
37     private boolean written = false;
38     private boolean parsed = false;
39
40     TypeSpec typeSpec;
41
42     public ValueBoxDecl(int num)
43     {
44         super(num);
45         pack_name = "";
46     }
47
48     public Object JavaDoc clone()
49     {
50         return null;
51     }
52
53     public TypeDeclaration declaration()
54     {
55         return this;
56     };
57
58     public String JavaDoc typeName()
59     {
60         if (typeName == null)
61             setPrintPhaseNames();
62
63         if (typeSpec.typeSpec() instanceof BaseType)
64             return typeName;
65         else
66             return typeSpec.typeSpec().typeName();
67     }
68
69     public String JavaDoc boxTypeName()
70     {
71         if (typeName == null)
72             setPrintPhaseNames();
73         return typeName;
74     }
75
76     public boolean basic()
77     {
78         return false;
79     }
80
81
82     public void set_included(boolean i)
83     {
84         included = i;
85     }
86
87
88     public void setPackage(String JavaDoc s)
89     {
90         s = parser.pack_replace(s);
91         if (pack_name.length() > 0)
92             pack_name = s + "." + pack_name;
93         else
94             pack_name = s;
95
96         typeSpec.setPackage(s);
97     }
98
99     public void setEnclosingSymbol(IdlSymbol s)
100     {
101         if (enclosing_symbol != null && enclosing_symbol != s)
102         {
103             logger.error("was " + enclosing_symbol.getClass().getName() +
104                                 " now: " + s.getClass().getName());
105             throw new RuntimeException JavaDoc("Compiler Error: trying to reassign container for " + name);
106         }
107         enclosing_symbol = s;
108     }
109
110
111     public String JavaDoc toString()
112     {
113         return typeName();
114     }
115
116
117     public void parse()
118     {
119         if (parsed)
120             throw new RuntimeException JavaDoc("Compiler error: Value box already parsed!");
121
122         escapeName();
123
124         typeSpec.parse();
125
126         try
127         {
128             ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
129             ctspec.c_type_spec = this;
130
131             NameTable.define(full_name(), "type");
132             TypeMap.typedef(full_name(), ctspec);
133         }
134         catch (NameAlreadyDefined nad)
135         {
136             parser.error("Value box " + typeName() + " already defined", token);
137         }
138
139         parsed = true;
140     }
141
142
143     public String JavaDoc className()
144     {
145         String JavaDoc fullName = typeName();
146         if (fullName.indexOf('.') > 0)
147         {
148             return fullName.substring(fullName.lastIndexOf('.') + 1);
149         }
150         else
151         {
152             return fullName;
153         }
154     }
155
156     public String JavaDoc printReadExpression(String JavaDoc streamname)
157     {
158         return "(" + typeName() + ")((org.omg.CORBA_2_3.portable.InputStream)" + streamname + ").read_value (new " + helperName() + "())";
159     }
160
161     public String JavaDoc printWriteStatement(String JavaDoc var_name, String JavaDoc streamname)
162     {
163         return "((org.omg.CORBA_2_3.portable.OutputStream)" + streamname + ").write_value (" + var_name + ", new " + helperName() + "());";
164     }
165
166     public String JavaDoc holderName()
167     {
168         return boxTypeName() + "Holder";
169     }
170
171     public String JavaDoc helperName()
172     {
173         return boxTypeName() + "Helper";
174     }
175
176     /**
177      * @return a string for an expression of type TypeCode that
178      * describes this type
179      *
180      */

181
182     public String JavaDoc getTypeCodeExpression(Set knownTypes)
183     {
184         if (knownTypes.contains(this))
185         {
186             return this.getRecursiveTypeCodeExpression();
187         }
188         else
189         {
190             knownTypes.add(this);
191             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
192             String JavaDoc className = boxTypeName();
193             if (className.indexOf('.') > 0)
194                 className = className.substring(className.lastIndexOf('.') + 1);
195             sb.append("org.omg.CORBA.ORB.init().create_value_box_tc(" +
196                        helperName() + ".id(),\"" + className + "\"," +
197                        typeSpec.typeSpec().getTypeCodeExpression() + ")");
198
199             return sb.toString();
200         }
201     }
202
203     public String JavaDoc getTypeCodeExpression()
204     {
205         return this.getTypeCodeExpression(new HashSet());
206     }
207
208     private void printHolderClass(String JavaDoc className, PrintWriter JavaDoc ps)
209     {
210         if (Environment.JAVA14 && pack_name.equals(""))
211             lexer.emit_warn
212                 ("No package defined for " + className + " - illegal in JDK1.4", token);
213         if (!pack_name.equals(""))
214             ps.println("package " + pack_name + ";");
215
216         ps.println("public" + parser.getFinalString() + " class " + className + "Holder");
217         ps.println("\timplements org.omg.CORBA.portable.Streamable");
218         ps.println("{");
219
220         ps.println("\tpublic " + typeName() + " value;\n");
221
222         ps.println("\tpublic " + className + "Holder ()");
223         ps.println("\t{");
224         ps.println("\t}");
225
226         ps.println("\tpublic " + className + "Holder (final " + typeName() + " initial)");
227         ps.println("\t{");
228         ps.println("\t\tvalue = initial;");
229         ps.println("\t}");
230
231         ps.println("\tpublic org.omg.CORBA.TypeCode _type ()");
232         ps.println("\t{");
233         ps.println("\t\treturn " + helperName() + ".type ();");
234         ps.println("\t}");
235
236         ps.println("\tpublic void _read (final org.omg.CORBA.portable.InputStream _in)");
237         ps.println("\t{");
238         ps.println("\t\tvalue = " + helperName() + ".read (_in);");
239         ps.println("\t}");
240
241         ps.println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream _out)");
242         ps.println("\t{");
243         ps.println("\t\t" + helperName() + ".write (_out,value);");
244         ps.println("\t}");
245
246         ps.println("}");
247     }
248
249
250     private void printHelperClass(String JavaDoc className, PrintWriter JavaDoc ps)
251     {
252         if (Environment.JAVA14 && pack_name.equals(""))
253             lexer.emit_warn
254                 ("No package defined for " + className + " - illegal in JDK1.4", token);
255         if (!pack_name.equals(""))
256             ps.println("package " + pack_name + ";");
257
258         ps.println("public" + parser.getFinalString() + " class " + className + "Helper");
259         ps.println("\timplements org.omg.CORBA.portable.BoxedValueHelper");
260         ps.println("{");
261         ps.println("\tprivate static org.omg.CORBA.TypeCode _type = " + getTypeCodeExpression() + ";");
262
263         String JavaDoc type = typeName();
264
265         TypeSpec.printHelperClassMethods( ps, type);
266
267         printIdMethod(ps); // inherited from IdlSymbol
268

269         /* read */
270         ps.println("\tpublic static " + type +
271                     " read (final org.omg.CORBA.portable.InputStream in)");
272         ps.println("\t{");
273
274         if (typeSpec.typeSpec() instanceof BaseType)
275         {
276             ps.println("\t\t" + type + " result = new " + type +
277                         "(" + typeSpec.typeSpec().printReadExpression("in") + ");");
278         }
279         else
280         {
281             ps.println("\t\t" + type + " result;");
282             ps.println("\t\t" + typeSpec.typeSpec().printReadStatement("result", "in"));
283         }
284         ps.println("\t\treturn result;");
285         ps.println("\t}");
286
287         /* write */
288         ps.println("\tpublic static void write (final org.omg.CORBA.portable.OutputStream out, final " + type + " s)");
289         ps.println("\t{");
290         if (typeSpec.typeSpec() instanceof BaseType)
291         {
292             ps.println("\t\t" + typeSpec.typeSpec().printWriteStatement("s.value", "out"));
293         }
294         else
295             ps.println("\t\t" + typeSpec.typeSpec().printWriteStatement("s", "out"));
296         ps.println("\t}");
297
298         ps.println("\tpublic java.io.Serializable read_value (final org.omg.CORBA.portable.InputStream is)");
299         ps.println("\t{");
300         ps.println("\t\treturn " + helperName() + ".read (is);");
301         ps.println("\t}");
302
303         ps.println("\tpublic void write_value (final org.omg.CORBA.portable.OutputStream os, final java.io.Serializable value)");
304         ps.println("\t{");
305         ps.println("\t\t" + helperName() + ".write (os, (" + type + ")value);");
306         ps.println("\t}");
307
308         ps.println("\tpublic java.lang.String get_id()");
309         ps.println("\t{");
310         ps.println("\t\treturn " + helperName() + ".id();");
311         ps.println("\t}");
312         ps.println("}");
313     }
314
315     private void printValueClass(String JavaDoc className, PrintWriter JavaDoc ps)
316     {
317         String JavaDoc fullClassName = className;
318
319         if (Environment.JAVA14 && pack_name.equals(""))
320             lexer.emit_warn
321                 ("No package defined for " + className + " - illegal in JDK1.4", token);
322         if (!pack_name.equals(""))
323         {
324             fullClassName = pack_name + "." + className;
325
326             ps.println("package " + pack_name + ";");
327         }
328
329         ps.println("public class " + className);
330         ps.println("\timplements org.omg.CORBA.portable.ValueBase");
331         ps.println("{");
332         ps.println("\tpublic " + typeSpec.typeName() + " value;");
333         ps.println("\tprivate static String[] _ids = { " + className + "Helper.id() };");
334
335         ps.println("\tpublic " + className + "(" + typeSpec.typeName() + " initial)");
336         ps.println("\t{");
337         ps.println("\t\tvalue = initial;");
338         ps.println("\t}");
339
340         ps.println("\tpublic String[] _truncatable_ids()");
341         ps.println("\t{");
342         ps.println("\t\treturn _ids;");
343         ps.println("\t}");
344
345         ps.println("}");
346     }
347
348     /** generate required classes */
349
350     public void print(PrintWriter JavaDoc ps)
351     {
352         setPrintPhaseNames();
353
354         // no code generation for included definitions
355
if (included && !generateIncluded())
356             return;
357
358         // only write once
359

360         if (!written)
361         {
362             try
363             {
364                 String JavaDoc className = boxTypeName();
365                 if (className.indexOf('.') > 0)
366                     className = className.substring(className.lastIndexOf('.') + 1);
367
368                 String JavaDoc path = parser.out_dir + fileSeparator +
369                     pack_name.replace('.', fileSeparator);
370
371                 File JavaDoc dir = new File JavaDoc(path);
372                 if (!dir.exists())
373                     if (!dir.mkdirs())
374                     {
375                         org.jacorb.idl.parser.fatal_error("Unable to create " + path, null);
376                     }
377
378                 // print the mapped java class
379
PrintWriter JavaDoc decl_ps;
380                 String JavaDoc fname = className + ".java";
381                 File JavaDoc f = new File JavaDoc(dir, fname);
382
383                 if (typeSpec.typeSpec() instanceof BaseType)
384                 {
385
386                     if (GlobalInputStream.isMoreRecentThan(f))
387                     {
388                         decl_ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
389                         printValueClass(className, decl_ps);
390                         decl_ps.close();
391                     }
392                 }
393
394                 // print the holder class */
395

396                 fname = className + "Holder.java";
397                 f = new File JavaDoc(dir, fname);
398
399                 if (GlobalInputStream.isMoreRecentThan(f))
400                 {
401                     decl_ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
402                     printHolderClass(className, decl_ps);
403                     decl_ps.close();
404                 }
405
406                 // print the helper class
407

408                 fname = className + "Helper.java";
409                 f = new File JavaDoc(dir, fname);
410
411                 if (GlobalInputStream.isMoreRecentThan(f))
412                 {
413                     decl_ps = new PrintWriter JavaDoc(new java.io.FileWriter JavaDoc(f));
414                     printHelperClass(className, decl_ps);
415                     decl_ps.close();
416                 }
417
418                 written = true;
419             }
420             catch (java.io.IOException JavaDoc i)
421             {
422                 throw new RuntimeException JavaDoc("File IO error" + i);
423             }
424         }
425     }
426
427     public void accept(IDLTreeVisitor visitor)
428     {
429         visitor.visitValue(this);
430     }
431 }
432
Popular Tags