View Javadoc

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.geronimo.ews.jaxrpcmapping;
17  
18  import org.apache.axis.utils.JavaUtils;
19  import org.apache.axis.utils.Messages;
20  import org.apache.axis.wsdl.symbolTable.TypeEntry;
21  import org.apache.axis.wsdl.toJava.JavaClassWriter;
22  import org.apache.axis.wsdl.toJava.Utils;
23  
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.util.Vector;
27  
28  /***
29   * This is Wsdl2java's Complex Type Writer.  It writes the <typeName>.java file.
30   *
31   * @author Ias (iasandcb@tmax.co.kr)
32   * @deprecated no more used by J2eeGeneratorFactory
33   */
34  public class J2eeEnumTypeWriter extends JavaClassWriter {
35      private Vector elements;
36      private TypeEntry type;
37  
38      /***
39       * Constructor.
40       */
41      protected J2eeEnumTypeWriter(J2eeEmitter emitter,
42                                   TypeEntry type, Vector elements) {
43          super(emitter, type.getName(), "enumType");
44          this.elements = elements;
45          this.type = type;
46      } // ctor
47  
48      /***
49       * Return "implements java.io.Serializable ".
50       */
51      protected String getImplementsText() {
52          return "implements java.io.Serializable ";
53      } // getImplementsText
54  
55      /***
56       * Generate the binding for the given enumeration type.
57       * The values vector contains the base type (first index) and
58       * the values (subsequent Strings)
59       */
60      protected void writeFileBody(PrintWriter pw) throws IOException {
61          // Get the java name of the type
62          String javaName = getClassName();
63  
64          // The first index is the base type.
65          // The base type could be a non-object, if so get the corresponding Class.
66          String baseType = ((TypeEntry) elements.get(0)).getName();
67          String baseClass = baseType;
68          if (baseType.indexOf("int") == 0) {
69              baseClass = "java.lang.Integer";
70          } else if (baseType.indexOf("char") == 0) {
71              baseClass = "java.lang.Character";
72          } else if (baseType.indexOf("short") == 0) {
73              baseClass = "java.lang.Short";
74          } else if (baseType.indexOf("long") == 0) {
75              baseClass = "java.lang.Long";
76          } else if (baseType.indexOf("double") == 0) {
77              baseClass = "java.lang.Double";
78          } else if (baseType.indexOf("float") == 0) {
79              baseClass = "java.lang.Float";
80          } else if (baseType.indexOf("byte") == 0) {
81              baseClass = "java.lang.Byte";
82          }
83          
84          // Create a list of the literal values.
85          Vector values = new Vector();
86          for (int i = 1; i < elements.size(); i++) {
87              String value = (String) elements.get(i);
88              if (baseClass.equals("java.lang.String")) {
89                  value = "\"" + value + "\"";  // Surround literal with double quotes
90              } else if (baseClass.equals("java.lang.Character")) {
91                  value = "'" + value + "'";
92              } else if (baseClass.equals("java.lang.Float")) {
93                  if (!value.endsWith("F") && // Indicate float literal so javac
94                          !value.endsWith("f"))     // doesn't complain about precision.
95                      value += "F";
96              } else if (baseClass.equals("java.lang.Long")) {
97                  if (!value.endsWith("L") && // Indicate float literal so javac
98                          !value.endsWith("l"))     // doesn't complain about precision.
99                      value += "L";
100             } else if (baseClass.equals(baseType)) {
101                 // Construct baseClass object with literal string
102                 value = "new " + baseClass + "(\"" + value + "\")";
103             }
104             values.add(value);
105         }
106         
107         // Create a list of ids
108         Vector ids = getEnumValueIds(elements);
109 
110         // Each object has a private _value_ variable to store the base value
111         pw.println("    private " + baseType + " _value_;");
112 
113         // The enumeration values are kept in a hashtable
114         pw.println("    private static java.util.HashMap _table_ = new java.util.HashMap();");
115         pw.println("");
116 
117         // A protected constructor is used to create the static enumeration values
118         pw.println("    // " + Messages.getMessage("ctor00"));
119         pw.println("    protected " + javaName + "(" + baseType + " value) {");
120         pw.println("        _value_ = value;");
121         if (baseClass.equals("java.lang.String") ||
122                 baseClass.equals(baseType)) {
123             pw.println("        _table_.put(_value_,this);");
124         } else {
125             pw.println("        _table_.put(new " + baseClass + "(_value_),this);");
126         }
127         pw.println("    }");
128         pw.println("");
129 
130         // A public static variable of the base type is generated for each enumeration value.
131         // Each variable is preceded by an _.
132         for (int i = 0; i < ids.size(); i++) {
133             pw.println("    public static final " + baseType + " _" + ids.get(i)
134                     + " = " + values.get(i) + ";");
135         }
136 
137         // A public static variable is generated for each enumeration value.
138         for (int i = 0; i < ids.size(); i++) {
139             pw.println("    public static final " + javaName + " " + ids.get(i)
140                     + " = new " + javaName + "(_" + ids.get(i) + ");");
141         }
142 
143         // Getter that returns the base value of the enumeration value
144         pw.println("    public " + baseType + " getValue() { return _value_;}");
145 
146         // FromValue returns the unique enumeration value object from the table
147         pw.println("    public static " + javaName + " fromValue(" + baseType + " value)");
148         pw.println("          throws java.lang.IllegalStateException {");
149         pw.println("        " + javaName + " enumeration = (" + javaName + ")");
150         if (baseClass.equals("java.lang.String") ||
151                 baseClass.equals(baseType)) {
152             pw.println("            _table_.get(value);");
153         } else {
154             pw.println("            _table_.get(new " + baseClass + "(value));");
155         }
156         pw.println("        if (enumeration==null) throw new java.lang.IllegalStateException();");
157         pw.println("        return enumeration;");
158         pw.println("    }");
159         
160         // FromString returns the unique enumeration value object from a string representation
161         pw.println("    public static " + javaName + " fromString(java.lang.String value)");
162         pw.println("          throws java.lang.IllegalStateException {");
163         if (baseClass.equals("java.lang.String")) {
164             pw.println("        return fromValue(value);");
165         } else if (baseClass.equals(baseType)) {
166             pw.println("        try {");
167             pw.println("            return fromValue(new " + baseClass + "(value));");
168             pw.println("        } catch (Exception e) {");
169             pw.println("            throw new java.lang.IllegalStateException();");
170             pw.println("        }");
171         } else if (baseClass.equals("java.lang.Character")) {
172             pw.println("        if (value != null && value.length() == 1);");
173             pw.println("            return fromValue(value.charAt(0));");
174             pw.println("        throw new java.lang.IllegalStateException();");
175         } else if (baseClass.equals("java.lang.Integer")) {
176             pw.println("        try {");
177             pw.println("            return fromValue(java.lang.Integer.parseInt(value));");
178             pw.println("        } catch (Exception e) {");
179             pw.println("            throw new java.lang.IllegalStateException();");
180             pw.println("        }");
181         } else {
182             String parse = "parse" + baseClass.substring(baseClass.lastIndexOf(".") + 1);
183             pw.println("        try {");
184             pw.println("            return fromValue(" + baseClass + "." + parse + "(value));");
185             pw.println("        } catch (Exception e) {");
186             pw.println("            throw new java.lang.IllegalStateException();");
187             pw.println("        }");
188         }
189         pw.println("    }");
190 
191         // Equals == to determine equality value.
192         // Since enumeration values are singletons, == is appropriate for equals()
193         pw.println("    public boolean equals(java.lang.Object obj) {return (obj == this);}");
194         
195         // Provide a reasonable hashCode method (hashCode of the string value of the enumeration)
196         pw.println("    public int hashCode() { return toString().hashCode();}");
197         
198         // toString returns a string representation of the enumerated value
199         if (baseClass.equals("java.lang.String")) {
200             pw.println("    public java.lang.String toString() { return _value_;}");
201         } else if (baseClass.equals(baseType)) {
202             pw.println("    public java.lang.String toString() { return _value_.toString();}");
203         } else {
204             pw.println("    public java.lang.String toString() { return java.lang.String.valueOf(_value_);}");
205         }
206         pw.println("    public java.lang.Object readResolve() throws java.io.ObjectStreamException { return fromValue(_value_);}");
207         pw.println("    public static org.apache.axis.encoding.Serializer getSerializer(");
208         pw.println("           java.lang.String mechType, ");
209         pw.println("           java.lang.Class _javaType,  ");
210         pw.println("           javax.xml.namespace.QName _xmlType) {");
211         pw.println("        return ");
212         pw.println("          new org.apache.axis.encoding.ser.EnumSerializer(");
213         pw.println("            _javaType, _xmlType);");
214         pw.println("    }");
215         pw.println("    public static org.apache.axis.encoding.Deserializer getDeserializer(");
216         pw.println("           java.lang.String mechType, ");
217         pw.println("           java.lang.Class _javaType,  ");
218         pw.println("           javax.xml.namespace.QName _xmlType) {");
219         pw.println("        return ");
220         pw.println("          new org.apache.axis.encoding.ser.EnumDeserializer(");
221         pw.println("            _javaType, _xmlType);");
222         pw.println("    }");
223         pw.println("    // " + Messages.getMessage("typeMeta"));
224         pw.println("    private static org.apache.axis.description.TypeDesc typeDesc =");
225         pw.println("        new org.apache.axis.description.TypeDesc(" +
226                 Utils.getJavaLocalName(type.getName()) + ".class);");
227         pw.println();
228         pw.println("    static {");
229         pw.println("        typeDesc.setXmlType(" + Utils.getNewQName(type.getQName()) + ");");
230         pw.println("    }");
231         pw.println("    /**");
232         pw.println("     * " + Messages.getMessage("returnTypeMeta"));
233         pw.println("     */");
234         pw.println("    public static org.apache.axis.description.TypeDesc getTypeDesc() {");
235         pw.println("        return typeDesc;");
236         pw.println("    }");
237         pw.println();
238     } // writeFileBody
239 
240     /***
241      * Get the enumeration names for the values.
242      * The name is affected by whether all of the values of the enumeration
243      * can be expressed as valid java identifiers.
244      *
245      * @param bv Vector base and values vector from getEnumerationBaseAndValues
246      * @return Vector names of enum value identifiers.
247      */
248     public static Vector getEnumValueIds(Vector bv) {
249         boolean validJava = true;  // Assume all enum values are valid ids
250         // Walk the values looking for invalid ids
251         for (int i = 1; i < bv.size() && validJava; i++) {
252             String value = (String) bv.get(i);
253             if (!JavaUtils.isJavaId(value))
254                 validJava = false;
255         }
256         // Build the vector of ids
257         Vector ids = new Vector();
258         for (int i = 1; i < bv.size(); i++) {
259             // If any enum values are not valid java, then
260             // all of the ids are of the form value<1..N>.
261             if (!validJava) {
262                 ids.add("value" + i);
263             } else {
264                 ids.add((String) bv.get(i));
265             }
266         }
267         return ids;
268     }
269 }