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: import java.io.PrintWriter;
024: import java.util.*;
025:
026: /**
027: * @author Gerald Brose
028: * @version $Id: InitDecl.java,v 1.11 2006/06/19 10:34:57 alphonse.bendt Exp $
029: */
030:
031: public class InitDecl extends Declaration {
032: public Vector paramDecls;
033: public IdlSymbol myValue;
034:
035: /** new in CORBA 3.0, factory methods may raise exceptions */
036: public RaisesExpr raisesExpr;
037:
038: public InitDecl(int num) {
039: super (num);
040: paramDecls = new Vector();
041: }
042:
043: public void setPackage(String s) {
044: s = parser.pack_replace(s);
045:
046: if (pack_name.length() > 0)
047: pack_name = s + "." + pack_name;
048: else
049: pack_name = s;
050:
051: for (Enumeration e = paramDecls.elements(); e.hasMoreElements(); ((ParamDecl) e
052: .nextElement()).setPackage(s))
053: ;
054: raisesExpr.setPackage(s);
055: }
056:
057: public void setEnclosingSymbol(IdlSymbol s) {
058: if (enclosing_symbol != null && enclosing_symbol != s)
059: throw new RuntimeException(
060: "Compiler Error: trying to reassign container for "
061: + name);
062: enclosing_symbol = s;
063: raisesExpr.setEnclosingSymbol(s);
064: }
065:
066: public void parse() {
067: myValue = enclosing_symbol;
068:
069: try {
070: NameTable.define(full_name(), "factory");
071: } catch (NameAlreadyDefined nad) {
072: parser.error("Factory " + full_name() + " already defined",
073: token);
074: }
075:
076: for (Enumeration e = paramDecls.elements(); e.hasMoreElements();) {
077: ParamDecl param = (ParamDecl) e.nextElement();
078: param.parse();
079: try {
080: NameTable.define(full_name() + "."
081: + param.simple_declarator.name(), "argument");
082: } catch (NameAlreadyDefined nad) {
083: parser.error("Argument "
084: + param.simple_declarator.name()
085: + " already defined in operation "
086: + full_name(), token);
087: }
088: }
089: raisesExpr.parse();
090: }
091:
092: /**
093: * Prints the method's signature, for inclusion in the
094: * factory interface.
095: */
096: public void print(PrintWriter ps, String type_name) {
097: ps.print("\t" + type_name + " " + name + "( ");
098:
099: Enumeration e = paramDecls.elements();
100:
101: if (e.hasMoreElements())
102: ((ParamDecl) e.nextElement()).print(ps);
103:
104: for (; e.hasMoreElements();) {
105: ps.print(", ");
106: ((ParamDecl) e.nextElement()).print(ps);
107: }
108: ps.print(")");
109: raisesExpr.print(ps);
110: ps.println(";");
111: }
112:
113: /**
114: * Prints the Helper method that corresponds to this factory method.
115: */
116: public void printHelperMethod(PrintWriter ps, String type_name) {
117: ps.print("\tpublic static " + type_name + " " + name + "( ");
118: ps.print("org.omg.CORBA.ORB orb");
119:
120: for (Enumeration e = paramDecls.elements(); e.hasMoreElements();) {
121: ps.print(", ");
122: ((ParamDecl) e.nextElement()).print(ps);
123: }
124: ps.println(" )");
125:
126: ps.println("\t{");
127: ps
128: .println("\t\t"
129: + type_name
130: + "ValueFactory f = "
131: + "( "
132: + type_name
133: + "ValueFactory )"
134: + "((org.omg.CORBA_2_3.ORB)orb).lookup_value_factory(id());");
135: ps.println("\t\tif (f == null)");
136: ps.println("\t\t\tthrow new org.omg.CORBA.MARSHAL( "
137: + "1, org.omg.CORBA.CompletionStatus.COMPLETED_NO );");
138: ps.print("\t\treturn f." + name + "( ");
139:
140: for (Enumeration e = paramDecls.elements(); e.hasMoreElements();) {
141: ps.print(((ParamDecl) e.nextElement()).simple_declarator);
142: if (e.hasMoreElements())
143: ps.print(", ");
144: }
145:
146: ps.println(" );");
147:
148: ps.println("\t}");
149: }
150:
151: public String name() {
152: return name;
153: }
154:
155: public String opName() {
156: return name();
157: }
158: }
|