1 16 package org.apache.axis.wsdl.toJava; 17 18 import org.apache.axis.utils.Messages; 19 import org.apache.axis.wsdl.symbolTable.ContainedAttribute; 20 import org.apache.axis.utils.JavaUtils; 21 import org.apache.axis.wsdl.symbolTable.DefinedType; 22 import org.apache.axis.wsdl.symbolTable.ElementDecl; 23 import org.apache.axis.wsdl.symbolTable.SchemaUtils; 24 import org.apache.axis.wsdl.symbolTable.TypeEntry; 25 import org.apache.axis.wsdl.symbolTable.CollectionTE; 26 27 import javax.xml.namespace.QName ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.util.Vector ; 31 import java.util.Set ; 32 33 36 public class JavaBeanHelperWriter extends JavaClassWriter { 37 38 39 protected TypeEntry type; 40 41 42 protected Vector elements; 43 44 45 protected Vector attributes; 46 47 48 protected TypeEntry extendType; 49 50 51 protected PrintWriter wrapperPW = null; 52 53 54 protected Vector elementMetaData = null; 55 56 57 protected boolean canSearchParents; 58 59 60 protected Set reservedPropNames; 61 62 71 protected JavaBeanHelperWriter(Emitter emitter, TypeEntry type, 72 Vector elements, TypeEntry extendType, 73 Vector attributes, Set reservedPropNames) { 74 75 super(emitter, type.getName() + "_Helper", "helper"); 76 77 this.type = type; 78 this.elements = elements; 79 this.attributes = attributes; 80 this.extendType = extendType; 81 this.reservedPropNames = reservedPropNames; 82 83 if ((null != extendType) 92 && (null 93 != SchemaUtils.getComplexElementRestrictionBase( 94 type.getNode(), emitter.getSymbolTable()))) { 95 this.canSearchParents = false; 96 } else { 97 this.canSearchParents = true; 98 } 99 } 101 109 protected void setPrintWriter(PrintWriter pw) { 110 this.wrapperPW = pw; 111 } 113 124 protected PrintWriter getPrintWriter(String filename) throws IOException { 125 126 return (wrapperPW == null) 127 ? super.getPrintWriter(filename) 128 : wrapperPW; 129 } 131 137 protected void registerFile(String file) { 138 139 if (wrapperPW == null) { 140 super.registerFile(file); 141 } 142 } 144 151 protected String verboseMessage(String file) { 152 153 if (wrapperPW == null) { 154 return super.verboseMessage(file); 155 } else { 156 return null; 157 } 158 } 160 167 protected void writeFileHeader(PrintWriter pw) throws IOException { 168 169 if (wrapperPW == null) { 170 super.writeFileHeader(pw); 171 } 172 } 174 180 protected void writeFileBody(PrintWriter pw) throws IOException { 181 182 writeMetaData(pw); 183 writeSerializer(pw); 184 writeDeserializer(pw); 185 } 187 194 protected void writeFileFooter(PrintWriter pw) throws IOException { 195 196 if (wrapperPW == null) { 197 super.writeFileFooter(pw); 198 } 199 } 201 209 protected void closePrintWriter(PrintWriter pw) { 210 211 if (wrapperPW == null) { 215 pw.close(); 216 } 217 } 219 225 protected void writeMetaData(PrintWriter pw) throws IOException { 226 227 if (elements != null) { 229 for (int i = 0; i < elements.size(); i++) { 230 ElementDecl elem = (ElementDecl) elements.get(i); 231 232 if (elementMetaData == null) { 254 elementMetaData = new Vector (); 255 } 256 257 elementMetaData.add(elem); 258 259 } 261 } 262 263 pw.println(" // " + Messages.getMessage("typeMeta")); 264 pw.println( 265 " private static org.apache.axis.description.TypeDesc typeDesc ="); 266 pw.println(" new org.apache.axis.description.TypeDesc(" 267 + Utils.getJavaLocalName(type.getName()) + ".class, " 268 + (this.canSearchParents 269 ? "true" 270 : "false") + ");"); 271 pw.println(); 272 pw.println(" static {"); 273 pw.println(" typeDesc.setXmlType(" 274 + Utils.getNewQName(type.getQName()) + ");"); 275 276 if ((attributes != null) || (elementMetaData != null)) { 278 if (attributes != null) { 279 boolean wroteAttrDecl = false; 280 281 for (int i = 0; i < attributes.size(); i++) { 282 ContainedAttribute attr = (ContainedAttribute) attributes.get(i); 283 TypeEntry te = attr.getType(); 284 QName attrName = attr.getQName(); 285 String fieldName = getAsFieldName(attr.getName()); 286 287 QName attrXmlType = te.getQName(); 288 289 pw.print(" "); 290 291 if (!wroteAttrDecl) { 292 pw.print("org.apache.axis.description.AttributeDesc "); 293 294 wroteAttrDecl = true; 295 } 296 297 pw.println( 298 "attrField = new org.apache.axis.description.AttributeDesc();"); 299 pw.println(" attrField.setFieldName(\"" + fieldName 300 + "\");"); 301 pw.println(" attrField.setXmlName(" 302 + Utils.getNewQNameWithLastLocalPart(attrName) + ");"); 303 304 if (attrXmlType != null) { 305 pw.println(" attrField.setXmlType(" 306 + Utils.getNewQName(attrXmlType) + ");"); 307 } 308 309 pw.println(" typeDesc.addFieldDesc(attrField);"); 310 } 311 } 312 313 if (elementMetaData != null) { 314 boolean wroteElemDecl = false; 315 316 for (int i = 0; i < elementMetaData.size(); i++) { 317 ElementDecl elem = 318 (ElementDecl) elementMetaData.elementAt(i); 319 320 if (elem.getAnyElement()) { 321 continue; 322 } 323 324 String fieldName = getAsFieldName(elem.getName()); 325 QName xmlName = elem.getQName(); 326 327 TypeEntry elemType = elem.getType(); 329 QName xmlType = null; 330 331 if ((elemType.getDimensions().length() > 1) 332 && (elemType.getClass() == DefinedType.class)) { 333 334 elemType = elemType.getRefType(); 338 } else { 339 while (elemType instanceof CollectionTE) { 342 elemType = elemType.getRefType(); 343 } 344 } 345 xmlType = elemType.getQName(); 346 347 pw.print(" "); 348 349 if (!wroteElemDecl) { 350 pw.print("org.apache.axis.description.ElementDesc "); 351 352 wroteElemDecl = true; 353 } 354 355 pw.println( 356 "elemField = new org.apache.axis.description.ElementDesc();"); 357 pw.println(" elemField.setFieldName(\"" + fieldName 358 + "\");"); 359 pw.println(" elemField.setXmlName(" 360 + Utils.getNewQNameWithLastLocalPart(xmlName) + ");"); 361 362 if (xmlType != null) { 363 pw.println(" elemField.setXmlType(" 364 + Utils.getNewQName(xmlType) + ");"); 365 } 366 367 if (elem.getMinOccursIs0()) { 368 pw.println(" elemField.setMinOccurs(0);"); 369 } 370 if (elem.getNillable()) { 371 pw.println(" elemField.setNillable(true);"); 372 } else { 373 pw.println(" elemField.setNillable(false);"); 374 } 375 376 if(elem.getMaxOccursIsUnbounded()) { 377 pw.println(" elemField.setMaxOccursUnbounded(true);"); 378 } 379 QName itemQName = elem.getType().getItemQName(); 380 if (itemQName != null) { 381 pw.println(" elemField.setItemQName(" + 382 Utils.getNewQName(itemQName) + ");"); 383 } 384 385 pw.println(" typeDesc.addFieldDesc(elemField);"); 386 } 387 } 388 } 389 390 pw.println(" }"); 391 pw.println(); 392 pw.println(" /**"); 393 pw.println(" * " + Messages.getMessage("returnTypeMeta")); 394 pw.println(" */"); 395 pw.println( 396 " public static org.apache.axis.description.TypeDesc getTypeDesc() {"); 397 pw.println(" return typeDesc;"); 398 pw.println(" }"); 399 pw.println(); 400 } 401 402 418 private String getAsFieldName(String fieldName) { 419 420 if ((fieldName.length() > 1) 424 && Character.isUpperCase(fieldName.charAt(1))) { 425 fieldName = Utils.capitalizeFirstChar(fieldName); 426 } 427 428 return JavaUtils.getUniqueValue(reservedPropNames, fieldName); 430 } 431 432 439 protected void writeSerializer(PrintWriter pw) throws IOException { 440 441 String typeDesc = "typeDesc"; 442 String ser = " org.apache.axis.encoding.ser.BeanSerializer"; 443 444 if (type.isSimpleType()) { 445 ser = " org.apache.axis.encoding.ser.SimpleSerializer"; 446 } 447 448 pw.println(" /**"); 449 pw.println(" * Get Custom Serializer"); 450 pw.println(" */"); 451 pw.println( 452 " public static org.apache.axis.encoding.Serializer getSerializer("); 453 pw.println(" java.lang.String mechType, "); 454 pw.println(" java.lang.Class _javaType, "); 455 pw.println(" javax.xml.namespace.QName _xmlType) {"); 456 pw.println(" return "); 457 pw.println(" new " + ser + "("); 458 pw.println(" _javaType, _xmlType, " + typeDesc + ");"); 459 pw.println(" }"); 460 pw.println(); 461 } 462 463 470 protected void writeDeserializer(PrintWriter pw) throws IOException { 471 472 String typeDesc = "typeDesc"; 473 String dser = " org.apache.axis.encoding.ser.BeanDeserializer"; 474 475 if (type.isSimpleType()) { 476 dser = " org.apache.axis.encoding.ser.SimpleDeserializer"; 477 } 478 479 pw.println(" /**"); 480 pw.println(" * Get Custom Deserializer"); 481 pw.println(" */"); 482 pw.println( 483 " public static org.apache.axis.encoding.Deserializer getDeserializer("); 484 pw.println(" java.lang.String mechType, "); 485 pw.println(" java.lang.Class _javaType, "); 486 pw.println(" javax.xml.namespace.QName _xmlType) {"); 487 pw.println(" return "); 488 pw.println(" new " + dser + "("); 489 pw.println(" _javaType, _xmlType, " + typeDesc + ");"); 490 pw.println(" }"); 491 pw.println(); 492 } 493 } | Popular Tags |