KickJava   Java API By Example, From Geeks To Geeks.

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


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.gen.Generator;
59 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
60 import org.jboss.axis.wsdl.symbolTable.FaultInfo;
61 import org.jboss.axis.wsdl.symbolTable.MessageEntry;
62 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
63
64 import javax.wsdl.Binding;
65 import javax.wsdl.Definition;
66 import javax.wsdl.Import;
67 import javax.wsdl.Message;
68 import java.io.IOException JavaDoc;
69 import java.util.ArrayList JavaDoc;
70 import java.util.HashSet JavaDoc;
71 import java.util.Iterator JavaDoc;
72 import java.util.Map JavaDoc;
73 import java.util.Vector JavaDoc;
74
75 /**
76  * This is Wsdl2java's Definition Writer.
77  * It currently writes the following files:
78  * Faults as needed.
79  */

80 public class JavaDefinitionWriter implements Generator
81 {
82    protected Emitter emitter;
83    protected Definition definition;
84    protected SymbolTable symbolTable;
85
86    /**
87     * Constructor.
88     */

89    public JavaDefinitionWriter(Emitter emitter, Definition definition,
90                                SymbolTable symbolTable)
91    {
92       this.emitter = emitter;
93       this.definition = definition;
94       this.symbolTable = symbolTable;
95    } // ctor
96

97    /**
98     * Write other items from the definition as needed.
99     */

100    public void generate() throws IOException JavaDoc
101    {
102       writeFaults();
103    } // generate
104

105    /**
106     * Write all the simple type faults.
107     * The complexType Faults are automatically handled by JavaTypeWriter.
108     * The fault name is derived from the fault message name per JAX-RPC
109     */

110    private void writeFaults() throws IOException JavaDoc
111    {
112       ArrayList JavaDoc faults = new ArrayList JavaDoc();
113       collectFaults(definition, faults);
114
115       // Fault classes we're actually writing (for dup checking)
116
HashSet JavaDoc generatedFaults = new HashSet JavaDoc();
117
118       // iterate over fault list, emitting code.
119
Iterator JavaDoc fi = faults.iterator();
120       while (fi.hasNext())
121       {
122          FaultInfo faultInfo = (FaultInfo)fi.next();
123          Message message = faultInfo.getMessage();
124          String JavaDoc name = Utils.getFullExceptionName(message, symbolTable);
125          if (generatedFaults.contains(name))
126          {
127             continue;
128          }
129          generatedFaults.add(name);
130
131          // Generate the 'Simple' Faults.
132
// The complexType Faults are automatically handled
133
// by JavaTypeWriter.
134
MessageEntry me = symbolTable.getMessageEntry(message.getQName());
135          boolean emitSimpleFault = true;
136          if (me != null)
137          {
138             Boolean JavaDoc complexTypeFault = (Boolean JavaDoc)
139                     me.getDynamicVar(JavaGeneratorFactory.COMPLEX_TYPE_FAULT);
140             if (complexTypeFault != null &&
141                     complexTypeFault.booleanValue())
142             {
143                emitSimpleFault = false;
144             }
145          }
146          if (emitSimpleFault)
147          {
148             try
149             {
150                JavaFaultWriter writer =
151                        new JavaFaultWriter(emitter,
152                                symbolTable,
153                                faultInfo);
154                // Go write the file
155
writer.generate();
156             }
157             catch (DuplicateFileException dfe)
158             {
159                System.err.println(Messages.getMessage("fileExistError00", dfe.getFileName()));
160             }
161          }
162       }
163    } // writeFaults
164

165    /**
166     * Collect all of the faults used in this definition.
167     */

168    private HashSet JavaDoc importedFiles = new HashSet JavaDoc();
169
170    private void collectFaults(Definition def, ArrayList JavaDoc faults) throws IOException JavaDoc
171    {
172       Map JavaDoc imports = def.getImports();
173       Object JavaDoc[] importValues = imports.values().toArray();
174       for (int i = 0; i < importValues.length; ++i)
175       {
176          Vector JavaDoc v = (Vector JavaDoc)importValues[i];
177          for (int j = 0; j < v.size(); ++j)
178          {
179             Import imp = (Import)v.get(j);
180             if (!importedFiles.contains(imp.getLocationURI()))
181             {
182                importedFiles.add(imp.getLocationURI());
183                Definition importDef = imp.getDefinition();
184                if (importDef != null)
185                {
186                   collectFaults(importDef, faults);
187                }
188             }
189          }
190       }
191
192       // Traverse the bindings to find faults
193
Map JavaDoc bindings = def.getBindings();
194       Iterator JavaDoc bindi = bindings.values().iterator();
195       while (bindi.hasNext())
196       {
197          Binding binding = (Binding)bindi.next();
198          BindingEntry entry = symbolTable.getBindingEntry(binding.getQName());
199          if (entry.isReferenced())
200          {
201             // use the map of bindingOperation -> fault info
202
// created in SymbolTable
203
Map JavaDoc faultMap = entry.getFaults();
204             Iterator JavaDoc it = faultMap.values().iterator();
205             while (it.hasNext())
206             {
207                ArrayList JavaDoc list = (ArrayList JavaDoc)it.next();
208                // Accumulate total list of faults
209
faults.addAll(list);
210             }
211          }
212       }
213    } // collectFaults
214

215 } // class JavaDefinitionWriter
216
Popular Tags