1 55 package org.jboss.axis.wsdl.toJava; 56 57 import org.jboss.axis.utils.Messages; 58 import org.jboss.axis.wsdl.symbolTable.DefinedType; 59 import org.jboss.axis.wsdl.symbolTable.ElementDecl; 60 import org.jboss.axis.wsdl.symbolTable.TypeEntry; 61 62 import javax.xml.namespace.QName ; 63 import java.io.IOException ; 64 import java.io.PrintWriter ; 65 import java.util.Vector ; 66 67 70 public class JavaBeanHelperWriter extends JavaClassWriter 71 { 72 protected TypeEntry type; 73 protected Vector elements; 74 protected Vector attributes; 75 protected TypeEntry extendType; 76 protected PrintWriter wrapperPW = null; 77 protected Vector elementMetaData = null; 78 79 88 protected JavaBeanHelperWriter(Emitter emitter, 89 TypeEntry type, 90 Vector elements, 91 TypeEntry extendType, 92 Vector attributes) 93 { 94 super(emitter, type.getName() + "_Helper", "helper"); 95 this.type = type; 96 this.elements = elements; 97 this.attributes = attributes; 98 this.extendType = extendType; 99 } 101 107 protected void setPrintWriter(PrintWriter pw) 108 { 109 this.wrapperPW = pw; 110 } 112 119 protected PrintWriter getPrintWriter(String filename) throws IOException 120 { 121 return wrapperPW == null ? super.getPrintWriter(filename) : wrapperPW; 122 } 124 128 protected void registerFile(String file) 129 { 130 if (wrapperPW == null) 131 super.registerFile(file); 132 } 134 138 protected String verboseMessage(String file) 139 { 140 if (wrapperPW == null) 141 { 142 return super.verboseMessage(file); 143 } 144 else 145 { 146 return null; 147 } 148 } 150 154 protected void writeFileHeader(PrintWriter pw) throws IOException 155 { 156 if (wrapperPW == null) 157 { 158 super.writeFileHeader(pw); 159 } 160 } 162 165 protected void writeFileBody(PrintWriter pw) throws IOException 166 { 167 writeMetaData(pw); 168 writeSerializer(pw); 169 writeDeserializer(pw); 170 } 172 176 protected void writeFileFooter(PrintWriter pw) throws IOException 177 { 178 if (wrapperPW == null) 179 { 180 super.writeFileFooter(pw); 181 } 182 } 184 190 protected void closePrintWriter(PrintWriter pw) 191 { 192 if (wrapperPW == null) 196 { 197 pw.close(); 198 } 199 } 201 204 protected void writeMetaData(PrintWriter pw) throws IOException 205 { 206 if (elements != null) 208 { 209 for (int i = 0; i < elements.size(); i++) 210 { 211 ElementDecl elem = (ElementDecl)elements.get(i); 212 215 219 220 if (elementMetaData == null) 237 elementMetaData = new Vector (); 238 239 elementMetaData.add(elem); 240 } 242 } 243 pw.println(" // " + Messages.getMessage("typeMeta")); 244 pw.println(" private static org.jboss.axis.description.TypeDesc typeDesc ="); 245 pw.println(" new org.jboss.axis.description.TypeDesc(" + 246 Utils.getJavaLocalName(type.getName()) + ".class);"); 247 pw.println(); 248 249 pw.println(" static {"); 250 pw.println(" typeDesc.setXmlType(" + Utils.getNewQName(type.getQName()) + ");"); 251 252 if (attributes != null || elementMetaData != null) 254 { 255 if (attributes != null) 256 { 257 boolean wroteAttrDecl = false; 258 259 for (int i = 0; i < attributes.size(); i += 2) 260 { 261 TypeEntry te = (TypeEntry)attributes.get(i); 262 QName attrName = (QName )attributes.get(i + 1); 263 String attrLocalName = attrName.getLocalPart(); 264 String fieldName = Utils.xmlNameToJava(attrLocalName); 265 fieldName = getAsFieldName(fieldName); 266 QName attrXmlType = te.getQName(); 267 pw.print(" "); 268 if (!wroteAttrDecl) 269 { 270 pw.print("org.jboss.axis.description.AttributeDesc "); 271 wroteAttrDecl = true; 272 } 273 pw.println("attrField = new org.jboss.axis.description.AttributeDesc();"); 274 pw.println(" attrField.setFieldName(\"" + fieldName + "\");"); 275 pw.println(" attrField.setXmlName(" + Utils.getNewQName(attrName) + ");"); 276 if (attrXmlType != null) 277 { 278 pw.println(" attrField.setXmlType(" + Utils.getNewQName(attrXmlType) + ");"); 279 } 280 pw.println(" typeDesc.addFieldDesc(attrField);"); 281 } 282 } 283 284 if (elementMetaData != null) 285 { 286 boolean wroteElemDecl = false; 287 288 for (int i = 0; i < elementMetaData.size(); i++) 289 { 290 ElementDecl elem = (ElementDecl)elementMetaData.elementAt(i); 291 292 if (elem.getAnyElement()) 293 { 294 continue; 295 } 296 297 String elemLocalName = elem.getName().getLocalPart(); 298 String fieldName = Utils.xmlNameToJava(elemLocalName); 299 fieldName = getAsFieldName(fieldName); 300 QName xmlName = elem.getName(); 301 302 TypeEntry elemType = elem.getType(); 304 QName xmlType = null; 305 306 if (elemType.getDimensions().length() > 1 && 307 (elemType.getClass() == DefinedType.class)) 308 { 309 xmlType = elemType.getRefType().getQName(); 313 } 314 else 315 { 316 while (elemType.getRefType() != null) 319 { 320 elemType = elemType.getRefType(); 321 } 322 xmlType = elemType.getQName(); 323 } 324 325 pw.print(" "); 326 if (!wroteElemDecl) 327 { 328 pw.print("org.jboss.axis.description.ElementDesc "); 329 wroteElemDecl = true; 330 } 331 pw.println("elemField = new org.jboss.axis.description.ElementDesc();"); 332 pw.println(" elemField.setFieldName(\"" + fieldName + "\");"); 333 pw.println(" elemField.setXmlName(" + Utils.getNewQName(xmlName) + ");"); 334 if (xmlType != null) 335 { 336 pw.println(" elemField.setXmlType(" + Utils.getNewQName(xmlType) + ");"); 337 } 338 if (elem.getMinOccursIs0()) 339 { 340 pw.println(" elemField.setMinOccurs(0);"); 341 } 342 pw.println(" typeDesc.addFieldDesc(elemField);"); 343 } 344 } 345 } 346 347 pw.println(" }"); 348 pw.println(); 349 350 pw.println(" /**"); 351 pw.println(" * " + Messages.getMessage("returnTypeMeta")); 352 pw.println(" */"); 353 pw.println(" public static org.jboss.axis.description.TypeDesc getTypeDesc() {"); 354 pw.println(" return typeDesc;"); 355 pw.println(" }"); 356 pw.println(); 357 } 358 359 369 private String getAsFieldName(String fieldName) 370 { 371 if (fieldName.length() > 1 && 375 Character.isUpperCase(fieldName.charAt(1))) 376 { 377 return Utils.capitalizeFirstChar(fieldName); 378 } 379 380 return fieldName; 381 } 382 383 387 protected void writeSerializer(PrintWriter pw) throws IOException 388 { 389 String typeDesc = "typeDesc"; 390 String ser = " org.jboss.axis.encoding.ser.BeanSerializer"; 391 if (type.isSimpleType()) 392 { 393 ser = " org.jboss.axis.encoding.ser.SimpleSerializer"; 394 } 395 pw.println(" /**"); 396 pw.println(" * Get Custom Serializer"); 397 pw.println(" */"); 398 pw.println(" public static org.jboss.axis.encoding.Serializer getSerializer("); 399 pw.println(" java.lang.String mechType, "); 400 pw.println(" java.lang.Class _javaType, "); 401 pw.println(" javax.xml.namespace.QName _xmlType) {"); 402 pw.println(" return "); 403 pw.println(" new " + ser + "("); 404 pw.println(" _javaType, _xmlType, " + typeDesc + ");"); 405 pw.println(" }"); 406 pw.println(); 407 } 408 409 413 protected void writeDeserializer(PrintWriter pw) throws IOException 414 { 415 String typeDesc = "typeDesc"; 416 String dser = " org.jboss.axis.encoding.ser.BeanDeserializer"; 417 if (type.isSimpleType()) 418 { 419 dser = " org.jboss.axis.encoding.ser.SimpleDeserializer"; 420 } 421 pw.println(" /**"); 422 pw.println(" * Get Custom Deserializer"); 423 pw.println(" */"); 424 pw.println(" public static org.jboss.axis.encoding.Deserializer getDeserializer("); 425 pw.println(" java.lang.String mechType, "); 426 pw.println(" java.lang.Class _javaType, "); 427 pw.println(" javax.xml.namespace.QName _xmlType) {"); 428 pw.println(" return "); 429 pw.println(" new " + dser + "("); 430 pw.println(" _javaType, _xmlType, " + typeDesc + ");"); 431 pw.println(" }"); 432 pw.println(); 433 } 434 } | Popular Tags |