KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.toJava;
56
57 import org.jboss.axis.utils.JavaUtils;
58 import org.jboss.axis.utils.Messages;
59 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
60
61 import java.io.IOException JavaDoc;
62 import java.io.PrintWriter JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 /**
66  * This is Wsdl2java's Complex Type Writer. It writes the <typeName>.java file.
67  */

68 public class JavaEnumTypeWriter extends JavaClassWriter
69 {
70    private Vector JavaDoc elements;
71
72    /**
73     * Constructor.
74     */

75    protected JavaEnumTypeWriter(Emitter emitter,
76                                 TypeEntry type, Vector JavaDoc elements)
77    {
78       super(emitter, type.getName(), "enumType");
79       this.elements = elements;
80    } // ctor
81

82    /**
83     * Return "implements java.io.Serializable ".
84     */

85    protected String JavaDoc getImplementsText()
86    {
87       return "implements java.io.Serializable ";
88    } // getImplementsText
89

90    /**
91     * Generate the binding for the given enumeration type.
92     * The values vector contains the base type (first index) and
93     * the values (subsequent Strings)
94     */

95    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
96    {
97       // Get the java name of the type
98
String JavaDoc javaName = getClassName();
99
100       // The first index is the base type.
101
// The base type could be a non-object, if so get the corresponding Class.
102
String JavaDoc baseType = ((TypeEntry)elements.get(0)).getName();
103       String JavaDoc baseClass = baseType;
104       if (baseType.indexOf("int") == 0)
105       {
106          baseClass = "java.lang.Integer";
107       }
108       else if (baseType.indexOf("char") == 0)
109       {
110          baseClass = "java.lang.Character";
111       }
112       else if (baseType.indexOf("short") == 0)
113       {
114          baseClass = "java.lang.Short";
115       }
116       else if (baseType.indexOf("long") == 0)
117       {
118          baseClass = "java.lang.Long";
119       }
120       else if (baseType.indexOf("double") == 0)
121       {
122          baseClass = "java.lang.Double";
123       }
124       else if (baseType.indexOf("float") == 0)
125       {
126          baseClass = "java.lang.Float";
127       }
128       else if (baseType.indexOf("byte") == 0)
129       {
130          baseClass = "java.lang.Byte";
131       }
132
133       // Create a list of the literal values.
134
Vector JavaDoc values = new Vector JavaDoc();
135       for (int i = 1; i < elements.size(); i++)
136       {
137          String JavaDoc value = (String JavaDoc)elements.get(i);
138          if (baseClass.equals("java.lang.String"))
139          {
140             value = "\"" + value + "\""; // Surround literal with double quotes
141
}
142          else if (baseClass.equals("java.lang.Character"))
143          {
144             value = "'" + value + "'";
145          }
146          else if (baseClass.equals("java.lang.Float"))
147          {
148             if (!value.endsWith("F") && // Indicate float literal so javac
149
!value.endsWith("f")) // doesn't complain about precision.
150
value += "F";
151          }
152          else if (baseClass.equals("java.lang.Long"))
153          {
154             if (!value.endsWith("L") && // Indicate float literal so javac
155
!value.endsWith("l")) // doesn't complain about precision.
156
value += "L";
157          }
158          else if (baseClass.equals(baseType))
159          {
160             // Construct baseClass object with literal string
161
value = "new " + baseClass + "(\"" + value + "\")";
162          }
163          values.add(value);
164       }
165
166       // Create a list of ids
167
Vector JavaDoc ids = getEnumValueIds(elements);
168
169       // Each object has a private _value_ variable to store the base value
170
pw.println(" private " + baseType + " _value_;");
171
172       // The enumeration values are kept in a hashtable
173
pw.println(" private static java.util.HashMap _table_ = new java.util.HashMap();");
174       pw.println("");
175
176       // A protected constructor is used to create the static enumeration values
177
pw.println(" // " + Messages.getMessage("ctor00"));
178       pw.println(" protected " + javaName + "(" + baseType + " value) {");
179       pw.println(" _value_ = value;");
180       if (baseClass.equals("java.lang.String") ||
181               baseClass.equals(baseType))
182       {
183          pw.println(" _table_.put(_value_,this);");
184       }
185       else
186       {
187          pw.println(" _table_.put(new " + baseClass + "(_value_),this);");
188       }
189       pw.println(" }");
190       pw.println("");
191
192       // A public static variable of the base type is generated for each enumeration value.
193
// Each variable is preceded by an _.
194
for (int i = 0; i < ids.size(); i++)
195       {
196          pw.println(" public static final " + baseType + " _" + ids.get(i)
197                  + " = " + values.get(i) + ";");
198       }
199
200       // A public static variable is generated for each enumeration value.
201
for (int i = 0; i < ids.size(); i++)
202       {
203          pw.println(" public static final " + javaName + " " + ids.get(i)
204                  + " = new " + javaName + "(_" + ids.get(i) + ");");
205       }
206
207       // Getter that returns the base value of the enumeration value
208
pw.println(" public " + baseType + " getValue() { return _value_;}");
209
210       // FromValue returns the unique enumeration value object from the table
211
pw.println(" public static " + javaName + " fromValue(" + baseType + " value)");
212       pw.println(" throws java.lang.IllegalStateException {");
213       pw.println(" " + javaName + " enums = (" + javaName + ")");
214       if (baseClass.equals("java.lang.String") ||
215               baseClass.equals(baseType))
216       {
217          pw.println(" _table_.get(value);");
218       }
219       else
220       {
221          pw.println(" _table_.get(new " + baseClass + "(value));");
222       }
223       pw.println(" if (enums==null) throw new java.lang.IllegalStateException();");
224       pw.println(" return enums;");
225       pw.println(" }");
226
227       // FromString returns the unique enumeration value object from a string representation
228
pw.println(" public static " + javaName + " fromString(java.lang.String value)");
229       pw.println(" throws java.lang.IllegalStateException {");
230       if (baseClass.equals("java.lang.String"))
231       {
232          pw.println(" return fromValue(value);");
233       }
234       else if (baseClass.equals(baseType))
235       {
236          pw.println(" try {");
237          pw.println(" return fromValue(new " + baseClass + "(value));");
238          pw.println(" } catch (Exception e) {");
239          pw.println(" throw new java.lang.IllegalStateException();");
240          pw.println(" }");
241       }
242       else if (baseClass.equals("java.lang.Character"))
243       {
244          pw.println(" if (value != null && value.length() == 1);");
245          pw.println(" return fromValue(value.charAt(0));");
246          pw.println(" throw new java.lang.IllegalStateException();");
247       }
248       else if (baseClass.equals("java.lang.Integer"))
249       {
250          pw.println(" try {");
251          pw.println(" return fromValue(java.lang.Integer.parseInt(value));");
252          pw.println(" } catch (Exception e) {");
253          pw.println(" throw new java.lang.IllegalStateException();");
254          pw.println(" }");
255       }
256       else
257       {
258          String JavaDoc parse = "parse" + baseClass.substring(baseClass.lastIndexOf(".") + 1);
259          pw.println(" try {");
260          pw.println(" return fromValue(" + baseClass + "." + parse + "(value));");
261          pw.println(" } catch (Exception e) {");
262          pw.println(" throw new java.lang.IllegalStateException();");
263          pw.println(" }");
264       }
265
266       pw.println(" }");
267
268       // Equals == to determine equality value.
269
// Since enumeration values are singletons, == is appropriate for equals()
270
pw.println(" public boolean equals(java.lang.Object obj) {return (obj == this);}");
271
272       // Provide a reasonable hashCode method (hashCode of the string value of the enumeration)
273
pw.println(" public int hashCode() { return toString().hashCode();}");
274
275       // toString returns a string representation of the enumerated value
276
if (baseClass.equals("java.lang.String"))
277       {
278          pw.println(" public java.lang.String toString() { return _value_;}");
279       }
280       else if (baseClass.equals(baseType))
281       {
282          pw.println(" public java.lang.String toString() { return _value_.toString();}");
283       }
284       else
285       {
286          pw.println(" public java.lang.String toString() { return java.lang.String.valueOf(_value_);}");
287       }
288
289       pw.println(" public java.lang.Object readResolve() throws java.io.ObjectStreamException { return fromValue(_value_);}");
290    } // writeFileBody
291

292    /**
293     * Get the enumeration names for the values.
294     * The name is affected by whether all of the values of the enumeration
295     * can be expressed as valid java identifiers.
296     *
297     * @param bv Vector base and values vector from getEnumerationBaseAndValues
298     * @return Vector names of enums value identifiers.
299     */

300    public static Vector JavaDoc getEnumValueIds(Vector JavaDoc bv)
301    {
302       boolean validJava = true; // Assume all enums values are valid ids
303
// Walk the values looking for invalid ids
304
for (int i = 1; i < bv.size() && validJava; i++)
305       {
306          String JavaDoc value = (String JavaDoc)bv.get(i);
307          if (!JavaUtils.isJavaId(value))
308             validJava = false;
309       }
310       // Build the vector of ids
311
Vector JavaDoc ids = new Vector JavaDoc();
312       for (int i = 1; i < bv.size(); i++)
313       {
314          // If any enums values are not valid java, then
315
// all of the ids are of the form value<1..N>.
316
if (!validJava)
317          {
318             ids.add("value" + i);
319          }
320          else
321          {
322             ids.add((String JavaDoc)bv.get(i));
323          }
324       }
325       return ids;
326    }
327 } // class JavaEnumTypeWriter
328
Popular Tags