001: package org.jacorb.idl;
002:
003: /*
004: * JacORB - a free Java ORB
005: *
006: * Copyright (C) 1997-2004 Gerald Brose.
007: *
008: * This library is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * This library is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * Library General Public License for more details.
017: *
018: * You should have received a copy of the GNU Library General Public
019: * License along with this library; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: */
022:
023: import java.io.File;
024: import java.io.PrintWriter;
025:
026: import java.util.*;
027:
028: /**
029: * @author Gerald Brose
030: * @version $Id: ValueBoxDecl.java,v 1.31 2006/07/03 14:12:13 alphonse.bendt Exp $
031: */
032:
033: public class ValueBoxDecl extends Value {
034: private boolean written = false;
035: private boolean parsed = false;
036:
037: TypeSpec typeSpec;
038:
039: public ValueBoxDecl(int num) {
040: super (num);
041: pack_name = "";
042: }
043:
044: public Object clone() {
045: return null;
046: }
047:
048: public TypeDeclaration declaration() {
049: return this ;
050: };
051:
052: public String typeName() {
053: if (typeName == null)
054: setPrintPhaseNames();
055:
056: if (typeSpec.typeSpec() instanceof BaseType)
057: return typeName;
058: else
059: return typeSpec.typeSpec().typeName();
060: }
061:
062: public String boxTypeName() {
063: if (typeName == null)
064: setPrintPhaseNames();
065: return typeName;
066: }
067:
068: public boolean basic() {
069: return false;
070: }
071:
072: public void set_included(boolean i) {
073: included = i;
074: }
075:
076: public void setPackage(String s) {
077: s = parser.pack_replace(s);
078: if (pack_name.length() > 0)
079: pack_name = s + "." + pack_name;
080: else
081: pack_name = s;
082:
083: typeSpec.setPackage(s);
084: }
085:
086: public void setEnclosingSymbol(IdlSymbol s) {
087: if (enclosing_symbol != null && enclosing_symbol != s) {
088: logger.error("was " + enclosing_symbol.getClass().getName()
089: + " now: " + s.getClass().getName());
090: throw new RuntimeException(
091: "Compiler Error: trying to reassign container for "
092: + name);
093: }
094: enclosing_symbol = s;
095: }
096:
097: public String toString() {
098: return typeName();
099: }
100:
101: public void parse() {
102: if (parsed)
103: throw new RuntimeException(
104: "Compiler error: Value box already parsed!");
105:
106: escapeName();
107:
108: typeSpec.parse();
109:
110: try {
111: ConstrTypeSpec ctspec = new ConstrTypeSpec(new_num());
112: ctspec.c_type_spec = this ;
113:
114: NameTable.define(full_name(), "type");
115: TypeMap.typedef(full_name(), ctspec);
116: } catch (NameAlreadyDefined nad) {
117: parser.error(
118: "Value box " + typeName() + " already defined",
119: token);
120: }
121:
122: parsed = true;
123: }
124:
125: public String className() {
126: String fullName = typeName();
127: if (fullName.indexOf('.') > 0) {
128: return fullName.substring(fullName.lastIndexOf('.') + 1);
129: } else {
130: return fullName;
131: }
132: }
133:
134: public String printReadExpression(String streamname) {
135: return "(" + typeName()
136: + ")((org.omg.CORBA_2_3.portable.InputStream)"
137: + streamname + ").read_value (new " + helperName()
138: + "())";
139: }
140:
141: public String printWriteStatement(String var_name, String streamname) {
142: return "((org.omg.CORBA_2_3.portable.OutputStream)"
143: + streamname + ").write_value (" + var_name + ", new "
144: + helperName() + "());";
145: }
146:
147: public String holderName() {
148: return boxTypeName() + "Holder";
149: }
150:
151: public String helperName() {
152: return boxTypeName() + "Helper";
153: }
154:
155: /**
156: * @return a string for an expression of type TypeCode that
157: * describes this type
158: *
159: */
160:
161: public String getTypeCodeExpression(Set knownTypes) {
162: if (knownTypes.contains(this )) {
163: return this .getRecursiveTypeCodeExpression();
164: } else {
165: knownTypes.add(this );
166: StringBuffer sb = new StringBuffer();
167: String className = boxTypeName();
168: if (className.indexOf('.') > 0)
169: className = className.substring(className
170: .lastIndexOf('.') + 1);
171: sb
172: .append("org.omg.CORBA.ORB.init().create_value_box_tc("
173: + helperName()
174: + ".id(),\""
175: + className
176: + "\","
177: + typeSpec.typeSpec()
178: .getTypeCodeExpression() + ")");
179:
180: return sb.toString();
181: }
182: }
183:
184: public String getTypeCodeExpression() {
185: return this .getTypeCodeExpression(new HashSet());
186: }
187:
188: private void printHolderClass(String className, PrintWriter ps) {
189: if (Environment.JAVA14 && pack_name.equals(""))
190: lexer.emit_warn("No package defined for " + className
191: + " - illegal in JDK1.4", token);
192: if (!pack_name.equals(""))
193: ps.println("package " + pack_name + ";");
194:
195: ps.println("public" + parser.getFinalString() + " class "
196: + className + "Holder");
197: ps.println("\timplements org.omg.CORBA.portable.Streamable");
198: ps.println("{");
199:
200: ps.println("\tpublic " + typeName() + " value;\n");
201:
202: ps.println("\tpublic " + className + "Holder ()");
203: ps.println("\t{");
204: ps.println("\t}");
205:
206: ps.println("\tpublic " + className + "Holder (final "
207: + typeName() + " initial)");
208: ps.println("\t{");
209: ps.println("\t\tvalue = initial;");
210: ps.println("\t}");
211:
212: ps.println("\tpublic org.omg.CORBA.TypeCode _type ()");
213: ps.println("\t{");
214: ps.println("\t\treturn " + helperName() + ".type ();");
215: ps.println("\t}");
216:
217: ps
218: .println("\tpublic void _read (final org.omg.CORBA.portable.InputStream _in)");
219: ps.println("\t{");
220: ps.println("\t\tvalue = " + helperName() + ".read (_in);");
221: ps.println("\t}");
222:
223: ps
224: .println("\tpublic void _write (final org.omg.CORBA.portable.OutputStream _out)");
225: ps.println("\t{");
226: ps.println("\t\t" + helperName() + ".write (_out,value);");
227: ps.println("\t}");
228:
229: ps.println("}");
230: }
231:
232: private void printHelperClass(String className, PrintWriter ps) {
233: if (Environment.JAVA14 && pack_name.equals("")) {
234: lexer.emit_warn("No package defined for " + className
235: + " - illegal in JDK1.4", token);
236: }
237: if (!pack_name.equals("")) {
238: ps.println("package " + pack_name + ";");
239: }
240:
241: ps.println("public" + parser.getFinalString() + " class "
242: + className + "Helper");
243: ps
244: .println("\timplements org.omg.CORBA.portable.BoxedValueHelper");
245: ps.println("{");
246: ps.println("\tprivate static org.omg.CORBA.TypeCode _type = "
247: + getTypeCodeExpression() + ";");
248:
249: String type = typeName();
250:
251: ps.println("\tpublic static org.omg.CORBA.TypeCode type()");
252: ps.println("\t{");
253: ps.println("\t\treturn _type;");
254: ps.println("\t}");
255:
256: ps.println();
257:
258: ps
259: .println("\tpublic static void insert (final org.omg.CORBA.Any any, final "
260: + type + " s)");
261: ps.println("\t{");
262: ps.println("\t\tany.insert_Value(s, type());");
263: ps.println("\t}\n");
264:
265: ps.println("\tpublic static " + type
266: + " extract (final org.omg.CORBA.Any any)");
267: ps.println("\t{");
268: ps.println("\t\treturn (" + type + ") any.extract_Value();");
269: ps.println("\t}\n");
270:
271: printIdMethod(ps); // inherited from IdlSymbol
272:
273: /* read */
274: ps
275: .println("\tpublic static "
276: + type
277: + " read (final org.omg.CORBA.portable.InputStream in)");
278: ps.println("\t{");
279:
280: if (typeSpec.typeSpec() instanceof BaseType) {
281: ps.println("\t\t" + type + " result = new " + type + "("
282: + typeSpec.typeSpec().printReadExpression("in")
283: + ");");
284: } else {
285: ps.println("\t\t" + type + " result;");
286: ps.println("\t\t"
287: + typeSpec.typeSpec().printReadStatement("result",
288: "in"));
289: }
290: ps.println("\t\treturn result;");
291: ps.println("\t}");
292:
293: /* write */
294: ps
295: .println("\tpublic static void write (final org.omg.CORBA.portable.OutputStream out, final "
296: + type + " s)");
297: ps.println("\t{");
298: if (typeSpec.typeSpec() instanceof BaseType) {
299: ps.println("\t\t"
300: + typeSpec.typeSpec().printWriteStatement(
301: "s.value", "out"));
302: } else
303: ps.println("\t\t"
304: + typeSpec.typeSpec().printWriteStatement("s",
305: "out"));
306: ps.println("\t}");
307:
308: ps
309: .println("\tpublic java.io.Serializable read_value (final org.omg.CORBA.portable.InputStream is)");
310: ps.println("\t{");
311: ps.println("\t\treturn " + helperName() + ".read (is);");
312: ps.println("\t}");
313:
314: ps
315: .println("\tpublic void write_value (final org.omg.CORBA.portable.OutputStream os, final java.io.Serializable value)");
316: ps.println("\t{");
317: ps.println("\t\t" + helperName() + ".write (os, (" + type
318: + ")value);");
319: ps.println("\t}");
320:
321: ps.println("\tpublic java.lang.String get_id()");
322: ps.println("\t{");
323: ps.println("\t\treturn " + helperName() + ".id();");
324: ps.println("\t}");
325: ps.println("}");
326: }
327:
328: private void printValueClass(String className, PrintWriter ps) {
329: String fullClassName = className;
330:
331: if (Environment.JAVA14 && pack_name.equals(""))
332: lexer.emit_warn("No package defined for " + className
333: + " - illegal in JDK1.4", token);
334: if (!pack_name.equals("")) {
335: fullClassName = pack_name + "." + className;
336:
337: ps.println("package " + pack_name + ";");
338: }
339:
340: ps.println("public class " + className);
341: ps.println("\timplements org.omg.CORBA.portable.ValueBase");
342: ps.println("{");
343: ps.println("\tpublic " + typeSpec.typeName() + " value;");
344: ps.println("\tprivate static String[] _ids = { " + className
345: + "Helper.id() };");
346:
347: ps.println("\tpublic " + className + "(" + typeSpec.typeName()
348: + " initial)");
349: ps.println("\t{");
350: ps.println("\t\tvalue = initial;");
351: ps.println("\t}");
352:
353: ps.println("\tpublic String[] _truncatable_ids()");
354: ps.println("\t{");
355: ps.println("\t\treturn _ids;");
356: ps.println("\t}");
357:
358: ps.println("}");
359: }
360:
361: /** generate required classes */
362:
363: public void print(PrintWriter ps) {
364: setPrintPhaseNames();
365:
366: // no code generation for included definitions
367: if (included && !generateIncluded())
368: return;
369:
370: // only write once
371:
372: if (!written) {
373: try {
374: String className = boxTypeName();
375: if (className.indexOf('.') > 0)
376: className = className.substring(className
377: .lastIndexOf('.') + 1);
378:
379: String path = parser.out_dir + fileSeparator
380: + pack_name.replace('.', fileSeparator);
381:
382: File dir = new File(path);
383: if (!dir.exists())
384: if (!dir.mkdirs()) {
385: org.jacorb.idl.parser.fatal_error(
386: "Unable to create " + path, null);
387: }
388:
389: // print the mapped java class
390: PrintWriter decl_ps;
391: String fname = className + ".java";
392: File f = new File(dir, fname);
393:
394: if (typeSpec.typeSpec() instanceof BaseType) {
395:
396: if (GlobalInputStream.isMoreRecentThan(f)) {
397: decl_ps = new PrintWriter(
398: new java.io.FileWriter(f));
399: printValueClass(className, decl_ps);
400: decl_ps.close();
401: }
402: }
403:
404: // print the holder class */
405:
406: fname = className + "Holder.java";
407: f = new File(dir, fname);
408:
409: if (GlobalInputStream.isMoreRecentThan(f)) {
410: decl_ps = new PrintWriter(new java.io.FileWriter(f));
411: printHolderClass(className, decl_ps);
412: decl_ps.close();
413: }
414:
415: // print the helper class
416:
417: fname = className + "Helper.java";
418: f = new File(dir, fname);
419:
420: if (GlobalInputStream.isMoreRecentThan(f)) {
421: decl_ps = new PrintWriter(new java.io.FileWriter(f));
422: printHelperClass(className, decl_ps);
423: decl_ps.close();
424: }
425:
426: written = true;
427: } catch (java.io.IOException i) {
428: throw new RuntimeException("File IO error" + i);
429: }
430: }
431: }
432:
433: public void printInsertIntoAny(PrintWriter ps, String anyname,
434: String varname) {
435: ps.println("\t\t" + anyname + ".type("
436: + getTypeCodeExpression() + ");");
437: ps.println("\t\t" + helperName() + ".write(" + anyname
438: + ".create_output_stream()," + varname + ");");
439:
440: }
441:
442: public void printExtractResult(PrintWriter ps, String resultname,
443: String anyname, String resulttype) {
444: ps.println("\t\t" + resultname + " = (" + resulttype + ")"
445: + anyname + ".extract_Value();");
446: }
447:
448: public void accept(IDLTreeVisitor visitor) {
449: visitor.visitValue(this);
450: }
451: }
|