📄 javaenumtypewriter.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.wsdl.toJava;import org.apache.axis.utils.JavaUtils;import org.apache.axis.utils.Messages;import org.apache.axis.wsdl.symbolTable.TypeEntry;import java.io.IOException;import java.io.PrintWriter;import java.util.Vector;/** * This is Wsdl2java's Complex Type Writer. It writes the <typeName>.java file. */public class JavaEnumTypeWriter extends JavaClassWriter { /** Field elements */ private Vector elements; /** Field type */ private TypeEntry type; /** * Constructor. * * @param emitter * @param type * @param elements */ protected JavaEnumTypeWriter(Emitter emitter, TypeEntry type, Vector elements) { super(emitter, type.getName(), "enumType"); this.elements = elements; this.type = type; } // ctor /** * Return "implements java.io.Serializable ". * * @return */ protected String getImplementsText() { return "implements java.io.Serializable "; } // getImplementsText /** * Generate the binding for the given enumeration type. * The values vector contains the base type (first index) and * the values (subsequent Strings) * * @param pw * @throws IOException */ protected void writeFileBody(PrintWriter pw) throws IOException { // Get the java name of the type String javaName = getClassName(); // The first index is the base type. // The base type could be a non-object, if so get the corresponding Class. String baseType = ((TypeEntry) elements.get(0)).getName(); String baseClass = baseType; if (baseType.indexOf("int") == 0) { baseClass = "java.lang.Integer"; } else if (baseType.indexOf("char") == 0) { baseClass = "java.lang.Character"; } else if (baseType.indexOf("short") == 0) { baseClass = "java.lang.Short"; } else if (baseType.indexOf("long") == 0) { baseClass = "java.lang.Long"; } else if (baseType.indexOf("double") == 0) { baseClass = "java.lang.Double"; } else if (baseType.indexOf("float") == 0) { baseClass = "java.lang.Float"; } else if (baseType.indexOf("byte") == 0) { baseClass = "java.lang.Byte"; } // Create a list of the literal values. Vector values = new Vector(); for (int i = 1; i < elements.size(); i++) { String value = (String) elements.get(i); if (baseClass.equals("java.lang.String")) { value = "\"" + value + "\""; // Surround literal with double quotes } else if (baseClass.equals("java.lang.Character")) { value = "'" + value + "'"; } else if (baseClass.equals("java.lang.Float")) { if (!value.endsWith("F") && // Indicate float literal so javac !value.endsWith( "f")) { // doesn't complain about precision. value += "F"; } } else if (baseClass.equals("java.lang.Long")) { if (!value.endsWith("L") && // Indicate float literal so javac !value.endsWith( "l")) { // doesn't complain about precision. value += "L"; } } else if (baseClass.equals("javax.xml.namespace.QName")) { value = org.apache.axis.wsdl.symbolTable.Utils.getQNameFromPrefixedName(type.getNode(), value).toString(); value = "javax.xml.namespace.QName.valueOf(\"" + value + "\")"; } else if (baseClass.equals(baseType)) { // Construct baseClass object with literal string value = "new " + baseClass + "(\"" + value + "\")"; } values.add(value); } // Create a list of ids Vector ids = getEnumValueIds(elements); // Each object has a private _value_ variable to store the base value pw.println(" private " + baseType + " _value_;"); // The enumeration values are kept in a hashtable pw.println( " private static java.util.HashMap _table_ = new java.util.HashMap();"); pw.println(""); // A protected constructor is used to create the static enumeration values pw.println(" // " + Messages.getMessage("ctor00")); pw.println(" protected " + javaName + "(" + baseType + " value) {"); pw.println(" _value_ = value;"); if (baseClass.equals("java.lang.String") || baseClass.equals(baseType)) { pw.println(" _table_.put(_value_,this);"); } else { pw.println(" _table_.put(new " + baseClass + "(_value_),this);"); } pw.println(" }"); pw.println(""); // A public static variable of the base type is generated for each enumeration value. // Each variable is preceded by an _. for (int i = 0; i < ids.size(); i++) { // Need to catch the checked MalformedURIException for URI base types if(baseType.equals("org.apache.axis.types.URI")) { pw.println(" public static final " + baseType + " _" + ids.get(i) + ";"); pw.println(" static {"); pw.println(" try {"); pw.println(" _" + ids.get(i) + " = " + values.get(i) + ";"); pw.println(" }"); pw.println(" catch (org.apache.axis.types.URI.MalformedURIException mue) {"); pw.println(" throw new java.lang.RuntimeException(mue.toString());"); pw.println(" }"); pw.println(" }"); pw.println(""); } else { pw.println(" public static final " + baseType + " _" + ids.get(i) + " = " + values.get(i) + ";"); } } // A public static variable is generated for each enumeration value. for (int i = 0; i < ids.size(); i++) { pw.println(" public static final " + javaName + " " + ids.get(i) + " = new " + javaName + "(_" + ids.get(i) + ");"); } // Getter that returns the base value of the enumeration value pw.println(" public " + baseType + " getValue() { return _value_;}");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -