001: /*
002: * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: /*
026: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)DefaultFactory.java 1.18 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -D62023 klr new class
041:
042: import java.io.PrintWriter;
043: import java.util.Vector;
044:
045: import com.sun.tools.corba.se.idl.GenFileStream;
046: import com.sun.tools.corba.se.idl.SymtabEntry;
047: import com.sun.tools.corba.se.idl.MethodEntry;
048: import com.sun.tools.corba.se.idl.ValueEntry;
049:
050: /**
051: *
052: **/
053: public class DefaultFactory implements AuxGen {
054: /**
055: * Public zero-argument constructor.
056: **/
057: public DefaultFactory() {
058: } // ctor
059:
060: /**
061: * Generate the default value factory class. Provides general algorithm for
062: * auxiliary binding generation:
063: * 1.) Initialize symbol table and symbol table entry members,
064: * common to all generators.
065: * 2.) Initialize members unique to this generator.
066: * 3.) Open print stream
067: * 4.) Write class heading (package, prologue, source comment, class
068: * statement, open curly
069: * 5.) Write class body (member data and methods)
070: * 6.) Write class closing (close curly)
071: * 7.) Close the print stream
072: **/
073: public void generate(java.util.Hashtable symbolTable,
074: com.sun.tools.corba.se.idl.SymtabEntry entry) {
075: this .symbolTable = symbolTable;
076: this .entry = entry;
077: init();
078: openStream();
079: if (stream == null)
080: return;
081: writeHeading();
082: writeBody();
083: writeClosing();
084: closeStream();
085: } // generate
086:
087: /**
088: * Initialize variables unique to this generator.
089: **/
090: protected void init() {
091: factoryClass = entry.name() + "DefaultFactory";
092: factoryInterface = entry.name() + "ValueFactory";
093: factoryType = Util.javaName(entry);
094: implType = entry.name() + "Impl"; // default implementation class
095: } // init
096:
097: /**
098: * @return true if entry has any factory methods declared
099: **/
100: protected boolean hasFactoryMethods() {
101: Vector init = ((ValueEntry) entry).initializers();
102: if (init != null && init.size() > 0)
103: return true;
104: else
105: return false;
106: } // hasFactoryMethods
107:
108: /**
109: * Open the print stream for subsequent output.
110: **/
111: protected void openStream() {
112: stream = Util.stream(entry, "DefaultFactory.java");
113: } // openStream
114:
115: /**
116: * Generate the heading, including the package, imports,
117: * source comment, class statement, and left curly.
118: **/
119: protected void writeHeading() {
120: Util.writePackage(stream, entry, Util.TypeFile); // REVISIT - same as interface?
121: Util.writeProlog(stream, stream.name());
122: if (entry.comment() != null)
123: entry.comment().generate("", stream);
124: stream.print("public class " + factoryClass + " implements ");
125: if (hasFactoryMethods())
126: stream.print(factoryInterface);
127: else
128: stream.print("org.omg.CORBA.portable.ValueFactory");
129: stream.println(" {");
130: } // writeHeading
131:
132: /**
133: * Generate the contents of this class
134: **/
135: protected void writeBody() {
136: writeFactoryMethods();
137: stream.println();
138: writeReadValue();
139: } // writeBody
140:
141: /**
142: * Generate members of this class.
143: **/
144: protected void writeFactoryMethods() {
145: Vector init = ((ValueEntry) entry).initializers();
146: if (init != null) {
147: for (int i = 0; i < init.size(); i++) {
148: MethodEntry element = (MethodEntry) init.elementAt(i);
149: element.valueMethod(true); //tag value method if not tagged previously
150: ((MethodGen24) element.generator())
151: .defaultFactoryMethod(symbolTable, element,
152: stream);
153: }
154: }
155: } // writeFactoryMethods
156:
157: /**
158: * Generate default read_value
159: **/
160: protected void writeReadValue() {
161: stream
162: .println(" public java.io.Serializable read_value (org.omg.CORBA_2_3.portable.InputStream is)");
163: stream.println(" {");
164: stream.println(" return is.read_value(new " + implType
165: + " ());");
166: stream.println(" }");
167: } // writeReadValue
168:
169: /**
170: * Generate the closing statements.
171: **/
172: protected void writeClosing() {
173: stream.println('}');
174: } // writeClosing
175:
176: /**
177: * Write the stream to file by closing the print stream.
178: **/
179: protected void closeStream() {
180: stream.close();
181: } // closeStream
182:
183: protected java.util.Hashtable symbolTable;
184: protected com.sun.tools.corba.se.idl.SymtabEntry entry;
185: protected GenFileStream stream;
186:
187: // Unique to this generator
188: protected String factoryClass;
189: protected String factoryInterface;
190: protected String factoryType;
191: protected String implType;
192: } // class Holder
|