1 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 ; 23 import java.io.PrintWriter ; 24 import java.util.Vector ; 25 26 29 public class JavaEnumTypeWriter extends JavaClassWriter { 30 31 32 private Vector elements; 33 34 35 private TypeEntry type; 36 37 44 protected JavaEnumTypeWriter(Emitter emitter, TypeEntry type, 45 Vector elements) { 46 47 super(emitter, type.getName(), "enumType"); 48 49 this.elements = elements; 50 this.type = type; 51 } 53 58 protected String getImplementsText() { 59 return "implements java.io.Serializable "; 60 } 62 70 protected void writeFileBody(PrintWriter pw) throws IOException { 71 72 String javaName = getClassName(); 74 75 String baseType = ((TypeEntry) elements.get(0)).getName(); 78 String 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 Vector values = new Vector (); 98 99 for (int i = 1; i < elements.size(); i++) { 100 String value = (String ) elements.get(i); 101 102 if (baseClass.equals("java.lang.String")) { 103 value = "\"" + value 104 + "\""; } else if (baseClass.equals("java.lang.Character")) { 106 value = "'" + value + "'"; 107 } else if (baseClass.equals("java.lang.Float")) { 108 if (!value.endsWith("F") && !value.endsWith( 110 "f")) { value += "F"; 112 } 113 } else if (baseClass.equals("java.lang.Long")) { 114 if (!value.endsWith("L") && !value.endsWith( 116 "l")) { 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 value = "new " + baseClass + "(\"" + value + "\")"; 126 } 127 128 values.add(value); 129 } 130 131 Vector ids = getEnumValueIds(elements); 133 134 pw.println(" private " + baseType + " _value_;"); 136 137 pw.println( 139 " private static java.util.HashMap _table_ = new java.util.HashMap();"); 140 pw.println(""); 141 142 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 for (int i = 0; i < ids.size(); i++) { 161 162 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 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 pw.println(" public " + baseType + " getValue() { return _value_;}"); 189 190 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 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 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 pw.println( 264 " public boolean equals(java.lang.Object obj) {return (obj == this);}"); 265 266 pw.println( 268 " public int hashCode() { return toString().hashCode();}"); 269 270 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 } 324 332 public static Vector getEnumValueIds(Vector bv) { 333 334 boolean validJava = true; 336 for (int i = 1; (i < bv.size()) && validJava; i++) { 338 String value = (String ) bv.get(i); 339 340 if (!JavaUtils.isJavaId(value)) { 341 validJava = false; 342 } 343 } 344 345 Vector ids = new Vector (); 347 348 for (int i = 1; i < bv.size(); i++) { 349 350 if (!validJava) { 353 ids.add("value" + i); 354 } else { 355 ids.add((String ) bv.get(i)); 356 } 357 } 358 359 return ids; 360 } 361 362 365 public void generate() throws IOException { 366 String fqcn = getPackage() + "." + getClassName(); 367 if (emitter.isDeploy()) { 368 if (!emitter.doesExist(fqcn)) { 369 super.generate(); 370 } 371 } else { 372 super.generate(); 373 } 374 } 375 } | Popular Tags |