KickJava   Java API By Example, From Geeks To Geeks.

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


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.wsdl.gen.Generator;
19 import org.apache.axis.wsdl.symbolTable.SchemaUtils;
20 import org.apache.axis.wsdl.symbolTable.SymTabEntry;
21 import org.apache.axis.wsdl.symbolTable.SymbolTable;
22 import org.apache.axis.wsdl.symbolTable.Type;
23 import org.apache.axis.wsdl.symbolTable.TypeEntry;
24 import org.w3c.dom.Node JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 /**
32  * This is Wsdl2java's Type Writer. It writes the following files, as appropriate:
33  * <typeName>.java, <typeName>Holder.java.
34  */

35 public class JavaTypeWriter implements Generator {
36
37     /** Field HOLDER_IS_NEEDED */
38     public static final String JavaDoc HOLDER_IS_NEEDED = "Holder is needed";
39
40     /** Field typeWriter */
41     private Generator typeWriter = null;
42
43     /** Field holderWriter */
44     private Generator holderWriter = null;
45
46     /**
47      * Constructor.
48      *
49      * @param emitter
50      * @param type
51      * @param symbolTable
52      */

53     public JavaTypeWriter(Emitter emitter, TypeEntry type,
54                           SymbolTable symbolTable) {
55
56         if (type.isReferenced() && !type.isOnlyLiteralReferenced()) {
57
58             // Determine what sort of type this is and instantiate
59
// the appropriate Writer.
60
Node JavaDoc node = type.getNode();
61
62             boolean isSimpleList = SchemaUtils.isListWithItemType(node);
63             // If it's an array, don't emit a class
64
if (!type.getName().endsWith("[]") && !isSimpleList) {
65                 
66                 // Generate the proper class for either "complex" or "enumeration" types
67
Vector JavaDoc v = Utils.getEnumerationBaseAndValues(node, symbolTable);
68
69                 if (v != null) {
70                     typeWriter = getEnumTypeWriter(emitter, type, v);
71                 } else {
72                     TypeEntry base =
73                             SchemaUtils.getComplexElementExtensionBase(node,
74                                     symbolTable);
75
76                     if (base == null) {
77                         base = SchemaUtils.getComplexElementRestrictionBase(
78                                 node, symbolTable);
79                     }
80
81                     if (base == null) {
82                         QName JavaDoc baseQName = SchemaUtils.getSimpleTypeBase(node);
83
84                         if (baseQName != null) {
85                             base = symbolTable.getType(baseQName);
86                         }
87                     }
88
89                     typeWriter = getBeanWriter(emitter, type, base);
90                 }
91             }
92
93             // If the holder is needed (ie., something uses this type as an out or inout
94
// parameter), instantiate the holder writer.
95
if (holderIsNeeded(type)) {
96                 holderWriter = getHolderWriter(emitter, type);
97             }
98             
99             if (typeWriter != null && type instanceof Type) {
100                 ((Type)type).setGenerated(true);
101             }
102         }
103     } // ctor
104

105     /**
106      * Write all the service bindnigs: service and testcase.
107      *
108      * @throws IOException
109      */

110     public void generate() throws IOException JavaDoc {
111
112         if (typeWriter != null) {
113             typeWriter.generate();
114         }
115
116         if (holderWriter != null) {
117             holderWriter.generate();
118         }
119     } // generate
120

121     /**
122      * Does anything use this type as an inout/out parameter? Query the Type dynamicVar
123      *
124      * @param entry
125      * @return
126      */

127     private boolean holderIsNeeded(SymTabEntry entry) {
128
129         Boolean JavaDoc holderIsNeeded =
130                 (Boolean JavaDoc) entry.getDynamicVar(HOLDER_IS_NEEDED);
131
132         return ((holderIsNeeded != null) && holderIsNeeded.booleanValue());
133     } // holderIsNeeded
134

135     /**
136      * getEnumWriter
137      *
138      * @param emitter
139      * @param type
140      * @param v
141      * @return
142      */

143     protected JavaWriter getEnumTypeWriter(Emitter emitter, TypeEntry type,
144                                            Vector JavaDoc v) {
145         return new JavaEnumTypeWriter(emitter, type, v);
146     }
147
148     /**
149      * getBeanWriter
150      *
151      * @param emitter
152      * @param type
153      * @param base
154      * @return
155      */

156     protected JavaWriter getBeanWriter(Emitter emitter, TypeEntry type, TypeEntry base) { // CONTAINED_ELEM_AND_ATTR
157
Vector JavaDoc elements = type.getContainedElements();
158         Vector JavaDoc attributes = type.getContainedAttributes();
159         
160         // If this complexType is referenced in a
161
// fault context, emit a bean-like exception
162
// class
163
Boolean JavaDoc isComplexFault = (Boolean JavaDoc) type.getDynamicVar(
164                 JavaGeneratorFactory.COMPLEX_TYPE_FAULT);
165
166         if ((isComplexFault != null) && isComplexFault.booleanValue()) {
167
168             return new JavaBeanFaultWriter(emitter, type, elements, base,
169                 attributes,
170                 getBeanHelperWriter(emitter, type, elements, base,
171                                     attributes, true));
172         }
173
174         return new JavaBeanWriter(emitter, type, elements, base, attributes,
175                 getBeanHelperWriter(emitter, type, elements, base,
176                                     attributes, false));
177     }
178
179     /**
180      * getHelperWriter
181      *
182      * @param emitter
183      * @param type
184      * @param elements
185      * @param base
186      * @param attributes
187      * @return
188      */

189     protected JavaWriter getBeanHelperWriter(
190             Emitter emitter, TypeEntry type, Vector JavaDoc elements, TypeEntry base,
191             Vector JavaDoc attributes, boolean forException) {
192         return new JavaBeanHelperWriter(
193                 emitter, type, elements, base, attributes,
194                 forException ? JavaBeanFaultWriter.RESERVED_PROPERTY_NAMES
195                               : Collections.EMPTY_SET);
196     }
197
198     /**
199      * getHolderWriter
200      *
201      * @param emitter
202      * @param type
203      * @return
204      */

205     protected Generator getHolderWriter(Emitter emitter, TypeEntry type) {
206         return new JavaHolderWriter(emitter, type);
207     }
208 } // class JavaTypeWriter
209
Popular Tags