View Javadoc

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  
17  package org.apache.geronimo.ews.jaxrpcmapping;
18  
19  import org.apache.axis.utils.Messages;
20  import org.apache.axis.wsdl.gen.Generator;
21  import org.apache.axis.wsdl.symbolTable.BindingEntry;
22  import org.apache.axis.wsdl.symbolTable.FaultInfo;
23  import org.apache.axis.wsdl.symbolTable.MessageEntry;
24  import org.apache.axis.wsdl.symbolTable.SymbolTable;
25  import org.apache.axis.wsdl.toJava.DuplicateFileException;
26  import org.apache.axis.wsdl.toJava.JavaGeneratorFactory;
27  import org.apache.axis.wsdl.toJava.Utils;
28  
29  import javax.wsdl.Binding;
30  import javax.wsdl.Definition;
31  import javax.wsdl.Import;
32  import javax.wsdl.Message;
33  import java.io.IOException;
34  import java.util.ArrayList;
35  import java.util.HashSet;
36  import java.util.Iterator;
37  import java.util.Map;
38  import java.util.Vector;
39  
40  /***
41   * This is Wsdl2java's Definition Writer.
42   * It currently writes the following files:
43   * Faults as needed.
44   *
45   * @author Ias (iasandcb@tmax.co.kr)
46   * @deprecated no more used by J2eeGeneratorFactory
47   */
48  public class J2eeDefinitionWriter implements Generator {
49      protected J2eeEmitter emitter;
50      protected Definition definition;
51      protected SymbolTable symbolTable;
52  
53      /***
54       * Constructor.
55       */
56      public J2eeDefinitionWriter(J2eeEmitter emitter, Definition definition,
57                                  SymbolTable symbolTable) {
58          this.emitter = emitter;
59          this.definition = definition;
60          this.symbolTable = symbolTable;
61      } // ctor
62  
63      /***
64       * Write other items from the definition as needed.
65       */
66      public void generate() throws IOException {
67          writeFaults();
68      } // generate
69  
70      /***
71       * Write all the simple type faults.
72       * The complexType Faults are automatically handled by JavaTypeWriter.
73       * The fault name is derived from the fault message name per JAX-RPC
74       */
75      private void writeFaults() throws IOException {
76          ArrayList faults = new ArrayList();
77          collectFaults(definition, faults);
78          
79          // Fault classes we're actually writing (for dup checking)
80          HashSet generatedFaults = new HashSet();
81  
82          // iterate over fault list, emitting code.
83          Iterator fi = faults.iterator();
84          while (fi.hasNext()) {
85              FaultInfo faultInfo = (FaultInfo) fi.next();
86              Message message = faultInfo.getMessage();
87              String name = Utils.getFullExceptionName(message, symbolTable);
88              if (generatedFaults.contains(name)) {
89                  continue;
90              }
91              generatedFaults.add(name);
92  
93              // Generate the 'Simple' Faults.
94              // The complexType Faults are automatically handled
95              // by JavaTypeWriter.
96              MessageEntry me = symbolTable.getMessageEntry(message.getQName());
97              boolean emitSimpleFault = true;
98              if (me != null) {
99                  Boolean complexTypeFault = (Boolean)
100                         me.getDynamicVar(JavaGeneratorFactory.COMPLEX_TYPE_FAULT);
101                 if (complexTypeFault != null &&
102                         complexTypeFault.booleanValue()) {
103                     emitSimpleFault = false;
104                 }
105             }
106             if (emitSimpleFault) {
107                 try {
108                     J2eeFaultWriter writer =
109                             new J2eeFaultWriter(emitter,
110                                     symbolTable,
111                                     faultInfo); 
112                     // Go write the file
113                     writer.generate();
114                 } catch (DuplicateFileException dfe) {
115                     System.err.println(Messages.getMessage("fileExistError00", dfe.getFileName()));
116                 }
117             }
118         }
119     } // writeFaults
120 
121     /***
122      * Collect all of the faults used in this definition.
123      */
124     private HashSet importedFiles = new HashSet();
125 
126     private void collectFaults(Definition def, ArrayList faults) throws IOException {
127         Map imports = def.getImports();
128         Object[] importValues = imports.values().toArray();
129         for (int i = 0; i < importValues.length; ++i) {
130             Vector v = (Vector) importValues[i];
131             for (int j = 0; j < v.size(); ++j) {
132                 Import imp = (Import) v.get(j);
133                 if (!importedFiles.contains(imp.getLocationURI())) {
134                     importedFiles.add(imp.getLocationURI());
135                     Definition importDef = imp.getDefinition();
136                     if (importDef != null) {
137                         collectFaults(importDef, faults);
138                     }
139                 }
140             }
141         }
142 
143         // Traverse the bindings to find faults
144         Map bindings = def.getBindings();
145         Iterator bindi = bindings.values().iterator();
146         while (bindi.hasNext()) {
147             Binding binding = (Binding) bindi.next();
148             BindingEntry entry = symbolTable.getBindingEntry(binding.getQName());
149             if (entry.isReferenced()) {
150                 // use the map of bindingOperation -> fault info
151                 // created in SymbolTable
152                 Map faultMap = entry.getFaults();
153                 Iterator it = faultMap.values().iterator();
154                 while (it.hasNext()) {
155                     ArrayList list = (ArrayList) it.next();
156                     // Accumulate total list of faults
157                     faults.addAll(list);
158                 }
159             }
160         }
161     } // collectFaults
162 
163 }