1 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 ; 62 import java.io.PrintWriter ; 63 import java.util.Vector ; 64 65 68 public class JavaEnumTypeWriter extends JavaClassWriter 69 { 70 private Vector elements; 71 72 75 protected JavaEnumTypeWriter(Emitter emitter, 76 TypeEntry type, Vector elements) 77 { 78 super(emitter, type.getName(), "enumType"); 79 this.elements = elements; 80 } 82 85 protected String getImplementsText() 86 { 87 return "implements java.io.Serializable "; 88 } 90 95 protected void writeFileBody(PrintWriter pw) throws IOException 96 { 97 String javaName = getClassName(); 99 100 String baseType = ((TypeEntry)elements.get(0)).getName(); 103 String 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 Vector values = new Vector (); 135 for (int i = 1; i < elements.size(); i++) 136 { 137 String value = (String )elements.get(i); 138 if (baseClass.equals("java.lang.String")) 139 { 140 value = "\"" + value + "\""; } 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") && !value.endsWith("f")) value += "F"; 151 } 152 else if (baseClass.equals("java.lang.Long")) 153 { 154 if (!value.endsWith("L") && !value.endsWith("l")) value += "L"; 157 } 158 else if (baseClass.equals(baseType)) 159 { 160 value = "new " + baseClass + "(\"" + value + "\")"; 162 } 163 values.add(value); 164 } 165 166 Vector ids = getEnumValueIds(elements); 168 169 pw.println(" private " + baseType + " _value_;"); 171 172 pw.println(" private static java.util.HashMap _table_ = new java.util.HashMap();"); 174 pw.println(""); 175 176 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 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 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 pw.println(" public " + baseType + " getValue() { return _value_;}"); 209 210 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 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 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 pw.println(" public boolean equals(java.lang.Object obj) {return (obj == this);}"); 271 272 pw.println(" public int hashCode() { return toString().hashCode();}"); 274 275 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 } 292 300 public static Vector getEnumValueIds(Vector bv) 301 { 302 boolean validJava = true; for (int i = 1; i < bv.size() && validJava; i++) 305 { 306 String value = (String )bv.get(i); 307 if (!JavaUtils.isJavaId(value)) 308 validJava = false; 309 } 310 Vector ids = new Vector (); 312 for (int i = 1; i < bv.size(); i++) 313 { 314 if (!validJava) 317 { 318 ids.add("value" + i); 319 } 320 else 321 { 322 ids.add((String )bv.get(i)); 323 } 324 } 325 return ids; 326 } 327 } | Popular Tags |