KickJava   Java API By Example, From Geeks To Geeks.

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


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.gen.Generator;
20 import org.apache.axis.wsdl.symbolTable.BindingEntry;
21 import org.apache.axis.wsdl.symbolTable.FaultInfo;
22 import org.apache.axis.wsdl.symbolTable.MessageEntry;
23 import org.apache.axis.wsdl.symbolTable.SymbolTable;
24
25 import javax.wsdl.Binding;
26 import javax.wsdl.Definition;
27 import javax.wsdl.Import;
28 import javax.wsdl.Message;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 /**
37  * This is Wsdl2java's Definition Writer.
38  * It currently writes the following files:
39  * Faults as needed.
40  */

41 public class JavaDefinitionWriter implements Generator {
42
43     /** Field emitter */
44     protected Emitter emitter;
45
46     /** Field definition */
47     protected Definition definition;
48
49     /** Field symbolTable */
50     protected SymbolTable symbolTable;
51
52     /**
53      * Constructor.
54      *
55      * @param emitter
56      * @param definition
57      * @param symbolTable
58      */

59     public JavaDefinitionWriter(Emitter emitter, Definition definition,
60                                 SymbolTable symbolTable) {
61
62         this.emitter = emitter;
63         this.definition = definition;
64         this.symbolTable = symbolTable;
65     } // ctor
66

67     /**
68      * Write other items from the definition as needed.
69      *
70      * @throws IOException
71      */

72     public void generate() throws IOException JavaDoc {
73         writeFaults();
74     } // generate
75

76     /**
77      * Write all the simple type faults.
78      * The complexType Faults are automatically handled by JavaTypeWriter.
79      * The fault name is derived from the fault message name per JAX-RPC
80      *
81      * @throws IOException
82      */

83     protected void writeFaults() throws IOException JavaDoc {
84
85         ArrayList JavaDoc faults = new ArrayList JavaDoc();
86
87         collectFaults(definition, faults);
88
89         // Fault classes we're actually writing (for dup checking)
90
HashSet JavaDoc generatedFaults = new HashSet JavaDoc();
91
92         // iterate over fault list, emitting code.
93
Iterator JavaDoc fi = faults.iterator();
94
95         while (fi.hasNext()) {
96             FaultInfo faultInfo = (FaultInfo) fi.next();
97             Message message = faultInfo.getMessage();
98             String JavaDoc name = Utils.getFullExceptionName(message,
99                     symbolTable);
100
101             if (generatedFaults.contains(name)) {
102                 continue;
103             }
104
105             generatedFaults.add(name);
106
107             // Generate the 'Simple' Faults.
108
// The complexType Faults are automatically handled
109
// by JavaTypeWriter.
110
MessageEntry me =
111                     symbolTable.getMessageEntry(message.getQName());
112             boolean emitSimpleFault = true;
113
114             if (me != null) {
115                 Boolean JavaDoc complexTypeFault = (Boolean JavaDoc) me.getDynamicVar(
116                         JavaGeneratorFactory.COMPLEX_TYPE_FAULT);
117
118                 if ((complexTypeFault != null)
119                         && complexTypeFault.booleanValue()) {
120                     emitSimpleFault = false;
121                 }
122             }
123
124             if (emitSimpleFault) {
125                 try {
126                     JavaFaultWriter writer = new JavaFaultWriter(emitter,
127                             symbolTable, faultInfo);
128
129                     // Go write the file
130
writer.generate();
131                 } catch (DuplicateFileException dfe) {
132                     System.err.println(Messages.getMessage("fileExistError00",
133                             dfe.getFileName()));
134                 }
135             }
136         }
137     } // writeFaults
138

139     /** Collect all of the faults used in this definition. */
140     private HashSet JavaDoc importedFiles = new HashSet JavaDoc();
141
142     /**
143      * Method collectFaults
144      *
145      * @param def
146      * @param faults
147      * @throws IOException
148      */

149     private void collectFaults(Definition def, ArrayList JavaDoc faults)
150             throws IOException JavaDoc {
151
152         Map JavaDoc imports = def.getImports();
153         Object JavaDoc[] importValues = imports.values().toArray();
154
155         for (int i = 0; i < importValues.length; ++i) {
156             Vector JavaDoc v = (Vector JavaDoc) importValues[i];
157
158             for (int j = 0; j < v.size(); ++j) {
159                 Import imp = (Import) v.get(j);
160
161                 if (!importedFiles.contains(imp.getLocationURI())) {
162                     importedFiles.add(imp.getLocationURI());
163
164                     Definition importDef = imp.getDefinition();
165
166                     if (importDef != null) {
167                         collectFaults(importDef, faults);
168                     }
169                 }
170             }
171         }
172
173         // Traverse the bindings to find faults
174
Map JavaDoc bindings = def.getBindings();
175         Iterator JavaDoc bindi = bindings.values().iterator();
176
177         while (bindi.hasNext()) {
178             Binding binding = (Binding) bindi.next();
179             BindingEntry entry =
180                     symbolTable.getBindingEntry(binding.getQName());
181
182             if (entry.isReferenced()) {
183
184                 // use the map of bindingOperation -> fault info
185
// created in SymbolTable
186
Map JavaDoc faultMap = entry.getFaults();
187                 Iterator JavaDoc it = faultMap.values().iterator();
188
189                 while (it.hasNext()) {
190                     ArrayList JavaDoc list = (ArrayList JavaDoc) it.next();
191
192                     // Accumulate total list of faults
193
faults.addAll(list);
194                 }
195             }
196         }
197     } // collectFaults
198
} // class JavaDefinitionWriter
199
Popular Tags