KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > CodeGen > GeneratedObject


1 /* $Id: GeneratedObject.java,v 1.2 2004/05/20 14:23:51 bures Exp $ */
2 package SOFA.SOFAnode.Made.CodeGen;
3
4 import java.io.IOException JavaDoc;
5 import java.rmi.RemoteException JavaDoc;
6
7 import SOFA.SOFAnode.Made.TIR.Contained;
8 import SOFA.SOFAnode.Made.TIR.Identification;
9 import SOFA.SOFAnode.Made.TIR.PrimitiveDef;
10 import SOFA.SOFAnode.Made.TIR.PrimitiveKind;
11 import SOFA.SOFAnode.Made.TIR.TIRObject;
12
13 /** Superclass for representation of generated objects.
14   *
15   * @author Petr Hnetynka
16   */

17 abstract public class GeneratedObject {
18   /** Package with standard holders, helpers,... */
19   public static final String JavaDoc stdPackage = "cz.cuni.sofa.lib.";
20   /** Default name of ostream in helpers */
21   public static final String JavaDoc defaultOstream = "ostream";
22   /** Default name of istream in helpers */
23   public static final String JavaDoc defaultIstream = "istream";
24
25   /** In reference kind */
26   public static final int INREF = 1;
27   /** Out (or inout) reference kind */
28   public static final int OUTREF = 2;
29
30   /** Key of object. It is string representation of id of objects and it is used in for searching correct type */
31   public String JavaDoc key;
32
33
34   /** Compares key of object with given key.
35     *
36     * @param key key
37     * @return <tt>true</tt> if keys are same
38     */

39   public boolean compareKey(String JavaDoc key) {
40     return this.key.compareTo(key) == 0;
41   }
42
43   /** Returns generated reference to object.
44     *
45     * @param kind Kind of reference - <tt>INREF</tt> or <tt>OUTREF</tt>
46     */

47   public abstract String JavaDoc genReference(int kind);
48   
49   /** Writes reference to the <tt>out</tt>.
50     *
51     * @param kind Kind of reference - <tt>INREF</tt> or <tt>OUTREF</tt>
52     * @param out output writer
53     * @exception IOException if an I/O error occurs
54     */

55   public abstract void genReference(CGFileWriter out, int kind) throws IOException JavaDoc;
56
57   /** Returns special generated reference to object - only for some types.
58     *
59     * @param kind Kind of reference - <tt>INREF</tt> or <tt>OUTREF</tt>
60     * @param specType type of reference
61     */

62   public String JavaDoc genSpecReference(int kind, int specType) { return ""; }
63   
64   /** Writes special reference to the <tt>out</tt> - only for some types.
65     *
66     * @param kind Kind of reference - <tt>INREF</tt> or <tt>OUTREF</tt>
67     * @param out output writer
68     * @param specType type of reference
69     * @exception IOException if an I/O error occurs
70     */

71   public void genSpecReference(CGFileWriter out, int kind, int specType) throws IOException JavaDoc {;}
72   
73
74   /** Writes default value of the type to the <tt>out</tt>.
75     *
76     * @param out output writer
77     * @exception IOException if an I/O error occurs
78     */

79   public abstract void defaultValue(CGFileWriter out) throws IOException JavaDoc;
80
81   /** Returns default value of the type.
82     *
83     * @param out output writer
84     * @exception IOException if an I/O error occurs
85     */

86   public abstract String JavaDoc defaultValue() throws IOException JavaDoc;
87
88   /** Writes code for the reading of the value with this type.
89     *
90     * @param out output writer
91     * @param varName "written variable"
92     * @exception IOException if an I/O error occurs
93     */

94   public abstract void genReadType(CGFileWriter out, String JavaDoc varName) throws IOException JavaDoc;
95
96   /** Returns code for the reading of the value with this type.
97     *
98     * @param varName "written variable"
99     * @exception IOException if an I/O error occurs
100     */

101   public abstract String JavaDoc genReadType(String JavaDoc varName) throws IOException JavaDoc;
102
103   /** Writes code for the writing of the value with this type.
104     *
105     * @param out output writer
106     * @param varName "read variable"
107     * @exception IOException if an I/O error occurs
108     */

109   public abstract void genWriteType(CGFileWriter out, String JavaDoc varName) throws IOException JavaDoc;
110
111   /** Returns code for the writing of the value with this type.
112     *
113     * @param varName "read variable"
114     * @exception IOException if an I/O error occurs
115     */

116   public abstract String JavaDoc genWriteType(String JavaDoc varName) throws IOException JavaDoc;
117
118   /** Returns key created from given repository object.
119     *
120     * @param obj repository object
121     * @exception RemoteException remote exception
122     */

123   public static String JavaDoc createKey(TIRObject obj) throws RemoteException JavaDoc {
124     if (obj instanceof Contained) {
125       Identification idl = ((Contained) obj).get_identification();
126       return idl.absolute_name().name()+"?"+idl.version();
127     }
128     if (obj instanceof PrimitiveDef) {
129       switch (((PrimitiveDef) obj).kind().value()) {
130        case PrimitiveKind.pk_void:
131          return "void";
132        case PrimitiveKind.pk_ushort:
133          return "ushort";
134        case PrimitiveKind.pk_short:
135          return "short";
136        case PrimitiveKind.pk_ulong:
137          return "ulong";
138        case PrimitiveKind.pk_long:
139          return "long";
140        case PrimitiveKind.pk_ulonglong:
141          return "ulonglong";
142        case PrimitiveKind.pk_longlong:
143          return "longlong";
144        case PrimitiveKind.pk_float:
145          return "float";
146        case PrimitiveKind.pk_double:
147          return "double";
148        case PrimitiveKind.pk_longdouble:
149          return null; // !!!! no mapping
150
case PrimitiveKind.pk_boolean:
151          return "boolean";
152        case PrimitiveKind.pk_wchar:
153          return "wchar";
154        case PrimitiveKind.pk_char:
155          return "char";
156        case PrimitiveKind.pk_octet:
157          return "octet";
158        case PrimitiveKind.pk_wstring:
159          return "wstring";
160        case PrimitiveKind.pk_string:
161          return "string";
162        case PrimitiveKind.pk_any:
163          return "any";
164        case PrimitiveKind.pk_object:
165          return "object";
166       }
167     }
168     return null;
169   }
170
171   
172   static String JavaDoc[] reservedWords = {
173         "abstract", "default", "if", "private", "throw",
174         "boolean", "do", "implements", "protected", "throws",
175         "break", "double", "import", "public", "transient",
176         "byte", "else", "instanceof", "return", "try",
177         "case", "extends", "int", "short", "void",
178         "catch", "final", "interface", "static", "volatile",
179         "char", "finally", "long", "super", "while",
180         "class", "float", "native", "switch",
181         "const", "for", "new", "synchronized",
182         "continue", "goto", "package", "this",
183         "true", "false", "null",
184         "clone", "equals", "finalize", "getClass", "hashCode",
185         "notify", "notifyAll", "toString", "wait",
186         };
187
188
189   /** Test, if it is reserved name (then underscore will be prepended)
190     * @param name tested name
191     */

192   public static boolean isReservedName(String JavaDoc name) {
193     for (int i=0;i<reservedWords.length;i++) {
194       if (reservedWords[i].compareToIgnoreCase(name)==0)
195         return true;
196     }
197     String JavaDoc lname = name.toLowerCase();
198     if (lname.endsWith("helper"))
199       return true;
200     if (lname.endsWith("holder"))
201       return true;
202     if (lname.endsWith("operations"))
203       return true;
204     if (lname.endsWith("package"))
205       return true;
206     return false;
207   }
208 }
209
Popular Tags