StringGen.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25/*
26 * COMPONENT_NAME: idl.toJava
27 *
28 * ORIGINS: 27
29 *
30 * Licensed Materials - Property of IBM
31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
32 * RMI-IIOP v1.0
33 *
34 */
35
36package com.sun.tools.corba.se.idl.toJavaPortable;
37
38// NOTES:
39// -D61056   <klr> Use Util.helperName
40
41import java.io.PrintWriter;
42import java.util.Hashtable;
43
44import com.sun.tools.corba.se.idl.StringEntry;
45import com.sun.tools.corba.se.idl.SymtabEntry;
46
47/**
48 * Handles generation of CORBA strings as well as wstrings.  Be careful
49 * not to forget the wstrings.
50 **/
51public class StringGen implements com.sun.tools.corba.se.idl.StringGen, JavaGenerator
52{
53  /**
54   * Public zero-argument constructor.
55   **/
56  public StringGen ()
57  {
58  } // ctor
59
60  /**
61   * This should never be called.  This class exists for the
62   * JavaGenerator interface.
63   **/
64  public void generate (Hashtable symbolTable, StringEntry e, PrintWriter stream)
65  {
66  } // generate
67
68  ///////////////
69  // From JavaGenerator
70
71  public int helperType (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream)
72  {
73    return type(index, indent, tcoffsets, name, entry, stream);
74  } // helperType
75
76  public int type (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream) {
77    tcoffsets.set (entry);
78    StringEntry stringEntry = (StringEntry)entry;
79    String bound;
80    if (stringEntry.maxSize () == null)
81      bound = "0";
82    else
83      bound = Util.parseExpression (stringEntry.maxSize ());
84
85    // entry.name() is necessary to determine whether it is a
86    // string or wstring
87
88    stream.println (indent
89                    + name
90                    + " = org.omg.CORBA.ORB.init ().create_"
91                    + entry.name()
92                    + "_tc ("
93                    + bound + ");");
94    return index;
95  } // type
96
97  public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream)
98  {
99  } // helperRead
100
101  public void helperWrite (SymtabEntry entry, PrintWriter stream)
102  {
103  } // helperWrite
104
105  public int read (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
106  {
107    StringEntry string = (StringEntry)entry;
108    String entryName = entry.name ();
109    if (entryName.equals ("string"))
110      stream.println (indent + name + " = istream.read_string ();");
111    else if (entryName.equals ("wstring"))
112      stream.println (indent + name + " = istream.read_wstring ();");
113    if (string.maxSize () != null)
114    {
115      stream.println (indent + "if (" + name + ".length () > (" + Util.parseExpression (string.maxSize ()) + "))");
116      stream.println (indent + "  throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
117    }
118    return index;
119  } // read
120
121  public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
122  {
123    StringEntry string = (StringEntry)entry;
124    if (string.maxSize () != null)
125    {
126      stream.print (indent + "if (" + name + ".length () > (" + Util.parseExpression (string.maxSize ()) + "))");
127      stream.println (indent + "  throw new org.omg.CORBA.MARSHAL (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);");
128    }
129    String entryName = entry.name ();
130    if (entryName.equals ("string"))
131      stream.println (indent + "ostream.write_string (" + name + ");");
132    else if (entryName.equals ("wstring"))
133      stream.println (indent + "ostream.write_wstring (" + name + ");");
134    return index;
135  } // write
136
137  // From JavaGenerator
138  ///////////////
139} // class StringGen
140