KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Set JavaDoc;
32
33 /**
34  * This is Wsdl2java's Helper Type Writer. It writes the <typeName>.java file.
35  */

36 public class JavaBeanHelperWriter extends JavaClassWriter {
37
38     /** Field type */
39     protected TypeEntry type;
40
41     /** Field elements */
42     protected Vector JavaDoc elements;
43
44     /** Field attributes */
45     protected Vector JavaDoc attributes;
46
47     /** Field extendType */
48     protected TypeEntry extendType;
49
50     /** Field wrapperPW */
51     protected PrintWriter JavaDoc wrapperPW = null;
52
53     /** Field elementMetaData */
54     protected Vector JavaDoc elementMetaData = null;
55
56     /** Field canSearchParents */
57     protected boolean canSearchParents;
58
59     /** Field reservedPropNames */
60     protected Set JavaDoc reservedPropNames;
61
62     /**
63      * Constructor.
64      *
65      * @param emitter
66      * @param type The type representing this class
67      * @param elements Vector containing the Type and name of each property
68      * @param extendType The type representing the extended class (or null)
69      * @param attributes Vector containing the attribute types and names
70      */

71     protected JavaBeanHelperWriter(Emitter emitter, TypeEntry type,
72                                    Vector JavaDoc elements, TypeEntry extendType,
73                                    Vector JavaDoc attributes, Set JavaDoc 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         // is this a complex type that is derived from other types
84
// by restriction? if so, set the policy of the generated
85
// TypeDescription to ignore metadata associated with
86
// superclasses, as restricted types are required to
87
// define their entire content model. Hence the type
88
// description associated with the current type provides
89
// all of the types (and only those types) allowed in
90
// the restricted derivation.
91
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     } // 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      * @param pw
108      */

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

113     /**
114      * The default behaviour (of super.getPrintWriter) is, given the
115      * file name, create a PrintWriter for it. If the bean helper
116      * that this class is generating is embedded within a bean, then
117      * the PrintWriter returned by this method is the JavaBeanWriter's
118      * PrintWriter. Otherwise super.getPrintWriter is called.
119      *
120      * @param filename
121      * @return
122      * @throws IOException
123      */

124     protected PrintWriter JavaDoc getPrintWriter(String JavaDoc filename) throws IOException JavaDoc {
125
126         return (wrapperPW == null)
127                 ? super.getPrintWriter(filename)
128                 : wrapperPW;
129     } // getPrintWriter
130

131     /**
132      * Only register the filename if the bean helper is not wrapped
133      * within a bean.
134      *
135      * @param file
136      */

137     protected void registerFile(String JavaDoc file) {
138
139         if (wrapperPW == null) {
140             super.registerFile(file);
141         }
142     } // registerFile
143

144     /**
145      * Return the string: "Generating <file>".
146      * only if we are going to generate a new file.
147      *
148      * @param file
149      * @return
150      */

151     protected String JavaDoc verboseMessage(String JavaDoc file) {
152
153         if (wrapperPW == null) {
154             return super.verboseMessage(file);
155         } else {
156             return null;
157         }
158     } // verboseMessage
159

160     /**
161      * Only write the file header if the bean helper is not wrapped
162      * within a bean.
163      *
164      * @param pw
165      * @throws IOException
166      */

167     protected void writeFileHeader(PrintWriter JavaDoc pw) throws IOException JavaDoc {
168
169         if (wrapperPW == null) {
170             super.writeFileHeader(pw);
171         }
172     } // writeFileHeader
173

174     /**
175      * Generate the file body for the bean helper.
176      *
177      * @param pw
178      * @throws IOException
179      */

180     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
181
182         writeMetaData(pw);
183         writeSerializer(pw);
184         writeDeserializer(pw);
185     } // writeFileBody
186

187     /**
188      * Only write the file footer if the bean helper is not
189      * wrapped within a bean.
190      *
191      * @param pw
192      * @throws IOException
193      */

194     protected void writeFileFooter(PrintWriter JavaDoc pw) throws IOException JavaDoc {
195
196         if (wrapperPW == null) {
197             super.writeFileFooter(pw);
198         }
199     } // writeFileFooter
200

201     /**
202      * Only close the PrintWriter if the PrintWriter belongs to
203      * this class. If the bean helper is embedded within a bean
204      * then the PrintWriter belongs to JavaBeanWriter and THAT
205      * class is responsible for closing the PrintWriter.
206      *
207      * @param pw
208      */

209     protected void closePrintWriter(PrintWriter JavaDoc pw) {
210
211         // If the output of this writer is wrapped within
212
// another writer (JavaBeanWriter), then THAT
213
// writer will close the PrintWriter, not this one.
214
if (wrapperPW == null) {
215             pw.close();
216         }
217     } // closePrintWriter
218

