KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > toJava > JavaEnumTypeWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.wsdl.toJava;
17
18 import org.apache.axis.utils.JavaUtils;
19 import org.apache.axis.utils.Messages;
20 import org.apache.axis.wsdl.symbolTable.TypeEntry;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 /**
27  * This is Wsdl2java's Complex Type Writer. It writes the <typeName>.java file.
28  */

29 public class JavaEnumTypeWriter extends JavaClassWriter {
30
31     /** Field elements */
32     private Vector JavaDoc elements;
33
34     /** Field type */
35     private TypeEntry type;
36
37     /**
38      * Constructor.
39      *
40      * @param emitter
41      * @param type
42      * @param elements
43      */

44     protected JavaEnumTypeWriter(Emitter emitter, TypeEntry type,
45                                  Vector JavaDoc elements) {
46
47         super(emitter, type.getName(), "enumType");
48
49         this.elements = elements;
50         this.type = type;
51     } // ctor
52

53     /**
54      * Return "implements java.io.Serializable ".
55      *
56      * @return
57      */

58     protected String JavaDoc getImplementsText() {
59         return "implements java.io.Serializable ";
60     } // getImplementsText
61

62     /**
63      * Generate the binding for the given enumeration type.
64      * The values vector contains the base type (first index) and
65      * the values (subsequent Strings)
66      *
67      * @param pw
68      * @throws IOException
69      */

70     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
71
72         // Get the java name of the type
73
String JavaDoc javaName = getClassName();
74
75         // The first index is the base type.
76
// The base type could be a non-object, if so get the corresponding Class.
77
String JavaDoc baseType = ((TypeEntry) elements.get(0)).getName();
78         String JavaDoc baseClass = baseType;
79
80         if (baseType.indexOf("int") == 0) {
81             baseClass = "java.lang.Integer";
82         } else if (baseType.indexOf("char") == 0) {
83             baseClass = "java.lang.Character";
84         } else if (baseType.indexOf("short") == 0) {
85             baseClass = "java.lang.Short";
86         } else if (baseType.indexOf("long") == 0) {
87             baseClass = "java.lang.Long";
88         } else if (baseType.indexOf("double") == 0) {
89             baseClass = "java.lang.Double";
90         } else if (baseType.indexOf("float") == 0) {
91             baseClass = "java.lang.Float";
92         } else if (baseType.indexOf("byte") == 0) {
93             baseClass = "java.lang.Byte";
94         }
95
96         // Create a list of the literal values.
97
Vector JavaDoc values = new Vector JavaDoc();
98
99         for (int i = 1; i < elements.size(); i++) {
100             String JavaDoc value = (String JavaDoc) elements.get(i);
101
102             if (baseClass.equals("java.lang.String")) {
103                 value = "\"" + value
104                         + "\""; // Surround literal with double quotes
105
} else if (baseClass.equals("java.lang.Character")) {
106                 value = "'" + value + "'";
107             } else if (baseClass.equals("java.lang.Float")) {
108                 if (!value.endsWith("F") && // Indicate float literal so javac
109
!value.endsWith(
110                                 "f")) { // doesn't complain about precision.
111
value += "F";
112                 }
113             } else if (baseClass.equals("java.lang.Long")) {
114                 if (!value.endsWith("L") && // Indicate float literal so javac
115
!value.endsWith(
116                                 "l")) { // doesn't complain about precision.
117
value += "L";
118                 }
119             } else if (baseClass.equals("javax.xml.namespace.QName")) {
120                 value = org.apache.axis.wsdl.symbolTable.Utils.getQNameFromPrefixedName(type.getNode(), value).toString();
121                 value = "javax.xml.namespace.QName.valueOf(\"" + value + "\")";
122             } else if (baseClass.equals(baseType)) {
123
124                 // Construct baseClass object with literal string
125
value = "new " + baseClass + "(\"" + value + "\")";
126             }
127
128             values.add(value);
129         }
130
131         // Create a list of ids
132
Vector JavaDoc ids = getEnumValueIds(elements);
133
134         // Each object has a private _value_ variable to store the base value
135
pw.println(" private " + baseType + " _value_;");
136
137         // The enumeration values are kept in a hashtable
138
pw.println(
139                 " private static java.util.HashMap _table_ = new java.util.HashMap();");
140         pw.println("");
141
142         // A protected constructor is used to create the static enumeration values
143
pw.println(" // " + Messages.getMessage("ctor00"));
144         pw.println(" protected " + javaName + "(" + baseType + " value) {");
145         pw.println(" _value_ = value;");
146
147         if (baseClass.equals("java.lang.String")
148                 || baseClass.equals(baseType)) {
149             pw.println(" _table_.put(_value_,this);");
150         } else {
151             pw.println(" _table_.put(new " + baseClass
152                     + "(_value_),this);");
153         }
154
155         pw.println(" }");
156         pw.println("");
157
158         // A public static variable of the base type is generated for each enumeration value.
159
// Each variable is preceded by an _.
160
for (int i = 0; i < ids.size(); i++) {
161             
162             // Need to catch the checked MalformedURIException for URI base types
163
if(baseType.equals("org.apache.axis.types.URI")) {
164                 pw.println(" public static final " + baseType + " _" + ids.get(i) + ";");
165                 pw.println(" static {");
166                 pw.println(" try {");
167                 pw.println(" _" + ids.get(i) + " = " + values.get(i) + ";");
168                 pw.println(" }");
169                 pw.println(" catch (org.apache.axis.types.URI.MalformedURIException mue) {");
170                 pw.println(" throw new java.lang.RuntimeException(mue.toString());");
171                 pw.println(" }");
172                 pw.println(" }");
173                 pw.println("");
174             }
175             else {
176                 pw.println(" public static final " + baseType + " _"
177                     + ids.get(i) + " = " + values.get(i) + ";");
178             }
179         }
180
181         // A public static variable is generated for each enumeration value.
182
for (int i = 0; i < ids.size(); i++) {
183             pw.println(" public static final " + javaName + " " + ids.get(i)
184                     + " = new " + javaName + "(_" + ids.get(i) + ");");
185         }
186
187         // Getter that returns the base value of the enumeration value
188
pw.println(" public " + baseType + " getValue() { return _value_;}");
189
190         // FromValue returns the unique enumeration value object from the table
191
pw.println(" public static " + javaName + " fromValue(" + baseType
192                 + " value)");
193         pw.println(" throws java.lang.IllegalArgumentException {");
194         pw.println(" " + javaName + " enumeration = (" + javaName + ")");
195
196         if (baseClass.equals("java.lang.String")
197                 || baseClass.equals(baseType)) {
198             pw.println(" _table_.get(value);");
199         } else {
200             pw.println(" _table_.get(new " + baseClass
201                     + "(value));");
202         }
203
204         pw.println(
205                 " if (enumeration==null) throw new java.lang.IllegalArgumentException();");
206         pw.println(" return enumeration;");
207         pw.println(" }");
208
209         // FromString returns the unique enumeration value object from a string representation
210
pw.println(" public static " + javaName
211                 + " fromString(java.lang.String value)");
212         pw.println(" throws java.lang.IllegalArgumentException {");
213
214         if (baseClass.equals("java.lang.String")) {
215             pw.println(" return fromValue(value);");
216         } else if (baseClass.equals("javax.xml.namespace.QName")) {
217             pw.println(" try {");
218             pw.println(" return fromValue(javax.xml.namespace.QName.valueOf"
219                     + "(value));");
220             pw.println(" } catch (Exception e) {");
221             pw.println(
222                     " throw new java.lang.IllegalArgumentException();");
223             pw.println(" }");
224         } else if (baseClass.equals(baseType)) {
225             pw.println(" try {");
226             pw.println(" return fromValue(new " + baseClass
227                     + "(value));");
228             pw.println(" } catch (Exception e) {");
229             pw.println(
230                     " throw new java.lang.IllegalArgumentException();");
231             pw.println(" }");
232         } else if (baseClass.equals("java.lang.Character")) {
233             pw.println(" if (value != null && value.length() == 1);");
234             pw.println(" return fromValue(value.charAt(0));");
235             pw.println(
236                     " throw new java.lang.IllegalArgumentException();");
237         } else if (baseClass.equals("java.lang.Integer")) {
238             pw.println(" try {");
239             pw.println(
240                     " return fromValue(java.lang.Integer.parseInt(value));");
241             pw.println(" } catch (Exception e) {");
242             pw.println(
243                     " throw new java.lang.IllegalArgumentException();");
244             pw.println(" }");
245         } else {
246             String JavaDoc parse = "parse"
247                     + baseClass.substring(baseClass.lastIndexOf(".")
248                     + 1);
249
250             pw.println(" try {");
251             pw.println(" return fromValue(" + baseClass + "."
252                     + parse + "(value));");
253             pw.println(" } catch (Exception e) {");
254             pw.println(
255                     " throw new java.lang.IllegalArgumentException();");
256             pw.println(" }");
257         }
258
259         pw.println(" }");
260
261         // Equals == to determine equality value.
262
// Since enumeration values are singletons, == is appropriate for equals()
263
pw.println(
264                 " public boolean equals(java.lang.Object obj) {return (obj == this);}");
265
266         // Provide a reasonable hashCode method (hashCode of the string value of the enumeration)
267
pw.println(
268                 " public int hashCode() { return toString().hashCode();}");
269
270         // toString returns a string representation of the enumerated value
271
if (baseClass.equals("java.lang.String")) {
272             pw.println(
273                     " public java.lang.String toString() { return _value_;}");
274         } else if (baseClass.equals(baseType)) {
275             pw.println(
276                     " public java.lang.String toString() { return _value_.toString();}");
277         } else {
278             pw.println(
279                     " public java.lang.String toString() { return java.lang.String.valueOf(_value_);}");
280         }
281
282         pw.println(
283                 " public java.lang.Object readResolve() throws java.io.ObjectStreamException { return fromValue(_value_);}");
284         pw.println(
285                 " public static org.apache.axis.encoding.Serializer getSerializer(");
286         pw.println(" java.lang.String mechType, ");
287         pw.println(" java.lang.Class _javaType, ");
288         pw.println(" javax.xml.namespace.QName _xmlType) {");
289         pw.println(" return ");
290         pw.println(
291                 " new org.apache.axis.encoding.ser.EnumSerializer(");
292         pw.println(" _javaType, _xmlType);");
293         pw.println(" }");
294         pw.println(
295                 " public static org.apache.axis.encoding.Deserializer getDeserializer(");
296         pw.println(" java.lang.String mechType, ");
297         pw.println(" java.lang.Class _javaType, ");
298         pw.println(" javax.xml.namespace.QName _xmlType) {");
299         pw.println(" return ");
300         pw.println(
301                 " new org.apache.axis.encoding.ser.EnumDeserializer(");
302         pw.println(" _javaType, _xmlType);");
303         pw.println(" }");
304         pw.println(" // " + Messages.getMessage("typeMeta"));
305         pw.println(
306                 " private static org.apache.axis.description.TypeDesc typeDesc =");
307         pw.println(" new org.apache.axis.description.TypeDesc("
308                 + Utils.getJavaLocalName(type.getName()) + ".class);");
309         pw.println();
310         pw.println(" static {");
311         pw.println(" typeDesc.setXmlType("
312                 + Utils.getNewQName(type.getQName()) + ");");
313         pw.println(" }");
314         pw.println(" /**");
315         pw.println(" * " + Messages.getMessage("returnTypeMeta"));
316         pw.println(" */");
317         pw.println(
318                 " public static org.apache.axis.description.TypeDesc getTypeDesc() {");
319         pw.println(" return typeDesc;");
320         pw.println(" }");
321         pw.println();
322     } // writeFileBody
323

