KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > wsdl > toJava > JavaBeanHelperWriter


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

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 JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.io.PrintWriter JavaDoc;
65 import java.util.Vector JavaDoc;
66
67 /**
68  * This is Wsdl2java's Helper Type Writer. It writes the <typeName>.java file.
69  */

70 public class JavaBeanHelperWriter extends JavaClassWriter
71 {
72    protected TypeEntry type;
73    protected Vector JavaDoc elements;
74    protected Vector JavaDoc attributes;
75    protected TypeEntry extendType;
76    protected PrintWriter JavaDoc wrapperPW = null;
77    protected Vector JavaDoc elementMetaData = null;
78
79    /**
80     * Constructor.
81     *
82     * @param emitter
83     * @param type The type representing this class
84     * @param elements Vector containing the Type and name of each property
85     * @param extendType The type representing the extended class (or null)
86     * @param attributes Vector containing the attribute types and names
87     */

88    protected JavaBeanHelperWriter(Emitter emitter,
89                                   TypeEntry type,
90                                   Vector JavaDoc elements,
91                                   TypeEntry extendType,
92                                   Vector JavaDoc 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    } // ctor
100

101    /**
102     * The bean helper class may be its own class, or it may be
103     * embedded within the bean class. If it's embedded within the
104     * bean class, the JavaBeanWriter will set JavaBeanHelperWriter's
105     * PrintWriter to its own.
106     */

107    protected void setPrintWriter(PrintWriter JavaDoc pw)
108    {
109       this.wrapperPW = pw;
110    } // setPrintWriter
111

112    /**
113     * The default behaviour (of super.getPrintWriter) is, given the
114     * file name, create a PrintWriter for it. If the bean helper
115     * that this class is generating is embedded within a bean, then
116     * the PrintWriter returned by this method is the JavaBeanWriter's
117     * PrintWriter. Otherwise super.getPrintWriter is called.
118     */

119    protected PrintWriter JavaDoc getPrintWriter(String JavaDoc filename) throws IOException JavaDoc
120    {
121       return wrapperPW == null ? super.getPrintWriter(filename) : wrapperPW;
122    } // getPrintWriter
123

124    /**
125     * Only register the filename if the bean helper is not wrapped
126     * within a bean.
127     */

128    protected void registerFile(String JavaDoc file)
129    {
130       if (wrapperPW == null)
131          super.registerFile(file);
132    } // registerFile
133

134    /**
135     * Return the string: "Generating <file>".
136     * only if we are going to generate a new file.
137     */

138    protected String JavaDoc verboseMessage(String JavaDoc file)
139    {
140       if (wrapperPW == null)
141       {
142          return super.verboseMessage(file);
143       }
144       else
145       {
146          return null;
147       }
148    } // verboseMessage
149

150    /**
151     * Only write the file header if the bean helper is not wrapped
152     * within a bean.
153     */

154    protected void writeFileHeader(PrintWriter JavaDoc pw) throws IOException JavaDoc
155    {
156       if (wrapperPW == null)
157       {
158          super.writeFileHeader(pw);
159       }
160    } // writeFileHeader
161

162    /**
163     * Generate the file body for the bean helper.
164     */

165    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
166    {
167       writeMetaData(pw);
168       writeSerializer(pw);
169       writeDeserializer(pw);
170    } // writeFileBody
171

172    /**
173     * Only write the file footer if the bean helper is not
174     * wrapped within a bean.
175     */

176    protected void writeFileFooter(PrintWriter JavaDoc pw) throws IOException JavaDoc
177    {
178       if (wrapperPW == null)
179       {
180          super.writeFileFooter(pw);
181       }
182    } // writeFileFooter
183

184    /**
185     * Only close the PrintWriter if the PrintWriter belongs to
186     * this class. If the bean helper is embedded within a bean
187     * then the PrintWriter belongs to JavaBeanWriter and THAT
188     * class is responsible for closing the PrintWriter.
189     */

190    protected void closePrintWriter(PrintWriter JavaDoc pw)
191    {
192       // If the output of this writer is wrapped within
193
// another writer (JavaBeanWriter), then THAT
194
// writer will close the PrintWriter, not this one.
195
if (wrapperPW == null)
196       {
197          pw.close();
198       }
199    } // closePrintWriter
200

201    /**
202     * write MetaData code
203     */

204    protected void writeMetaData(PrintWriter JavaDoc pw) throws IOException JavaDoc
205    {
206       // Collect elementMetaData
207
if (elements != null)
208       {
209          for (int i = 0; i < elements.size(); i++)
210          {
211             ElementDecl elem = (ElementDecl)elements.get(i);
212             // String elemName = elem.getName().getLocalPart();
213
// String javaName = Utils.xmlNameToJava(elemName);
214

215             // Changed the code to write meta data
216
// for all of the elements in order to
217
// support sequences. Defect 9060
218

219
220             // Meta data is needed if the default serializer
221
// action cannot map the javaName back to the
222
// element's qname. This occurs if:
223
// - the javaName and element name local part are different.
224
// - the javaName starts with uppercase char (this is a wierd
225
// case and we have several problems with the mapping rules.
226
// Seems best to gen meta data in this case.)
227
// - the element name is qualified (has a namespace uri)
228
// its also needed if:
229
// - the element has the minoccurs flag set
230
//if (!javaName.equals(elemName) ||
231
// Character.isUpperCase(javaName.charAt(0)) ||
232
//!elem.getName().getNamespaceURI().equals("") ||
233
//elem.getMinOccursIs0()) {
234
// If we did some mangling, make sure we'll write out the XML
235
// the correct way.
236
if (elementMetaData == null)
237                elementMetaData = new Vector JavaDoc();
238
239             elementMetaData.add(elem);
240             //}
241
}
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       // Add attribute and element field descriptors
253
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 JavaDoc attrName = (QName JavaDoc)attributes.get(i + 1);
263                String JavaDoc attrLocalName = attrName.getLocalPart();
264                String JavaDoc fieldName = Utils.xmlNameToJava(attrLocalName);
265                fieldName = getAsFieldName(fieldName);
266                QName JavaDoc 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 JavaDoc elemLocalName = elem.getName().getLocalPart();
298                String JavaDoc fieldName = Utils.xmlNameToJava(elemLocalName);
299                fieldName = getAsFieldName(fieldName);
300                QName JavaDoc xmlName = elem.getName();
301
302                // Some special handling for arrays.
303
TypeEntry elemType = elem.getType();
304                QName JavaDoc xmlType = null;
305
306                if (elemType.getDimensions().length() > 1 &&
307                        (elemType.getClass() == DefinedType.class))
308                {
309                   // If we have a DefinedType with dimensions, it must
310
// be a SOAP array derived type. In this case, use
311
// the refType's QName for the metadata.
312
xmlType = elemType.getRefType().getQName();
313                }
314                else
315                {
316                   // Otherwise, use the type at the end of the ref
317
// chain.
318
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    /**
360     * Utility function to get the bean property name (as will be returned
361     * by the Introspector) for a given field name. This just means
362     * we capitalize the first character if the second character is
363     * capitalized. Example: a field named "fOO" will turn into
364     * getter/setter methods "getFOO()/setFOO()". So when the Introspector
365     * looks at that bean, the property name will be "FOO", not "fOO" due
366     * to the rules in the JavaBeans spec. So this makes sure the
367     * metadata will match.
368     */

369    private String JavaDoc getAsFieldName(String JavaDoc fieldName)
370    {
371       // If there's a second character, and it is uppercase, then the
372
// bean property name will have a capitalized first character
373
// (because setURL() maps to a property named "URL", not "uRL")
374
if (fieldName.length() > 1 &&
375               Character.isUpperCase(fieldName.charAt(1)))
376       {
377          return Utils.capitalizeFirstChar(fieldName);
378       }
379
380       return fieldName;
381    }
382
383    /**
384     * write Serializer getter code and pass in meta data to avoid
385     * undo introspection.
386     */

387    protected void writeSerializer(PrintWriter JavaDoc pw) throws IOException JavaDoc
388    {
389       String JavaDoc typeDesc = "typeDesc";
390       String JavaDoc 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    /**
410     * write Deserializer getter code and pass in meta data to avoid
411     * undo introspection.
412     */

413    protected void writeDeserializer(PrintWriter JavaDoc pw) throws IOException JavaDoc
414    {
415       String JavaDoc typeDesc = "typeDesc";
416       String JavaDoc 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 } // class JavaBeanHelperWriter
435
Popular Tags