219     /**
220      * write MetaData code
221      *
222      * @param pw
223      * @throws IOException
224      */

225     protected void writeMetaData(PrintWriter JavaDoc pw) throws IOException JavaDoc {
226
227         // Collect elementMetaData
228
if (elements != null) {
229             for (int i = 0; i < elements.size(); i++) {
230                 ElementDecl elem = (ElementDecl) elements.get(i);
231
232                 // String elemName = elem.getName().getLocalPart();
233
// String javaName = Utils.xmlNameToJava(elemName);
234
// Changed the code to write meta data
235
// for all of the elements in order to
236
// support sequences. Defect 9060
237
// Meta data is needed if the default serializer
238
// action cannot map the javaName back to the
239
// element's qname. This occurs if:
240
// - the javaName and element name local part are different.
241
// - the javaName starts with uppercase char (this is a wierd
242
// case and we have several problems with the mapping rules.
243
// Seems best to gen meta data in this case.)
244
// - the element name is qualified (has a namespace uri)
245
// its also needed if:
246
// - the element has the minoccurs flag set
247
// if (!javaName.equals(elemName) ||
248
// Character.isUpperCase(javaName.charAt(0)) ||
249
// !elem.getName().getNamespaceURI().equals("") ||
250
// elem.getMinOccursIs0()) {
251
// If we did some mangling, make sure we'll write out the XML
252
// the correct way.
253
if (elementMetaData == null) {
254                     elementMetaData = new Vector JavaDoc();
255                 }
256
257                 elementMetaData.add(elem);
258
259                 // }
260
}
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         // Add attribute and element field descriptors
277
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 JavaDoc attrName = attr.getQName();
285                     String JavaDoc fieldName = getAsFieldName(attr.getName());
286
287                     QName JavaDoc 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 JavaDoc fieldName = getAsFieldName(elem.getName());
325                     QName JavaDoc xmlName = elem.getQName();
326
327                     // Some special handling for arrays.
328
TypeEntry elemType = elem.getType();
329                     QName JavaDoc xmlType = null;
330
331                     if ((elemType.getDimensions().length() > 1)
332                             && (elemType.getClass() == DefinedType.class)) {
333
334                         // If we have a DefinedType with dimensions, it must
335
// be a SOAP array derived type. In this case, use
336
// the refType's QName for the metadata.
337
elemType = elemType.getRefType();
338                     } else {
339                         // Otherwise, use the first non-Collection type we
340
// encounter up the ref chain.
341
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 JavaDoc 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     /**
403      * Utility function to get the bean property name (as will be returned
404      * by the Introspector) for a given field name. This just means
405      * we capitalize the first character if the second character is
406      * capitalized. Example: a field named "fOO" will turn into
407      * getter/setter methods "getFOO()/setFOO()". So when the Introspector
408      * looks at that bean, the property name will be "FOO", not "fOO" due
409      * to the rules in the JavaBeans spec. So this makes sure the
410      * metadata will match. <p>
411      *
412      * The method also makes sure that the returned property name is not in
413      * the set of reserved properties as defined by {@link #reservedPropNames}.
414      *
415      * @param fieldName
416      * @return
417      */

418     private String JavaDoc getAsFieldName(String JavaDoc fieldName) {
419
420         // If there's a second character, and it is uppercase, then the
421
// bean property name will have a capitalized first character
422
// (because setURL() maps to a property named "URL", not "uRL")
423
if ((fieldName.length() > 1)
424                 && Character.isUpperCase(fieldName.charAt(1))) {
425             fieldName = Utils.capitalizeFirstChar(fieldName);
426         }
427
428         // Make sure the property name is not reserved.
429
return JavaUtils.getUniqueValue(reservedPropNames, fieldName);
430     }
431
432     /**
433      * write Serializer getter code and pass in meta data to avoid
434      * undo introspection.
435      *
436      * @param pw
437      * @throws IOException
438      */

439     protected void writeSerializer(PrintWriter JavaDoc pw) throws IOException JavaDoc {
440
441         String JavaDoc typeDesc = "typeDesc";
442         String JavaDoc 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     /**
464      * write Deserializer getter code and pass in meta data to avoid
465      * undo introspection.
466      *
467      * @param pw
468      * @throws IOException
469      */

470     protected void writeDeserializer(PrintWriter JavaDoc pw) throws IOException JavaDoc {
471
472         String JavaDoc typeDesc = "typeDesc";
473         String JavaDoc 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 } // class JavaBeanHelperWriter
494
Popular Tags