324     /**
325      * Get the enumeration names for the values.
326      * The name is affected by whether all of the values of the enumeration
327      * can be expressed as valid java identifiers.
328      *
329      * @param bv Vector base and values vector from getEnumerationBaseAndValues
330      * @return Vector names of enum value identifiers.
331      */

332     public static Vector JavaDoc getEnumValueIds(Vector JavaDoc bv) {
333
334         boolean validJava = true; // Assume all enum values are valid ids
335

336         // Walk the values looking for invalid ids
337
for (int i = 1; (i < bv.size()) && validJava; i++) {
338             String JavaDoc value = (String JavaDoc) bv.get(i);
339
340             if (!JavaUtils.isJavaId(value)) {
341                 validJava = false;
342             }
343         }
344
345         // Build the vector of ids
346
Vector JavaDoc ids = new Vector JavaDoc();
347
348         for (int i = 1; i < bv.size(); i++) {
349
350             // If any enum values are not valid java, then
351
// all of the ids are of the form value<1..N>.
352
if (!validJava) {
353                 ids.add("value" + i);
354             } else {
355                 ids.add((String JavaDoc) bv.get(i));
356             }
357         }
358
359         return ids;
360     }
361
362     /** Generate a java source file for enum class.
363      * If the emitter works in deploy mode and the class already exists, the source wull not be generated.
364      */

365     public void generate() throws IOException JavaDoc {
366         String JavaDoc fqcn = getPackage() + "." + getClassName();
367         if (emitter.isDeploy()) {
368             if (!emitter.doesExist(fqcn)) {
369                 super.generate();
370             }
371         } else {
372             super.generate();
373         }
374     }
375 } // class JavaEnumTypeWriter
376
Popular Tags